@tangible/ui 0.1.0 → 0.2.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/components/Button/types.d.ts +5 -2
- package/components/Chip/Chip.d.ts +28 -0
- package/components/Chip/Chip.js +19 -3
- package/components/Combobox/Combobox.d.ts +1 -1
- package/components/Combobox/Combobox.js +2 -2
- package/components/Combobox/types.d.ts +13 -0
- package/components/IconButton/types.d.ts +2 -2
- package/components/MultiCombobox/MultiCombobox.d.ts +33 -0
- package/components/MultiCombobox/MultiCombobox.js +595 -0
- package/components/MultiCombobox/MultiComboboxContext.d.ts +8 -0
- package/components/MultiCombobox/MultiComboboxContext.js +36 -0
- package/components/MultiCombobox/index.d.ts +3 -0
- package/components/MultiCombobox/index.js +2 -0
- package/components/MultiCombobox/types.d.ts +244 -0
- package/components/MultiCombobox/types.js +7 -0
- package/components/MultiSelect/MultiSelect.js +22 -2
- package/components/Select/Select.js +14 -0
- package/components/Slider/Slider.d.ts +2 -0
- package/components/Slider/Slider.js +29 -0
- package/components/Slider/index.d.ts +2 -0
- package/components/Slider/index.js +1 -0
- package/components/Slider/types.d.ts +39 -0
- package/components/Slider/types.js +1 -0
- package/components/index.d.ts +3 -0
- package/components/index.js +2 -0
- package/package.json +9 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +707 -50
- package/styles/all.expanded.unlayered.css +707 -50
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +4 -0
- package/styles/components/button.scss +45 -35
- package/styles/components/chip.scss +57 -0
- package/styles/components/combobox.scss +16 -2
- package/styles/components/icon-button.scss +32 -26
- package/styles/components/input/index.scss +1 -2
- package/styles/components/multi-combobox.scss +457 -0
- package/styles/components/multi-select.scss +34 -18
- package/styles/components/pager.scss +4 -2
- package/styles/components/progress.scss +1 -1
- package/styles/components/segmented-control.scss +44 -18
- package/styles/components/select.scss +19 -3
- package/styles/components/slider.scss +184 -0
- package/styles/components/switch.scss +1 -1
- package/styles/core.css +1 -1
- package/styles/core.unlayered.css +1 -1
- package/styles/system/_tokens.scss +13 -0
- package/tui-manifest.json +215 -6
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import React, { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
3
|
+
import { isDev } from '../../utils/is-dev.js';
|
|
4
|
+
import { useFloating, offset, flip, shift, size as sizeMiddleware, autoUpdate, FloatingPortal, useDismiss, useInteractions, useListNavigation, useRole, } from '@floating-ui/react';
|
|
5
|
+
import { cx } from '../../utils/cx.js';
|
|
6
|
+
import { toKey } from '../../utils/value-key.js';
|
|
7
|
+
import { getPortalRootFor } from '../../utils/portal.js';
|
|
8
|
+
import { hashForId } from '../../utils/hash.js';
|
|
9
|
+
import { composeEventHandlers } from '../../utils/compose-events.js';
|
|
10
|
+
import { Icon } from '../Icon/index.js';
|
|
11
|
+
import { Chip } from '../Chip/index.js';
|
|
12
|
+
import { MultiComboboxActionsContext, MultiComboboxStateContext, MultiComboboxContentContext, useMultiComboboxContext, useMultiComboboxContentContext, } from './MultiComboboxContext.js';
|
|
13
|
+
import { defaultMultiComboboxLabels } from './types.js';
|
|
14
|
+
// Stable empty value so an unset controlled `value` doesn't churn identity
|
|
15
|
+
// (which would re-run isSelected/state memos every render).
|
|
16
|
+
const EMPTY_VALUE = [];
|
|
17
|
+
// =============================================================================
|
|
18
|
+
// MultiCombobox Root
|
|
19
|
+
// =============================================================================
|
|
20
|
+
function MultiComboboxRoot({ value: controlledValue, defaultValue, onValueChange, inputValue: controlledInputValue, onInputChange, open: controlledOpen, defaultOpen, onOpenChange, disabled = false, placeholder = '', size = 'md', overflow = 'trim', openOnFocus = true, filterMode = 'always', onQueryChange, max, onMaxReached, clearable = true, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, inputClassName, labels: labelsProp, children, }) {
|
|
21
|
+
const labels = { ...defaultMultiComboboxLabels, ...labelsProp };
|
|
22
|
+
// Controlled/uncontrolled value (initialize from defaultValue)
|
|
23
|
+
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue ?? []);
|
|
24
|
+
const isValueControlled = useRef(controlledValue !== undefined).current;
|
|
25
|
+
const value = useMemo(() => (isValueControlled ? controlledValue ?? EMPTY_VALUE : uncontrolledValue), [isValueControlled, controlledValue, uncontrolledValue]);
|
|
26
|
+
// Read current value synchronously in stable callbacks (avoids stale closures)
|
|
27
|
+
const valueRef = useRef(value);
|
|
28
|
+
valueRef.current = value;
|
|
29
|
+
// Chip labels captured at selection time. An async option that has since left
|
|
30
|
+
// the filtered result set still resolves a correct chip label from here.
|
|
31
|
+
const selectedLabelsRef = useRef(new Map());
|
|
32
|
+
// Controlled/uncontrolled inputValue
|
|
33
|
+
const [uncontrolledInputValue, setUncontrolledInputValue] = useState('');
|
|
34
|
+
const isInputControlled = controlledInputValue !== undefined;
|
|
35
|
+
const inputValue = isInputControlled ? controlledInputValue : uncontrolledInputValue;
|
|
36
|
+
// Controlled/uncontrolled open
|
|
37
|
+
const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen ?? false);
|
|
38
|
+
const isOpenControlled = controlledOpen !== undefined;
|
|
39
|
+
const open = isOpenControlled ? controlledOpen : uncontrolledOpen;
|
|
40
|
+
// Internal searching state (private) — true only while the user is typing
|
|
41
|
+
const [searching, setSearching] = useState(false);
|
|
42
|
+
// Compute effective query for filtering
|
|
43
|
+
const query = filterMode === 'when-searching' && !searching ? '' : inputValue;
|
|
44
|
+
// Track previous query to fire onQueryChange
|
|
45
|
+
const prevQueryRef = useRef(query);
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (prevQueryRef.current !== query) {
|
|
48
|
+
prevQueryRef.current = query;
|
|
49
|
+
onQueryChange?.(query);
|
|
50
|
+
}
|
|
51
|
+
}, [query, onQueryChange]);
|
|
52
|
+
const maxReached = max !== undefined && value.length >= max;
|
|
53
|
+
// Option registration
|
|
54
|
+
const optionsRef = useRef(new Map());
|
|
55
|
+
const [registryVersion, setRegistryVersion] = useState(0);
|
|
56
|
+
// Track open state via ref so unregisterOption can check synchronously
|
|
57
|
+
const openRef = useRef(false);
|
|
58
|
+
openRef.current = open;
|
|
59
|
+
// IDs
|
|
60
|
+
const baseId = useId();
|
|
61
|
+
const inputId = `${baseId}-input`;
|
|
62
|
+
const listboxId = `${baseId}-listbox`;
|
|
63
|
+
// Refs
|
|
64
|
+
const inputRef = useRef(null);
|
|
65
|
+
const listRef = useRef([]);
|
|
66
|
+
const tokensRef = useRef(null);
|
|
67
|
+
// Trim mode: whether chips are clipped off the inline-start (drives the fade)
|
|
68
|
+
const [overflowStart, setOverflowStart] = useState(false);
|
|
69
|
+
// Active index for navigation (-1 means no active option)
|
|
70
|
+
const [activeIndex, setActiveIndex] = useState(-1);
|
|
71
|
+
// Track previous highlighted value to maintain selection across filter changes
|
|
72
|
+
const prevHighlightedValueRef = useRef(null);
|
|
73
|
+
// Set open state
|
|
74
|
+
const setOpen = useCallback((nextOpen) => {
|
|
75
|
+
if (disabled)
|
|
76
|
+
return;
|
|
77
|
+
if (!isOpenControlled) {
|
|
78
|
+
setUncontrolledOpen(nextOpen);
|
|
79
|
+
}
|
|
80
|
+
onOpenChange?.(nextOpen);
|
|
81
|
+
}, [disabled, isOpenControlled, onOpenChange]);
|
|
82
|
+
// Set input value
|
|
83
|
+
const setInputValue = useCallback((nextValue) => {
|
|
84
|
+
if (!isInputControlled) {
|
|
85
|
+
setUncontrolledInputValue(nextValue);
|
|
86
|
+
}
|
|
87
|
+
onInputChange?.(nextValue);
|
|
88
|
+
}, [isInputControlled, onInputChange]);
|
|
89
|
+
const commitValue = useCallback((next) => {
|
|
90
|
+
if (!isValueControlled) {
|
|
91
|
+
setUncontrolledValue(next);
|
|
92
|
+
}
|
|
93
|
+
onValueChange?.(next);
|
|
94
|
+
}, [isValueControlled, onValueChange]);
|
|
95
|
+
// Toggle an option. Unlike Combobox's selectOption, this keeps the listbox
|
|
96
|
+
// open, clears the search input, and refocuses for the next query.
|
|
97
|
+
const toggleOption = useCallback((optionValue, textValue) => {
|
|
98
|
+
const key = toKey(optionValue);
|
|
99
|
+
const exists = valueRef.current.some((v) => toKey(v) === key);
|
|
100
|
+
if (exists) {
|
|
101
|
+
commitValue(valueRef.current.filter((v) => toKey(v) !== key));
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
if (max !== undefined && valueRef.current.length >= max) {
|
|
105
|
+
onMaxReached?.();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
selectedLabelsRef.current.set(key, textValue);
|
|
109
|
+
commitValue([...valueRef.current, optionValue]);
|
|
110
|
+
}
|
|
111
|
+
setInputValue('');
|
|
112
|
+
setSearching(false);
|
|
113
|
+
inputRef.current?.focus();
|
|
114
|
+
}, [commitValue, max, onMaxReached, setInputValue]);
|
|
115
|
+
// Remove a single value (chip remove button, Backspace). Refocus the input so
|
|
116
|
+
// keyboard removal (Tab to a chip's ×, Enter) doesn't drop focus to <body>
|
|
117
|
+
// when the button unmounts — mirrors toggleOption/handleClear.
|
|
118
|
+
const removeValue = useCallback((optionValue) => {
|
|
119
|
+
const key = toKey(optionValue);
|
|
120
|
+
selectedLabelsRef.current.delete(key);
|
|
121
|
+
commitValue(valueRef.current.filter((v) => toKey(v) !== key));
|
|
122
|
+
inputRef.current?.focus();
|
|
123
|
+
}, [commitValue]);
|
|
124
|
+
// Clear all selected values and the input. Routes through commitValue so the
|
|
125
|
+
// controlled/uncontrolled lock is respected (handleClear refocuses the input).
|
|
126
|
+
const clearAll = useCallback(() => {
|
|
127
|
+
selectedLabelsRef.current.clear();
|
|
128
|
+
commitValue([]);
|
|
129
|
+
setInputValue('');
|
|
130
|
+
setSearching(false);
|
|
131
|
+
}, [commitValue, setInputValue]);
|
|
132
|
+
// Get ordered options
|
|
133
|
+
const getOrderedOptions = useCallback(() => {
|
|
134
|
+
const options = Array.from(optionsRef.current.values());
|
|
135
|
+
return options.sort((a, b) => {
|
|
136
|
+
if (!a.ref.current || !b.ref.current)
|
|
137
|
+
return 0;
|
|
138
|
+
const position = a.ref.current.compareDocumentPosition(b.ref.current);
|
|
139
|
+
if (position & Node.DOCUMENT_POSITION_FOLLOWING)
|
|
140
|
+
return -1;
|
|
141
|
+
if (position & Node.DOCUMENT_POSITION_PRECEDING)
|
|
142
|
+
return 1;
|
|
143
|
+
return 0;
|
|
144
|
+
});
|
|
145
|
+
}, []);
|
|
146
|
+
// Memoized ordered options
|
|
147
|
+
const orderedOptions = useMemo(() => {
|
|
148
|
+
return getOrderedOptions();
|
|
149
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
150
|
+
}, [registryVersion]);
|
|
151
|
+
// Compute disabled indices for keyboard navigation.
|
|
152
|
+
// At max, unselected options are treated as disabled (not navigable-to-select).
|
|
153
|
+
const disabledIndices = useMemo(() => orderedOptions.reduce((acc, opt, i) => {
|
|
154
|
+
const selected = valueRef.current.some((v) => toKey(v) === toKey(opt.value));
|
|
155
|
+
if (opt.disabled || (maxReached && !selected))
|
|
156
|
+
acc.push(i);
|
|
157
|
+
return acc;
|
|
158
|
+
}, []),
|
|
159
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
160
|
+
[orderedOptions, maxReached, value]);
|
|
161
|
+
// Floating UI setup
|
|
162
|
+
const { refs, floatingStyles, context } = useFloating({
|
|
163
|
+
placement: 'bottom-start',
|
|
164
|
+
open,
|
|
165
|
+
onOpenChange: setOpen,
|
|
166
|
+
middleware: [
|
|
167
|
+
offset(4),
|
|
168
|
+
flip({ padding: 8 }),
|
|
169
|
+
shift({ padding: 8 }),
|
|
170
|
+
sizeMiddleware({
|
|
171
|
+
apply({ availableHeight, rects, elements }) {
|
|
172
|
+
Object.assign(elements.floating.style, {
|
|
173
|
+
maxHeight: `${Math.min(availableHeight - 16, 300)}px`,
|
|
174
|
+
minWidth: `${rects.reference.width}px`,
|
|
175
|
+
});
|
|
176
|
+
},
|
|
177
|
+
padding: 8,
|
|
178
|
+
}),
|
|
179
|
+
],
|
|
180
|
+
whileElementsMounted: autoUpdate,
|
|
181
|
+
});
|
|
182
|
+
// Floating UI interactions
|
|
183
|
+
const dismiss = useDismiss(context);
|
|
184
|
+
const role = useRole(context, { role: 'listbox' });
|
|
185
|
+
const listNavigation = useListNavigation(context, {
|
|
186
|
+
listRef,
|
|
187
|
+
activeIndex: activeIndex >= 0 ? activeIndex : null,
|
|
188
|
+
onNavigate: (index) => setActiveIndex(index ?? -1),
|
|
189
|
+
loop: true,
|
|
190
|
+
virtual: true,
|
|
191
|
+
disabledIndices,
|
|
192
|
+
});
|
|
193
|
+
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([
|
|
194
|
+
dismiss,
|
|
195
|
+
role,
|
|
196
|
+
listNavigation,
|
|
197
|
+
]);
|
|
198
|
+
// Flag: first registerOption call after open clears stale entries.
|
|
199
|
+
const prevOpenForFlush = useRef(open);
|
|
200
|
+
const needsFlushRef = useRef(false);
|
|
201
|
+
if (open && !prevOpenForFlush.current)
|
|
202
|
+
needsFlushRef.current = true;
|
|
203
|
+
prevOpenForFlush.current = open;
|
|
204
|
+
// Register option
|
|
205
|
+
const registerOption = useCallback((option) => {
|
|
206
|
+
if (needsFlushRef.current) {
|
|
207
|
+
optionsRef.current.clear();
|
|
208
|
+
needsFlushRef.current = false;
|
|
209
|
+
}
|
|
210
|
+
optionsRef.current.set(toKey(option.value), option);
|
|
211
|
+
setRegistryVersion((v) => v + 1);
|
|
212
|
+
}, []);
|
|
213
|
+
const unregisterOption = useCallback((optionValue) => {
|
|
214
|
+
// When the dropdown closes, options unmount and call unregister in cleanup.
|
|
215
|
+
// Skip the delete to preserve the registry — chip labels and filtering
|
|
216
|
+
// depend on it while closed. Options re-register on next open (same keys).
|
|
217
|
+
if (!openRef.current)
|
|
218
|
+
return;
|
|
219
|
+
optionsRef.current.delete(toKey(optionValue));
|
|
220
|
+
setRegistryVersion((v) => v + 1);
|
|
221
|
+
}, []);
|
|
222
|
+
// Active option ID for aria-activedescendant (hash-based for stability)
|
|
223
|
+
const activeOptionId = activeIndex >= 0 && orderedOptions[activeIndex]
|
|
224
|
+
? `${listboxId}-opt-${hashForId(toKey(orderedOptions[activeIndex].value))}`
|
|
225
|
+
: undefined;
|
|
226
|
+
// Update activeIndex when options change (filtering)
|
|
227
|
+
// Keep highlighted if still present, else first enabled, else -1
|
|
228
|
+
useEffect(() => {
|
|
229
|
+
if (!open)
|
|
230
|
+
return;
|
|
231
|
+
const prevValue = prevHighlightedValueRef.current;
|
|
232
|
+
const isNavigable = (opt) => !opt.disabled && !(maxReached && !valueRef.current.some((v) => toKey(v) === toKey(opt.value)));
|
|
233
|
+
const enabledOptions = orderedOptions.filter(isNavigable);
|
|
234
|
+
if (enabledOptions.length === 0) {
|
|
235
|
+
setActiveIndex(-1);
|
|
236
|
+
prevHighlightedValueRef.current = null;
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (prevValue !== null) {
|
|
240
|
+
const prevIndex = orderedOptions.findIndex((opt) => toKey(opt.value) === toKey(prevValue) && isNavigable(opt));
|
|
241
|
+
if (prevIndex >= 0) {
|
|
242
|
+
setActiveIndex(prevIndex);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const firstEnabledIndex = orderedOptions.findIndex(isNavigable);
|
|
247
|
+
setActiveIndex(firstEnabledIndex >= 0 ? firstEnabledIndex : -1);
|
|
248
|
+
if (firstEnabledIndex >= 0) {
|
|
249
|
+
prevHighlightedValueRef.current = orderedOptions[firstEnabledIndex].value;
|
|
250
|
+
}
|
|
251
|
+
}, [open, orderedOptions, maxReached]);
|
|
252
|
+
// Track highlighted value when activeIndex changes
|
|
253
|
+
useEffect(() => {
|
|
254
|
+
if (activeIndex >= 0 && orderedOptions[activeIndex]) {
|
|
255
|
+
prevHighlightedValueRef.current = orderedOptions[activeIndex].value;
|
|
256
|
+
}
|
|
257
|
+
}, [activeIndex, orderedOptions]);
|
|
258
|
+
// Reset activeIndex when closing
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
if (!open) {
|
|
261
|
+
setActiveIndex(-1);
|
|
262
|
+
prevHighlightedValueRef.current = null;
|
|
263
|
+
}
|
|
264
|
+
}, [open]);
|
|
265
|
+
// Scroll active option into view
|
|
266
|
+
useEffect(() => {
|
|
267
|
+
if (open && activeIndex >= 0 && listRef.current[activeIndex]) {
|
|
268
|
+
listRef.current[activeIndex]?.scrollIntoView({ block: 'nearest' });
|
|
269
|
+
}
|
|
270
|
+
}, [open, activeIndex]);
|
|
271
|
+
// Trim mode: keep the typing area in view by scrolling the token row to its
|
|
272
|
+
// end whenever the selection changes; chips clip off the inline-start.
|
|
273
|
+
useLayoutEffect(() => {
|
|
274
|
+
if (overflow !== 'trim')
|
|
275
|
+
return;
|
|
276
|
+
const el = tokensRef.current;
|
|
277
|
+
if (!el)
|
|
278
|
+
return;
|
|
279
|
+
el.scrollLeft = el.scrollWidth;
|
|
280
|
+
setOverflowStart(el.scrollLeft > 1);
|
|
281
|
+
// Re-pin on selection change AND while typing (the input grows the row).
|
|
282
|
+
}, [overflow, value, inputValue]);
|
|
283
|
+
const handleTokensScroll = useCallback(() => {
|
|
284
|
+
const el = tokensRef.current;
|
|
285
|
+
if (el)
|
|
286
|
+
setOverflowStart(el.scrollLeft > 1);
|
|
287
|
+
}, []);
|
|
288
|
+
// Handle input change (user typing)
|
|
289
|
+
const handleInputChange = useCallback((e) => {
|
|
290
|
+
setInputValue(e.target.value);
|
|
291
|
+
setSearching(true);
|
|
292
|
+
if (!open) {
|
|
293
|
+
setOpen(true);
|
|
294
|
+
}
|
|
295
|
+
}, [setInputValue, open, setOpen]);
|
|
296
|
+
// Handle input focus
|
|
297
|
+
const handleInputFocus = useCallback(() => {
|
|
298
|
+
if (openOnFocus && !disabled && !open) {
|
|
299
|
+
setOpen(true);
|
|
300
|
+
}
|
|
301
|
+
}, [openOnFocus, disabled, open, setOpen]);
|
|
302
|
+
// Handle input keydown
|
|
303
|
+
const handleInputKeyDown = useCallback((e) => {
|
|
304
|
+
if (disabled)
|
|
305
|
+
return;
|
|
306
|
+
switch (e.key) {
|
|
307
|
+
case 'ArrowDown':
|
|
308
|
+
case 'ArrowUp':
|
|
309
|
+
e.preventDefault();
|
|
310
|
+
if (!open) {
|
|
311
|
+
setOpen(true);
|
|
312
|
+
}
|
|
313
|
+
break;
|
|
314
|
+
case 'Enter':
|
|
315
|
+
if (open && activeIndex >= 0) {
|
|
316
|
+
e.preventDefault();
|
|
317
|
+
const option = orderedOptions[activeIndex];
|
|
318
|
+
const selected = option && valueRef.current.some((v) => toKey(v) === toKey(option.value));
|
|
319
|
+
if (option && !option.disabled && !(maxReached && !selected)) {
|
|
320
|
+
toggleOption(option.value, option.textValue);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
break;
|
|
324
|
+
case 'Backspace':
|
|
325
|
+
// Remove the last chip when the input is empty
|
|
326
|
+
if (inputValue === '' && valueRef.current.length > 0) {
|
|
327
|
+
e.preventDefault();
|
|
328
|
+
removeValue(valueRef.current[valueRef.current.length - 1]);
|
|
329
|
+
}
|
|
330
|
+
break;
|
|
331
|
+
case 'Escape':
|
|
332
|
+
if (open) {
|
|
333
|
+
e.preventDefault();
|
|
334
|
+
setSearching(false);
|
|
335
|
+
setOpen(false);
|
|
336
|
+
}
|
|
337
|
+
break;
|
|
338
|
+
case 'Tab':
|
|
339
|
+
if (open) {
|
|
340
|
+
setSearching(false);
|
|
341
|
+
setOpen(false);
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}, [disabled, open, setOpen, activeIndex, orderedOptions, toggleOption, removeValue, inputValue, maxReached]);
|
|
346
|
+
// Handle input blur with blur guard
|
|
347
|
+
const handleInputBlur = useCallback((e) => {
|
|
348
|
+
if (!open)
|
|
349
|
+
return;
|
|
350
|
+
const relatedTarget = e.relatedTarget;
|
|
351
|
+
if (relatedTarget === null) {
|
|
352
|
+
setSearching(false);
|
|
353
|
+
setOpen(false);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const floating = refs.floating.current;
|
|
357
|
+
if (floating && relatedTarget && floating.contains(relatedTarget)) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
// Focus moving to a chip's remove button inside our own wrapper — stay open
|
|
361
|
+
const reference = refs.reference.current;
|
|
362
|
+
if (reference && relatedTarget && reference.contains?.(relatedTarget)) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
setSearching(false);
|
|
366
|
+
setOpen(false);
|
|
367
|
+
}, [open, setOpen, refs.floating, refs.reference]);
|
|
368
|
+
// Handle clear-all button
|
|
369
|
+
const handleClear = useCallback((e) => {
|
|
370
|
+
e.preventDefault();
|
|
371
|
+
e.stopPropagation();
|
|
372
|
+
clearAll();
|
|
373
|
+
inputRef.current?.focus();
|
|
374
|
+
}, [clearAll]);
|
|
375
|
+
// Clicking anywhere in the token field (other than the input) routes focus to
|
|
376
|
+
// the input. A chip's × still removes on click — its onClick fires and
|
|
377
|
+
// removeValue refocuses the input regardless — so the preventDefault here just
|
|
378
|
+
// stops the wrapper mousedown from parking focus somewhere non-typeable.
|
|
379
|
+
const handleWrapperMouseDown = useCallback((e) => {
|
|
380
|
+
if (e.target !== inputRef.current) {
|
|
381
|
+
e.preventDefault();
|
|
382
|
+
inputRef.current?.focus();
|
|
383
|
+
}
|
|
384
|
+
}, []);
|
|
385
|
+
const isSelected = useCallback((optionValue) => value.some((v) => toKey(v) === toKey(optionValue)), [value]);
|
|
386
|
+
// Resolve a chip's display label: captured-at-select > registered > raw value.
|
|
387
|
+
// Tradeoff: the captured label deliberately wins over a currently-registered
|
|
388
|
+
// one so async options that have left the filtered result set still render a
|
|
389
|
+
// correct chip. The cost is a chip keeps its captured label if an option's
|
|
390
|
+
// textValue legitimately changes while still selected (e.g. a renamed record
|
|
391
|
+
// re-fetched). Captured entries are pruned on remove/clear.
|
|
392
|
+
const labelFor = useCallback((optionValue) => {
|
|
393
|
+
const key = toKey(optionValue);
|
|
394
|
+
return (selectedLabelsRef.current.get(key) ??
|
|
395
|
+
orderedOptions.find((o) => toKey(o.value) === key)?.textValue ??
|
|
396
|
+
String(optionValue));
|
|
397
|
+
}, [orderedOptions]);
|
|
398
|
+
// ==========================================================================
|
|
399
|
+
// Split Context Values
|
|
400
|
+
// ==========================================================================
|
|
401
|
+
const actionsValue = useMemo(() => ({
|
|
402
|
+
disabled,
|
|
403
|
+
placeholder,
|
|
404
|
+
openOnFocus,
|
|
405
|
+
max,
|
|
406
|
+
inputId,
|
|
407
|
+
listboxId,
|
|
408
|
+
ariaLabel,
|
|
409
|
+
ariaLabelledBy,
|
|
410
|
+
setOpen,
|
|
411
|
+
setInputValue,
|
|
412
|
+
toggleOption,
|
|
413
|
+
registerOption,
|
|
414
|
+
unregisterOption,
|
|
415
|
+
refs,
|
|
416
|
+
inputRef,
|
|
417
|
+
listRef,
|
|
418
|
+
getReferenceProps,
|
|
419
|
+
getFloatingProps,
|
|
420
|
+
getItemProps,
|
|
421
|
+
}), [
|
|
422
|
+
disabled,
|
|
423
|
+
placeholder,
|
|
424
|
+
openOnFocus,
|
|
425
|
+
max,
|
|
426
|
+
inputId,
|
|
427
|
+
listboxId,
|
|
428
|
+
ariaLabel,
|
|
429
|
+
ariaLabelledBy,
|
|
430
|
+
setOpen,
|
|
431
|
+
setInputValue,
|
|
432
|
+
toggleOption,
|
|
433
|
+
registerOption,
|
|
434
|
+
unregisterOption,
|
|
435
|
+
refs,
|
|
436
|
+
getReferenceProps,
|
|
437
|
+
getFloatingProps,
|
|
438
|
+
getItemProps,
|
|
439
|
+
]);
|
|
440
|
+
const stateValue = useMemo(() => ({
|
|
441
|
+
open,
|
|
442
|
+
value,
|
|
443
|
+
inputValue,
|
|
444
|
+
maxReached,
|
|
445
|
+
isSelected,
|
|
446
|
+
query,
|
|
447
|
+
activeIndex,
|
|
448
|
+
activeOptionId,
|
|
449
|
+
orderedOptions,
|
|
450
|
+
floatingStyles,
|
|
451
|
+
floatingContext: context,
|
|
452
|
+
}), [
|
|
453
|
+
open,
|
|
454
|
+
value,
|
|
455
|
+
inputValue,
|
|
456
|
+
maxReached,
|
|
457
|
+
isSelected,
|
|
458
|
+
query,
|
|
459
|
+
activeIndex,
|
|
460
|
+
activeOptionId,
|
|
461
|
+
orderedOptions,
|
|
462
|
+
floatingStyles,
|
|
463
|
+
context,
|
|
464
|
+
]);
|
|
465
|
+
const referenceProps = getReferenceProps({
|
|
466
|
+
onFocus: handleInputFocus,
|
|
467
|
+
onBlur: handleInputBlur,
|
|
468
|
+
onKeyDown: handleInputKeyDown,
|
|
469
|
+
});
|
|
470
|
+
const hasValue = value.length > 0;
|
|
471
|
+
// Chips track the control size so they stay proportionate.
|
|
472
|
+
const chipSize = size === 'lg' ? 'md' : size === 'sm' ? 'xs' : 'sm';
|
|
473
|
+
return (_jsx(MultiComboboxActionsContext.Provider, { value: actionsValue, children: _jsx(MultiComboboxStateContext.Provider, { value: stateValue, children: _jsxs("div", { className: "tui-multicombobox", children: [_jsxs("div", { ref: refs.setReference, className: cx('tui-multicombobox__control', size !== 'md' && `is-size-${size}`, overflow === 'trim' && 'is-overflow-trim'), "data-overflow-start": overflow === 'trim' && overflowStart ? '' : undefined, onMouseDown: handleWrapperMouseDown, children: [_jsxs("div", { ref: tokensRef, className: "tui-multicombobox__tokens", onScroll: overflow === 'trim' ? handleTokensScroll : undefined, children: [value.map((v) => {
|
|
474
|
+
const label = labelFor(v);
|
|
475
|
+
return (_jsx(Chip, { size: chipSize, className: "tui-multicombobox__chip", disabled: disabled, removeLabel: labels.remove(label), onRemove: () => removeValue(v), children: label }, toKey(v)));
|
|
476
|
+
}), _jsx("input", { ref: (node) => {
|
|
477
|
+
inputRef.current = node;
|
|
478
|
+
}, type: "text", id: inputId, className: cx('tui-multicombobox__input', inputClassName), role: "combobox", "aria-expanded": open, "aria-controls": open ? listboxId : undefined, "aria-autocomplete": "list", "aria-activedescendant": activeOptionId, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, "aria-invalid": ariaInvalid, "aria-required": ariaRequired, disabled: disabled, placeholder: hasValue ? undefined : placeholder, value: inputValue, autoComplete: "off", ...referenceProps, onChange: handleInputChange })] }), clearable && hasValue && !disabled && (_jsx("button", { type: "button", className: "tui-multicombobox__clear", onClick: handleClear, onMouseDown: (e) => e.preventDefault(), "aria-label": labels.clear, tabIndex: -1, children: _jsx(Icon, { name: "system/close" }) })), _jsx("span", { className: "tui-multicombobox__icon", "aria-hidden": "true", children: _jsx(Icon, { name: "system/chevron-down" }) })] }), children, _jsx("div", { className: "tui-visually-hidden", role: "status", children: labels.status(value.length, max) })] }) }) }));
|
|
479
|
+
}
|
|
480
|
+
MultiComboboxRoot.displayName = 'MultiCombobox';
|
|
481
|
+
// =============================================================================
|
|
482
|
+
// MultiCombobox.Content
|
|
483
|
+
// =============================================================================
|
|
484
|
+
function MultiComboboxContentComponent({ className, children }) {
|
|
485
|
+
const { open, listboxId, inputId, refs, floatingStyles, getFloatingProps, listRef, activeIndex, orderedOptions, } = useMultiComboboxContext();
|
|
486
|
+
// Before first open, mount children in a hidden div for option registration
|
|
487
|
+
// (resolves chip labels for initial values). After first open, mount only
|
|
488
|
+
// when open (in the portal).
|
|
489
|
+
const hasEverOpened = useRef(false);
|
|
490
|
+
if (open)
|
|
491
|
+
hasEverOpened.current = true;
|
|
492
|
+
const portalRoot = getPortalRootFor(refs.reference.current);
|
|
493
|
+
const contentContext = useMemo(() => ({
|
|
494
|
+
listRef,
|
|
495
|
+
activeIndex,
|
|
496
|
+
orderedOptions,
|
|
497
|
+
}), [listRef, activeIndex, orderedOptions]);
|
|
498
|
+
return (_jsxs(_Fragment, { children: [!open && !hasEverOpened.current && (_jsx("div", { id: listboxId, role: "listbox", "aria-multiselectable": "true", style: { display: 'none' }, "aria-hidden": "true", children: _jsx(MultiComboboxContentContext.Provider, { value: contentContext, children: children }) })), open && (_jsx(FloatingPortal, { root: portalRoot, children: _jsx("div", { ref: refs.setFloating, id: listboxId, role: "listbox", "aria-multiselectable": "true", "aria-labelledby": inputId, className: cx('tui-multicombobox__content', className), style: {
|
|
499
|
+
...floatingStyles,
|
|
500
|
+
minWidth: refs.reference.current?.offsetWidth,
|
|
501
|
+
pointerEvents: 'auto',
|
|
502
|
+
}, tabIndex: -1, ...getFloatingProps(), children: _jsx(MultiComboboxContentContext.Provider, { value: contentContext, children: children }) }) }))] }));
|
|
503
|
+
}
|
|
504
|
+
MultiComboboxContentComponent.displayName = 'MultiCombobox.Content';
|
|
505
|
+
// =============================================================================
|
|
506
|
+
// MultiCombobox.Option
|
|
507
|
+
// =============================================================================
|
|
508
|
+
function MultiComboboxOptionComponent({ value: optionValue, disabled = false, textValue: explicitTextValue, className, children, }) {
|
|
509
|
+
const { isSelected, maxReached, toggleOption, listboxId, registerOption, unregisterOption, getItemProps, } = useMultiComboboxContext();
|
|
510
|
+
const { listRef, activeIndex, orderedOptions } = useMultiComboboxContentContext();
|
|
511
|
+
const ref = useRef(null);
|
|
512
|
+
const textValue = explicitTextValue ?? (typeof children === 'string' ? children : '');
|
|
513
|
+
const warnedTextValueRef = useRef(false);
|
|
514
|
+
useEffect(() => {
|
|
515
|
+
if (isDev() && !textValue && !warnedTextValueRef.current) {
|
|
516
|
+
warnedTextValueRef.current = true;
|
|
517
|
+
console.warn(`MultiCombobox.Option with value="${optionValue}" has no textValue. Provide textValue prop when children is not a string.`);
|
|
518
|
+
}
|
|
519
|
+
}, [textValue, optionValue]);
|
|
520
|
+
// Register option
|
|
521
|
+
useLayoutEffect(() => {
|
|
522
|
+
registerOption({ value: optionValue, ref, disabled, textValue });
|
|
523
|
+
return () => unregisterOption(optionValue);
|
|
524
|
+
}, [optionValue, disabled, textValue, registerOption, unregisterOption]);
|
|
525
|
+
const index = orderedOptions.findIndex((opt) => toKey(opt.value) === toKey(optionValue));
|
|
526
|
+
useEffect(() => {
|
|
527
|
+
if (index >= 0) {
|
|
528
|
+
listRef.current[index] = ref.current;
|
|
529
|
+
}
|
|
530
|
+
}, [index, listRef]);
|
|
531
|
+
const selected = isSelected(optionValue);
|
|
532
|
+
// At max, unselected options are blocked (but remain visible + toggleable-off)
|
|
533
|
+
const blocked = disabled || (maxReached && !selected);
|
|
534
|
+
const isHighlighted = activeIndex === index;
|
|
535
|
+
const handleClick = useCallback(() => {
|
|
536
|
+
if (blocked)
|
|
537
|
+
return;
|
|
538
|
+
toggleOption(optionValue, textValue);
|
|
539
|
+
}, [blocked, optionValue, textValue, toggleOption]);
|
|
540
|
+
// Prevent blur on input when clicking option
|
|
541
|
+
const handlePointerDown = useCallback((e) => {
|
|
542
|
+
e.preventDefault();
|
|
543
|
+
}, []);
|
|
544
|
+
const optionId = `${listboxId}-opt-${hashForId(toKey(optionValue))}`;
|
|
545
|
+
const itemProps = getItemProps({
|
|
546
|
+
onClick: handleClick,
|
|
547
|
+
});
|
|
548
|
+
return (_jsxs("div", { ref: ref, id: optionId, role: "option", className: cx('tui-multicombobox__option', className), "aria-selected": selected, "aria-disabled": blocked || undefined, "data-highlighted": isHighlighted || undefined, "data-disabled": blocked || undefined, "data-selected": selected || undefined, ...itemProps, onPointerDown: composeEventHandlers(handlePointerDown, itemProps.onPointerDown), children: [_jsx("span", { className: "tui-multicombobox__option-check", "aria-hidden": "true", children: selected && _jsx(Icon, { name: "system/check" }) }), _jsx("span", { className: "tui-multicombobox__option-content", children: children })] }));
|
|
549
|
+
}
|
|
550
|
+
MultiComboboxOptionComponent.displayName = 'MultiCombobox.Option';
|
|
551
|
+
// =============================================================================
|
|
552
|
+
// MultiCombobox.Group
|
|
553
|
+
// =============================================================================
|
|
554
|
+
function MultiComboboxGroupComponent({ className, children }) {
|
|
555
|
+
const groupId = useId();
|
|
556
|
+
const groupRef = useRef(null);
|
|
557
|
+
const [hasLabel, setHasLabel] = useState(false);
|
|
558
|
+
useLayoutEffect(() => {
|
|
559
|
+
if (groupRef.current) {
|
|
560
|
+
const labelEl = groupRef.current.querySelector(`#${CSS.escape(`${groupId}-label`)}`);
|
|
561
|
+
setHasLabel(!!labelEl);
|
|
562
|
+
}
|
|
563
|
+
}, [groupId, children]);
|
|
564
|
+
return (_jsx("div", { ref: groupRef, role: "group", "aria-labelledby": hasLabel ? `${groupId}-label` : undefined, className: cx('tui-multicombobox__group', className), children: _jsx(MultiComboboxGroupContext.Provider, { value: { groupId }, children: children }) }));
|
|
565
|
+
}
|
|
566
|
+
MultiComboboxGroupComponent.displayName = 'MultiCombobox.Group';
|
|
567
|
+
const MultiComboboxGroupContext = React.createContext(null);
|
|
568
|
+
// =============================================================================
|
|
569
|
+
// MultiCombobox.Label
|
|
570
|
+
// =============================================================================
|
|
571
|
+
function MultiComboboxLabelComponent({ className, children }) {
|
|
572
|
+
const groupContext = React.useContext(MultiComboboxGroupContext);
|
|
573
|
+
const warnedRef = useRef(false);
|
|
574
|
+
useEffect(() => {
|
|
575
|
+
if (isDev() && !groupContext && !warnedRef.current) {
|
|
576
|
+
warnedRef.current = true;
|
|
577
|
+
console.warn('MultiCombobox.Label should be used inside MultiCombobox.Group for accessibility.');
|
|
578
|
+
}
|
|
579
|
+
}, [groupContext]);
|
|
580
|
+
return (_jsx("div", { id: groupContext ? `${groupContext.groupId}-label` : undefined, className: cx('tui-multicombobox__label', className), children: children }));
|
|
581
|
+
}
|
|
582
|
+
MultiComboboxLabelComponent.displayName = 'MultiCombobox.Label';
|
|
583
|
+
export const MultiCombobox = MultiComboboxRoot;
|
|
584
|
+
MultiCombobox.Content = MultiComboboxContentComponent;
|
|
585
|
+
MultiCombobox.Option = MultiComboboxOptionComponent;
|
|
586
|
+
MultiCombobox.Group = MultiComboboxGroupComponent;
|
|
587
|
+
MultiCombobox.Label = MultiComboboxLabelComponent;
|
|
588
|
+
// Named exports
|
|
589
|
+
export const MultiComboboxContent = MultiComboboxContentComponent;
|
|
590
|
+
export const MultiComboboxOption = MultiComboboxOptionComponent;
|
|
591
|
+
export const MultiComboboxGroup = MultiComboboxGroupComponent;
|
|
592
|
+
export const MultiComboboxLabel = MultiComboboxLabelComponent;
|
|
593
|
+
// Hook for advanced use cases
|
|
594
|
+
// eslint-disable-next-line react-refresh/only-export-components
|
|
595
|
+
export { useMultiComboboxContext as useMultiCombobox } from './MultiComboboxContext.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { MultiComboboxActionsContextValue, MultiComboboxStateContextValue, MultiComboboxContentContextValue } from './types';
|
|
2
|
+
export declare const MultiComboboxActionsContext: import("react").Context<MultiComboboxActionsContextValue | null>;
|
|
3
|
+
export declare const MultiComboboxStateContext: import("react").Context<MultiComboboxStateContextValue | null>;
|
|
4
|
+
export declare function useMultiComboboxActions(): MultiComboboxActionsContextValue;
|
|
5
|
+
export declare function useMultiComboboxState(): MultiComboboxStateContextValue;
|
|
6
|
+
export declare function useMultiComboboxContext(): MultiComboboxActionsContextValue & MultiComboboxStateContextValue;
|
|
7
|
+
export declare const MultiComboboxContentContext: import("react").Context<MultiComboboxContentContextValue | null>;
|
|
8
|
+
export declare function useMultiComboboxContentContext(): MultiComboboxContentContextValue;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// Split Contexts for Performance
|
|
4
|
+
// =============================================================================
|
|
5
|
+
export const MultiComboboxActionsContext = createContext(null);
|
|
6
|
+
export const MultiComboboxStateContext = createContext(null);
|
|
7
|
+
export function useMultiComboboxActions() {
|
|
8
|
+
const context = useContext(MultiComboboxActionsContext);
|
|
9
|
+
if (!context) {
|
|
10
|
+
throw new Error('MultiCombobox components must be used within a MultiCombobox');
|
|
11
|
+
}
|
|
12
|
+
return context;
|
|
13
|
+
}
|
|
14
|
+
export function useMultiComboboxState() {
|
|
15
|
+
const context = useContext(MultiComboboxStateContext);
|
|
16
|
+
if (!context) {
|
|
17
|
+
throw new Error('MultiCombobox components must be used within a MultiCombobox');
|
|
18
|
+
}
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
export function useMultiComboboxContext() {
|
|
22
|
+
const actions = useMultiComboboxActions();
|
|
23
|
+
const state = useMultiComboboxState();
|
|
24
|
+
return { ...actions, ...state };
|
|
25
|
+
}
|
|
26
|
+
// =============================================================================
|
|
27
|
+
// Content Context (for Option registration)
|
|
28
|
+
// =============================================================================
|
|
29
|
+
export const MultiComboboxContentContext = createContext(null);
|
|
30
|
+
export function useMultiComboboxContentContext() {
|
|
31
|
+
const context = useContext(MultiComboboxContentContext);
|
|
32
|
+
if (!context) {
|
|
33
|
+
throw new Error('MultiCombobox.Option must be used within MultiCombobox.Content');
|
|
34
|
+
}
|
|
35
|
+
return context;
|
|
36
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { MultiCombobox, MultiComboboxContent, MultiComboboxOption, MultiComboboxGroup, MultiComboboxLabel, useMultiCombobox, } from './MultiCombobox';
|
|
2
|
+
export type { MultiComboboxProps, MultiComboboxContentProps, MultiComboboxOptionProps, MultiComboboxGroupProps, MultiComboboxLabelProps, MultiComboboxLabels, MultiComboboxValue, FilterMode as MultiComboboxFilterMode, RegisteredOption as MultiComboboxRegisteredOption, } from './types';
|
|
3
|
+
export { defaultMultiComboboxLabels } from './types';
|