/
Items.tsx
416 lines (371 loc) · 14.1 KB
/
Items.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import * as React from 'react';
import { useMemo, useState, useRef, useEffect } from 'react';
import { AutocompleteRenderGetTagProps, AutocompleteRenderOptionState } from "@mui/material/Autocomplete";
import { AutocompleteRenderInputParams } from "@mui/material/Autocomplete";
import Autocomplete from "@mui/material/Autocomplete";
import CircularProgress from "@mui/material/CircularProgress";
import MatTextField from "@mui/material/TextField";
import Checkbox from '@mui/material/Checkbox';
import Chip from "@mui/material/Chip";
import VirtualListBox from '../../common/VirtualListBox';
import compareArray from '../../../../../utils/compareArray';
import debounce from '../../../../../utils/hof/debounce';
import isObject from '../../../../../utils/isObject';
import { useOneState } from '../../../context/StateProvider';
import { useOneProps } from '../../../context/PropsProvider';
import { useOnePayload } from '../../../context/PayloadProvider';
import { useOneMenu } from '../../../context/MenuProvider';
import { useSubject } from '../../../../../hooks/useSubject';
import { useAsyncAction } from '../../../../../hooks/useAsyncAction';
import { useActualValue } from '../../../../../hooks/useActualValue';
import { useRenderWaiter } from '../../../../../hooks/useRenderWaiter';
import { useReloadTrigger } from '../../../../../hooks/useReloadTrigger';
import useItemModal from '../../../../../hooks/useItemModal';
import useMediaContext from '../../../../../hooks/useMediaContext';
import CheckBoxIcon from '@mui/icons-material/CheckBox';
import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank';
import { IItemsSlot } from '../../../slots/ItemsSlot';
import FieldType from '../../../../../model/FieldType';
const icon = <CheckBoxOutlineBlankIcon fontSize="small" />;
const checkedIcon = <CheckBoxIcon fontSize="small" />;
const EMPTY_ARRAY = [] as any;
const MOUSE_OUT_DEBOUNCE = 45;
const getArrayHash = (value: any) =>
Object.values<string>(value || {})
.sort((a, b) => b.localeCompare(a))
.join('-');
interface IState {
options: string[];
labels: Record<string, string>;
}
/**
* @param Items - The main function that renders the Autocomplete component.
* @param Items.value - The value of the autocomplete field. It can be a string or an object.
* @param Items.disabled - Determines whether the autocomplete field is disabled or not.
* @param Items.readonly - Determines whether the autocomplete field is readonly or not.
* @param Items.description - The description of the autocomplete field.
* @param Items.placeholder - The placeholder text of the autocomplete field.
* @param Items.outlined - Determines whether the autocomplete field has an outlined style or not.
* @param Items.itemList - The list of items to be populated in the autocomplete dropdown.
* @param Items.freeSolo - Determines whether the user can input values that are not in the itemList.
* @param Items.noDeselect - Determines whether the user can deselect values in the autocomplete field.
* @param Items.virtualListBox - Determines whether to use a virtual listbox for rendering the autocomplete dropdown.
* @param Items.watchItemList - Determines whether to watch for changes in the itemList.
* @param Items.labelShrink - Determines whether to shrink the label when the autocomplete field has a value.
* @param Items.dirty - Determines whether the autocomplete field has been modified.
* @param Items.invalid - Determines whether the autocomplete field has an invalid value.
* @param Items.incorrect - Determines whether the autocomplete field has an incorrect value.
* @param Items.title - The title text of the autocomplete field.
* @param Items.tr - A translation function that takes a string and returns a translated string.
* @param Items.onChange - A callback function that is called when the value of the autocomplete field changes.
* @param Items.withContextMenu - Determines whether to show a context menu for the autocomplete field.
* @returns The Autocomplete component.
*/
export const Items = ({
value: upperValue,
disabled,
readonly,
description,
placeholder,
outlined = false,
itemList = [],
freeSolo,
noDeselect,
virtualListBox,
watchItemList,
labelShrink,
dirty,
invalid,
incorrect,
title,
tr = (s) => s.toString(),
onChange,
withContextMenu,
}: IItemsSlot) => {
const { isMobile } = useMediaContext();
const { object } = useOneState();
const payload = useOnePayload();
const { requestSubject } = useOneMenu();
const { reloadTrigger, doReload } = useReloadTrigger();
const [state, setState] = useState<IState>(() => ({
options: [],
labels: {},
}));
const [opened, setOpened] = useState(false);
const initComplete = useRef(false);
const waitForRender = useRenderWaiter([state], 10);
const labels$ = useActualValue(state.labels);
/**
* Memoized value casted to array.
*
* @type {Array}
* @param upperValue - The value used to compute the memoized array.
* @returns - The memoized array value.
*/
const arrayValue = useMemo(() => {
if (typeof upperValue === 'string') {
return [upperValue];
}
if (upperValue) {
const result = Object.values<string>(upperValue);
return isObject(result) ? [] : result;
}
return [];
}, [upperValue]);
const pickModal = useItemModal({
data: object,
payload,
itemList,
keepRaw: false,
onValueChange: onChange,
placeholder,
tip: undefined,
title,
tr,
type: FieldType.Items,
value: arrayValue,
});
const prevValue = useRef(arrayValue);
/**
* Memoizes the value based on the input array value.
*
* @param arrayValue - The array value.
* @returns - The memoized value.
*/
const value = useMemo(() => {
if (compareArray(prevValue.current, arrayValue)) {
return prevValue.current;
}
prevValue.current = arrayValue;
return arrayValue;
}, [arrayValue]);
const value$ = useActualValue(value);
const {
fallback,
} = useOneProps();
const {
loading,
execute,
} = useAsyncAction(async (object) => {
const labels: Record<string, string> = {};
itemList = itemList || [];
const options: string[] = [...new Set(Object.values(typeof itemList === 'function' ? await Promise.resolve(itemList(object, payload)) : itemList))];
await Promise.all(options.map(async (item) => labels[item] = await Promise.resolve(tr(item, object, payload))));
if (freeSolo) {
value.forEach((item) => {
if (!options.includes(item)) {
options.push(item);
}
});
}
setState({ options, labels });
initComplete.current = true;
await waitForRender();
}, {
fallback,
});
const valueHash = getArrayHash(value);
const prevObject = useRef<any>(null);
const initial = useRef(true);
useEffect(() => {
if (!initial.current) {
if (prevObject.current === object) {
return;
}
if (!watchItemList) {
return;
}
}
prevObject.current = object;
initial.current = false;
execute(object);
}, [
valueHash,
disabled,
dirty,
invalid,
incorrect,
object,
readonly,
]);
const changeSubject = useSubject<void>();
useEffect(() => {
if (!opened) {
return;
}
let unsubscribeRef = changeSubject.once(() => {
const handler = debounce(({ clientX, clientY }: MouseEvent) => {
const target = document.elementFromPoint(clientX, clientY);
if (!target?.closest('.MuiAutocomplete-popper')) {
setOpened(false);
doReload();
}
}, MOUSE_OUT_DEBOUNCE);
document.addEventListener('mousemove', handler);
unsubscribeRef = () => {
document.removeEventListener('mousemove', handler);
handler.clear();
};
});
return () => unsubscribeRef();
}, [opened]);
useEffect(() => withContextMenu && requestSubject.subscribe(() => {
setOpened(false);
}), []);
/**
* Handles a change event by calling the provided onChange function with the value.
* If the value is an empty string or undefined, null is passed to the onChange function.
* After calling onChange, the changeSubject is notified.
*
* @param value - The value to be passed to the onChange function.
* @returns
*/
const handleChange = (value: any) => {
onChange(value?.length ? value : null);
changeSubject.next();
};
/**
* Renders tags based on the given values using the Autocomplete component.
*
* @param value - An array of values to render as tags.
* @param getTagProps - A function that receives an index and returns props for a tag.
* @returns An array of Chip components representing the rendered tags.
*/
const renderTags = (value: any[], getTagProps: AutocompleteRenderGetTagProps) => {
const { current: labels } = labels$;
return value.map((option: string, index: number) => (
<Chip
variant={outlined ? "outlined" : "filled"}
label={freeSolo ? option : (labels[option] || `${option} (unknown)`)}
{...getTagProps({ index })}
/>
))
};
/**
* Retrieves the option label based on the provided value.
*
* @param v - The value for which to retrieve the label.
* @returns The label for the given value.
*/
const getOptionLabel = (v: string) => {
const { current: labels } = labels$;
if (freeSolo) {
return v;
}
return labels[v] || `${v} (unknown)`;
};
/**
* Renders an Autocomplete input field with given parameters.
*
* @param loading - Indicates if the input field is in a loading state.
* @param readonly - Indicates if the input field is set to readonly.
* @param params - AutocompleteRenderInputParams object containing input parameters.
* @returns - JSX element representing the input field.
*/
const createRenderInput = (loading: boolean, readonly: boolean) => (params: AutocompleteRenderInputParams) => (
<MatTextField
{...params}
sx={{
...(!outlined && {
position: 'relative',
mt: 1,
'& .MuiFormHelperText-root': {
position: 'absolute',
top: '100%',
},
})
}}
variant={outlined ? "outlined" : "standard"}
label={title}
helperText={(dirty && (invalid || incorrect)) || description}
placeholder={loading ? undefined : value$.current.length ? undefined : placeholder}
error={dirty && (invalid !== null || incorrect !== null)}
InputProps={{
...params.InputProps,
readOnly: readonly,
endAdornment: (
<>
{loading ? <CircularProgress color="inherit" size={20} /> : null}
{params.InputProps.endAdornment}
</>
),
}}
InputLabelProps={{
...params.InputLabelProps,
...(labelShrink && { shrink: true }),
}}
/>
);
/**
* Render an option for the Autocomplete component.
*
* @param props - The HTML attributes for the <li> element.
* @param option - The option to render.
* @param state - The state of the option being rendered.
* @returns - The rendered option.
*/
const renderOption = (props: React.HTMLAttributes<HTMLLIElement>, option: any, state: AutocompleteRenderOptionState) => {
const { current: labels } = labels$;
return (
<li {...props}>
<Checkbox
icon={icon}
checkedIcon={checkedIcon}
style={{ marginRight: 8 }}
checked={state.selected}
/>
{freeSolo ? option : (labels[option] || `${option} (unknown)`)}
</li>
);
};
if (loading || !initComplete.current) {
return (
<Autocomplete
multiple
disableCloseOnSelect
disableClearable={noDeselect}
loading
disabled
freeSolo={freeSolo}
onChange={() => null}
value={EMPTY_ARRAY}
options={EMPTY_ARRAY}
ListboxComponent={virtualListBox ? VirtualListBox : undefined}
getOptionLabel={getOptionLabel}
renderTags={renderTags}
renderInput={createRenderInput(true, true)}
renderOption={renderOption}
/>
);
}
return (
<Autocomplete
key={reloadTrigger}
multiple
loading={loading}
disabled={disabled}
disableCloseOnSelect
disableClearable={noDeselect}
freeSolo={freeSolo}
readOnly={readonly}
open={opened}
onChange={({ }, value) => handleChange(value)}
onOpen={() => {
if (!isMobile) {
setOpened(true)
return;
}
pickModal();
}}
onClose={() => setOpened(false)}
getOptionLabel={getOptionLabel}
ListboxComponent={virtualListBox ? VirtualListBox : undefined}
value={value}
options={state.options}
renderTags={renderTags}
renderInput={createRenderInput(false, !!readonly)}
renderOption={renderOption}
/>
);
};
export default Items;