@cloud-ru/ds-fields 2.5.8 → 3.0.0
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/CHANGELOG.md +6 -0
- package/README.md +2 -1
- package/dist/cjs/components/FieldSelect/FieldSelect.js +136 -65
- package/dist/cjs/components/FieldSelect/constants.d.ts +15 -0
- package/dist/cjs/components/FieldSelect/constants.js +32 -1
- package/dist/cjs/components/FieldSelect/hooks.d.ts +32 -0
- package/dist/cjs/components/FieldSelect/hooks.js +94 -0
- package/dist/cjs/components/FieldSelect/types.d.ts +24 -8
- package/dist/cjs/components/FieldSelect/utils/customOption.d.ts +20 -0
- package/dist/cjs/components/FieldSelect/utils/customOption.js +48 -0
- package/dist/cjs/components/FieldSelect/utils/extract.d.ts +11 -0
- package/dist/cjs/components/FieldSelect/utils/extract.js +39 -0
- package/dist/cjs/components/FieldSelect/utils/filter.d.ts +3 -0
- package/dist/cjs/components/FieldSelect/utils/filter.js +40 -0
- package/dist/cjs/components/FieldSelect/utils/index.d.ts +6 -0
- package/dist/cjs/components/FieldSelect/utils/index.js +22 -0
- package/dist/cjs/components/FieldSelect/utils/items.d.ts +9 -0
- package/dist/cjs/components/FieldSelect/utils/items.js +47 -0
- package/dist/cjs/components/FieldSelect/utils/selection.d.ts +11 -0
- package/dist/cjs/components/FieldSelect/utils/selection.js +18 -0
- package/dist/cjs/components/FieldSelect/utils/tagSize.d.ts +4 -0
- package/dist/cjs/components/FieldSelect/utils/tagSize.js +5 -0
- package/dist/esm/components/FieldSelect/FieldSelect.js +138 -67
- package/dist/esm/components/FieldSelect/constants.d.ts +15 -0
- package/dist/esm/components/FieldSelect/constants.js +31 -0
- package/dist/esm/components/FieldSelect/hooks.d.ts +32 -0
- package/dist/esm/components/FieldSelect/hooks.js +91 -0
- package/dist/esm/components/FieldSelect/types.d.ts +24 -8
- package/dist/esm/components/FieldSelect/utils/customOption.d.ts +20 -0
- package/dist/esm/components/FieldSelect/utils/customOption.js +41 -0
- package/dist/esm/components/FieldSelect/utils/extract.d.ts +11 -0
- package/dist/esm/components/FieldSelect/utils/extract.js +34 -0
- package/dist/esm/components/FieldSelect/utils/filter.d.ts +3 -0
- package/dist/esm/components/FieldSelect/utils/filter.js +36 -0
- package/dist/esm/components/FieldSelect/utils/index.d.ts +6 -0
- package/dist/esm/components/FieldSelect/utils/index.js +6 -0
- package/dist/esm/components/FieldSelect/utils/items.d.ts +9 -0
- package/dist/esm/components/FieldSelect/utils/items.js +41 -0
- package/dist/esm/components/FieldSelect/utils/selection.d.ts +11 -0
- package/dist/esm/components/FieldSelect/utils/selection.js +14 -0
- package/dist/esm/components/FieldSelect/utils/tagSize.d.ts +4 -0
- package/dist/esm/components/FieldSelect/utils/tagSize.js +2 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +8 -8
- package/src/components/FieldSelect/FieldSelect.tsx +190 -82
- package/src/components/FieldSelect/constants.ts +36 -0
- package/src/components/FieldSelect/hooks.ts +170 -0
- package/src/components/FieldSelect/types.ts +80 -53
- package/src/components/FieldSelect/utils/customOption.ts +69 -0
- package/src/components/FieldSelect/utils/extract.ts +49 -0
- package/src/components/FieldSelect/utils/filter.ts +51 -0
- package/src/components/FieldSelect/utils/index.ts +6 -0
- package/src/components/FieldSelect/utils/items.ts +58 -0
- package/src/components/FieldSelect/utils/selection.ts +25 -0
- package/src/components/FieldSelect/utils/tagSize.ts +5 -0
- package/dist/cjs/components/FieldSelect/utils.d.ts +0 -19
- package/dist/cjs/components/FieldSelect/utils.js +0 -103
- package/dist/esm/components/FieldSelect/utils.d.ts +0 -19
- package/dist/esm/components/FieldSelect/utils.js +0 -92
- package/src/components/FieldSelect/utils.ts +0 -132
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-ru/ds-fields",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Поля ввода с единой обёрткой label/caption/hint/error и общими осями size/validationState — текст, пароль, число, ползунок, дата, время, выбор, цвет.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -41,21 +41,21 @@
|
|
|
41
41
|
"react-textarea-autosize": "^8.5.9",
|
|
42
42
|
"@cloud-ru/ds-adaptive": "^1.0.2",
|
|
43
43
|
"@cloud-ru/ds-bottom-sheet": "^2.0.3",
|
|
44
|
-
"@cloud-ru/ds-calendar": "^1.1.
|
|
44
|
+
"@cloud-ru/ds-calendar": "^1.1.14",
|
|
45
|
+
"@cloud-ru/ds-divider": "^1.0.2",
|
|
45
46
|
"@cloud-ru/ds-button": "^1.0.4",
|
|
46
|
-
"@cloud-ru/ds-color-picker": "^1.0.16",
|
|
47
47
|
"@cloud-ru/ds-dropdown": "^1.1.4",
|
|
48
|
-
"@cloud-ru/ds-
|
|
49
|
-
"@cloud-ru/ds-input-private": "^1.0.12",
|
|
48
|
+
"@cloud-ru/ds-color-picker": "^1.0.16",
|
|
50
49
|
"@cloud-ru/ds-icons": "^1.6.0",
|
|
51
|
-
"@cloud-ru/ds-list": "^2.4.0",
|
|
52
50
|
"@cloud-ru/ds-field-decorator": "^1.1.5",
|
|
51
|
+
"@cloud-ru/ds-input-private": "^1.0.12",
|
|
52
|
+
"@cloud-ru/ds-materials": "^2.0.1",
|
|
53
53
|
"@cloud-ru/ds-loader": "^1.0.3",
|
|
54
|
+
"@cloud-ru/ds-list": "^3.0.0",
|
|
54
55
|
"@cloud-ru/ds-scroll": "^1.0.5",
|
|
55
56
|
"@cloud-ru/ds-skeleton": "^1.0.2",
|
|
56
|
-
"@cloud-ru/ds-slider": "^1.0.13",
|
|
57
57
|
"@cloud-ru/ds-tag": "^1.1.14",
|
|
58
|
-
"@cloud-ru/ds-
|
|
58
|
+
"@cloud-ru/ds-slider": "^1.0.13",
|
|
59
59
|
"@cloud-ru/ds-tooltip": "^1.0.13",
|
|
60
60
|
"@cloud-ru/ds-utils": "^1.2.0"
|
|
61
61
|
}
|
|
@@ -2,7 +2,7 @@ import { ChevronDownSVG } from '@cloud-ru/ds-icons/interface/system';
|
|
|
2
2
|
import { InputPrivate, useButtonNavigation, useClearButton } from '@cloud-ru/ds-input-private';
|
|
3
3
|
import { Droplist, ItemId } from '@cloud-ru/ds-list';
|
|
4
4
|
import { Tag } from '@cloud-ru/ds-tag';
|
|
5
|
-
import { useLayoutEffect } from '@cloud-ru/ds-utils';
|
|
5
|
+
import { isBrowser, useLayoutEffect } from '@cloud-ru/ds-utils';
|
|
6
6
|
import cn from 'classnames';
|
|
7
7
|
import mergeRefs from 'merge-refs';
|
|
8
8
|
import {
|
|
@@ -33,14 +33,17 @@ import {
|
|
|
33
33
|
useCopyButton,
|
|
34
34
|
} from '../shared';
|
|
35
35
|
import fieldStyles from '../shared/styles.module.scss';
|
|
36
|
+
import { useCustomOption } from './hooks';
|
|
36
37
|
import styles from './styles.module.scss';
|
|
37
|
-
import { FieldSelectProps } from './types';
|
|
38
|
+
import { FieldSelectMultipleProps, FieldSelectProps, FieldSelectSingleProps } from './types';
|
|
38
39
|
import {
|
|
39
40
|
extractAppearance,
|
|
40
41
|
extractLabel,
|
|
41
42
|
filterItems,
|
|
42
43
|
findItem,
|
|
43
44
|
flatten,
|
|
45
|
+
getMissingSelectedItems,
|
|
46
|
+
getSelectedIds,
|
|
44
47
|
isMultiple,
|
|
45
48
|
TAG_SIZE_MAP,
|
|
46
49
|
WithIdContent,
|
|
@@ -77,7 +80,6 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
77
80
|
searchable = true,
|
|
78
81
|
search,
|
|
79
82
|
autocomplete = false,
|
|
80
|
-
addOptionByEnter = false,
|
|
81
83
|
resetSearchOnOptionSelection = true,
|
|
82
84
|
showClearButton = true,
|
|
83
85
|
showCopyButton = true,
|
|
@@ -86,6 +88,8 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
86
88
|
name,
|
|
87
89
|
autoFocus,
|
|
88
90
|
layoutPresets,
|
|
91
|
+
value,
|
|
92
|
+
onChange,
|
|
89
93
|
onFocus: onFocusProp,
|
|
90
94
|
onBlur: onBlurProp,
|
|
91
95
|
enableFuzzySearch = true,
|
|
@@ -111,12 +115,18 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
111
115
|
const chips = multiple ? (props.chips ?? true) : false;
|
|
112
116
|
const removeByBackspace = multiple ? (props.removeByBackspace ?? true) : false;
|
|
113
117
|
const closeDroplistOnItemClick = props.closeDroplistOnItemClick ?? !multiple;
|
|
118
|
+
const allowCustomOption = props.allowCustomOption === true;
|
|
119
|
+
const addCustomOptionTriggers = allowCustomOption ? props.addCustomOptionTriggers : undefined;
|
|
120
|
+
|
|
121
|
+
const onSingleChange = onChange as FieldSelectSingleProps['onChange'];
|
|
122
|
+
const onMultipleChange = onChange as FieldSelectMultipleProps['onChange'];
|
|
114
123
|
|
|
115
124
|
const resolvedAutoFocus = useAdaptiveAutoFocus(autoFocus, layoutPresets);
|
|
116
125
|
|
|
117
126
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
118
127
|
// Отдельный ref на реальный <input>: `inputRef.current` перезаписывается span-обёрткой PopoverPrivate.
|
|
119
128
|
const inputElementRef = useRef<HTMLInputElement>(null);
|
|
129
|
+
const restoreInputFocusRef = useRef(false);
|
|
120
130
|
const clearButtonRef = useRef<HTMLButtonElement>(null);
|
|
121
131
|
const copyButtonRef = useRef<HTMLButtonElement>(null);
|
|
122
132
|
const contentRef = useRef<HTMLDivElement>(null);
|
|
@@ -139,11 +149,11 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
139
149
|
const singleControlled = useRef(false);
|
|
140
150
|
const multipleControlled = useRef(false);
|
|
141
151
|
|
|
142
|
-
if (!multiple &&
|
|
152
|
+
if (!multiple && value !== undefined) {
|
|
143
153
|
singleControlled.current = true;
|
|
144
154
|
}
|
|
145
155
|
|
|
146
|
-
if (multiple && Array.isArray(
|
|
156
|
+
if (multiple && Array.isArray(value)) {
|
|
147
157
|
multipleControlled.current = true;
|
|
148
158
|
}
|
|
149
159
|
|
|
@@ -152,11 +162,11 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
152
162
|
return undefined;
|
|
153
163
|
}
|
|
154
164
|
|
|
155
|
-
const raw = singleControlled.current ? (
|
|
165
|
+
const raw = singleControlled.current ? (value as ItemId | undefined) : singleLocal;
|
|
156
166
|
|
|
157
167
|
// `''` = «значение не выбрано»: формы отдают пустое значение строкой.
|
|
158
168
|
return raw === '' ? undefined : raw;
|
|
159
|
-
}, [multiple,
|
|
169
|
+
}, [multiple, value, singleLocal]);
|
|
160
170
|
|
|
161
171
|
const multipleValue = useMemo<ItemId[]>(() => {
|
|
162
172
|
if (!multiple) {
|
|
@@ -164,11 +174,11 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
164
174
|
}
|
|
165
175
|
|
|
166
176
|
if (multipleControlled.current) {
|
|
167
|
-
return Array.isArray(
|
|
177
|
+
return Array.isArray(value) ? (value as ItemId[]) : [];
|
|
168
178
|
}
|
|
169
179
|
|
|
170
180
|
return multipleLocal;
|
|
171
|
-
}, [multiple,
|
|
181
|
+
}, [multiple, value, multipleLocal]);
|
|
172
182
|
|
|
173
183
|
const effectiveValidationState = useMemo(
|
|
174
184
|
() => (error ? VALIDATION_STATE.Error : validationState),
|
|
@@ -177,18 +187,31 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
177
187
|
|
|
178
188
|
const allItems = useMemo(() => [...(pinTop ?? []), ...items, ...(pinBottom ?? [])], [items, pinTop, pinBottom]);
|
|
179
189
|
|
|
190
|
+
const selectedIds = useMemo(
|
|
191
|
+
() => getSelectedIds({ multiple, multipleValue, singleValue }),
|
|
192
|
+
[multiple, multipleValue, singleValue],
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// Выбранное, которого нет в `items` (кастом по allowCustomOption, ленивая подгрузка) — в список, не только в чип.
|
|
196
|
+
const missingSelectedItems = useMemo(() => getMissingSelectedItems(selectedIds, allItems), [selectedIds, allItems]);
|
|
197
|
+
|
|
198
|
+
const itemsWithPlaceholders = useMemo(
|
|
199
|
+
() => (missingSelectedItems.length > 0 ? [...missingSelectedItems, ...items] : items),
|
|
200
|
+
[missingSelectedItems, items],
|
|
201
|
+
);
|
|
202
|
+
|
|
180
203
|
// Выбранное значение переживает смену `items` (серверный поиск, ленивая подгрузка).
|
|
181
204
|
const seenItems = useRef(new Map<ItemId, WithIdContent>());
|
|
182
205
|
|
|
183
206
|
const resolveItem = useMemo(() => {
|
|
184
|
-
for (const item of flatten(allItems)) {
|
|
207
|
+
for (const item of flatten([...missingSelectedItems, ...allItems])) {
|
|
185
208
|
if (item.id !== undefined) {
|
|
186
209
|
seenItems.current.set(item.id, item);
|
|
187
210
|
}
|
|
188
211
|
}
|
|
189
212
|
|
|
190
|
-
return (id: ItemId) => findItem(allItems, id) ?? seenItems.current.get(id);
|
|
191
|
-
}, [allItems]);
|
|
213
|
+
return (id: ItemId) => findItem(allItems, id) ?? findItem(missingSelectedItems, id) ?? seenItems.current.get(id);
|
|
214
|
+
}, [allItems, missingSelectedItems]);
|
|
192
215
|
|
|
193
216
|
const selectedPairs = useMemo(() => {
|
|
194
217
|
if (!multiple || multipleValue.length === 0) {
|
|
@@ -213,6 +236,8 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
213
236
|
[selectedOptionFormatter],
|
|
214
237
|
);
|
|
215
238
|
|
|
239
|
+
const formatSelected = 'formatSelected' in props && props.formatSelected;
|
|
240
|
+
|
|
216
241
|
const selectedLabel = useMemo(() => {
|
|
217
242
|
if (!multiple) {
|
|
218
243
|
if (singleValue === undefined) {
|
|
@@ -229,12 +254,12 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
229
254
|
return '';
|
|
230
255
|
}
|
|
231
256
|
|
|
232
|
-
if (
|
|
233
|
-
return
|
|
257
|
+
if (formatSelected) {
|
|
258
|
+
return formatSelected(selectedPairs);
|
|
234
259
|
}
|
|
235
260
|
|
|
236
261
|
return selectedPairs.map(formatPair).join(', ');
|
|
237
|
-
}, [multiple, singleValue, selectedPairs, chips,
|
|
262
|
+
}, [multiple, singleValue, selectedPairs, chips, formatSelected, formatPair, resolveItem]);
|
|
238
263
|
|
|
239
264
|
const [localInput, setLocalInput] = useState(search?.defaultValue ?? selectedLabel);
|
|
240
265
|
const inputValue = search?.value !== undefined ? search.value : localInput;
|
|
@@ -265,14 +290,14 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
265
290
|
setInputValue(selectedLabel);
|
|
266
291
|
}, [selectedLabel, typing, resetSearchOnOptionSelection, setInputValue]);
|
|
267
292
|
|
|
268
|
-
// При autocomplete фильтрует сервер — items берутся как
|
|
293
|
+
// При autocomplete фильтрует сервер — items берутся как есть, плюс выбранное вне списка.
|
|
269
294
|
const filteredItems = useMemo(() => {
|
|
270
295
|
if (!searchable || autocomplete || !typing) {
|
|
271
|
-
return
|
|
296
|
+
return itemsWithPlaceholders;
|
|
272
297
|
}
|
|
273
298
|
|
|
274
|
-
return filterItems(
|
|
275
|
-
}, [searchable, autocomplete, typing,
|
|
299
|
+
return filterItems(itemsWithPlaceholders, inputValue, enableFuzzySearch);
|
|
300
|
+
}, [searchable, autocomplete, typing, itemsWithPlaceholders, inputValue, enableFuzzySearch]);
|
|
276
301
|
|
|
277
302
|
const filteredPinTop = useMemo(() => {
|
|
278
303
|
if (!searchable || autocomplete || !typing || !pinTop) {
|
|
@@ -290,61 +315,118 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
290
315
|
return filterItems(pinBottom, inputValue, enableFuzzySearch);
|
|
291
316
|
}, [searchable, autocomplete, typing, pinBottom, inputValue, enableFuzzySearch]);
|
|
292
317
|
|
|
293
|
-
const
|
|
294
|
-
(next:
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
if (openProp === undefined) {
|
|
298
|
-
setOpenLocal(next);
|
|
318
|
+
const handleSingleChange = useCallback(
|
|
319
|
+
(next: ItemId | undefined) => {
|
|
320
|
+
if (multiple) {
|
|
321
|
+
return;
|
|
299
322
|
}
|
|
300
323
|
|
|
301
|
-
|
|
324
|
+
if (value === undefined) {
|
|
325
|
+
setSingleLocal(next);
|
|
326
|
+
}
|
|
302
327
|
|
|
303
|
-
|
|
328
|
+
onSingleChange?.(next);
|
|
329
|
+
setTyping(false);
|
|
330
|
+
},
|
|
331
|
+
[multiple, value, onSingleChange],
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
const restoreInputFocus = useCallback(() => {
|
|
335
|
+
restoreInputFocusRef.current = isBrowser() && inputElementRef.current === document.activeElement;
|
|
336
|
+
}, []);
|
|
337
|
+
|
|
338
|
+
const handleMultipleChange = useCallback(
|
|
339
|
+
(next: ItemId[]) => {
|
|
340
|
+
if (!multiple) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
restoreInputFocus();
|
|
345
|
+
|
|
346
|
+
if (value === undefined) {
|
|
347
|
+
setMultipleLocal(next);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
onMultipleChange?.(next);
|
|
351
|
+
|
|
352
|
+
if (searchable) {
|
|
304
353
|
setTyping(false);
|
|
305
354
|
|
|
306
355
|
if (resetSearchOnOptionSelection) {
|
|
307
|
-
setInputValue(
|
|
356
|
+
setInputValue('');
|
|
308
357
|
}
|
|
309
358
|
}
|
|
310
359
|
},
|
|
311
|
-
[
|
|
360
|
+
[multiple, onMultipleChange, value, searchable, resetSearchOnOptionSelection, setInputValue, restoreInputFocus],
|
|
312
361
|
);
|
|
313
362
|
|
|
314
|
-
const
|
|
315
|
-
if (multiple) {
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
if (props.value === undefined) {
|
|
320
|
-
setSingleLocal(next);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
props.onChange?.(next);
|
|
363
|
+
const resetCustomOptionSearch = useCallback(() => {
|
|
324
364
|
setTyping(false);
|
|
325
|
-
};
|
|
326
365
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
return;
|
|
366
|
+
if (resetSearchOnOptionSelection) {
|
|
367
|
+
setInputValue(multiple ? '' : selectedLabel);
|
|
330
368
|
}
|
|
369
|
+
}, [resetSearchOnOptionSelection, setInputValue, multiple, selectedLabel]);
|
|
331
370
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
371
|
+
const {
|
|
372
|
+
listRef,
|
|
373
|
+
suppressCloseCommit,
|
|
374
|
+
enableCloseCommit,
|
|
375
|
+
tryCommitOnClose,
|
|
376
|
+
tryCommitOnBlur,
|
|
377
|
+
tryCommitFromKeyboard,
|
|
378
|
+
handleListSingleChange,
|
|
379
|
+
handleListMultipleChange,
|
|
380
|
+
} = useCustomOption({
|
|
381
|
+
allowCustomOption,
|
|
382
|
+
addCustomOptionTriggers,
|
|
383
|
+
multiple,
|
|
384
|
+
inputValue,
|
|
385
|
+
typing,
|
|
386
|
+
multipleValue,
|
|
387
|
+
commitSingle: handleSingleChange,
|
|
388
|
+
commitMultiple: handleMultipleChange,
|
|
389
|
+
resetSearch: resetCustomOptionSearch,
|
|
390
|
+
inputElementRef,
|
|
391
|
+
clearButtonRef,
|
|
392
|
+
copyButtonRef,
|
|
393
|
+
footerActiveElementsRefs,
|
|
394
|
+
});
|
|
335
395
|
|
|
336
|
-
|
|
396
|
+
const handleOpenChange = useCallback(
|
|
397
|
+
(next: boolean) => {
|
|
398
|
+
// readonly/disabled поле открывать нельзя, закрывать — можно.
|
|
399
|
+
if (next && (disabled || readOnly)) return;
|
|
400
|
+
if (openProp === undefined) {
|
|
401
|
+
setOpenLocal(next);
|
|
402
|
+
}
|
|
337
403
|
|
|
338
|
-
|
|
339
|
-
|
|
404
|
+
onOpenChange?.(next);
|
|
405
|
+
|
|
406
|
+
if (!next) {
|
|
407
|
+
setTyping(false);
|
|
408
|
+
const committed = tryCommitOnClose();
|
|
340
409
|
|
|
341
|
-
|
|
342
|
-
|
|
410
|
+
// Кастомная опция не зафиксирована — вернуть лейбл выбранного значения.
|
|
411
|
+
if (resetSearchOnOptionSelection && !committed) {
|
|
412
|
+
setInputValue(selectedLabel);
|
|
413
|
+
}
|
|
343
414
|
}
|
|
344
|
-
}
|
|
345
|
-
|
|
415
|
+
},
|
|
416
|
+
[
|
|
417
|
+
openProp,
|
|
418
|
+
onOpenChange,
|
|
419
|
+
disabled,
|
|
420
|
+
readOnly,
|
|
421
|
+
resetSearchOnOptionSelection,
|
|
422
|
+
setInputValue,
|
|
423
|
+
selectedLabel,
|
|
424
|
+
tryCommitOnClose,
|
|
425
|
+
],
|
|
426
|
+
);
|
|
346
427
|
|
|
347
428
|
const handleInputChange = (next: string) => {
|
|
429
|
+
enableCloseCommit();
|
|
348
430
|
setInputValue(next);
|
|
349
431
|
setTyping(true);
|
|
350
432
|
|
|
@@ -358,17 +440,17 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
358
440
|
// Отключённые выбранные значения остаются при очистке (паритет с легаси).
|
|
359
441
|
const keepDisabled = selectedPairs.filter(pair => pair.disabled).map(pair => pair.id);
|
|
360
442
|
|
|
361
|
-
if (
|
|
443
|
+
if (value === undefined) {
|
|
362
444
|
setMultipleLocal(keepDisabled);
|
|
363
445
|
}
|
|
364
446
|
|
|
365
|
-
|
|
447
|
+
onMultipleChange?.(keepDisabled);
|
|
366
448
|
} else {
|
|
367
|
-
if (
|
|
449
|
+
if (value === undefined) {
|
|
368
450
|
setSingleLocal(undefined);
|
|
369
451
|
}
|
|
370
452
|
|
|
371
|
-
|
|
453
|
+
onSingleChange?.(undefined);
|
|
372
454
|
}
|
|
373
455
|
|
|
374
456
|
setInputValue('');
|
|
@@ -379,7 +461,17 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
379
461
|
if (required && !open) {
|
|
380
462
|
handleOpenChange(true);
|
|
381
463
|
}
|
|
382
|
-
}, [
|
|
464
|
+
}, [
|
|
465
|
+
multiple,
|
|
466
|
+
selectedPairs,
|
|
467
|
+
setInputValue,
|
|
468
|
+
required,
|
|
469
|
+
open,
|
|
470
|
+
handleOpenChange,
|
|
471
|
+
value,
|
|
472
|
+
onSingleChange,
|
|
473
|
+
onMultipleChange,
|
|
474
|
+
]);
|
|
383
475
|
|
|
384
476
|
const removeChip = useCallback(
|
|
385
477
|
(id: ItemId) => {
|
|
@@ -394,13 +486,13 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
394
486
|
|
|
395
487
|
const next = multipleValue.filter(v => v !== id);
|
|
396
488
|
|
|
397
|
-
if (
|
|
489
|
+
if (value === undefined) {
|
|
398
490
|
setMultipleLocal(next);
|
|
399
491
|
}
|
|
400
492
|
|
|
401
|
-
|
|
493
|
+
onMultipleChange?.(next);
|
|
402
494
|
},
|
|
403
|
-
[multiple, multipleValue,
|
|
495
|
+
[multiple, multipleValue, selectedPairs, value, onMultipleChange],
|
|
404
496
|
);
|
|
405
497
|
|
|
406
498
|
const handleRemoveChip = useCallback(
|
|
@@ -471,21 +563,13 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
471
563
|
|
|
472
564
|
if (event.key === 'Escape' && open) {
|
|
473
565
|
event.preventDefault();
|
|
566
|
+
suppressCloseCommit();
|
|
474
567
|
handleOpenChange(false);
|
|
475
568
|
}
|
|
476
569
|
|
|
477
|
-
if (
|
|
478
|
-
// Введённый текст становится новым значением (создание опции «на лету»).
|
|
570
|
+
if (tryCommitFromKeyboard(event)) {
|
|
479
571
|
event.preventDefault();
|
|
480
|
-
|
|
481
|
-
if (multiple) {
|
|
482
|
-
if (!multipleValue.includes(inputValue)) {
|
|
483
|
-
handleMultipleChange([...multipleValue, inputValue]);
|
|
484
|
-
}
|
|
485
|
-
} else {
|
|
486
|
-
handleSingleChange(inputValue);
|
|
487
|
-
}
|
|
488
|
-
|
|
572
|
+
event.stopPropagation();
|
|
489
573
|
return;
|
|
490
574
|
}
|
|
491
575
|
|
|
@@ -498,6 +582,13 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
498
582
|
event.preventDefault();
|
|
499
583
|
const last = multipleValue[multipleValue.length - 1];
|
|
500
584
|
removeChip(last);
|
|
585
|
+
restoreInputFocus();
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const handleTriggerKeyDownCapture = (event: KeyboardEvent<HTMLElement>) => {
|
|
590
|
+
if (event.key === 'Escape') {
|
|
591
|
+
suppressCloseCommit();
|
|
501
592
|
}
|
|
502
593
|
};
|
|
503
594
|
|
|
@@ -536,20 +627,35 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
536
627
|
[searchable, onFocusProp],
|
|
537
628
|
);
|
|
538
629
|
|
|
539
|
-
const handleInputBlur =
|
|
540
|
-
(
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
630
|
+
const handleInputBlur = (event: FocusEvent<HTMLInputElement>) => {
|
|
631
|
+
setFocusVisible(false);
|
|
632
|
+
const blurResult = tryCommitOnBlur(event);
|
|
633
|
+
|
|
634
|
+
// Закрытый список не получит onOpenChange — вернуть лейбл выбранного значения.
|
|
635
|
+
// Клик по айтему открытого списка даёт relatedTarget === null, восстановление делает закрытие.
|
|
636
|
+
if (resetSearchOnOptionSelection && blurResult === 'skipped' && (!open || event.relatedTarget !== null)) {
|
|
637
|
+
setTyping(false);
|
|
638
|
+
setInputValue(selectedLabel);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
onBlurProp?.(event);
|
|
642
|
+
};
|
|
546
643
|
|
|
547
644
|
const droplistSelection = multiple
|
|
548
|
-
? { mode: 'multiple' as const, value: multipleValue, onChange:
|
|
549
|
-
: { mode: 'single' as const, value: singleValue, onChange:
|
|
645
|
+
? { mode: 'multiple' as const, value: multipleValue, onChange: handleListMultipleChange }
|
|
646
|
+
: { mode: 'single' as const, value: singleValue, onChange: handleListSingleChange };
|
|
550
647
|
|
|
551
648
|
const hasChips = chips && selectedPairs.length > 0;
|
|
552
649
|
|
|
650
|
+
useLayoutEffect(() => {
|
|
651
|
+
if (!restoreInputFocusRef.current) {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
restoreInputFocusRef.current = false;
|
|
656
|
+
inputElementRef.current?.focus();
|
|
657
|
+
}, [multipleValue.length]);
|
|
658
|
+
|
|
553
659
|
// minWidth поискового input'а = ширина введённого текста (`.inputPlug`), но не больше строки чипов.
|
|
554
660
|
// Замер до paint, иначе input мигает при вводе.
|
|
555
661
|
useLayoutEffect(() => {
|
|
@@ -583,7 +689,7 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
583
689
|
);
|
|
584
690
|
|
|
585
691
|
const searchInput = hasChips ? (
|
|
586
|
-
<div className={styles.inputWrapper} style={{ minWidth: chipInputMinWidth } as CSSProperties}>
|
|
692
|
+
<div key='search-input' className={styles.inputWrapper} style={{ minWidth: chipInputMinWidth } as CSSProperties}>
|
|
587
693
|
{searchInputField}
|
|
588
694
|
</div>
|
|
589
695
|
) : (
|
|
@@ -605,6 +711,7 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
605
711
|
data-test-id={dataTestId}
|
|
606
712
|
onMouseEnter={handleTriggerMouseEnter}
|
|
607
713
|
onMouseLeave={handleTriggerMouseLeave}
|
|
714
|
+
onKeyDownCapture={handleTriggerKeyDownCapture}
|
|
608
715
|
onClick={handleTriggerClick}
|
|
609
716
|
>
|
|
610
717
|
<div className={fieldStyles.backgroundWrapper}>
|
|
@@ -698,6 +805,7 @@ export const FieldSelect = forwardRef<HTMLInputElement, FieldSelectProps>(functi
|
|
|
698
805
|
placement={placement}
|
|
699
806
|
widthStrategy={widthStrategy}
|
|
700
807
|
triggerElemRef={inputRef}
|
|
808
|
+
listRef={listRef}
|
|
701
809
|
size={size}
|
|
702
810
|
open={open}
|
|
703
811
|
onOpenChange={handleOpenChange}
|
|
@@ -2,3 +2,39 @@ export const SELECTION_MODE = {
|
|
|
2
2
|
Single: 'single',
|
|
3
3
|
Multiple: 'multiple',
|
|
4
4
|
} as const;
|
|
5
|
+
|
|
6
|
+
/** События, по которым произвольный ввод фиксируется как выбранная опция. */
|
|
7
|
+
export const ADD_CUSTOM_OPTION_TRIGGER = {
|
|
8
|
+
Enter: 'enter',
|
|
9
|
+
Blur: 'blur',
|
|
10
|
+
Space: 'space',
|
|
11
|
+
Comma: 'comma',
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
/** Разрешенные триггеры для `selection='single'` при `allowCustomOption`. */
|
|
15
|
+
export const ADD_CUSTOM_OPTION_TRIGGERS_SINGLE_ALLOWED = [
|
|
16
|
+
ADD_CUSTOM_OPTION_TRIGGER.Enter,
|
|
17
|
+
ADD_CUSTOM_OPTION_TRIGGER.Blur,
|
|
18
|
+
] as const;
|
|
19
|
+
|
|
20
|
+
/** Триггеры по умолчанию для `selection='single'` при `allowCustomOption`. */
|
|
21
|
+
export const ADD_CUSTOM_OPTION_TRIGGERS_SINGLE_DEFAULT = [
|
|
22
|
+
ADD_CUSTOM_OPTION_TRIGGER.Enter,
|
|
23
|
+
ADD_CUSTOM_OPTION_TRIGGER.Blur,
|
|
24
|
+
] as const;
|
|
25
|
+
|
|
26
|
+
/** Разрешенные триггеры для `selection='multiple'` при `allowCustomOption`. */
|
|
27
|
+
export const ADD_CUSTOM_OPTION_TRIGGERS_MULTIPLE_ALLOWED = [
|
|
28
|
+
ADD_CUSTOM_OPTION_TRIGGER.Enter,
|
|
29
|
+
ADD_CUSTOM_OPTION_TRIGGER.Blur,
|
|
30
|
+
ADD_CUSTOM_OPTION_TRIGGER.Space,
|
|
31
|
+
ADD_CUSTOM_OPTION_TRIGGER.Comma,
|
|
32
|
+
] as const;
|
|
33
|
+
|
|
34
|
+
/** Триггеры по умолчанию для `selection='multiple'` при `allowCustomOption`. */
|
|
35
|
+
export const ADD_CUSTOM_OPTION_TRIGGERS_MULTIPLE_DEFAULT = [
|
|
36
|
+
ADD_CUSTOM_OPTION_TRIGGER.Enter,
|
|
37
|
+
ADD_CUSTOM_OPTION_TRIGGER.Blur,
|
|
38
|
+
ADD_CUSTOM_OPTION_TRIGGER.Space,
|
|
39
|
+
ADD_CUSTOM_OPTION_TRIGGER.Comma,
|
|
40
|
+
] as const;
|