@springmicro/cart 0.5.1 → 0.5.2

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,261 +1,261 @@
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.keys(allCountries)}
47
- autoHighlight
48
- getOptionLabel={(option) => allCountries[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
- allCountries[option]
79
- ].toLowerCase()}.png`}
80
- srcSet={`https://flagcdn.com/w40/${allCountriesReverse[
81
- allCountries[option]
82
- ].toLowerCase()}.png 2x`}
83
- alt=""
84
- />
85
- {allCountries[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 = formik.values["country"] as Alpha2Code;
104
-
105
- useEffect(() => {
106
- setAddressRegionFieldOptional(
107
- provinces.filter(
108
- (province: Province) => province.country === country2code
109
- ).length === 0
110
- );
111
- }, [country2code]);
112
-
113
- const baseProps = {
114
- sx: { width: 300, ...sx },
115
- label: getRegionLabel(country2code),
116
- name: name as string,
117
- id: name as string,
118
- value: formik.values[name],
119
- onChange: formik.handleChange,
120
- onBlur: formik.handleBlur,
121
- };
122
-
123
- if (!country2code || !getRegionLabel(country2code)) {
124
- return <></>;
125
- }
126
-
127
- if (addressRegionFieldOptional) {
128
- return (
129
- <TextField
130
- {...baseProps}
131
- required={false}
132
- sx={{ display: "block", ...baseProps.sx }}
133
- />
134
- );
135
- } else {
136
- return (
137
- <TextField {...baseProps} select required={true}>
138
- {allProvinces
139
- .filter((val) => val.country === country2code)
140
- .map((province, i) => (
141
- <MenuItem key={i} value={province.value}>
142
- {province.name}
143
- </MenuItem>
144
- ))}
145
- </TextField>
146
- );
147
- }
148
- }
149
-
150
- export function AddressStreetField({
151
- formik,
152
- name,
153
- sx,
154
- required = true,
155
- lineNo,
156
- }: AddressFieldProps & {
157
- required?: boolean;
158
- lineNo?: "1" | "2" | "3" | undefined;
159
- }) {
160
- return (
161
- <>
162
- <TextField
163
- label={lineNo ? `Street Address ${lineNo}` : "Street Address"}
164
- sx={{ width: 400, display: "block", ...sx }}
165
- name={name as string}
166
- id={name as string}
167
- value={formik.values[name]}
168
- onChange={formik.handleChange}
169
- onBlur={formik.handleBlur}
170
- required={required}
171
- />
172
- </>
173
- );
174
- }
175
-
176
- export function AddressEmailField({ formik, name, sx }: AddressFieldProps) {
177
- return (
178
- <>
179
- <TextField
180
- label={"Email"}
181
- sx={{ width: 400, display: "block", ...sx }}
182
- name={name as string}
183
- id={name as string}
184
- value={formik.values[name]}
185
- onChange={formik.handleChange}
186
- onBlur={formik.handleBlur}
187
- type="email"
188
- required={true}
189
- />
190
- </>
191
- );
192
- }
193
-
194
- export function AddressOrganizationNameField({
195
- formik,
196
- name,
197
- sx,
198
- }: AddressFieldProps) {
199
- return (
200
- <TextField
201
- label="Organization Name"
202
- sx={{ width: 400, display: "block", ...sx }}
203
- name={name as string}
204
- id={name as string}
205
- value={formik.values[name]}
206
- onChange={formik.handleChange}
207
- onBlur={formik.handleBlur}
208
- required={false}
209
- helperText="Only include organization name if you want it included on the address."
210
- />
211
- );
212
- }
213
-
214
- export function AddressCityField({ formik, name, sx }: AddressFieldProps) {
215
- return (
216
- <TextField
217
- label="City"
218
- sx={{ width: 400, display: "block", ...sx }}
219
- name={name as string}
220
- id={name as string}
221
- value={formik.values[name]}
222
- onChange={formik.handleChange}
223
- onBlur={formik.handleBlur}
224
- required={true}
225
- />
226
- );
227
- }
228
-
229
- export function AddressPostalCodeField({
230
- formik,
231
- name,
232
- sx,
233
- }: AddressFieldProps) {
234
- const country2code = formik.values["country"] as Alpha2Code;
235
-
236
- React.useEffect(() => {
237
- if (getPostalCodeDefault(country2code)) {
238
- formik
239
- .setFieldValue(name, getPostalCodeDefault(country2code))
240
- .then(() => formik.setFieldTouched(name, true));
241
- }
242
- }, [country2code]);
243
-
244
- return (
245
- <TextField
246
- label={getPostalCodeLabel(country2code)}
247
- sx={{ width: 400, display: "block", ...sx }}
248
- name={name as string}
249
- id={name as string}
250
- value={formik.values[name]}
251
- onChange={formik.handleChange}
252
- onBlur={formik.handleBlur}
253
- required={true}
254
- helperText={
255
- getPostalCodeDefault(country2code)
256
- ? 'Our records indicate that your country does not have a postal code system, so "00000" will be used by default.'
257
- : ""
258
- }
259
- />
260
- );
261
- }
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.keys(allCountries)}
47
+ autoHighlight
48
+ getOptionLabel={(option) => allCountries[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
+ allCountries[option]
79
+ ].toLowerCase()}.png`}
80
+ srcSet={`https://flagcdn.com/w40/${allCountriesReverse[
81
+ allCountries[option]
82
+ ].toLowerCase()}.png 2x`}
83
+ alt=""
84
+ />
85
+ {allCountries[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 = formik.values["country"] as Alpha2Code;
104
+
105
+ useEffect(() => {
106
+ setAddressRegionFieldOptional(
107
+ provinces.filter(
108
+ (province: Province) => province.country === country2code
109
+ ).length === 0
110
+ );
111
+ }, [country2code]);
112
+
113
+ const baseProps = {
114
+ sx: { width: 300, ...sx },
115
+ label: getRegionLabel(country2code),
116
+ name: name as string,
117
+ id: name as string,
118
+ value: formik.values[name],
119
+ onChange: formik.handleChange,
120
+ onBlur: formik.handleBlur,
121
+ };
122
+
123
+ if (!country2code || !getRegionLabel(country2code)) {
124
+ return <></>;
125
+ }
126
+
127
+ if (addressRegionFieldOptional) {
128
+ return (
129
+ <TextField
130
+ {...baseProps}
131
+ required={false}
132
+ sx={{ display: "block", ...baseProps.sx }}
133
+ />
134
+ );
135
+ } else {
136
+ return (
137
+ <TextField {...baseProps} select required={true}>
138
+ {allProvinces
139
+ .filter((val) => val.country === country2code)
140
+ .map((province, i) => (
141
+ <MenuItem key={i} value={province.value}>
142
+ {province.name}
143
+ </MenuItem>
144
+ ))}
145
+ </TextField>
146
+ );
147
+ }
148
+ }
149
+
150
+ export function AddressStreetField({
151
+ formik,
152
+ name,
153
+ sx,
154
+ required = true,
155
+ lineNo,
156
+ }: AddressFieldProps & {
157
+ required?: boolean;
158
+ lineNo?: "1" | "2" | "3" | undefined;
159
+ }) {
160
+ return (
161
+ <>
162
+ <TextField
163
+ label={lineNo ? `Street Address ${lineNo}` : "Street Address"}
164
+ sx={{ width: 400, display: "block", ...sx }}
165
+ name={name as string}
166
+ id={name as string}
167
+ value={formik.values[name]}
168
+ onChange={formik.handleChange}
169
+ onBlur={formik.handleBlur}
170
+ required={required}
171
+ />
172
+ </>
173
+ );
174
+ }
175
+
176
+ export function AddressEmailField({ formik, name, sx }: AddressFieldProps) {
177
+ return (
178
+ <>
179
+ <TextField
180
+ label={"Email"}
181
+ sx={{ width: 400, display: "block", ...sx }}
182
+ name={name as string}
183
+ id={name as string}
184
+ value={formik.values[name]}
185
+ onChange={formik.handleChange}
186
+ onBlur={formik.handleBlur}
187
+ type="email"
188
+ required={true}
189
+ />
190
+ </>
191
+ );
192
+ }
193
+
194
+ export function AddressOrganizationNameField({
195
+ formik,
196
+ name,
197
+ sx,
198
+ }: AddressFieldProps) {
199
+ return (
200
+ <TextField
201
+ label="Organization Name"
202
+ sx={{ width: 400, display: "block", ...sx }}
203
+ name={name as string}
204
+ id={name as string}
205
+ value={formik.values[name]}
206
+ onChange={formik.handleChange}
207
+ onBlur={formik.handleBlur}
208
+ required={false}
209
+ helperText="Only include organization name if you want it included on the address."
210
+ />
211
+ );
212
+ }
213
+
214
+ export function AddressCityField({ formik, name, sx }: AddressFieldProps) {
215
+ return (
216
+ <TextField
217
+ label="City"
218
+ sx={{ width: 400, display: "block", ...sx }}
219
+ name={name as string}
220
+ id={name as string}
221
+ value={formik.values[name]}
222
+ onChange={formik.handleChange}
223
+ onBlur={formik.handleBlur}
224
+ required={true}
225
+ />
226
+ );
227
+ }
228
+
229
+ export function AddressPostalCodeField({
230
+ formik,
231
+ name,
232
+ sx,
233
+ }: AddressFieldProps) {
234
+ const country2code = formik.values["country"] as Alpha2Code;
235
+
236
+ React.useEffect(() => {
237
+ if (getPostalCodeDefault(country2code)) {
238
+ formik
239
+ .setFieldValue(name, getPostalCodeDefault(country2code))
240
+ .then(() => formik.setFieldTouched(name, true));
241
+ }
242
+ }, [country2code]);
243
+
244
+ return (
245
+ <TextField
246
+ label={getPostalCodeLabel(country2code)}
247
+ sx={{ width: 400, display: "block", ...sx }}
248
+ name={name as string}
249
+ id={name as string}
250
+ value={formik.values[name]}
251
+ onChange={formik.handleChange}
252
+ onBlur={formik.handleBlur}
253
+ required={true}
254
+ helperText={
255
+ getPostalCodeDefault(country2code)
256
+ ? 'Our records indicate that your country does not have a postal code system, so "00000" will be used by default.'
257
+ : ""
258
+ }
259
+ />
260
+ );
261
+ }