@etsoo/materialui 1.0.58 → 1.0.59
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.
- package/__tests__/SelectEx.tsx +8 -10
- package/lib/SelectEx.js +7 -10
- package/package.json +1 -1
- package/src/SelectEx.tsx +10 -10
package/__tests__/SelectEx.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { SelectEx } from '../src';
|
|
3
3
|
import { findByText, fireEvent, render, screen } from '@testing-library/react';
|
|
4
4
|
import '@testing-library/jest-dom/extend-expect';
|
|
5
|
-
import { Utils } from '@etsoo/shared';
|
|
5
|
+
import { ListType1, Utils } from '@etsoo/shared';
|
|
6
6
|
import { act } from 'react-dom/test-utils';
|
|
7
7
|
|
|
8
8
|
it('Render SelectEx', async () => {
|
|
@@ -54,16 +54,16 @@ it('Render SelectEx', async () => {
|
|
|
54
54
|
|
|
55
55
|
it('Render multiple SelectEx', async () => {
|
|
56
56
|
// Arrange
|
|
57
|
-
type T =
|
|
57
|
+
type T = ListType1;
|
|
58
58
|
const options: T[] = [
|
|
59
|
-
{ id: 1,
|
|
60
|
-
{ id: 2,
|
|
61
|
-
{ id: 3,
|
|
59
|
+
{ id: '1', label: 'Name 1' },
|
|
60
|
+
{ id: '2', label: 'Name 2' },
|
|
61
|
+
{ id: '3', label: 'Name 3' }
|
|
62
62
|
];
|
|
63
63
|
|
|
64
64
|
const itemChangeCallback = jest.fn((option, userAction) => {
|
|
65
|
-
if (userAction) expect(option.id).toBe(3);
|
|
66
|
-
else expect(option.id).toBe(1);
|
|
65
|
+
if (userAction) expect(option.id).toBe('3');
|
|
66
|
+
else expect(option.id).toBe('1');
|
|
67
67
|
});
|
|
68
68
|
|
|
69
69
|
// Render component
|
|
@@ -72,10 +72,8 @@ it('Render multiple SelectEx', async () => {
|
|
|
72
72
|
options={options}
|
|
73
73
|
name="test"
|
|
74
74
|
onItemChange={itemChangeCallback}
|
|
75
|
-
value={[1, 2]}
|
|
75
|
+
value={['1', '2']}
|
|
76
76
|
multiple
|
|
77
|
-
search
|
|
78
|
-
labelField="name"
|
|
79
77
|
/>
|
|
80
78
|
);
|
|
81
79
|
|
package/lib/SelectEx.js
CHANGED
|
@@ -46,13 +46,7 @@ export function SelectEx(props) {
|
|
|
46
46
|
}, [options, propertyWay]);
|
|
47
47
|
// Local value
|
|
48
48
|
const v = defaultValue !== null && defaultValue !== void 0 ? defaultValue : value;
|
|
49
|
-
const valueSource = multiple
|
|
50
|
-
? v
|
|
51
|
-
? Array.isArray(v)
|
|
52
|
-
? v
|
|
53
|
-
: [v]
|
|
54
|
-
: []
|
|
55
|
-
: v !== null && v !== void 0 ? v : '';
|
|
49
|
+
const valueSource = React.useMemo(() => (multiple ? (v ? (Array.isArray(v) ? v : [v]) : []) : v !== null && v !== void 0 ? v : ''), [multiple, v]);
|
|
56
50
|
// Value state
|
|
57
51
|
const [valueState, setValueStateBase] = React.useState(valueSource);
|
|
58
52
|
const valueRef = React.useRef();
|
|
@@ -69,7 +63,7 @@ export function SelectEx(props) {
|
|
|
69
63
|
// Set item
|
|
70
64
|
const setItemValue = (id) => {
|
|
71
65
|
var _a;
|
|
72
|
-
if (id
|
|
66
|
+
if (id !== valueRef.current) {
|
|
73
67
|
// Difference
|
|
74
68
|
const diff = multiple
|
|
75
69
|
? Utils.arrayDifferences(id, valueRef.current)
|
|
@@ -130,7 +124,7 @@ export function SelectEx(props) {
|
|
|
130
124
|
isMounted.current = false;
|
|
131
125
|
input === null || input === void 0 ? void 0 : input.removeEventListener('change', inputChange);
|
|
132
126
|
};
|
|
133
|
-
}, []);
|
|
127
|
+
}, [multiple]);
|
|
134
128
|
// Layout
|
|
135
129
|
return (React.createElement(Stack, { direction: "row" },
|
|
136
130
|
React.createElement(FormControl, { size: search ? MUGlobal.searchFieldSize : MUGlobal.inputFieldSize, fullWidth: fullWidth, error: error },
|
|
@@ -149,7 +143,10 @@ export function SelectEx(props) {
|
|
|
149
143
|
return;
|
|
150
144
|
}
|
|
151
145
|
// Set item value
|
|
152
|
-
const
|
|
146
|
+
const value = event.target.value;
|
|
147
|
+
if (multiple && !Array.isArray(value))
|
|
148
|
+
return;
|
|
149
|
+
const diff = setItemValue(value);
|
|
153
150
|
if (diff != null) {
|
|
154
151
|
doItemChange(localOptions, diff, true);
|
|
155
152
|
}
|
package/package.json
CHANGED
package/src/SelectEx.tsx
CHANGED
|
@@ -177,13 +177,10 @@ export function SelectEx<
|
|
|
177
177
|
|
|
178
178
|
// Local value
|
|
179
179
|
const v = defaultValue ?? value;
|
|
180
|
-
const valueSource =
|
|
181
|
-
? v
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
: [v]
|
|
185
|
-
: []
|
|
186
|
-
: v ?? '';
|
|
180
|
+
const valueSource = React.useMemo(
|
|
181
|
+
() => (multiple ? (v ? (Array.isArray(v) ? v : [v]) : []) : v ?? ''),
|
|
182
|
+
[multiple, v]
|
|
183
|
+
);
|
|
187
184
|
|
|
188
185
|
// Value state
|
|
189
186
|
const [valueState, setValueStateBase] =
|
|
@@ -203,7 +200,7 @@ export function SelectEx<
|
|
|
203
200
|
|
|
204
201
|
// Set item
|
|
205
202
|
const setItemValue = (id: unknown) => {
|
|
206
|
-
if (id
|
|
203
|
+
if (id !== valueRef.current) {
|
|
207
204
|
// Difference
|
|
208
205
|
const diff = multiple
|
|
209
206
|
? Utils.arrayDifferences(
|
|
@@ -272,7 +269,7 @@ export function SelectEx<
|
|
|
272
269
|
isMounted.current = false;
|
|
273
270
|
input?.removeEventListener('change', inputChange);
|
|
274
271
|
};
|
|
275
|
-
}, []);
|
|
272
|
+
}, [multiple]);
|
|
276
273
|
|
|
277
274
|
// Layout
|
|
278
275
|
return (
|
|
@@ -324,7 +321,10 @@ export function SelectEx<
|
|
|
324
321
|
}
|
|
325
322
|
|
|
326
323
|
// Set item value
|
|
327
|
-
const
|
|
324
|
+
const value = event.target.value;
|
|
325
|
+
if (multiple && !Array.isArray(value)) return;
|
|
326
|
+
|
|
327
|
+
const diff = setItemValue(value);
|
|
328
328
|
if (diff != null) {
|
|
329
329
|
doItemChange(localOptions, diff, true);
|
|
330
330
|
}
|