@etsoo/materialui 1.1.44 → 1.1.45

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,4 +1,4 @@
1
- import { AddressApi } from "@etsoo/appscript";
1
+ import { AddressApi, AddressCity, AddressDistrict, AddressRegionDb, AddressState } from "@etsoo/appscript";
2
2
  import React from "react";
3
3
  /**
4
4
  * Address field
@@ -9,6 +9,7 @@ export declare enum AddressField {
9
9
  City = "city",
10
10
  District = "district"
11
11
  }
12
+ type AddressFieldType<F extends AddressField> = F extends AddressField.Region ? [F, AddressRegionDb | null] : F extends AddressField.State ? [F, AddressState | null] : F extends AddressField.City ? [F, AddressCity | null] : [F, AddressDistrict | null];
12
13
  /**
13
14
  * Address selector props
14
15
  */
@@ -49,6 +50,11 @@ export type AddressSelectorProps = {
49
50
  * Label
50
51
  */
51
52
  label?: string;
53
+ /**
54
+ * Onchange hanlder
55
+ * @param event Event
56
+ */
57
+ onChange?: <F extends AddressField>(event: AddressFieldType<F>) => void;
52
58
  /**
53
59
  * Country or region
54
60
  */
@@ -79,3 +85,4 @@ export type AddressSelectorProps = {
79
85
  * @param props Props
80
86
  */
81
87
  export declare function AddressSelector(props: AddressSelectorProps): JSX.Element;
88
+ export {};
@@ -22,7 +22,7 @@ export function AddressSelector(props) {
22
22
  // Labels
23
23
  const { city: cityDefault = "City", district: districtDefault = "District", region: regionDefault = "Region", state: stateDefault = "State" } = (_a = globalApp === null || globalApp === void 0 ? void 0 : globalApp.getLabels("region", "state", "city", "district")) !== null && _a !== void 0 ? _a : {};
24
24
  // Destruct
25
- const { api, city, cityLabel = cityDefault, district, districtLabel = districtDefault, error, helperText, hideRegion, label, region, regionLabel = regionDefault, required, search, state, stateLabel = stateDefault } = props;
25
+ const { api, city, cityLabel = cityDefault, district, districtLabel = districtDefault, error, helperText, hideRegion, label, onChange, region, regionLabel = regionDefault, required, search, state, stateLabel = stateDefault } = props;
26
26
  // States
27
27
  const [regionState, setRegion] = React.useState(region);
28
28
  const [stateState, setState] = React.useState(state);
@@ -68,13 +68,16 @@ export function AddressSelector(props) {
68
68
  // Field size
69
69
  const fieldSize = hideRegion ? 4 : 3;
70
70
  // Handle field change
71
- const handleChange = (field, value) => {
71
+ const handleChange = (event) => {
72
+ if (onChange)
73
+ onChange(event);
74
+ const [field, data] = event;
72
75
  if (field === AddressField.Region) {
73
- if (value == null) {
76
+ if (data == null) {
74
77
  setRegion(undefined);
75
78
  }
76
79
  else {
77
- setRegion(value);
80
+ setRegion(data.id);
78
81
  }
79
82
  setState(undefined);
80
83
  setCity(undefined);
@@ -82,50 +85,43 @@ export function AddressSelector(props) {
82
85
  return;
83
86
  }
84
87
  if (field === AddressField.State) {
85
- if (value == null) {
88
+ if (data == null) {
86
89
  setState(undefined);
87
90
  }
88
91
  else {
89
- setState(value);
92
+ setState(data.id);
90
93
  }
91
94
  setCity(undefined);
92
95
  setDistrict(undefined);
93
96
  return;
94
97
  }
95
98
  if (field === AddressField.City) {
96
- if (value == null) {
99
+ if (data == null) {
97
100
  setCity(undefined);
98
101
  }
99
- else if (typeof value === "number") {
100
- setCity(value);
101
- }
102
102
  else {
103
- setCity(parseInt(`${value}`));
103
+ setCity(data.id);
104
104
  }
105
105
  setDistrict(undefined);
106
106
  return;
107
107
  }
108
- if (value == null) {
108
+ if (data == null) {
109
109
  setDistrict(undefined);
110
110
  }
111
- else if (typeof value === "number") {
112
- setDistrict(value);
113
- }
114
111
  else {
115
- setDistrict(parseInt(`${value}`));
112
+ setDistrict(data.id);
116
113
  }
117
114
  };
118
- console.log(regionState, stateState, cityState, districtState);
119
115
  // Layout
120
116
  return (React.createElement(React.Fragment, null,
121
117
  label && (React.createElement(Grid, { item: true, xs: 12 },
122
118
  React.createElement(FormLabel, { required: required, sx: { fontSize: (theme) => theme.typography.caption } }, label))),
123
119
  !hideRegion && (React.createElement(Grid, { item: true, xs: 12, md: 6, lg: fieldSize },
124
- React.createElement(Tiplist, { label: regionLabel, name: AddressField.Region, search: search, fullWidth: true, idValue: regionState, loadData: (keyword, id, items) => api.getRegions({ keyword, id, items }), inputRequired: required, inputError: error, inputHelperText: helperText, onChange: (_event, value) => handleChange(AddressField.Region, value === null || value === void 0 ? void 0 : value.id) }))),
120
+ React.createElement(Tiplist, { label: regionLabel, name: AddressField.Region, search: search, fullWidth: true, idValue: regionState, loadData: (keyword, id, items) => api.getRegions({ keyword, id, items }), inputRequired: required, inputError: error, inputHelperText: helperText, onChange: (_event, value) => handleChange([AddressField.Region, value]) }))),
125
121
  React.createElement(Grid, { item: true, xs: 12, md: 6, lg: fieldSize },
126
- React.createElement(ComboBox, { name: AddressField.State, label: stateLabel, search: search, fullWidth: true, idValue: stateState, options: states, inputRequired: hideRegion ? required : undefined, inputError: hideRegion ? error : undefined, inputHelperText: hideRegion ? helperText : undefined, onChange: (_event, value) => handleChange(AddressField.State, value === null || value === void 0 ? void 0 : value.id) })),
122
+ React.createElement(ComboBox, { name: AddressField.State, label: stateLabel, search: search, fullWidth: true, idValue: stateState, options: states, inputRequired: hideRegion ? required : undefined, inputError: hideRegion ? error : undefined, inputHelperText: hideRegion ? helperText : undefined, onChange: (_event, value) => handleChange([AddressField.State, value]) })),
127
123
  React.createElement(Grid, { item: true, xs: 12, md: 6, lg: fieldSize },
128
- React.createElement(ComboBox, { name: AddressField.City, label: cityLabel, search: search, fullWidth: true, idValue: cityState, options: cities, onChange: (_event, value) => handleChange(AddressField.City, value === null || value === void 0 ? void 0 : value.id) })),
124
+ React.createElement(ComboBox, { name: AddressField.City, label: cityLabel, search: search, fullWidth: true, idValue: cityState, options: cities, onChange: (_event, value) => handleChange([AddressField.City, value]) })),
129
125
  React.createElement(Grid, { item: true, xs: 12, md: 6, lg: fieldSize },
130
- React.createElement(ComboBox, { name: AddressField.District, label: districtLabel, search: search, fullWidth: true, idValue: districtState, options: districts, onChange: (_event, value) => handleChange(AddressField.District, value === null || value === void 0 ? void 0 : value.id) }))));
126
+ React.createElement(ComboBox, { name: AddressField.District, label: districtLabel, search: search, fullWidth: true, idValue: districtState, options: districts, onChange: (_event, value) => handleChange([AddressField.District, value]) }))));
131
127
  }
package/lib/ComboBox.js CHANGED
@@ -26,8 +26,13 @@ export function ComboBox(props) {
26
26
  // [options] will cause infinite loop
27
27
  const propertyWay = loadData == null;
28
28
  React.useEffect(() => {
29
- if (propertyWay && options != null)
29
+ if (propertyWay && options != null) {
30
30
  setOptions(options);
31
+ if (stateValue != null &&
32
+ !options.some((option) => option[idField] === stateValue[idField])) {
33
+ setStateValue(null);
34
+ }
35
+ }
31
36
  }, [options, propertyWay]);
32
37
  // Local default value
33
38
  const localValue = idValue != null
@@ -33,8 +33,18 @@ export function ComboBoxMultiple(props) {
33
33
  // [options] will cause infinite loop
34
34
  const propertyWay = loadData == null;
35
35
  React.useEffect(() => {
36
- if (propertyWay && options != null)
36
+ if (propertyWay && options != null) {
37
37
  setOptions(options);
38
+ if (stateValue != null) {
39
+ if (Array.isArray(stateValue)) {
40
+ const newState = stateValue.filter((item) => options.some((option) => option[idField] === item[idField]));
41
+ setStateValue(newState);
42
+ }
43
+ else if (!options.some((option) => option[idField] === stateValue[idField])) {
44
+ setStateValue(null);
45
+ }
46
+ }
47
+ }
38
48
  }, [options, propertyWay]);
39
49
  // Local default value
40
50
  const localValue = idValue != null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/materialui",
3
- "version": "1.1.44",
3
+ "version": "1.1.45",
4
4
  "description": "TypeScript Material-UI Implementation",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -21,6 +21,14 @@ export enum AddressField {
21
21
  District = "district"
22
22
  }
23
23
 
24
+ type AddressFieldType<F extends AddressField> = F extends AddressField.Region
25
+ ? [F, AddressRegionDb | null]
26
+ : F extends AddressField.State
27
+ ? [F, AddressState | null]
28
+ : F extends AddressField.City
29
+ ? [F, AddressCity | null]
30
+ : [F, AddressDistrict | null];
31
+
24
32
  /**
25
33
  * Address selector props
26
34
  */
@@ -70,6 +78,12 @@ export type AddressSelectorProps = {
70
78
  */
71
79
  label?: string;
72
80
 
81
+ /**
82
+ * Onchange hanlder
83
+ * @param event Event
84
+ */
85
+ onChange?: <F extends AddressField>(event: AddressFieldType<F>) => void;
86
+
73
87
  /**
74
88
  * Country or region
75
89
  */
@@ -125,6 +139,7 @@ export function AddressSelector(props: AddressSelectorProps) {
125
139
  helperText,
126
140
  hideRegion,
127
141
  label,
142
+ onChange,
128
143
  region,
129
144
  regionLabel = regionDefault,
130
145
  required,
@@ -177,12 +192,16 @@ export function AddressSelector(props: AddressSelectorProps) {
177
192
  const fieldSize = hideRegion ? 4 : 3;
178
193
 
179
194
  // Handle field change
180
- const handleChange = (field: AddressField, value: unknown) => {
195
+ const handleChange = <F extends AddressField>(event: AddressFieldType<F>) => {
196
+ if (onChange) onChange(event);
197
+
198
+ const [field, data] = event;
199
+
181
200
  if (field === AddressField.Region) {
182
- if (value == null) {
201
+ if (data == null) {
183
202
  setRegion(undefined);
184
203
  } else {
185
- setRegion(value as string);
204
+ setRegion(data.id);
186
205
  }
187
206
  setState(undefined);
188
207
  setCity(undefined);
@@ -192,10 +211,10 @@ export function AddressSelector(props: AddressSelectorProps) {
192
211
  }
193
212
 
194
213
  if (field === AddressField.State) {
195
- if (value == null) {
214
+ if (data == null) {
196
215
  setState(undefined);
197
216
  } else {
198
- setState(value as string);
217
+ setState(data.id);
199
218
  }
200
219
  setCity(undefined);
201
220
  setDistrict(undefined);
@@ -204,29 +223,23 @@ export function AddressSelector(props: AddressSelectorProps) {
204
223
  }
205
224
 
206
225
  if (field === AddressField.City) {
207
- if (value == null) {
226
+ if (data == null) {
208
227
  setCity(undefined);
209
- } else if (typeof value === "number") {
210
- setCity(value);
211
228
  } else {
212
- setCity(parseInt(`${value}`));
229
+ setCity(data.id);
213
230
  }
214
231
  setDistrict(undefined);
215
232
 
216
233
  return;
217
234
  }
218
235
 
219
- if (value == null) {
236
+ if (data == null) {
220
237
  setDistrict(undefined);
221
- } else if (typeof value === "number") {
222
- setDistrict(value);
223
238
  } else {
224
- setDistrict(parseInt(`${value}`));
239
+ setDistrict(data.id);
225
240
  }
226
241
  };
227
242
 
228
- console.log(regionState, stateState, cityState, districtState);
229
-
230
243
  // Layout
231
244
  return (
232
245
  <React.Fragment>
@@ -255,7 +268,7 @@ export function AddressSelector(props: AddressSelectorProps) {
255
268
  inputError={error}
256
269
  inputHelperText={helperText}
257
270
  onChange={(_event, value) =>
258
- handleChange(AddressField.Region, value?.id)
271
+ handleChange([AddressField.Region, value])
259
272
  }
260
273
  />
261
274
  </Grid>
@@ -272,7 +285,7 @@ export function AddressSelector(props: AddressSelectorProps) {
272
285
  inputError={hideRegion ? error : undefined}
273
286
  inputHelperText={hideRegion ? helperText : undefined}
274
287
  onChange={(_event, value) =>
275
- handleChange(AddressField.State, value?.id)
288
+ handleChange([AddressField.State, value])
276
289
  }
277
290
  />
278
291
  </Grid>
@@ -284,9 +297,7 @@ export function AddressSelector(props: AddressSelectorProps) {
284
297
  fullWidth
285
298
  idValue={cityState}
286
299
  options={cities}
287
- onChange={(_event, value) =>
288
- handleChange(AddressField.City, value?.id)
289
- }
300
+ onChange={(_event, value) => handleChange([AddressField.City, value])}
290
301
  />
291
302
  </Grid>
292
303
  <Grid item xs={12} md={6} lg={fieldSize}>
@@ -298,7 +309,7 @@ export function AddressSelector(props: AddressSelectorProps) {
298
309
  idValue={districtState}
299
310
  options={districts}
300
311
  onChange={(_event, value) =>
301
- handleChange(AddressField.District, value?.id)
312
+ handleChange([AddressField.District, value])
302
313
  }
303
314
  />
304
315
  </Grid>
package/src/ComboBox.tsx CHANGED
@@ -110,7 +110,15 @@ export function ComboBox<
110
110
  // [options] will cause infinite loop
111
111
  const propertyWay = loadData == null;
112
112
  React.useEffect(() => {
113
- if (propertyWay && options != null) setOptions(options);
113
+ if (propertyWay && options != null) {
114
+ setOptions(options);
115
+ if (
116
+ stateValue != null &&
117
+ !options.some((option) => option[idField] === stateValue[idField])
118
+ ) {
119
+ setStateValue(null);
120
+ }
121
+ }
114
122
  }, [options, propertyWay]);
115
123
 
116
124
  // Local default value
@@ -139,7 +139,22 @@ export function ComboBoxMultiple<
139
139
  // [options] will cause infinite loop
140
140
  const propertyWay = loadData == null;
141
141
  React.useEffect(() => {
142
- if (propertyWay && options != null) setOptions(options);
142
+ if (propertyWay && options != null) {
143
+ setOptions(options);
144
+
145
+ if (stateValue != null) {
146
+ if (Array.isArray(stateValue)) {
147
+ const newState = stateValue.filter((item) =>
148
+ options.some((option) => option[idField] === item[idField])
149
+ );
150
+ setStateValue(newState);
151
+ } else if (
152
+ !options.some((option) => option[idField] === stateValue[idField])
153
+ ) {
154
+ setStateValue(null);
155
+ }
156
+ }
157
+ }
143
158
  }, [options, propertyWay]);
144
159
 
145
160
  // Local default value