@tangible/ui 0.2.9 → 0.2.10
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type SearchSelectProps, type SearchSelectTriggerProps, type SearchSelectContentProps, type SearchSelectOptionProps } from './types';
|
|
2
|
-
declare function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange, open: controlledOpen, defaultOpen, onOpenChange, inputValue: controlledInputValue, onInputChange, filter, disabled, placeholder, size, id: triggerIdProp, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, labels: labelsProp, children, }: SearchSelectProps): import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
declare function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange, open: controlledOpen, defaultOpen, onOpenChange, inputValue: controlledInputValue, onInputChange, filter, disabled, placeholder, size, anchorRef, restoreFocusRef, id: triggerIdProp, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, labels: labelsProp, children, }: SearchSelectProps): import("react/jsx-runtime").JSX.Element;
|
|
3
3
|
declare namespace SearchSelectRoot {
|
|
4
4
|
var displayName: string;
|
|
5
5
|
}
|
|
@@ -14,7 +14,7 @@ const defaultFilter = (textValue, query) => textValue.toLowerCase().includes(que
|
|
|
14
14
|
// =============================================================================
|
|
15
15
|
// SearchSelect Root
|
|
16
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, }) {
|
|
17
|
+
function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange, open: controlledOpen, defaultOpen, onOpenChange, inputValue: controlledInputValue, onInputChange, filter = defaultFilter, disabled = false, placeholder = '', size = 'md', anchorRef, restoreFocusRef, id: triggerIdProp, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, labels: labelsProp, children, }) {
|
|
18
18
|
const labels = { ...defaultSearchSelectLabels, ...labelsProp };
|
|
19
19
|
// Controlled/uncontrolled value — lock decision at mount time (see the
|
|
20
20
|
// controlled→uncontrolled gotcha in CLAUDE.md)
|
|
@@ -84,6 +84,10 @@ function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange,
|
|
|
84
84
|
const listRef = useRef([]);
|
|
85
85
|
// Active index for navigation (-1 means no active option)
|
|
86
86
|
const [activeIndex, setActiveIndex] = useState(-1);
|
|
87
|
+
const returnFocus = useCallback(() => {
|
|
88
|
+
;
|
|
89
|
+
(restoreFocusRef?.current ?? triggerElRef.current)?.focus();
|
|
90
|
+
}, [restoreFocusRef]);
|
|
87
91
|
// Commit a value: capture label, close, reset the query, hand focus back
|
|
88
92
|
const selectValue = useCallback((optionValue, textValue) => {
|
|
89
93
|
knownLabelsRef.current.set(toKey(optionValue), textValue);
|
|
@@ -136,10 +140,21 @@ function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange,
|
|
|
136
140
|
onNavigate: (index) => setActiveIndex(index ?? -1),
|
|
137
141
|
disabledIndices,
|
|
138
142
|
withClick: false,
|
|
139
|
-
dismissOutsidePress: (event) =>
|
|
140
|
-
|
|
141
|
-
|
|
143
|
+
dismissOutsidePress: (event) => {
|
|
144
|
+
const toggleEl = restoreFocusRef?.current ?? triggerElRef.current;
|
|
145
|
+
return !(toggleEl && event.target instanceof Node && toggleEl.contains(event.target));
|
|
146
|
+
},
|
|
142
147
|
});
|
|
148
|
+
// EXTERNAL-TRIGGER MODE: anchor the panel to the provided element instead
|
|
149
|
+
// of the built-in Trigger (re-checked on every open — the anchor can mount
|
|
150
|
+
// late). Focus returns via restoreFocusRef (usually the opening button).
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (anchorRef?.current) {
|
|
153
|
+
refs.setPositionReference(anchorRef.current);
|
|
154
|
+
}
|
|
155
|
+
// refs is stable; re-run on open so late-mounted anchors are picked up
|
|
156
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
157
|
+
}, [open, anchorRef]);
|
|
143
158
|
// Active option ID for aria-activedescendant (hash-based — filtering shifts indices)
|
|
144
159
|
const activeOptionId = activeIndex >= 0 && orderedOptions[activeIndex]
|
|
145
160
|
? optionIdForValue(listboxId, orderedOptions[activeIndex].value)
|
|
@@ -157,7 +172,7 @@ function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange,
|
|
|
157
172
|
open,
|
|
158
173
|
setOpen,
|
|
159
174
|
floatingRef: refs.floating,
|
|
160
|
-
extraContainerRef: triggerElRef,
|
|
175
|
+
extraContainerRef: restoreFocusRef ?? triggerElRef,
|
|
161
176
|
});
|
|
162
177
|
const handleInputKeyDown = useCallback((e) => {
|
|
163
178
|
if (e.key === 'Enter') {
|
|
@@ -166,7 +181,7 @@ function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange,
|
|
|
166
181
|
const option = orderedOptions[activeIndex];
|
|
167
182
|
if (option && !option.disabled) {
|
|
168
183
|
selectValue(option.value, option.textValue);
|
|
169
|
-
|
|
184
|
+
returnFocus();
|
|
170
185
|
}
|
|
171
186
|
}
|
|
172
187
|
return;
|
|
@@ -180,9 +195,9 @@ function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange,
|
|
|
180
195
|
e.preventDefault();
|
|
181
196
|
e.stopPropagation();
|
|
182
197
|
setOpen(false);
|
|
183
|
-
|
|
198
|
+
returnFocus();
|
|
184
199
|
}
|
|
185
|
-
}, [activeIndex, orderedOptions, selectValue, setOpen]);
|
|
200
|
+
}, [activeIndex, orderedOptions, selectValue, setOpen, returnFocus]);
|
|
186
201
|
const contextValue = useMemo(() => ({
|
|
187
202
|
disabled,
|
|
188
203
|
placeholder,
|
|
@@ -202,10 +217,12 @@ function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange,
|
|
|
202
217
|
registerOption,
|
|
203
218
|
unregisterOption,
|
|
204
219
|
labelFor,
|
|
220
|
+
returnFocus,
|
|
205
221
|
handlePanelBlur,
|
|
206
222
|
handleInputKeyDown,
|
|
207
223
|
refs,
|
|
208
224
|
triggerElRef,
|
|
225
|
+
anchorRef,
|
|
209
226
|
inputRef,
|
|
210
227
|
listRef,
|
|
211
228
|
optionIndexMap,
|
|
@@ -244,6 +261,7 @@ function SearchSelectRoot({ value: controlledValue, defaultValue, onValueChange,
|
|
|
244
261
|
registerOption,
|
|
245
262
|
unregisterOption,
|
|
246
263
|
labelFor,
|
|
264
|
+
returnFocus,
|
|
247
265
|
handlePanelBlur,
|
|
248
266
|
handleInputKeyDown,
|
|
249
267
|
refs,
|
|
@@ -315,7 +333,7 @@ SearchSelectTriggerComponent.displayName = 'SearchSelect.Trigger';
|
|
|
315
333
|
// SearchSelect.Content
|
|
316
334
|
// =============================================================================
|
|
317
335
|
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();
|
|
336
|
+
const { labels, size, triggerId, listboxId, panelId, ariaLabel, setInputValue, handlePanelBlur, handleInputKeyDown, refs, triggerElRef, anchorRef, inputRef, listRef, optionIndexMap, getReferenceProps, getFloatingProps, open, inputValue, activeOptionId, orderedOptions, floatingStyles, } = useSearchSelectContext();
|
|
319
337
|
// Move focus into the search field when the panel opens. Callback ref, not
|
|
320
338
|
// an open-keyed effect: FloatingPortal can attach the subtree after the
|
|
321
339
|
// parent's effects run, so an effect may fire while the input is still
|
|
@@ -350,13 +368,13 @@ function SearchSelectContentComponent({ className, children }) {
|
|
|
350
368
|
}
|
|
351
369
|
if (!open)
|
|
352
370
|
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(),
|
|
371
|
+
return (_jsx(FloatingPortal, { root: getPortalRootFor(triggerElRef.current ?? anchorRef?.current ?? null), children: _jsxs("div", { ref: refs.setFloating, className: cx('tui-search-select__panel', className), style: floatingStyles, ...getFloatingProps(),
|
|
354
372
|
// AFTER the spread: useRole stamps role="listbox" and its own id on
|
|
355
373
|
// the floating element — this panel is a NON-MODAL dialog (no focus
|
|
356
374
|
// trap, no aria-modal; focus-out dismisses it via the blur guard
|
|
357
375
|
// below, like a combobox popup) holding a combobox + listbox. The
|
|
358
376
|
// trigger's aria-controls points at panelId.
|
|
359
|
-
id: panelId, role: "dialog", "aria-labelledby": triggerId,
|
|
377
|
+
id: panelId, role: "dialog", "aria-labelledby": triggerElRef.current ? triggerId : undefined, "aria-label": triggerElRef.current ? undefined : ariaLabel,
|
|
360
378
|
// Panel-level so blur from ANY descendant (input, search clear button,
|
|
361
379
|
// future panel furniture) is evaluated — input-only wiring left the
|
|
362
380
|
// panel orphaned open after Tab past the clear button.
|
|
@@ -367,14 +385,14 @@ function SearchSelectContentComponent({ className, children }) {
|
|
|
367
385
|
// combobox-in-dialog shape. aria-haspopup is useRole's listbox
|
|
368
386
|
// leftover — a combobox already conveys its popup via
|
|
369
387
|
// 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 }))] }) }));
|
|
388
|
+
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": triggerElRef.current ? triggerId : undefined, "aria-label": triggerElRef.current ? undefined : ariaLabel, 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
389
|
}
|
|
372
390
|
SearchSelectContentComponent.displayName = 'SearchSelect.Content';
|
|
373
391
|
// =============================================================================
|
|
374
392
|
// SearchSelect.Option
|
|
375
393
|
// =============================================================================
|
|
376
394
|
function SearchSelectOptionComponent({ value: optionValue, disabled = false, textValue: explicitTextValue, className, children, }) {
|
|
377
|
-
const { value: selectedValue, selectValue, listboxId, registerOption, unregisterOption, getItemProps, query, filter,
|
|
395
|
+
const { value: selectedValue, selectValue, listboxId, registerOption, unregisterOption, getItemProps, query, filter, returnFocus, } = useSearchSelectContext();
|
|
378
396
|
const { listRef, activeIndex, optionIndexMap } = useSearchSelectContentContext();
|
|
379
397
|
const ref = useRef(null);
|
|
380
398
|
// Built-in filtering: a non-matching option renders null AND leaves the
|
|
@@ -411,8 +429,8 @@ function SearchSelectOptionComponent({ value: optionValue, disabled = false, tex
|
|
|
411
429
|
if (disabled)
|
|
412
430
|
return;
|
|
413
431
|
selectValue(optionValue, textValue);
|
|
414
|
-
|
|
415
|
-
}, [disabled, optionValue, textValue, selectValue,
|
|
432
|
+
returnFocus();
|
|
433
|
+
}, [disabled, optionValue, textValue, selectValue, returnFocus]);
|
|
416
434
|
// Prevent the input losing focus when clicking an option
|
|
417
435
|
const handlePointerDown = useCallback((e) => {
|
|
418
436
|
e.preventDefault();
|
|
@@ -45,6 +45,21 @@ export type SearchSelectProps = {
|
|
|
45
45
|
size?: SizeStandard;
|
|
46
46
|
/** Trigger id — Field.Control injects this. */
|
|
47
47
|
id?: string;
|
|
48
|
+
/**
|
|
49
|
+
* EXTERNAL-TRIGGER MODE: anchor the panel to this element instead of the
|
|
50
|
+
* built-in Trigger. Use with controlled `open`/`onOpenChange` and no
|
|
51
|
+
* `<SearchSelect.Trigger>` — e.g. an IconButton that opens a picker panel
|
|
52
|
+
* under its host field. Provide `aria-label` (the panel's name — the
|
|
53
|
+
* trigger-labelled path doesn't exist here) and wire the external button's
|
|
54
|
+
* own aria-haspopup/aria-expanded.
|
|
55
|
+
*/
|
|
56
|
+
anchorRef?: React.RefObject<HTMLElement | null>;
|
|
57
|
+
/**
|
|
58
|
+
* External-trigger mode: where focus returns on commit/Escape (usually the
|
|
59
|
+
* button that opened the panel). Blur onto this element keeps the panel
|
|
60
|
+
* open, so the button's own click handler can toggle without flicker.
|
|
61
|
+
*/
|
|
62
|
+
restoreFocusRef?: React.RefObject<HTMLElement | null>;
|
|
48
63
|
'aria-label'?: string;
|
|
49
64
|
'aria-labelledby'?: string;
|
|
50
65
|
'aria-describedby'?: string;
|
|
@@ -99,6 +114,8 @@ export type SearchSelectContextValue = {
|
|
|
99
114
|
registerOption: (option: RegisteredOption) => void;
|
|
100
115
|
unregisterOption: (optionValue: OptionValue) => void;
|
|
101
116
|
labelFor: (value: OptionValue) => string;
|
|
117
|
+
/** Focus the trigger (or restoreFocusRef in external-trigger mode). */
|
|
118
|
+
returnFocus: () => void;
|
|
102
119
|
handlePanelBlur: (e: React.FocusEvent) => void;
|
|
103
120
|
handleInputKeyDown: (e: React.KeyboardEvent) => void;
|
|
104
121
|
refs: {
|
|
@@ -109,6 +126,8 @@ export type SearchSelectContextValue = {
|
|
|
109
126
|
setFloating: (node: HTMLElement | null) => void;
|
|
110
127
|
};
|
|
111
128
|
triggerElRef: MutableRefObject<HTMLButtonElement | null>;
|
|
129
|
+
/** External-trigger mode anchor (see SearchSelectProps.anchorRef). */
|
|
130
|
+
anchorRef?: React.RefObject<HTMLElement | null>;
|
|
112
131
|
inputRef: MutableRefObject<HTMLInputElement | null>;
|
|
113
132
|
listRef: MutableRefObject<(HTMLElement | null)[]>;
|
|
114
133
|
optionIndexMap: Map<string, number>;
|
package/package.json
CHANGED
package/tui-manifest.json
CHANGED