@dxos/react-ui-searchlist 0.8.4-main.406dc2a → 0.8.4-main.59c2e9b
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/dist/lib/browser/index.mjs +669 -337
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +669 -337
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/Combobox/Combobox.d.ts +48 -8
- package/dist/types/src/components/Combobox/Combobox.d.ts.map +1 -1
- package/dist/types/src/components/Combobox/Combobox.stories.d.ts +1 -1
- package/dist/types/src/components/Combobox/Combobox.stories.d.ts.map +1 -1
- package/dist/types/src/components/Listbox/Listbox.d.ts.map +1 -1
- package/dist/types/src/components/Listbox/Listbox.stories.d.ts +1 -1
- package/dist/types/src/components/SearchList/SearchList.d.ts +83 -20
- package/dist/types/src/components/SearchList/SearchList.d.ts.map +1 -1
- package/dist/types/src/components/SearchList/SearchList.stories.d.ts +10 -7
- package/dist/types/src/components/SearchList/SearchList.stories.d.ts.map +1 -1
- package/dist/types/src/components/SearchList/context.d.ts +33 -0
- package/dist/types/src/components/SearchList/context.d.ts.map +1 -0
- package/dist/types/src/components/SearchList/hooks/index.d.ts +5 -0
- package/dist/types/src/components/SearchList/hooks/index.d.ts.map +1 -0
- package/dist/types/src/components/SearchList/hooks/useGlobalFilter.d.ts +34 -0
- package/dist/types/src/components/SearchList/hooks/useGlobalFilter.d.ts.map +1 -0
- package/dist/types/src/components/SearchList/hooks/useSearchListInput.d.ts +12 -0
- package/dist/types/src/components/SearchList/hooks/useSearchListInput.d.ts.map +1 -0
- package/dist/types/src/components/SearchList/hooks/useSearchListItem.d.ts +10 -0
- package/dist/types/src/components/SearchList/hooks/useSearchListItem.d.ts.map +1 -0
- package/dist/types/src/components/SearchList/hooks/useSearchListResults.d.ts +36 -0
- package/dist/types/src/components/SearchList/hooks/useSearchListResults.d.ts.map +1 -0
- package/dist/types/src/components/SearchList/index.d.ts +1 -0
- package/dist/types/src/components/SearchList/index.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +2 -2
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -17
- package/src/components/Combobox/Combobox.stories.tsx +9 -4
- package/src/components/Combobox/Combobox.tsx +35 -14
- package/src/components/Listbox/Listbox.stories.tsx +1 -1
- package/src/components/Listbox/Listbox.tsx +8 -3
- package/src/components/SearchList/SearchList.stories.tsx +500 -30
- package/src/components/SearchList/SearchList.tsx +457 -62
- package/src/components/SearchList/context.ts +43 -0
- package/src/components/SearchList/hooks/index.ts +8 -0
- package/src/components/SearchList/hooks/useGlobalFilter.tsx +61 -0
- package/src/components/SearchList/hooks/useSearchListInput.ts +14 -0
- package/src/components/SearchList/hooks/useSearchListItem.ts +14 -0
- package/src/components/SearchList/hooks/useSearchListResults.ts +104 -0
- package/src/components/SearchList/index.ts +1 -0
- package/src/translations.ts +1 -1
- package/src/types/command-score.d.ts +16 -0
|
@@ -1,76 +1,384 @@
|
|
|
1
1
|
//
|
|
2
|
-
// Copyright
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import React, {
|
|
5
|
+
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
6
|
+
import React, {
|
|
7
|
+
type ChangeEvent,
|
|
8
|
+
type ComponentPropsWithRef,
|
|
9
|
+
type KeyboardEvent,
|
|
10
|
+
type PropsWithChildren,
|
|
11
|
+
type ReactNode,
|
|
12
|
+
forwardRef,
|
|
13
|
+
useCallback,
|
|
14
|
+
useEffect,
|
|
15
|
+
useMemo,
|
|
16
|
+
useRef,
|
|
17
|
+
useState,
|
|
18
|
+
} from 'react';
|
|
7
19
|
|
|
8
20
|
import {
|
|
9
|
-
type
|
|
21
|
+
type Density,
|
|
22
|
+
type Elevation,
|
|
23
|
+
Icon,
|
|
10
24
|
type ThemedClassName,
|
|
11
25
|
useDensityContext,
|
|
12
26
|
useElevationContext,
|
|
13
27
|
useThemeContext,
|
|
14
28
|
useTranslation,
|
|
15
29
|
} from '@dxos/react-ui';
|
|
16
|
-
import { mx } from '@dxos/
|
|
30
|
+
import { descriptionText, mx } from '@dxos/ui-theme';
|
|
17
31
|
|
|
18
32
|
import { translationKey } from '../../translations';
|
|
19
33
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
34
|
+
import {
|
|
35
|
+
SearchListInputContextProvider,
|
|
36
|
+
SearchListItemContextProvider,
|
|
37
|
+
useSearchListInputContext,
|
|
38
|
+
useSearchListItemContext,
|
|
39
|
+
} from './context';
|
|
23
40
|
|
|
24
|
-
|
|
25
|
-
|
|
41
|
+
//
|
|
42
|
+
// Internal types
|
|
43
|
+
//
|
|
44
|
+
|
|
45
|
+
type ItemData = {
|
|
46
|
+
element: HTMLElement;
|
|
47
|
+
disabled?: boolean;
|
|
48
|
+
onSelect?: () => void;
|
|
49
|
+
};
|
|
26
50
|
|
|
27
51
|
//
|
|
28
52
|
// Root
|
|
29
53
|
//
|
|
30
54
|
|
|
31
|
-
type
|
|
55
|
+
type SearchListRootProps = PropsWithChildren<{
|
|
56
|
+
/** Controlled query value. */
|
|
57
|
+
value?: string;
|
|
58
|
+
/** Default query value for uncontrolled mode. */
|
|
59
|
+
defaultValue?: string;
|
|
60
|
+
/** Debounce delay in milliseconds. */
|
|
61
|
+
debounceMs?: number;
|
|
62
|
+
/** Callback when search query changes (debounced). */
|
|
63
|
+
onSearch?: (query: string) => void;
|
|
64
|
+
}>;
|
|
65
|
+
|
|
66
|
+
const SearchListRoot = ({
|
|
67
|
+
children,
|
|
68
|
+
value: valueProp,
|
|
69
|
+
defaultValue = '',
|
|
70
|
+
debounceMs = 200,
|
|
71
|
+
onSearch,
|
|
72
|
+
}: SearchListRootProps) => {
|
|
73
|
+
const [query = '', setQuery] = useControllableState({
|
|
74
|
+
prop: valueProp,
|
|
75
|
+
defaultProp: defaultValue,
|
|
76
|
+
onChange: undefined,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const [selectedValue, setSelectedValue] = useState<string | undefined>(undefined);
|
|
80
|
+
|
|
81
|
+
// Track registered items: value -> { element, onSelect, disabled }.
|
|
82
|
+
const itemsRef = useRef<Map<string, ItemData>>(new Map());
|
|
83
|
+
|
|
84
|
+
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
85
|
+
|
|
86
|
+
const handleQueryChange = useCallback(
|
|
87
|
+
(newQuery: string) => {
|
|
88
|
+
setQuery(newQuery);
|
|
89
|
+
// Don't update selectedValue here - let the effect handle it when items actually change.
|
|
90
|
+
// This prevents unnecessary re-renders of items when query changes.
|
|
91
|
+
|
|
92
|
+
// Debounce onSearch callback.
|
|
93
|
+
if (debounceRef.current) {
|
|
94
|
+
clearTimeout(debounceRef.current);
|
|
95
|
+
}
|
|
96
|
+
debounceRef.current = setTimeout(() => {
|
|
97
|
+
onSearch?.(newQuery);
|
|
98
|
+
}, debounceMs);
|
|
99
|
+
},
|
|
100
|
+
[setQuery, onSearch, debounceMs],
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// Track when items change to trigger first-item selection.
|
|
104
|
+
const [itemVersion, setItemVersion] = useState(0);
|
|
105
|
+
|
|
106
|
+
// Cleanup debounce on unmount.
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
return () => {
|
|
109
|
+
if (debounceRef.current) {
|
|
110
|
+
clearTimeout(debounceRef.current);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}, []);
|
|
114
|
+
|
|
115
|
+
// Auto-select first non-disabled item when items change and no valid selection exists.
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
// Check if current selection is still valid (exists and not disabled).
|
|
118
|
+
const currentItem = selectedValue !== undefined ? itemsRef.current.get(selectedValue) : undefined;
|
|
119
|
+
const isSelectionValid = currentItem !== undefined && !currentItem.disabled;
|
|
120
|
+
if (!isSelectionValid && itemsRef.current.size > 0) {
|
|
121
|
+
// Get first non-disabled item in DOM order.
|
|
122
|
+
const entries = Array.from(itemsRef.current.entries()).filter(([, data]) => !data.disabled);
|
|
123
|
+
if (entries.length > 0) {
|
|
124
|
+
entries.sort(([, a], [, b]) => {
|
|
125
|
+
const position = a.element.compareDocumentPosition(b.element);
|
|
126
|
+
if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
|
|
127
|
+
return -1;
|
|
128
|
+
}
|
|
129
|
+
if (position & Node.DOCUMENT_POSITION_PRECEDING) {
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
return 0;
|
|
133
|
+
});
|
|
134
|
+
const firstValue = entries[0]?.[0];
|
|
135
|
+
if (firstValue !== undefined && firstValue !== selectedValue) {
|
|
136
|
+
setSelectedValue(firstValue);
|
|
137
|
+
}
|
|
138
|
+
} else if (selectedValue !== undefined) {
|
|
139
|
+
// No valid items available, clear selection
|
|
140
|
+
setSelectedValue(undefined);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}, [itemVersion, selectedValue]);
|
|
144
|
+
|
|
145
|
+
const registerItem = useCallback(
|
|
146
|
+
(value: string, element: HTMLElement | null, onSelect: (() => void) | undefined, disabled?: boolean) => {
|
|
147
|
+
if (element) {
|
|
148
|
+
itemsRef.current.set(value, { element, onSelect, disabled });
|
|
149
|
+
setItemVersion((v) => v + 1);
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
[],
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const unregisterItem = useCallback((value: string) => {
|
|
156
|
+
itemsRef.current.delete(value);
|
|
157
|
+
setItemVersion((v) => v + 1);
|
|
158
|
+
}, []);
|
|
159
|
+
|
|
160
|
+
// Get item values in DOM order by sorting registered elements (excludes disabled items).
|
|
161
|
+
const getItemValues = useCallback(() => {
|
|
162
|
+
return Array.from(itemsRef.current.entries())
|
|
163
|
+
.filter(([, data]) => !data.disabled)
|
|
164
|
+
.sort(([, a], [, b]) => {
|
|
165
|
+
// Sort by DOM position using compareDocumentPosition.
|
|
166
|
+
const position = a.element.compareDocumentPosition(b.element);
|
|
167
|
+
return position & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : position & Node.DOCUMENT_POSITION_PRECEDING ? 1 : 0;
|
|
168
|
+
})
|
|
169
|
+
.map(([value]) => value);
|
|
170
|
+
}, []);
|
|
171
|
+
|
|
172
|
+
const triggerSelect = useCallback(() => {
|
|
173
|
+
if (selectedValue !== undefined) {
|
|
174
|
+
const item = itemsRef.current.get(selectedValue);
|
|
175
|
+
item?.onSelect?.();
|
|
176
|
+
}
|
|
177
|
+
}, [selectedValue]);
|
|
178
|
+
|
|
179
|
+
// Item context; stable, doesn't change when query changes.
|
|
180
|
+
const itemContextValue = useMemo(
|
|
181
|
+
() => ({
|
|
182
|
+
selectedValue,
|
|
183
|
+
onSelectedValueChange: setSelectedValue,
|
|
184
|
+
registerItem,
|
|
185
|
+
unregisterItem,
|
|
186
|
+
}),
|
|
187
|
+
[selectedValue, registerItem, unregisterItem],
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const inputContextValue = useMemo(
|
|
191
|
+
() => ({
|
|
192
|
+
query,
|
|
193
|
+
onQueryChange: handleQueryChange,
|
|
194
|
+
selectedValue,
|
|
195
|
+
onSelectedValueChange: setSelectedValue,
|
|
196
|
+
getItemValues,
|
|
197
|
+
triggerSelect,
|
|
198
|
+
}),
|
|
199
|
+
[query, handleQueryChange, selectedValue, getItemValues, triggerSelect],
|
|
200
|
+
);
|
|
32
201
|
|
|
33
|
-
|
|
34
|
-
|
|
202
|
+
// NOTE: Separate contexts for items and input to avoid unnecessary re-renders of items when query changes.
|
|
203
|
+
return (
|
|
204
|
+
<SearchListInputContextProvider
|
|
205
|
+
query={inputContextValue.query}
|
|
206
|
+
onQueryChange={inputContextValue.onQueryChange}
|
|
207
|
+
selectedValue={inputContextValue.selectedValue}
|
|
208
|
+
onSelectedValueChange={inputContextValue.onSelectedValueChange}
|
|
209
|
+
getItemValues={inputContextValue.getItemValues}
|
|
210
|
+
triggerSelect={inputContextValue.triggerSelect}
|
|
211
|
+
>
|
|
212
|
+
<SearchListItemContextProvider
|
|
213
|
+
selectedValue={itemContextValue.selectedValue}
|
|
214
|
+
onSelectedValueChange={itemContextValue.onSelectedValueChange}
|
|
215
|
+
registerItem={itemContextValue.registerItem}
|
|
216
|
+
unregisterItem={itemContextValue.unregisterItem}
|
|
217
|
+
>
|
|
218
|
+
{children}
|
|
219
|
+
</SearchListItemContextProvider>
|
|
220
|
+
</SearchListInputContextProvider>
|
|
221
|
+
);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
SearchListRoot.displayName = 'SearchList.Root';
|
|
225
|
+
|
|
226
|
+
//
|
|
227
|
+
// Viewport
|
|
228
|
+
//
|
|
229
|
+
|
|
230
|
+
type SearchListViewportProps = ThemedClassName<PropsWithChildren<{}>>;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Scrollable viewport wrapper for Content.
|
|
234
|
+
* Only Content wrapped in Viewport will be scrollable.
|
|
235
|
+
*/
|
|
236
|
+
const SearchListViewport = ({ classNames, children }: SearchListViewportProps) => {
|
|
237
|
+
return (
|
|
238
|
+
<div role='none' className={mx('is-full min-bs-0 grow overflow-y-auto', classNames)}>
|
|
239
|
+
{children}
|
|
240
|
+
</div>
|
|
241
|
+
);
|
|
35
242
|
};
|
|
36
243
|
|
|
37
|
-
|
|
38
|
-
|
|
244
|
+
SearchListViewport.displayName = 'SearchList.Viewport';
|
|
245
|
+
|
|
246
|
+
//
|
|
247
|
+
// Content
|
|
248
|
+
//
|
|
249
|
+
|
|
250
|
+
type SearchListContentProps = ThemedClassName<PropsWithChildren<{}>>;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Container for search results. Does not scroll by default.
|
|
254
|
+
* Wrap in Viewport for scrollable content.
|
|
255
|
+
*/
|
|
256
|
+
const SearchListContent = forwardRef<HTMLDivElement, SearchListContentProps>(
|
|
257
|
+
({ classNames, children }, forwardedRef) => {
|
|
39
258
|
return (
|
|
40
|
-
<
|
|
259
|
+
<div
|
|
260
|
+
ref={forwardedRef}
|
|
261
|
+
role='listbox'
|
|
262
|
+
className={mx('flex flex-col is-full min-bs-0 grow overflow-hidden', classNames)}
|
|
263
|
+
>
|
|
41
264
|
{children}
|
|
42
|
-
</
|
|
265
|
+
</div>
|
|
43
266
|
);
|
|
44
267
|
},
|
|
45
268
|
);
|
|
46
269
|
|
|
47
|
-
|
|
270
|
+
SearchListContent.displayName = 'SearchList.Content';
|
|
48
271
|
|
|
49
272
|
//
|
|
50
273
|
// Input
|
|
51
274
|
//
|
|
52
275
|
|
|
53
|
-
type
|
|
276
|
+
type InputVariant = 'default' | 'subdued';
|
|
54
277
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
278
|
+
type SearchListInputProps = ThemedClassName<
|
|
279
|
+
Omit<ComponentPropsWithRef<'input'>, 'value'> & {
|
|
280
|
+
density?: Density;
|
|
281
|
+
elevation?: Elevation;
|
|
282
|
+
variant?: InputVariant;
|
|
283
|
+
}
|
|
284
|
+
>;
|
|
58
285
|
|
|
59
286
|
const SearchListInput = forwardRef<HTMLInputElement, SearchListInputProps>(
|
|
60
|
-
(
|
|
287
|
+
(
|
|
288
|
+
{ classNames, density: propsDensity, elevation: propsElevation, variant, placeholder, onChange, ...props },
|
|
289
|
+
forwardedRef,
|
|
290
|
+
) => {
|
|
291
|
+
const { query, onQueryChange, selectedValue, onSelectedValueChange, getItemValues, triggerSelect } =
|
|
292
|
+
useSearchListInputContext('SearchList.Input');
|
|
61
293
|
const { t } = useTranslation(translationKey);
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
// TODO(thure): Keep this in-sync with `TextInput`, or submit a PR for `cmdk` to support `asChild` so we don’t have to.
|
|
65
|
-
const { hasIosKeyboard } = useThemeContext();
|
|
66
|
-
const { tx } = useThemeContext();
|
|
294
|
+
const { hasIosKeyboard, tx } = useThemeContext();
|
|
67
295
|
const density = useDensityContext(propsDensity);
|
|
68
296
|
const elevation = useElevationContext(propsElevation);
|
|
297
|
+
const defaultPlaceholder = t('search.placeholder');
|
|
298
|
+
|
|
299
|
+
const handleChange = useCallback(
|
|
300
|
+
(event: ChangeEvent<HTMLInputElement>) => {
|
|
301
|
+
onQueryChange(event.target.value);
|
|
302
|
+
onChange?.(event);
|
|
303
|
+
},
|
|
304
|
+
[onQueryChange, onChange],
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const handleKeyDown = useCallback(
|
|
308
|
+
(event: KeyboardEvent<HTMLInputElement>) => {
|
|
309
|
+
const values = getItemValues();
|
|
310
|
+
if (values.length === 0) {
|
|
311
|
+
if (event.key === 'Escape') {
|
|
312
|
+
onQueryChange('');
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const currentIndex = selectedValue !== undefined ? values.indexOf(selectedValue) : -1;
|
|
318
|
+
|
|
319
|
+
switch (event.key) {
|
|
320
|
+
case 'ArrowDown': {
|
|
321
|
+
event.preventDefault();
|
|
322
|
+
const nextIndex = currentIndex === -1 ? 0 : Math.min(currentIndex + 1, values.length - 1);
|
|
323
|
+
const nextValue = values[nextIndex];
|
|
324
|
+
if (nextValue !== undefined) {
|
|
325
|
+
onSelectedValueChange(nextValue);
|
|
326
|
+
}
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
case 'ArrowUp': {
|
|
330
|
+
event.preventDefault();
|
|
331
|
+
const prevIndex = currentIndex === -1 ? values.length - 1 : Math.max(currentIndex - 1, 0);
|
|
332
|
+
const prevValue = values[prevIndex];
|
|
333
|
+
if (prevValue !== undefined) {
|
|
334
|
+
onSelectedValueChange(prevValue);
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
case 'Enter': {
|
|
339
|
+
if (selectedValue !== undefined) {
|
|
340
|
+
event.preventDefault();
|
|
341
|
+
triggerSelect();
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
case 'Home': {
|
|
346
|
+
event.preventDefault();
|
|
347
|
+
const firstValue = values[0];
|
|
348
|
+
if (firstValue !== undefined) {
|
|
349
|
+
onSelectedValueChange(firstValue);
|
|
350
|
+
}
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
case 'End': {
|
|
354
|
+
event.preventDefault();
|
|
355
|
+
const lastValue = values[values.length - 1];
|
|
356
|
+
if (lastValue !== undefined) {
|
|
357
|
+
onSelectedValueChange(lastValue);
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case 'Escape': {
|
|
362
|
+
event.preventDefault();
|
|
363
|
+
if (selectedValue !== undefined) {
|
|
364
|
+
onSelectedValueChange(undefined);
|
|
365
|
+
} else {
|
|
366
|
+
onQueryChange('');
|
|
367
|
+
}
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
[selectedValue, onSelectedValueChange, getItemValues, triggerSelect, onQueryChange],
|
|
373
|
+
);
|
|
69
374
|
|
|
70
375
|
return (
|
|
71
|
-
<
|
|
376
|
+
<input
|
|
72
377
|
{...props}
|
|
73
|
-
|
|
378
|
+
{...(props.autoFocus && !hasIosKeyboard && { autoFocus: true })}
|
|
379
|
+
type='text'
|
|
380
|
+
value={query}
|
|
381
|
+
placeholder={placeholder ?? defaultPlaceholder}
|
|
74
382
|
className={tx(
|
|
75
383
|
'input.input',
|
|
76
384
|
'input',
|
|
@@ -80,65 +388,150 @@ const SearchListInput = forwardRef<HTMLInputElement, SearchListInputProps>(
|
|
|
80
388
|
density,
|
|
81
389
|
elevation,
|
|
82
390
|
},
|
|
83
|
-
'mbe-cardSpacingBlock',
|
|
84
391
|
classNames,
|
|
85
392
|
)}
|
|
86
|
-
{
|
|
393
|
+
onChange={handleChange}
|
|
394
|
+
onKeyDown={handleKeyDown}
|
|
87
395
|
ref={forwardedRef}
|
|
88
396
|
/>
|
|
89
397
|
);
|
|
90
398
|
},
|
|
91
399
|
);
|
|
92
400
|
|
|
401
|
+
SearchListInput.displayName = 'SearchList.Input';
|
|
402
|
+
|
|
93
403
|
//
|
|
94
|
-
//
|
|
404
|
+
// Item
|
|
95
405
|
//
|
|
96
406
|
|
|
97
|
-
type
|
|
407
|
+
type SearchListItemProps = ThemedClassName<{
|
|
408
|
+
/** Unique identifier for the item. */
|
|
409
|
+
value: string;
|
|
410
|
+
/** Display label for the item. */
|
|
411
|
+
label: string;
|
|
412
|
+
/** Icon to display (string identifier for Icon component). */
|
|
413
|
+
icon?: string;
|
|
414
|
+
/** Whether to show a check icon. */
|
|
415
|
+
checked?: boolean;
|
|
416
|
+
/** Suffix text to display after the label. */
|
|
417
|
+
suffix?: string;
|
|
418
|
+
/** Callback when item is selected. */
|
|
419
|
+
onSelect?: () => void;
|
|
420
|
+
/** Whether the item is disabled. */
|
|
421
|
+
disabled?: boolean;
|
|
422
|
+
}>;
|
|
423
|
+
|
|
424
|
+
const SearchListItem = forwardRef<HTMLDivElement, SearchListItemProps>(
|
|
425
|
+
({ classNames, value, label, icon, checked, suffix, onSelect, disabled }, forwardedRef) => {
|
|
426
|
+
const { selectedValue, registerItem, unregisterItem } = useSearchListItemContext('SearchList.Item');
|
|
427
|
+
const internalRef = useRef<HTMLDivElement>(null);
|
|
428
|
+
|
|
429
|
+
const isSelected = selectedValue === value && !disabled;
|
|
430
|
+
|
|
431
|
+
// Register this item.
|
|
432
|
+
useEffect(() => {
|
|
433
|
+
const element = internalRef.current;
|
|
434
|
+
if (element) {
|
|
435
|
+
registerItem(value, element, onSelect, disabled);
|
|
436
|
+
}
|
|
437
|
+
return () => unregisterItem(value);
|
|
438
|
+
}, [value, onSelect, disabled, registerItem, unregisterItem]);
|
|
439
|
+
|
|
440
|
+
// Scroll into view when selected.
|
|
441
|
+
useEffect(() => {
|
|
442
|
+
if (isSelected && internalRef.current) {
|
|
443
|
+
internalRef.current.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
444
|
+
}
|
|
445
|
+
}, [isSelected]);
|
|
446
|
+
|
|
447
|
+
const handleClick = useCallback(() => {
|
|
448
|
+
if (!disabled) {
|
|
449
|
+
onSelect?.();
|
|
450
|
+
}
|
|
451
|
+
}, [onSelect, disabled]);
|
|
98
452
|
|
|
99
|
-
const SearchListContent = forwardRef<HTMLDivElement, SearchListContentProps>(
|
|
100
|
-
({ children, classNames, ...props }, forwardedRef) => {
|
|
101
453
|
return (
|
|
102
|
-
<
|
|
103
|
-
{
|
|
104
|
-
|
|
454
|
+
<div
|
|
455
|
+
ref={(node) => {
|
|
456
|
+
internalRef.current = node;
|
|
457
|
+
if (typeof forwardedRef === 'function') {
|
|
458
|
+
forwardedRef(node);
|
|
459
|
+
} else if (forwardedRef) {
|
|
460
|
+
forwardedRef.current = node;
|
|
461
|
+
}
|
|
462
|
+
}}
|
|
463
|
+
role='option'
|
|
464
|
+
aria-selected={isSelected}
|
|
465
|
+
aria-disabled={disabled}
|
|
466
|
+
data-selected={isSelected}
|
|
467
|
+
data-disabled={disabled}
|
|
468
|
+
data-value={value}
|
|
469
|
+
tabIndex={-1}
|
|
470
|
+
className={mx(
|
|
471
|
+
'flex gap-2 items-center',
|
|
472
|
+
'plb-1 pli-2 rounded-sm select-none cursor-pointer data-[selected=true]:bg-hoverOverlay hover:bg-hoverOverlay',
|
|
473
|
+
disabled && 'opacity-50 cursor-not-allowed hover:bg-transparent data-[selected=true]:bg-transparent',
|
|
474
|
+
classNames,
|
|
475
|
+
)}
|
|
476
|
+
onClick={handleClick}
|
|
477
|
+
>
|
|
478
|
+
{icon && <Icon icon={icon} size={5} />}
|
|
479
|
+
<span className='is-0 grow truncate'>{label}</span>
|
|
480
|
+
{suffix && <span className={mx('shrink-0', descriptionText)}>{suffix}</span>}
|
|
481
|
+
{checked && <Icon icon='ph--check--regular' size={5} />}
|
|
482
|
+
</div>
|
|
105
483
|
);
|
|
106
484
|
},
|
|
107
485
|
);
|
|
108
486
|
|
|
487
|
+
SearchListItem.displayName = 'SearchList.Item';
|
|
488
|
+
|
|
109
489
|
//
|
|
110
490
|
// Empty
|
|
111
491
|
//
|
|
112
492
|
|
|
113
|
-
type SearchListEmptyProps = ThemedClassName<
|
|
493
|
+
type SearchListEmptyProps = ThemedClassName<PropsWithChildren<{}>>;
|
|
114
494
|
|
|
115
|
-
const SearchListEmpty =
|
|
116
|
-
(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
495
|
+
const SearchListEmpty = ({ classNames, children }: SearchListEmptyProps) => {
|
|
496
|
+
return (
|
|
497
|
+
<div role='status' className={mx('flex flex-col is-full pli-2 plb-1', classNames)}>
|
|
498
|
+
{children}
|
|
499
|
+
</div>
|
|
500
|
+
);
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
SearchListEmpty.displayName = 'SearchList.Empty';
|
|
124
504
|
|
|
125
505
|
//
|
|
126
|
-
//
|
|
506
|
+
// Group
|
|
127
507
|
//
|
|
128
508
|
|
|
129
|
-
type
|
|
509
|
+
type SearchListGroupProps = ThemedClassName<
|
|
510
|
+
PropsWithChildren<{
|
|
511
|
+
/** Heading for the group. */
|
|
512
|
+
heading?: ReactNode;
|
|
513
|
+
}>
|
|
514
|
+
>;
|
|
130
515
|
|
|
131
|
-
|
|
132
|
-
|
|
516
|
+
/**
|
|
517
|
+
* Groups related search items with an optional heading.
|
|
518
|
+
*/
|
|
519
|
+
const SearchListGroup = forwardRef<HTMLDivElement, SearchListGroupProps>(
|
|
520
|
+
({ classNames, heading, children }, forwardedRef) => {
|
|
133
521
|
return (
|
|
134
|
-
<
|
|
522
|
+
<div ref={forwardedRef} role='group' className={mx('flex flex-col', classNames)}>
|
|
523
|
+
{heading && (
|
|
524
|
+
<div role='presentation' className='pli-2 plb-1 text-xs font-medium text-description'>
|
|
525
|
+
{heading}
|
|
526
|
+
</div>
|
|
527
|
+
)}
|
|
135
528
|
{children}
|
|
136
|
-
</
|
|
529
|
+
</div>
|
|
137
530
|
);
|
|
138
531
|
},
|
|
139
532
|
);
|
|
140
533
|
|
|
141
|
-
|
|
534
|
+
SearchListGroup.displayName = 'SearchList.Group';
|
|
142
535
|
|
|
143
536
|
//
|
|
144
537
|
// SearchList
|
|
@@ -146,18 +539,20 @@ SearchListItem.displayName = SEARCHLIST_ITEM_NAME;
|
|
|
146
539
|
|
|
147
540
|
export const SearchList = {
|
|
148
541
|
Root: SearchListRoot,
|
|
149
|
-
|
|
542
|
+
Viewport: SearchListViewport,
|
|
150
543
|
Content: SearchListContent,
|
|
151
|
-
|
|
544
|
+
Input: SearchListInput,
|
|
152
545
|
Item: SearchListItem,
|
|
546
|
+
Empty: SearchListEmpty,
|
|
547
|
+
Group: SearchListGroup,
|
|
153
548
|
};
|
|
154
549
|
|
|
155
550
|
export type {
|
|
156
551
|
SearchListRootProps,
|
|
157
|
-
|
|
552
|
+
SearchListViewportProps,
|
|
158
553
|
SearchListContentProps,
|
|
159
|
-
|
|
554
|
+
SearchListInputProps,
|
|
160
555
|
SearchListItemProps,
|
|
556
|
+
SearchListEmptyProps,
|
|
557
|
+
SearchListGroupProps,
|
|
161
558
|
};
|
|
162
|
-
|
|
163
|
-
export { commandItem, searchListItem };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { createContext } from '@radix-ui/react-context';
|
|
6
|
+
|
|
7
|
+
/** Context for items - stable, doesn't change when query changes */
|
|
8
|
+
export type SearchListItemContextValue = {
|
|
9
|
+
/** Currently selected item value for keyboard navigation. */
|
|
10
|
+
selectedValue: string | undefined;
|
|
11
|
+
/** Update the selected value. */
|
|
12
|
+
onSelectedValueChange: (value: string | undefined) => void;
|
|
13
|
+
/** Register an item for keyboard navigation. */
|
|
14
|
+
registerItem: (
|
|
15
|
+
value: string,
|
|
16
|
+
element: HTMLElement | null,
|
|
17
|
+
onSelect: (() => void) | undefined,
|
|
18
|
+
disabled?: boolean,
|
|
19
|
+
) => void;
|
|
20
|
+
/** Unregister an item. */
|
|
21
|
+
unregisterItem: (value: string) => void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Context for input - can change frequently with query */
|
|
25
|
+
export type SearchListInputContextValue = {
|
|
26
|
+
/** Current search query. */
|
|
27
|
+
query: string;
|
|
28
|
+
/** Update the query value. */
|
|
29
|
+
onQueryChange: (query: string) => void;
|
|
30
|
+
/** Currently selected item value for keyboard navigation. */
|
|
31
|
+
selectedValue: string | undefined;
|
|
32
|
+
/** Update the selected value. */
|
|
33
|
+
onSelectedValueChange: (value: string | undefined) => void;
|
|
34
|
+
/** Get ordered list of registered item values (excludes disabled items). */
|
|
35
|
+
getItemValues: () => string[];
|
|
36
|
+
/** Trigger selection of the currently highlighted item. */
|
|
37
|
+
triggerSelect: () => void;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const [SearchListItemContextProvider, useSearchListItemContext] =
|
|
41
|
+
createContext<SearchListItemContextValue>('SearchListItem');
|
|
42
|
+
export const [SearchListInputContextProvider, useSearchListInputContext] =
|
|
43
|
+
createContext<SearchListInputContextValue>('SearchListInput');
|