@tangible/ui 0.2.7 → 0.2.9
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/ColorPicker/ColorField.js +33 -10
- package/components/Combobox/Combobox.d.ts +16 -11
- package/components/Combobox/Combobox.js +42 -196
- package/components/Combobox/types.d.ts +3 -7
- package/components/MultiCombobox/MultiCombobox.d.ts +16 -11
- package/components/MultiCombobox/MultiCombobox.js +46 -157
- package/components/MultiCombobox/types.d.ts +4 -7
- package/components/MultiSelect/MultiSelect.d.ts +16 -11
- package/components/MultiSelect/MultiSelect.js +74 -210
- package/components/MultiSelect/types.d.ts +5 -8
- package/components/SearchSelect/SearchSelect.d.ts +34 -0
- package/components/SearchSelect/SearchSelect.js +444 -0
- package/components/SearchSelect/SearchSelectContext.d.ts +7 -0
- package/components/SearchSelect/SearchSelectContext.js +21 -0
- package/components/SearchSelect/index.d.ts +2 -0
- package/components/SearchSelect/index.js +1 -0
- package/components/SearchSelect/types.d.ts +134 -0
- package/components/SearchSelect/types.js +5 -0
- package/components/Select/Select.d.ts +16 -11
- package/components/Select/Select.js +78 -237
- package/components/Select/types.d.ts +5 -8
- package/components/Tabs/Tabs.js +16 -1
- package/components/index.d.ts +2 -0
- package/components/index.js +1 -0
- package/package.json +5 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +230 -34
- package/styles/all.expanded.unlayered.css +230 -34
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +8 -4
- package/styles/components/combobox.scss +29 -6
- package/styles/components/date-picker.scss +9 -3
- package/styles/components/multi-combobox.scss +18 -3
- package/styles/components/multi-select.scss +28 -9
- package/styles/components/notice.scss +7 -5
- package/styles/components/search-select.scss +244 -0
- package/styles/components/segmented-control.scss +10 -5
- package/styles/components/select.scss +16 -7
- package/styles/components/sidebar.scss +31 -22
- package/styles/components/tabs.scss +26 -15
- package/tui-manifest.json +75 -2
- package/utils/color/color.js +14 -2
- package/utils/listbox/blur.d.ts +14 -0
- package/utils/listbox/blur.js +51 -0
- package/utils/listbox/filter-active-index.d.ts +16 -0
- package/utils/listbox/filter-active-index.js +72 -0
- package/utils/listbox/floating.d.ts +7 -0
- package/utils/listbox/floating.js +34 -0
- package/utils/listbox/group.d.ts +26 -0
- package/utils/listbox/group.js +40 -0
- package/utils/listbox/ids.d.ts +9 -0
- package/utils/listbox/ids.js +31 -0
- package/utils/listbox/index.d.ts +9 -0
- package/utils/listbox/index.js +11 -0
- package/utils/listbox/navigation.d.ts +27 -0
- package/utils/listbox/navigation.js +53 -0
- package/utils/listbox/registry.d.ts +61 -0
- package/utils/listbox/registry.js +121 -0
- package/utils/listbox/scroll.d.ts +7 -0
- package/utils/listbox/scroll.js +15 -0
- package/utils/listbox/trigger.d.ts +16 -0
- package/utils/listbox/trigger.js +35 -0
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { FloatingPortal } from '@floating-ui/react';
|
|
4
|
+
import { cx } from '../../utils/cx.js';
|
|
5
|
+
import { toKey } from '../../utils/value-key.js';
|
|
6
|
+
import { getPortalRootFor } from '../../utils/portal.js';
|
|
7
|
+
import { useOptionRegistry, useOptionRegistration, deriveOptionText, useListboxIds, useListboxFloating, useListboxNavigation, useListboxBlurGuard, useTriggerPointerDownFix, useFilterStableActiveIndex, useActiveOptionScroll, optionIdForValue, createListboxGroupComponents, } from '../../utils/listbox/index.js';
|
|
8
|
+
import { Icon } from '../Icon/index.js';
|
|
9
|
+
import { SearchInput } from '../SearchInput/index.js';
|
|
10
|
+
import { SearchSelectContext, SearchSelectContentContext, useSearchSelectContext, useSearchSelectContentContext, } from './SearchSelectContext.js';
|
|
11
|
+
import { defaultSearchSelectLabels, } from './types.js';
|
|
12
|
+
// Default filter: case-insensitive substring on textValue.
|
|
13
|
+
const defaultFilter = (textValue, query) => textValue.toLowerCase().includes(query.trim().toLowerCase());
|
|
14
|
+
// =============================================================================
|
|
15
|
+
// SearchSelect Root
|
|
16
|
+
// =============================================================================
|
|
17
|
+
function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange, open: controlledOpen, defaultOpen, onOpenChange, inputValue: controlledInputValue, onInputChange, filter = defaultFilter, disabled = false, placeholder = '', size = 'md', id: triggerIdProp, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, labels: labelsProp, children, }) {
|
|
18
|
+
const labels = { ...defaultSearchSelectLabels, ...labelsProp };
|
|
19
|
+
// Controlled/uncontrolled value — lock decision at mount time (see the
|
|
20
|
+
// controlled→uncontrolled gotcha in CLAUDE.md)
|
|
21
|
+
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
|
|
22
|
+
const isValueControlled = useRef(controlledValue !== undefined).current;
|
|
23
|
+
const value = isValueControlled ? controlledValue : uncontrolledValue;
|
|
24
|
+
// Every label ever registered, NEVER pruned. Filtering unregisters options
|
|
25
|
+
// while the panel is open, so the closed-unregister skip alone can't keep
|
|
26
|
+
// the trigger label alive — an abandoned search would degrade the label to
|
|
27
|
+
// the raw value key. Populated from the registry below and at commit time.
|
|
28
|
+
const knownLabelsRef = useRef(new Map());
|
|
29
|
+
// Controlled/uncontrolled open
|
|
30
|
+
const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen ?? false);
|
|
31
|
+
const isOpenControlled = controlledOpen !== undefined;
|
|
32
|
+
const open = isOpenControlled ? controlledOpen : uncontrolledOpen;
|
|
33
|
+
// Render-fresh mirror so setOpen can be idempotent without churning identity
|
|
34
|
+
const openStateRef = useRef(open);
|
|
35
|
+
openStateRef.current = open;
|
|
36
|
+
// The trigger ELEMENT (not the Floating UI domReference — that's the search
|
|
37
|
+
// input, see the floating setup below). Used for focus return, the portal
|
|
38
|
+
// root, the blur-guard extra container, and the dismiss exclusion.
|
|
39
|
+
const triggerElRef = useRef(null);
|
|
40
|
+
// Controlled/uncontrolled search text — TRANSIENT: reset when the panel closes
|
|
41
|
+
const [uncontrolledInputValue, setUncontrolledInputValue] = useState('');
|
|
42
|
+
const isInputControlled = controlledInputValue !== undefined;
|
|
43
|
+
const inputValue = isInputControlled ? controlledInputValue : uncontrolledInputValue;
|
|
44
|
+
const query = inputValue;
|
|
45
|
+
const setOpen = useCallback((nextOpen) => {
|
|
46
|
+
// Idempotent: Floating UI's dismiss and our own Escape/blur handlers can
|
|
47
|
+
// both fire for one interaction — collapse the duplicate onOpenChange.
|
|
48
|
+
// The ref tracks the latest REQUESTED state (assigned below, before any
|
|
49
|
+
// re-render): both handlers run in the same event batch, so comparing
|
|
50
|
+
// against rendered state alone wouldn't catch the duplicate.
|
|
51
|
+
if (nextOpen === openStateRef.current)
|
|
52
|
+
return;
|
|
53
|
+
// disabled blocks OPENING only — a form disabling its controls while
|
|
54
|
+
// the panel is open must still be able to close it.
|
|
55
|
+
if (disabled && nextOpen)
|
|
56
|
+
return;
|
|
57
|
+
openStateRef.current = nextOpen;
|
|
58
|
+
if (!isOpenControlled) {
|
|
59
|
+
setUncontrolledOpen(nextOpen);
|
|
60
|
+
}
|
|
61
|
+
onOpenChange?.(nextOpen);
|
|
62
|
+
}, [disabled, isOpenControlled, onOpenChange]);
|
|
63
|
+
const setInputValue = useCallback((nextValue) => {
|
|
64
|
+
if (!isInputControlled) {
|
|
65
|
+
setUncontrolledInputValue(nextValue);
|
|
66
|
+
}
|
|
67
|
+
onInputChange?.(nextValue);
|
|
68
|
+
}, [isInputControlled, onInputChange]);
|
|
69
|
+
// Shared option registry (listbox engine)
|
|
70
|
+
const { registerOption, unregisterOption, orderedOptions, optionIndexMap } = useOptionRegistry({
|
|
71
|
+
open,
|
|
72
|
+
});
|
|
73
|
+
// Harvest every registered label into the persistent map (see knownLabelsRef)
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
for (const opt of orderedOptions) {
|
|
76
|
+
knownLabelsRef.current.set(toKey(opt.value), opt.textValue);
|
|
77
|
+
}
|
|
78
|
+
}, [orderedOptions]);
|
|
79
|
+
// IDs — the panel is a dialog, so it gets its own id for aria-controls
|
|
80
|
+
const { controlId: triggerId, listboxId } = useListboxIds(triggerIdProp, 'trigger');
|
|
81
|
+
const panelId = `${listboxId}-panel`;
|
|
82
|
+
// Refs
|
|
83
|
+
const inputRef = useRef(null);
|
|
84
|
+
const listRef = useRef([]);
|
|
85
|
+
// Active index for navigation (-1 means no active option)
|
|
86
|
+
const [activeIndex, setActiveIndex] = useState(-1);
|
|
87
|
+
// Commit a value: capture label, close, reset the query, hand focus back
|
|
88
|
+
const selectValue = useCallback((optionValue, textValue) => {
|
|
89
|
+
knownLabelsRef.current.set(toKey(optionValue), textValue);
|
|
90
|
+
if (!isValueControlled) {
|
|
91
|
+
setUncontrolledValue(optionValue);
|
|
92
|
+
}
|
|
93
|
+
onValueChange?.(optionValue);
|
|
94
|
+
setOpen(false);
|
|
95
|
+
}, [isValueControlled, onValueChange, setOpen]);
|
|
96
|
+
// Trigger label: from the persistent label map (populated by registration —
|
|
97
|
+
// the hidden pre-open render covers defaultValue — and at commit), else the
|
|
98
|
+
// live registry, else the raw value.
|
|
99
|
+
const labelFor = useCallback((optionValue) => {
|
|
100
|
+
const key = toKey(optionValue);
|
|
101
|
+
return (knownLabelsRef.current.get(key) ??
|
|
102
|
+
orderedOptions.find((o) => toKey(o.value) === key)?.textValue ??
|
|
103
|
+
String(optionValue));
|
|
104
|
+
}, [orderedOptions]);
|
|
105
|
+
// Reset the transient query when the panel closes
|
|
106
|
+
const prevOpenRef = useRef(open);
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
if (prevOpenRef.current && !open && inputValue !== '') {
|
|
109
|
+
setInputValue('');
|
|
110
|
+
}
|
|
111
|
+
prevOpenRef.current = open;
|
|
112
|
+
}, [open, inputValue, setInputValue]);
|
|
113
|
+
// Compute disabled indices for keyboard navigation
|
|
114
|
+
const disabledIndices = useMemo(() => orderedOptions.reduce((acc, opt, i) => {
|
|
115
|
+
if (opt.disabled)
|
|
116
|
+
acc.push(i);
|
|
117
|
+
return acc;
|
|
118
|
+
}, []), [orderedOptions]);
|
|
119
|
+
// Floating UI setup. The split: the TRIGGER is the POSITION reference (the
|
|
120
|
+
// panel anchors/sizes to it) while the search input is the DOM reference —
|
|
121
|
+
// Floating UI's isTypeableCombobox() reads role="combobox" off the DOM
|
|
122
|
+
// reference, and only then does useListNavigation leave Home/End to the
|
|
123
|
+
// text caret instead of hijacking them for first/last-option jumps.
|
|
124
|
+
const { refs, floatingStyles, context } = useListboxFloating({
|
|
125
|
+
open,
|
|
126
|
+
onOpenChange: setOpen,
|
|
127
|
+
});
|
|
128
|
+
// Shared interaction stack — no click (the trigger has its own onClick so a
|
|
129
|
+
// click on the open trigger closes rather than re-opening) and no typeahead
|
|
130
|
+
// (typing IS the search). The trigger is excluded from outside-press
|
|
131
|
+
// dismissal: it isn't the domReference, so without the exclusion dismiss
|
|
132
|
+
// would close on pointerdown and the trigger's click would re-open.
|
|
133
|
+
const { getReferenceProps, getFloatingProps, getItemProps } = useListboxNavigation(context, {
|
|
134
|
+
listRef,
|
|
135
|
+
activeIndex: activeIndex >= 0 ? activeIndex : null,
|
|
136
|
+
onNavigate: (index) => setActiveIndex(index ?? -1),
|
|
137
|
+
disabledIndices,
|
|
138
|
+
withClick: false,
|
|
139
|
+
dismissOutsidePress: (event) => !(triggerElRef.current &&
|
|
140
|
+
event.target instanceof Node &&
|
|
141
|
+
triggerElRef.current.contains(event.target)),
|
|
142
|
+
});
|
|
143
|
+
// Active option ID for aria-activedescendant (hash-based — filtering shifts indices)
|
|
144
|
+
const activeOptionId = activeIndex >= 0 && orderedOptions[activeIndex]
|
|
145
|
+
? optionIdForValue(listboxId, orderedOptions[activeIndex].value)
|
|
146
|
+
: undefined;
|
|
147
|
+
// Keep the highlighted OPTION stable as filtering shifts indices (engine)
|
|
148
|
+
useFilterStableActiveIndex({ open, orderedOptions, activeIndex, setActiveIndex, listRef });
|
|
149
|
+
// Scroll active option into view
|
|
150
|
+
useActiveOptionScroll(open, activeIndex, listRef);
|
|
151
|
+
// Focus loss — attached at the PANEL level (blur bubbles as focusout), so it
|
|
152
|
+
// covers every descendant: the input, the search clear button, anything a
|
|
153
|
+
// future panel grows. Moving within the panel stays open; the trigger is the
|
|
154
|
+
// extra container (blurring to it must not close — its own onClick performs
|
|
155
|
+
// the toggle without a close/re-open flicker); anything else closes.
|
|
156
|
+
const handlePanelBlur = useListboxBlurGuard({
|
|
157
|
+
open,
|
|
158
|
+
setOpen,
|
|
159
|
+
floatingRef: refs.floating,
|
|
160
|
+
extraContainerRef: triggerElRef,
|
|
161
|
+
});
|
|
162
|
+
const handleInputKeyDown = useCallback((e) => {
|
|
163
|
+
if (e.key === 'Enter') {
|
|
164
|
+
e.preventDefault();
|
|
165
|
+
if (activeIndex >= 0) {
|
|
166
|
+
const option = orderedOptions[activeIndex];
|
|
167
|
+
if (option && !option.disabled) {
|
|
168
|
+
selectValue(option.value, option.textValue);
|
|
169
|
+
triggerElRef.current?.focus();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (e.key === 'Escape') {
|
|
175
|
+
// NOTE on ordering: Floating UI composes user handlers LAST, so
|
|
176
|
+
// useDismiss's Escape has already called setOpen(false) by the time
|
|
177
|
+
// this runs — setOpen's idempotence collapses the duplicate. This
|
|
178
|
+
// handler's real jobs are the focus return and stopPropagation
|
|
179
|
+
// (suppresses the document-level dismiss listener).
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
e.stopPropagation();
|
|
182
|
+
setOpen(false);
|
|
183
|
+
triggerElRef.current?.focus();
|
|
184
|
+
}
|
|
185
|
+
}, [activeIndex, orderedOptions, selectValue, setOpen]);
|
|
186
|
+
const contextValue = useMemo(() => ({
|
|
187
|
+
disabled,
|
|
188
|
+
placeholder,
|
|
189
|
+
size,
|
|
190
|
+
labels,
|
|
191
|
+
triggerId,
|
|
192
|
+
listboxId,
|
|
193
|
+
panelId,
|
|
194
|
+
ariaLabel,
|
|
195
|
+
ariaLabelledBy,
|
|
196
|
+
ariaDescribedBy,
|
|
197
|
+
ariaInvalid,
|
|
198
|
+
ariaRequired,
|
|
199
|
+
setOpen,
|
|
200
|
+
selectValue,
|
|
201
|
+
setInputValue,
|
|
202
|
+
registerOption,
|
|
203
|
+
unregisterOption,
|
|
204
|
+
labelFor,
|
|
205
|
+
handlePanelBlur,
|
|
206
|
+
handleInputKeyDown,
|
|
207
|
+
refs,
|
|
208
|
+
triggerElRef,
|
|
209
|
+
inputRef,
|
|
210
|
+
listRef,
|
|
211
|
+
optionIndexMap,
|
|
212
|
+
context,
|
|
213
|
+
getReferenceProps,
|
|
214
|
+
getFloatingProps,
|
|
215
|
+
getItemProps,
|
|
216
|
+
open,
|
|
217
|
+
value,
|
|
218
|
+
inputValue,
|
|
219
|
+
query,
|
|
220
|
+
filter,
|
|
221
|
+
activeIndex,
|
|
222
|
+
activeOptionId,
|
|
223
|
+
orderedOptions,
|
|
224
|
+
floatingStyles,
|
|
225
|
+
}),
|
|
226
|
+
// labels is a fresh object each render but derives from labelsProp
|
|
227
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
228
|
+
[
|
|
229
|
+
disabled,
|
|
230
|
+
placeholder,
|
|
231
|
+
size,
|
|
232
|
+
labelsProp,
|
|
233
|
+
triggerId,
|
|
234
|
+
listboxId,
|
|
235
|
+
panelId,
|
|
236
|
+
ariaLabel,
|
|
237
|
+
ariaLabelledBy,
|
|
238
|
+
ariaDescribedBy,
|
|
239
|
+
ariaInvalid,
|
|
240
|
+
ariaRequired,
|
|
241
|
+
setOpen,
|
|
242
|
+
selectValue,
|
|
243
|
+
setInputValue,
|
|
244
|
+
registerOption,
|
|
245
|
+
unregisterOption,
|
|
246
|
+
labelFor,
|
|
247
|
+
handlePanelBlur,
|
|
248
|
+
handleInputKeyDown,
|
|
249
|
+
refs,
|
|
250
|
+
optionIndexMap,
|
|
251
|
+
context,
|
|
252
|
+
getReferenceProps,
|
|
253
|
+
getFloatingProps,
|
|
254
|
+
getItemProps,
|
|
255
|
+
open,
|
|
256
|
+
value,
|
|
257
|
+
inputValue,
|
|
258
|
+
query,
|
|
259
|
+
filter,
|
|
260
|
+
activeIndex,
|
|
261
|
+
activeOptionId,
|
|
262
|
+
orderedOptions,
|
|
263
|
+
floatingStyles,
|
|
264
|
+
]);
|
|
265
|
+
return (_jsx(SearchSelectContext.Provider, { value: contextValue, children: _jsx("div", { className: "tui-search-select", children: children }) }));
|
|
266
|
+
}
|
|
267
|
+
SearchSelectRoot.displayName = 'SearchSelect';
|
|
268
|
+
// =============================================================================
|
|
269
|
+
// SearchSelect.Trigger
|
|
270
|
+
// =============================================================================
|
|
271
|
+
function SearchSelectTriggerComponent({ className, children, onClick: onClickProp, onKeyDown: onKeyDownProp, onPointerDown: onPointerDownProp, ...rest }) {
|
|
272
|
+
const { disabled, placeholder, size, triggerId, panelId, ariaLabel, ariaLabelledBy, ariaDescribedBy, ariaInvalid, ariaRequired, setOpen, labelFor, refs, triggerElRef, open, value, } = useSearchSelectContext();
|
|
273
|
+
const hasValue = value !== undefined;
|
|
274
|
+
const valueId = `${triggerId}-value`;
|
|
275
|
+
// The trigger is the POSITION reference (the panel anchors to it); the
|
|
276
|
+
// search input is the DOM reference. Keep our own element ref for focus
|
|
277
|
+
// return / portal root / dismiss exclusion.
|
|
278
|
+
const handleRef = useCallback((node) => {
|
|
279
|
+
triggerElRef.current = node;
|
|
280
|
+
refs.setPositionReference(node);
|
|
281
|
+
}, [refs, triggerElRef]);
|
|
282
|
+
const handleClick = useCallback((e) => {
|
|
283
|
+
onClickProp?.(e);
|
|
284
|
+
if (e.defaultPrevented)
|
|
285
|
+
return;
|
|
286
|
+
setOpen(!open);
|
|
287
|
+
}, [open, setOpen, onClickProp]);
|
|
288
|
+
const handleKeyDown = useCallback((e) => {
|
|
289
|
+
onKeyDownProp?.(e);
|
|
290
|
+
if (e.defaultPrevented)
|
|
291
|
+
return;
|
|
292
|
+
// Enter/Space activate the button natively (click). ArrowDown also opens.
|
|
293
|
+
if (e.key === 'ArrowDown' && !open) {
|
|
294
|
+
e.preventDefault();
|
|
295
|
+
setOpen(true);
|
|
296
|
+
}
|
|
297
|
+
}, [open, setOpen, onKeyDownProp]);
|
|
298
|
+
// Safari/Firefox on macOS don't focus buttons on click — the search input
|
|
299
|
+
// then blurs with relatedTarget null, the blur guard closes the panel, and
|
|
300
|
+
// this click immediately re-opens it: the trigger appears to never toggle
|
|
301
|
+
// closed. Same fix as Select's trigger: focus it ourselves on pointerdown so
|
|
302
|
+
// the blur lands on the trigger (the guard's stay-open container).
|
|
303
|
+
const pointerDownFix = useTriggerPointerDownFix();
|
|
304
|
+
const handlePointerDown = useCallback((e) => {
|
|
305
|
+
onPointerDownProp?.(e);
|
|
306
|
+
if (e.defaultPrevented)
|
|
307
|
+
return;
|
|
308
|
+
pointerDownFix(e);
|
|
309
|
+
}, [onPointerDownProp, pointerDownFix]);
|
|
310
|
+
return (_jsxs("button", { ...rest, ref: handleRef, type: "button", id: triggerId, className: cx('tui-search-select__trigger', size !== 'md' && `is-size-${size}`, className), disabled: disabled, "aria-haspopup": "dialog", "aria-expanded": open, "aria-controls": open ? panelId : undefined, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy ? `${ariaLabelledBy} ${valueId}` : undefined, "aria-describedby": ariaDescribedBy, "aria-invalid": ariaInvalid, "aria-required": ariaRequired, "data-state": open ? 'open' : 'closed', onClick: handleClick, onKeyDown: handleKeyDown, onPointerDown: handlePointerDown, children: [_jsx("span", { id: valueId, className: "tui-search-select__value", children: children ??
|
|
311
|
+
(hasValue ? (labelFor(value)) : (_jsx("span", { className: "tui-search-select__placeholder", children: placeholder }))) }), _jsx(Icon, { name: "system/chevron-down", className: "tui-search-select__icon" })] }));
|
|
312
|
+
}
|
|
313
|
+
SearchSelectTriggerComponent.displayName = 'SearchSelect.Trigger';
|
|
314
|
+
// =============================================================================
|
|
315
|
+
// SearchSelect.Content
|
|
316
|
+
// =============================================================================
|
|
317
|
+
function SearchSelectContentComponent({ className, children }) {
|
|
318
|
+
const { labels, size, triggerId, listboxId, panelId, setInputValue, handlePanelBlur, handleInputKeyDown, refs, triggerElRef, inputRef, listRef, optionIndexMap, getReferenceProps, getFloatingProps, open, inputValue, activeOptionId, orderedOptions, floatingStyles, } = useSearchSelectContext();
|
|
319
|
+
// Move focus into the search field when the panel opens. Callback ref, not
|
|
320
|
+
// an open-keyed effect: FloatingPortal can attach the subtree after the
|
|
321
|
+
// parent's effects run, so an effect may fire while the input is still
|
|
322
|
+
// unmounted. Gated per open: SearchInput composes refs into a fresh closure
|
|
323
|
+
// each render, so React re-invokes this on EVERY render — an ungated focus()
|
|
324
|
+
// would yank focus back from the clear button on any re-render.
|
|
325
|
+
const focusedThisOpenRef = useRef(false);
|
|
326
|
+
useEffect(() => {
|
|
327
|
+
if (!open)
|
|
328
|
+
focusedThisOpenRef.current = false;
|
|
329
|
+
}, [open]);
|
|
330
|
+
const handleInputRef = useCallback((node) => {
|
|
331
|
+
inputRef.current = node;
|
|
332
|
+
// The input is the Floating UI DOM reference (isTypeableCombobox reads
|
|
333
|
+
// role="combobox" off it — that's what frees Home/End for the caret).
|
|
334
|
+
refs.setReference(node);
|
|
335
|
+
if (node && !focusedThisOpenRef.current) {
|
|
336
|
+
focusedThisOpenRef.current = true;
|
|
337
|
+
node.focus();
|
|
338
|
+
}
|
|
339
|
+
}, [inputRef, refs]);
|
|
340
|
+
const { activeIndex } = useSearchSelectContext();
|
|
341
|
+
const contentContextValue = useMemo(() => ({ listRef, activeIndex, orderedOptions, optionIndexMap }), [listRef, activeIndex, orderedOptions, optionIndexMap]);
|
|
342
|
+
// Hidden pre-first-open render keeps the registry populated while closed so
|
|
343
|
+
// a defaultValue's trigger label resolves (same invariant as the other four
|
|
344
|
+
// listboxes — the listbox id must not exist twice at once).
|
|
345
|
+
const hasEverOpened = useRef(false);
|
|
346
|
+
if (open)
|
|
347
|
+
hasEverOpened.current = true;
|
|
348
|
+
if (!open && !hasEverOpened.current) {
|
|
349
|
+
return (_jsx("div", { style: { display: 'none' }, "aria-hidden": "true", children: _jsx("div", { id: listboxId, role: "listbox", children: _jsx(SearchSelectContentContext.Provider, { value: contentContextValue, children: children }) }) }));
|
|
350
|
+
}
|
|
351
|
+
if (!open)
|
|
352
|
+
return null;
|
|
353
|
+
return (_jsx(FloatingPortal, { root: getPortalRootFor(triggerElRef.current), children: _jsxs("div", { ref: refs.setFloating, className: cx('tui-search-select__panel', className), style: floatingStyles, ...getFloatingProps(),
|
|
354
|
+
// AFTER the spread: useRole stamps role="listbox" and its own id on
|
|
355
|
+
// the floating element — this panel is a NON-MODAL dialog (no focus
|
|
356
|
+
// trap, no aria-modal; focus-out dismisses it via the blur guard
|
|
357
|
+
// below, like a combobox popup) holding a combobox + listbox. The
|
|
358
|
+
// trigger's aria-controls points at panelId.
|
|
359
|
+
id: panelId, role: "dialog", "aria-labelledby": triggerId,
|
|
360
|
+
// Panel-level so blur from ANY descendant (input, search clear button,
|
|
361
|
+
// future panel furniture) is evaluated — input-only wiring left the
|
|
362
|
+
// panel orphaned open after Tab past the clear button.
|
|
363
|
+
onBlur: handlePanelBlur, children: [_jsx("div", { className: "tui-search-select__search", children: _jsx(SearchInput, { ref: handleInputRef, size: size === 'lg' ? 'md' : 'sm', value: inputValue, onValueChange: setInputValue, searchLabel: labels.search, clearLabel: labels.clearSearch, placeholder: labels.search, ...getReferenceProps({
|
|
364
|
+
onKeyDown: handleInputKeyDown,
|
|
365
|
+
}),
|
|
366
|
+
// AFTER the spread: correct the reference props for the
|
|
367
|
+
// combobox-in-dialog shape. aria-haspopup is useRole's listbox
|
|
368
|
+
// leftover — a combobox already conveys its popup via
|
|
369
|
+
// aria-expanded/aria-controls.
|
|
370
|
+
role: "combobox", "aria-haspopup": undefined, "aria-label": labels.search, "aria-expanded": "true", "aria-controls": listboxId, "aria-autocomplete": "list", "aria-activedescendant": activeOptionId }) }), _jsx("div", { id: listboxId, role: "listbox", "aria-labelledby": triggerId, className: "tui-search-select__listbox", tabIndex: -1, children: _jsx(SearchSelectContentContext.Provider, { value: contentContextValue, children: children }) }), orderedOptions.length === 0 && (_jsx("div", { role: "status", className: "tui-search-select__empty", children: labels.noResults }))] }) }));
|
|
371
|
+
}
|
|
372
|
+
SearchSelectContentComponent.displayName = 'SearchSelect.Content';
|
|
373
|
+
// =============================================================================
|
|
374
|
+
// SearchSelect.Option
|
|
375
|
+
// =============================================================================
|
|
376
|
+
function SearchSelectOptionComponent({ value: optionValue, disabled = false, textValue: explicitTextValue, className, children, }) {
|
|
377
|
+
const { value: selectedValue, selectValue, listboxId, registerOption, unregisterOption, getItemProps, query, filter, triggerElRef, } = useSearchSelectContext();
|
|
378
|
+
const { listRef, activeIndex, optionIndexMap } = useSearchSelectContentContext();
|
|
379
|
+
const ref = useRef(null);
|
|
380
|
+
// Built-in filtering: a non-matching option renders null AND leaves the
|
|
381
|
+
// registry (enabled: false) so navigation and the empty state see the
|
|
382
|
+
// filtered list. `filter: false` → consumer owns filtering via children.
|
|
383
|
+
// Empty derived text (rich children without textValue) degrades to
|
|
384
|
+
// UNFILTERABLE, not invisible — ''.includes() would vanish it on the first
|
|
385
|
+
// keystroke. deriveOptionText is the hook's own rule, imported so the two
|
|
386
|
+
// can't drift.
|
|
387
|
+
const derivedText = deriveOptionText(children, explicitTextValue);
|
|
388
|
+
const visible = filter === false || query === '' || derivedText === '' || filter(derivedText, query);
|
|
389
|
+
const textValue = useOptionRegistration({
|
|
390
|
+
value: optionValue,
|
|
391
|
+
disabled,
|
|
392
|
+
textValue: explicitTextValue,
|
|
393
|
+
children,
|
|
394
|
+
componentName: 'SearchSelect.Option',
|
|
395
|
+
elementRef: ref,
|
|
396
|
+
registerOption,
|
|
397
|
+
unregisterOption,
|
|
398
|
+
enabled: visible,
|
|
399
|
+
});
|
|
400
|
+
// O(1) index lookup via the registry's map
|
|
401
|
+
const index = optionIndexMap.get(toKey(optionValue)) ?? -1;
|
|
402
|
+
// Assign ref to listRef for navigation
|
|
403
|
+
useEffect(() => {
|
|
404
|
+
if (index >= 0) {
|
|
405
|
+
listRef.current[index] = ref.current;
|
|
406
|
+
}
|
|
407
|
+
}, [index, listRef]);
|
|
408
|
+
const isSelected = selectedValue !== undefined && toKey(selectedValue) === toKey(optionValue);
|
|
409
|
+
const isHighlighted = index >= 0 && activeIndex === index;
|
|
410
|
+
const handleClick = useCallback(() => {
|
|
411
|
+
if (disabled)
|
|
412
|
+
return;
|
|
413
|
+
selectValue(optionValue, textValue);
|
|
414
|
+
triggerElRef.current?.focus();
|
|
415
|
+
}, [disabled, optionValue, textValue, selectValue, triggerElRef]);
|
|
416
|
+
// Prevent the input losing focus when clicking an option
|
|
417
|
+
const handlePointerDown = useCallback((e) => {
|
|
418
|
+
e.preventDefault();
|
|
419
|
+
}, []);
|
|
420
|
+
if (!visible)
|
|
421
|
+
return null;
|
|
422
|
+
return (_jsxs("div", { ref: ref, id: optionIdForValue(listboxId, optionValue), role: "option", className: cx('tui-search-select__option', className), "aria-selected": isSelected, "aria-disabled": disabled || undefined, "data-highlighted": isHighlighted || undefined, "data-disabled": disabled || undefined, ...getItemProps({
|
|
423
|
+
onClick: handleClick,
|
|
424
|
+
onPointerDown: handlePointerDown,
|
|
425
|
+
}), children: [_jsx("span", { className: "tui-search-select__option-content", children: children }), isSelected && _jsx(Icon, { name: "system/check", className: "tui-search-select__option-check" })] }));
|
|
426
|
+
}
|
|
427
|
+
SearchSelectOptionComponent.displayName = 'SearchSelect.Option';
|
|
428
|
+
// =============================================================================
|
|
429
|
+
// SearchSelect.Group / SearchSelect.Label (shared engine pair)
|
|
430
|
+
// =============================================================================
|
|
431
|
+
const { Group: SearchSelectGroupComponent, Label: SearchSelectLabelComponent } = createListboxGroupComponents({
|
|
432
|
+
componentName: 'SearchSelect',
|
|
433
|
+
groupClass: 'tui-search-select__group',
|
|
434
|
+
labelClass: 'tui-search-select__label',
|
|
435
|
+
});
|
|
436
|
+
const SearchSelect = SearchSelectRoot;
|
|
437
|
+
SearchSelect.Trigger = SearchSelectTriggerComponent;
|
|
438
|
+
SearchSelect.Content = SearchSelectContentComponent;
|
|
439
|
+
SearchSelect.Option = SearchSelectOptionComponent;
|
|
440
|
+
SearchSelect.Group = SearchSelectGroupComponent;
|
|
441
|
+
SearchSelect.Label = SearchSelectLabelComponent;
|
|
442
|
+
export { SearchSelect, SearchSelectTriggerComponent as SearchSelectTrigger, SearchSelectContentComponent as SearchSelectContent, SearchSelectOptionComponent as SearchSelectOption, SearchSelectGroupComponent as SearchSelectGroup, SearchSelectLabelComponent as SearchSelectLabel, };
|
|
443
|
+
// eslint-disable-next-line react-refresh/only-export-components
|
|
444
|
+
export { useSearchSelect } from './SearchSelectContext.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SearchSelectContextValue, SearchSelectContentContextValue } from './types';
|
|
2
|
+
export declare const SearchSelectContext: import("react").Context<SearchSelectContextValue | null>;
|
|
3
|
+
export declare function useSearchSelectContext(): SearchSelectContextValue;
|
|
4
|
+
/** Public escape hatch, mirroring useSelect/useCombobox. */
|
|
5
|
+
export declare function useSearchSelect(): SearchSelectContextValue;
|
|
6
|
+
export declare const SearchSelectContentContext: import("react").Context<SearchSelectContentContextValue | null>;
|
|
7
|
+
export declare function useSearchSelectContentContext(): SearchSelectContentContextValue;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
export const SearchSelectContext = createContext(null);
|
|
3
|
+
export function useSearchSelectContext() {
|
|
4
|
+
const context = useContext(SearchSelectContext);
|
|
5
|
+
if (!context) {
|
|
6
|
+
throw new Error('SearchSelect components must be used within <SearchSelect>');
|
|
7
|
+
}
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
/** Public escape hatch, mirroring useSelect/useCombobox. */
|
|
11
|
+
export function useSearchSelect() {
|
|
12
|
+
return useSearchSelectContext();
|
|
13
|
+
}
|
|
14
|
+
export const SearchSelectContentContext = createContext(null);
|
|
15
|
+
export function useSearchSelectContentContext() {
|
|
16
|
+
const context = useContext(SearchSelectContentContext);
|
|
17
|
+
if (!context) {
|
|
18
|
+
throw new Error('SearchSelect.Option must be used within <SearchSelect.Content>');
|
|
19
|
+
}
|
|
20
|
+
return context;
|
|
21
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { SearchSelect, SearchSelectTrigger, SearchSelectContent, SearchSelectOption, SearchSelectGroup, SearchSelectLabel, useSearchSelect, } from './SearchSelect';
|
|
2
|
+
export type { SearchSelectProps, SearchSelectTriggerProps, SearchSelectContentProps, SearchSelectOptionProps, SearchSelectGroupProps, SearchSelectLabelProps, SearchSelectLabels, } from './types';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SearchSelect, SearchSelectTrigger, SearchSelectContent, SearchSelectOption, SearchSelectGroup, SearchSelectLabel, useSearchSelect, } from './SearchSelect.js';
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { CSSProperties, MutableRefObject, ReactNode, RefObject } from 'react';
|
|
2
|
+
import type { FloatingContext, ReferenceType } from '@floating-ui/react';
|
|
3
|
+
import type { SizeStandard } from '../../types/sizes';
|
|
4
|
+
import type { OptionValue } from '../../utils/value-key';
|
|
5
|
+
export type SearchSelectLabels = {
|
|
6
|
+
/** Placeholder AND accessible name for the search field. @default 'Search' */
|
|
7
|
+
search?: string;
|
|
8
|
+
/** Clear-search button label (forwarded to the embedded SearchInput). @default 'Clear search' */
|
|
9
|
+
clearSearch?: string;
|
|
10
|
+
/** Empty-state text when no options match. @default 'No results' */
|
|
11
|
+
noResults?: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const defaultSearchSelectLabels: Required<SearchSelectLabels>;
|
|
14
|
+
export type SearchSelectProps = {
|
|
15
|
+
/** Controlled selected value. */
|
|
16
|
+
value?: OptionValue;
|
|
17
|
+
/** Uncontrolled initial value. */
|
|
18
|
+
defaultValue?: OptionValue;
|
|
19
|
+
/** Fires with the committed option value. */
|
|
20
|
+
onValueChange?: (value: OptionValue) => void;
|
|
21
|
+
/** Controlled open state. */
|
|
22
|
+
open?: boolean;
|
|
23
|
+
/** @default false */
|
|
24
|
+
defaultOpen?: boolean;
|
|
25
|
+
onOpenChange?: (open: boolean) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Controlled search text. Usually uncontrolled — the query is transient and
|
|
28
|
+
* resets when the panel closes.
|
|
29
|
+
*/
|
|
30
|
+
inputValue?: string;
|
|
31
|
+
/** Fires as the user types (and on the reset-on-close). */
|
|
32
|
+
onInputChange?: (value: string) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Built-in filtering: options whose textValue fails this test against the
|
|
35
|
+
* query unmount. Pass `false` to disable and own the filtering by rendering
|
|
36
|
+
* a different set of Options (async lists).
|
|
37
|
+
* @default case-insensitive substring match on textValue
|
|
38
|
+
*/
|
|
39
|
+
filter?: false | ((textValue: string, query: string) => boolean);
|
|
40
|
+
/** @default false */
|
|
41
|
+
disabled?: boolean;
|
|
42
|
+
/** Trigger text when nothing is selected. @default '' */
|
|
43
|
+
placeholder?: string;
|
|
44
|
+
/** Control size for the trigger. @default 'md' */
|
|
45
|
+
size?: SizeStandard;
|
|
46
|
+
/** Trigger id — Field.Control injects this. */
|
|
47
|
+
id?: string;
|
|
48
|
+
'aria-label'?: string;
|
|
49
|
+
'aria-labelledby'?: string;
|
|
50
|
+
'aria-describedby'?: string;
|
|
51
|
+
'aria-invalid'?: boolean | 'true' | 'false';
|
|
52
|
+
'aria-required'?: boolean | 'true' | 'false';
|
|
53
|
+
labels?: SearchSelectLabels;
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
};
|
|
56
|
+
export type SearchSelectTriggerProps = {
|
|
57
|
+
className?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Custom trigger content — replaces the default value/placeholder text
|
|
60
|
+
* (the chevron stays). The fields dynamic-value picker renders its
|
|
61
|
+
* bolt + type + key pill here. NOTE: with an `aria-label` on the Root the
|
|
62
|
+
* accessible name is frozen to that label — custom content is then
|
|
63
|
+
* invisible to AT (family-wide follow-up in the roadmap).
|
|
64
|
+
*/
|
|
65
|
+
children?: ReactNode;
|
|
66
|
+
} & Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'children' | 'className'>;
|
|
67
|
+
export type SearchSelectContentProps = {
|
|
68
|
+
className?: string;
|
|
69
|
+
children?: ReactNode;
|
|
70
|
+
};
|
|
71
|
+
export type SearchSelectOptionProps = {
|
|
72
|
+
value: OptionValue;
|
|
73
|
+
disabled?: boolean;
|
|
74
|
+
/** Required when children is not a plain string. */
|
|
75
|
+
textValue?: string;
|
|
76
|
+
className?: string;
|
|
77
|
+
children?: ReactNode;
|
|
78
|
+
};
|
|
79
|
+
export type { ListboxGroupProps as SearchSelectGroupProps } from '../../utils/listbox';
|
|
80
|
+
export type { ListboxLabelProps as SearchSelectLabelProps } from '../../utils/listbox';
|
|
81
|
+
import type { RegisteredOption } from '../../utils/listbox';
|
|
82
|
+
export type { RegisteredOption };
|
|
83
|
+
export type SearchSelectContextValue = {
|
|
84
|
+
disabled: boolean;
|
|
85
|
+
placeholder: string;
|
|
86
|
+
size: SizeStandard;
|
|
87
|
+
labels: Required<SearchSelectLabels>;
|
|
88
|
+
triggerId: string;
|
|
89
|
+
listboxId: string;
|
|
90
|
+
panelId: string;
|
|
91
|
+
ariaLabel?: string;
|
|
92
|
+
ariaLabelledBy?: string;
|
|
93
|
+
ariaDescribedBy?: string;
|
|
94
|
+
ariaInvalid?: boolean | 'true' | 'false';
|
|
95
|
+
ariaRequired?: boolean | 'true' | 'false';
|
|
96
|
+
setOpen: (open: boolean) => void;
|
|
97
|
+
selectValue: (value: OptionValue, textValue: string) => void;
|
|
98
|
+
setInputValue: (value: string) => void;
|
|
99
|
+
registerOption: (option: RegisteredOption) => void;
|
|
100
|
+
unregisterOption: (optionValue: OptionValue) => void;
|
|
101
|
+
labelFor: (value: OptionValue) => string;
|
|
102
|
+
handlePanelBlur: (e: React.FocusEvent) => void;
|
|
103
|
+
handleInputKeyDown: (e: React.KeyboardEvent) => void;
|
|
104
|
+
refs: {
|
|
105
|
+
reference: RefObject<ReferenceType | null>;
|
|
106
|
+
floating: RefObject<HTMLElement | null>;
|
|
107
|
+
setReference: (node: ReferenceType | null) => void;
|
|
108
|
+
setPositionReference: (node: ReferenceType | null) => void;
|
|
109
|
+
setFloating: (node: HTMLElement | null) => void;
|
|
110
|
+
};
|
|
111
|
+
triggerElRef: MutableRefObject<HTMLButtonElement | null>;
|
|
112
|
+
inputRef: MutableRefObject<HTMLInputElement | null>;
|
|
113
|
+
listRef: MutableRefObject<(HTMLElement | null)[]>;
|
|
114
|
+
optionIndexMap: Map<string, number>;
|
|
115
|
+
context: FloatingContext;
|
|
116
|
+
getReferenceProps: (userProps?: React.HTMLProps<Element>) => Record<string, unknown>;
|
|
117
|
+
getFloatingProps: (userProps?: React.HTMLProps<HTMLElement>) => Record<string, unknown>;
|
|
118
|
+
getItemProps: (userProps?: React.HTMLProps<HTMLElement>) => Record<string, unknown>;
|
|
119
|
+
open: boolean;
|
|
120
|
+
value: OptionValue | undefined;
|
|
121
|
+
inputValue: string;
|
|
122
|
+
query: string;
|
|
123
|
+
filter: false | ((textValue: string, query: string) => boolean);
|
|
124
|
+
activeIndex: number;
|
|
125
|
+
activeOptionId: string | undefined;
|
|
126
|
+
orderedOptions: RegisteredOption[];
|
|
127
|
+
floatingStyles: CSSProperties;
|
|
128
|
+
};
|
|
129
|
+
export type SearchSelectContentContextValue = {
|
|
130
|
+
listRef: MutableRefObject<(HTMLElement | null)[]>;
|
|
131
|
+
activeIndex: number;
|
|
132
|
+
orderedOptions: RegisteredOption[];
|
|
133
|
+
optionIndexMap: Map<string, number>;
|
|
134
|
+
};
|