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