@springmicro/cart 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,265 +1,265 @@
1
- import { FormikProps } from "formik";
2
- import {
3
- allCountries,
4
- allProvinces,
5
- allCountriesReverse,
6
- getRegionLabel,
7
- getPostalCodeLabel,
8
- getPostalCodeDefault,
9
- provinces,
10
- } from "@springmicro/utils/address";
11
- import type { Alpha2Code, Province } from "@springmicro/utils/address";
12
- import React from "react";
13
- import { useEffect, useState } from "react";
14
- import { SxProps } from "@mui/material";
15
- import MenuItem from "@mui/material/MenuItem";
16
- import Box from "@mui/material/Box";
17
- import TextField, { StandardTextFieldProps } from "@mui/material/TextField";
18
- import Autocomplete from "@mui/material/Autocomplete";
19
-
20
- export type AddressValues = {
21
- country: string;
22
- region: string;
23
- line1: string;
24
- line2?: string;
25
- organization?: string;
26
- email: string;
27
- city: string;
28
- postal_code: string;
29
- };
30
-
31
- export type AddressFieldProps = {
32
- formik: FormikProps<AddressValues & any>; // formik object might contain other values in addition to address
33
- name: keyof AddressValues;
34
- sx?: SxProps;
35
- };
36
-
37
- export function AddressCountryField({ formik, name, sx }: AddressFieldProps) {
38
- /**
39
- * Selects the full country name, need to do a reverse lookup for the Alpha2Code.
40
- */
41
- const [inputValue, setInputValue] = useState(formik.values[name] as string);
42
-
43
- return (
44
- <Autocomplete
45
- sx={{ width: 300, ...sx }}
46
- options={Object.values(allCountries)}
47
- autoHighlight
48
- getOptionLabel={(option) => option}
49
- value={formik.values[name] as string}
50
- onChange={(e, newValue) => {
51
- // console.log(newValue)
52
- formik.setValues({
53
- ...formik.values,
54
- region: "",
55
- country: newValue as string,
56
- line1: "",
57
- line2: "",
58
- organization: "",
59
- city: "",
60
- postal_code: "",
61
- });
62
- }}
63
- onBlur={formik.handleBlur}
64
- inputValue={inputValue}
65
- onInputChange={(event, newInputValue) => {
66
- setInputValue(newInputValue);
67
- }}
68
- renderOption={(props, option) => (
69
- <Box
70
- component="li"
71
- sx={{ "& > img": { mr: 2, flexShrink: 0 } }}
72
- {...props}
73
- >
74
- <img
75
- loading="lazy"
76
- width="20"
77
- src={`https://flagcdn.com/w20/${allCountriesReverse[
78
- option
79
- ].toLowerCase()}.png`}
80
- srcSet={`https://flagcdn.com/w40/${allCountriesReverse[
81
- option
82
- ].toLowerCase()}.png 2x`}
83
- alt=""
84
- />
85
- {option}
86
- </Box>
87
- )}
88
- renderInput={(params) => (
89
- <TextField
90
- {...params}
91
- required={true}
92
- name={name as string}
93
- label="Country"
94
- />
95
- )}
96
- />
97
- );
98
- }
99
-
100
- export function AddressRegionField({ formik, name, sx }: AddressFieldProps) {
101
- const [addressRegionFieldOptional, setAddressRegionFieldOptional] =
102
- useState<boolean>(false);
103
- const country2code = allCountriesReverse[
104
- formik.values["country"] as string
105
- ] as Alpha2Code;
106
-
107
- useEffect(() => {
108
- setAddressRegionFieldOptional(
109
- provinces.filter(
110
- (province: Province) => province.country === country2code
111
- ).length === 0
112
- );
113
- }, [country2code]);
114
-
115
- const baseProps = {
116
- sx: { width: 300, ...sx },
117
- label: getRegionLabel(country2code),
118
- name: name as string,
119
- id: name as string,
120
- value: formik.values[name],
121
- onChange: formik.handleChange,
122
- onBlur: formik.handleBlur,
123
- };
124
-
125
- if (!country2code || !getRegionLabel(country2code)) {
126
- return <></>;
127
- }
128
-
129
- if (addressRegionFieldOptional) {
130
- return (
131
- <TextField
132
- {...baseProps}
133
- required={false}
134
- sx={{ display: "block", ...baseProps.sx }}
135
- />
136
- );
137
- } else {
138
- return (
139
- <TextField {...baseProps} select required={true}>
140
- {allProvinces
141
- .filter((val) => val.country === country2code)
142
- .map((province, i) => (
143
- <MenuItem key={i} value={province.value}>
144
- {province.name}
145
- </MenuItem>
146
- ))}
147
- </TextField>
148
- );
149
- }
150
- }
151
-
152
- export function AddressStreetField({
153
- formik,
154
- name,
155
- sx,
156
- required = true,
157
- lineNo,
158
- }: AddressFieldProps & {
159
- required?: boolean;
160
- lineNo?: "1" | "2" | "3" | undefined;
161
- }) {
162
- return (
163
- <>
164
- <TextField
165
- label={lineNo ? `Street Address ${lineNo}` : "Street Address"}
166
- sx={{ width: 400, display: "block", ...sx }}
167
- name={name as string}
168
- id={name as string}
169
- value={formik.values[name]}
170
- onChange={formik.handleChange}
171
- onBlur={formik.handleBlur}
172
- required={required}
173
- />
174
- </>
175
- );
176
- }
177
-
178
- export function AddressEmailField({ formik, name, sx }: AddressFieldProps) {
179
- return (
180
- <>
181
- <TextField
182
- label={"Email"}
183
- sx={{ width: 400, display: "block", ...sx }}
184
- name={name as string}
185
- id={name as string}
186
- value={formik.values[name]}
187
- onChange={formik.handleChange}
188
- onBlur={formik.handleBlur}
189
- type="email"
190
- required={true}
191
- />
192
- </>
193
- );
194
- }
195
-
196
- export function AddressOrganizationNameField({
197
- formik,
198
- name,
199
- sx,
200
- }: AddressFieldProps) {
201
- return (
202
- <TextField
203
- label="Organization Name"
204
- sx={{ width: 400, display: "block", ...sx }}
205
- name={name as string}
206
- id={name as string}
207
- value={formik.values[name]}
208
- onChange={formik.handleChange}
209
- onBlur={formik.handleBlur}
210
- required={false}
211
- helperText="Only include organization name if you want it included on the address."
212
- />
213
- );
214
- }
215
-
216
- export function AddressCityField({ formik, name, sx }: AddressFieldProps) {
217
- return (
218
- <TextField
219
- label="City"
220
- sx={{ width: 400, display: "block", ...sx }}
221
- name={name as string}
222
- id={name as string}
223
- value={formik.values[name]}
224
- onChange={formik.handleChange}
225
- onBlur={formik.handleBlur}
226
- required={true}
227
- />
228
- );
229
- }
230
-
231
- export function AddressPostalCodeField({
232
- formik,
233
- name,
234
- sx,
235
- }: AddressFieldProps) {
236
- const country2code = allCountriesReverse[
237
- formik.values["country"] as string
238
- ] as Alpha2Code;
239
-
240
- React.useEffect(() => {
241
- if (getPostalCodeDefault(country2code)) {
242
- formik
243
- .setFieldValue(name, getPostalCodeDefault(country2code))
244
- .then(() => formik.setFieldTouched(name, true));
245
- }
246
- }, [country2code]);
247
-
248
- return (
249
- <TextField
250
- label={getPostalCodeLabel(country2code)}
251
- sx={{ width: 400, display: "block", ...sx }}
252
- name={name as string}
253
- id={name as string}
254
- value={formik.values[name]}
255
- onChange={formik.handleChange}
256
- onBlur={formik.handleBlur}
257
- required={true}
258
- helperText={
259
- getPostalCodeDefault(country2code)
260
- ? 'Our records indicate that your country does not have a postal code system, so "00000" will be used by default.'
261
- : ""
262
- }
263
- />
264
- );
265
- }
1
+ import { FormikProps } from "formik";
2
+ import {
3
+ allCountries,
4
+ allProvinces,
5
+ allCountriesReverse,
6
+ getRegionLabel,
7
+ getPostalCodeLabel,
8
+ getPostalCodeDefault,
9
+ provinces,
10
+ } from "@springmicro/utils/address";
11
+ import type { Alpha2Code, Province } from "@springmicro/utils/address";
12
+ import React from "react";
13
+ import { useEffect, useState } from "react";
14
+ import { SxProps } from "@mui/material";
15
+ import MenuItem from "@mui/material/MenuItem";
16
+ import Box from "@mui/material/Box";
17
+ import TextField, { StandardTextFieldProps } from "@mui/material/TextField";
18
+ import Autocomplete from "@mui/material/Autocomplete";
19
+
20
+ export type AddressValues = {
21
+ country: string;
22
+ region: string;
23
+ line1: string;
24
+ line2?: string;
25
+ organization?: string;
26
+ email: string;
27
+ city: string;
28
+ postal_code: string;
29
+ };
30
+
31
+ export type AddressFieldProps = {
32
+ formik: FormikProps<AddressValues & any>; // formik object might contain other values in addition to address
33
+ name: keyof AddressValues;
34
+ sx?: SxProps;
35
+ };
36
+
37
+ export function AddressCountryField({ formik, name, sx }: AddressFieldProps) {
38
+ /**
39
+ * Selects the full country name, need to do a reverse lookup for the Alpha2Code.
40
+ */
41
+ const [inputValue, setInputValue] = useState(formik.values[name] as string);
42
+
43
+ return (
44
+ <Autocomplete
45
+ sx={{ width: 300, ...sx }}
46
+ options={Object.values(allCountries)}
47
+ autoHighlight
48
+ getOptionLabel={(option) => option}
49
+ value={formik.values[name] as string}
50
+ onChange={(e, newValue) => {
51
+ // console.log(newValue)
52
+ formik.setValues({
53
+ ...formik.values,
54
+ region: "",
55
+ country: newValue as string,
56
+ line1: "",
57
+ line2: "",
58
+ organization: "",
59
+ city: "",
60
+ postal_code: "",
61
+ });
62
+ }}
63
+ onBlur={formik.handleBlur}
64
+ inputValue={inputValue}
65
+ onInputChange={(event, newInputValue) => {
66
+ setInputValue(newInputValue);
67
+ }}
68
+ renderOption={(props, option) => (
69
+ <Box
70
+ component="li"
71
+ sx={{ "& > img": { mr: 2, flexShrink: 0 } }}
72
+ {...props}
73
+ >
74
+ <img
75
+ loading="lazy"
76
+ width="20"
77
+ src={`https://flagcdn.com/w20/${allCountriesReverse[
78
+ option
79
+ ].toLowerCase()}.png`}
80
+ srcSet={`https://flagcdn.com/w40/${allCountriesReverse[
81
+ option
82
+ ].toLowerCase()}.png 2x`}
83
+ alt=""
84
+ />
85
+ {option}
86
+ </Box>
87
+ )}
88
+ renderInput={(params) => (
89
+ <TextField
90
+ {...params}
91
+ required={true}
92
+ name={name as string}
93
+ label="Country"
94
+ />
95
+ )}
96
+ />
97
+ );
98
+ }
99
+
100
+ export function AddressRegionField({ formik, name, sx }: AddressFieldProps) {
101
+ const [addressRegionFieldOptional, setAddressRegionFieldOptional] =
102
+ useState<boolean>(false);
103
+ const country2code = allCountriesReverse[
104
+ formik.values["country"] as string
105
+ ] as Alpha2Code;
106
+
107
+ useEffect(() => {
108
+ setAddressRegionFieldOptional(
109
+ provinces.filter(
110
+ (province: Province) => province.country === country2code
111
+ ).length === 0
112
+ );
113
+ }, [country2code]);
114
+
115
+ const baseProps = {
116
+ sx: { width: 300, ...sx },
117
+ label: getRegionLabel(country2code),
118
+ name: name as string,
119
+ id: name as string,
120
+ value: formik.values[name],
121
+ onChange: formik.handleChange,
122
+ onBlur: formik.handleBlur,
123
+ };
124
+
125
+ if (!country2code || !getRegionLabel(country2code)) {
126
+ return <></>;
127
+ }
128
+
129
+ if (addressRegionFieldOptional) {
130
+ return (
131
+ <TextField
132
+ {...baseProps}
133
+ required={false}
134
+ sx={{ display: "block", ...baseProps.sx }}
135
+ />
136
+ );
137
+ } else {
138
+ return (
139
+ <TextField {...baseProps} select required={true}>
140
+ {allProvinces
141
+ .filter((val) => val.country === country2code)
142
+ .map((province, i) => (
143
+ <MenuItem key={i} value={province.value}>
144
+ {province.name}
145
+ </MenuItem>
146
+ ))}
147
+ </TextField>
148
+ );
149
+ }
150
+ }
151
+
152
+ export function AddressStreetField({
153
+ formik,
154
+ name,
155
+ sx,
156
+ required = true,
157
+ lineNo,
158
+ }: AddressFieldProps & {
159
+ required?: boolean;
160
+ lineNo?: "1" | "2" | "3" | undefined;
161
+ }) {
162
+ return (
163
+ <>
164
+ <TextField
165
+ label={lineNo ? `Street Address ${lineNo}` : "Street Address"}
166
+ sx={{ width: 400, display: "block", ...sx }}
167
+ name={name as string}
168
+ id={name as string}
169
+ value={formik.values[name]}
170
+ onChange={formik.handleChange}
171
+ onBlur={formik.handleBlur}
172
+ required={required}
173
+ />
174
+ </>
175
+ );
176
+ }
177
+
178
+ export function AddressEmailField({ formik, name, sx }: AddressFieldProps) {
179
+ return (
180
+ <>
181
+ <TextField
182
+ label={"Email"}
183
+ sx={{ width: 400, display: "block", ...sx }}
184
+ name={name as string}
185
+ id={name as string}
186
+ value={formik.values[name]}
187
+ onChange={formik.handleChange}
188
+ onBlur={formik.handleBlur}
189
+ type="email"
190
+ required={true}
191
+ />
192
+ </>
193
+ );
194
+ }
195
+
196
+ export function AddressOrganizationNameField({
197
+ formik,
198
+ name,
199
+ sx,
200
+ }: AddressFieldProps) {
201
+ return (
202
+ <TextField
203
+ label="Organization Name"
204
+ sx={{ width: 400, display: "block", ...sx }}
205
+ name={name as string}
206
+ id={name as string}
207
+ value={formik.values[name]}
208
+ onChange={formik.handleChange}
209
+ onBlur={formik.handleBlur}
210
+ required={false}
211
+ helperText="Only include organization name if you want it included on the address."
212
+ />
213
+ );
214
+ }
215
+
216
+ export function AddressCityField({ formik, name, sx }: AddressFieldProps) {
217
+ return (
218
+ <TextField
219
+ label="City"
220
+ sx={{ width: 400, display: "block", ...sx }}
221
+ name={name as string}
222
+ id={name as string}
223
+ value={formik.values[name]}
224
+ onChange={formik.handleChange}
225
+ onBlur={formik.handleBlur}
226
+ required={true}
227
+ />
228
+ );
229
+ }
230
+
231
+ export function AddressPostalCodeField({
232
+ formik,
233
+ name,
234
+ sx,
235
+ }: AddressFieldProps) {
236
+ const country2code = allCountriesReverse[
237
+ formik.values["country"] as string
238
+ ] as Alpha2Code;
239
+
240
+ React.useEffect(() => {
241
+ if (getPostalCodeDefault(country2code)) {
242
+ formik
243
+ .setFieldValue(name, getPostalCodeDefault(country2code))
244
+ .then(() => formik.setFieldTouched(name, true));
245
+ }
246
+ }, [country2code]);
247
+
248
+ return (
249
+ <TextField
250
+ label={getPostalCodeLabel(country2code)}
251
+ sx={{ width: 400, display: "block", ...sx }}
252
+ name={name as string}
253
+ id={name as string}
254
+ value={formik.values[name]}
255
+ onChange={formik.handleChange}
256
+ onBlur={formik.handleBlur}
257
+ required={true}
258
+ helperText={
259
+ getPostalCodeDefault(country2code)
260
+ ? 'Our records indicate that your country does not have a postal code system, so "00000" will be used by default.'
261
+ : ""
262
+ }
263
+ />
264
+ );
265
+ }