@etsoo/materialui 1.0.57 → 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.
@@ -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 = { id: number; name: string };
57
+ type T = ListType1;
58
58
  const options: T[] = [
59
- { id: 1, name: 'Name 1' },
60
- { id: 2, name: 'Name 2' },
61
- { id: 3, name: 'Name 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
 
@@ -86,15 +84,22 @@ it('Render multiple SelectEx', async () => {
86
84
  fireEvent.mouseDown(button); // Not click
87
85
 
88
86
  // Get list item
87
+ const itemName1 = await findByText(baseElement, 'Name 1');
88
+ const checkbox1 = itemName1.closest('li')?.querySelector('input');
89
+
90
+ expect(checkbox1?.checked).toBeTruthy();
91
+
89
92
  const itemName3 = await findByText(baseElement, 'Name 3');
90
93
  expect(itemName3.nodeName).toBe('SPAN');
91
94
 
92
95
  // Checkbox
93
- const checkbox = itemName3.closest('li')?.querySelector('input');
96
+ const checkbox3 = itemName3.closest('li')?.querySelector('input');
94
97
 
95
98
  act(() => {
96
- checkbox?.click();
99
+ checkbox3?.click();
97
100
  });
98
101
 
102
+ expect(checkbox3?.checked).toBeTruthy();
103
+
99
104
  expect(itemChangeCallback).toBeCalledTimes(2);
100
105
  });
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();
@@ -66,16 +60,10 @@ export function SelectEx(props) {
66
60
  }, [valueSource]);
67
61
  // Label id
68
62
  const labelId = `selectex-label-${name}`;
69
- // Item checked or not
70
- const itemChecked = (id) => {
71
- if (Array.isArray(valueState))
72
- return valueState.indexOf(id) !== -1;
73
- return valueState === id;
74
- };
75
63
  // Set item
76
64
  const setItemValue = (id) => {
77
65
  var _a;
78
- if (id != valueRef.current) {
66
+ if (id !== valueRef.current) {
79
67
  // Difference
80
68
  const diff = multiple
81
69
  ? Utils.arrayDifferences(id, valueRef.current)
@@ -136,7 +124,7 @@ export function SelectEx(props) {
136
124
  isMounted.current = false;
137
125
  input === null || input === void 0 ? void 0 : input.removeEventListener('change', inputChange);
138
126
  };
139
- }, []);
127
+ }, [multiple]);
140
128
  // Layout
141
129
  return (React.createElement(Stack, { direction: "row" },
142
130
  React.createElement(FormControl, { size: search ? MUGlobal.searchFieldSize : MUGlobal.inputFieldSize, fullWidth: fullWidth, error: error },
@@ -155,7 +143,10 @@ export function SelectEx(props) {
155
143
  return;
156
144
  }
157
145
  // Set item value
158
- const diff = setItemValue(event.target.value);
146
+ const value = event.target.value;
147
+ if (multiple && !Array.isArray(value))
148
+ return;
149
+ const diff = setItemValue(value);
159
150
  if (diff != null) {
160
151
  doItemChange(localOptions, diff, true);
161
152
  }
@@ -183,7 +174,9 @@ export function SelectEx(props) {
183
174
  }, style: itemStyle == null
184
175
  ? undefined
185
176
  : itemStyle(option) },
186
- multiple && (React.createElement(Checkbox, { checked: itemChecked(id) })),
177
+ multiple && (React.createElement(Checkbox, { checked: Array.isArray(valueState)
178
+ ? valueState.includes(id)
179
+ : valueState === id })),
187
180
  React.createElement(ListItemText, { primary: label }),
188
181
  itemIconRenderer && (React.createElement(ListItemRightIcon, null, itemIconRenderer(option[idField])))));
189
182
  })),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/materialui",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
4
4
  "description": "TypeScript Material-UI Implementation",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/src/SelectEx.tsx CHANGED
@@ -8,7 +8,6 @@ import {
8
8
  MenuItem,
9
9
  OutlinedInput,
10
10
  Select,
11
- SelectChangeEvent,
12
11
  SelectProps,
13
12
  Stack
14
13
  } from '@mui/material';
@@ -178,13 +177,10 @@ export function SelectEx<
178
177
 
179
178
  // Local value
180
179
  const v = defaultValue ?? value;
181
- const valueSource = multiple
182
- ? v
183
- ? Array.isArray(v)
184
- ? v
185
- : [v]
186
- : []
187
- : v ?? '';
180
+ const valueSource = React.useMemo(
181
+ () => (multiple ? (v ? (Array.isArray(v) ? v : [v]) : []) : v ?? ''),
182
+ [multiple, v]
183
+ );
188
184
 
189
185
  // Value state
190
186
  const [valueState, setValueStateBase] =
@@ -202,15 +198,9 @@ export function SelectEx<
202
198
  // Label id
203
199
  const labelId = `selectex-label-${name}`;
204
200
 
205
- // Item checked or not
206
- const itemChecked = (id: unknown) => {
207
- if (Array.isArray(valueState)) return valueState.indexOf(id) !== -1;
208
- return valueState === id;
209
- };
210
-
211
201
  // Set item
212
202
  const setItemValue = (id: unknown) => {
213
- if (id != valueRef.current) {
203
+ if (id !== valueRef.current) {
214
204
  // Difference
215
205
  const diff = multiple
216
206
  ? Utils.arrayDifferences(
@@ -279,7 +269,7 @@ export function SelectEx<
279
269
  isMounted.current = false;
280
270
  input?.removeEventListener('change', inputChange);
281
271
  };
282
- }, []);
272
+ }, [multiple]);
283
273
 
284
274
  // Layout
285
275
  return (
@@ -331,7 +321,10 @@ export function SelectEx<
331
321
  }
332
322
 
333
323
  // Set item value
334
- const diff = setItemValue(event.target.value);
324
+ const value = event.target.value;
325
+ if (multiple && !Array.isArray(value)) return;
326
+
327
+ const diff = setItemValue(value);
335
328
  if (diff != null) {
336
329
  doItemChange(localOptions, diff, true);
337
330
  }
@@ -376,7 +369,13 @@ export function SelectEx<
376
369
  }
377
370
  >
378
371
  {multiple && (
379
- <Checkbox checked={itemChecked(id)} />
372
+ <Checkbox
373
+ checked={
374
+ Array.isArray(valueState)
375
+ ? valueState.includes(id)
376
+ : valueState === id
377
+ }
378
+ />
380
379
  )}
381
380
  <ListItemText primary={label} />
382
381
  {itemIconRenderer && (