@vendure-io/ui 3.0.0-next.5 → 3.0.0-next.7
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/package.json +1 -1
- package/src/components/atoms/breadcrumb.tsx +31 -11
- package/src/components/atoms/carousel.tsx +33 -7
- package/src/components/atoms/combobox.tsx +144 -17
- package/src/components/atoms/command.tsx +14 -4
- package/src/components/atoms/pagination.tsx +43 -12
- package/src/components/atoms/sidebar.tsx +24 -5
- package/src/components/atoms/spinner.tsx +7 -2
- package/src/components/molecules/action-needed.tsx +4 -4
- package/src/components/molecules/anonymized-token.tsx +19 -18
- package/src/components/molecules/app-shell.tsx +14 -5
- package/src/components/molecules/checklist/checklist.tsx +47 -17
- package/src/components/molecules/choice-card.tsx +3 -0
- package/src/components/molecules/confirm-dialog.tsx +6 -4
- package/src/components/molecules/data-table/data-table-filters.tsx +25 -7
- package/src/components/molecules/data-table/data-table-types.tsx +33 -16
- package/src/components/molecules/data-table/data-table-view-options.tsx +14 -4
- package/src/components/molecules/data-table/data-table.tsx +47 -26
- package/src/components/molecules/data-table/table-pagination.tsx +51 -29
- package/src/components/molecules/date-picker.tsx +4 -2
- package/src/components/molecules/date-range-picker.tsx +6 -2
- package/src/components/molecules/date-time-picker.tsx +4 -2
- package/src/components/molecules/file-dropzone.tsx +16 -5
- package/src/components/molecules/id-chip.tsx +10 -8
- package/src/components/molecules/issued-secret.tsx +34 -24
- package/src/components/molecules/multi-select.tsx +470 -107
- package/src/components/molecules/secret-value.tsx +26 -18
- package/src/components/molecules/skip-link.tsx +11 -9
- package/src/components/molecules/stat-card.tsx +2 -0
- package/src/components/molecules/state-views/error-state.tsx +18 -6
- package/src/components/molecules/state-views/loading-state.tsx +2 -0
- package/src/components/molecules/status-badge.tsx +5 -2
- package/src/lib/chip-fit.ts +62 -0
- package/src/lib/format-messages.ts +189 -0
- package/src/lib/multi-select-keys.ts +77 -0
|
@@ -1,19 +1,36 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
Combobox,
|
|
5
|
+
ComboboxChip,
|
|
6
|
+
ComboboxChipCount,
|
|
7
|
+
ComboboxChips,
|
|
8
|
+
ComboboxChipsActions,
|
|
9
|
+
ComboboxChipsInput,
|
|
10
|
+
ComboboxCollection,
|
|
11
|
+
ComboboxContent,
|
|
12
|
+
ComboboxEmpty,
|
|
13
|
+
ComboboxGroup,
|
|
14
|
+
ComboboxItem,
|
|
15
|
+
ComboboxLabel,
|
|
16
|
+
ComboboxList,
|
|
17
|
+
} from '@vendure-io/ui/components/atoms/combobox';
|
|
18
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '@vendure-io/ui/components/atoms/tooltip';
|
|
11
19
|
import { SkeletonLine } from '@vendure-io/ui/components/molecules/state-views/skeleton-parts';
|
|
12
20
|
import { cn } from '@vendure-io/ui/lib/utils';
|
|
13
|
-
import { Loader2Icon } from 'lucide-react';
|
|
14
21
|
import * as React from 'react';
|
|
22
|
+
import {
|
|
23
|
+
CHIP_GAP,
|
|
24
|
+
type ChipFit,
|
|
25
|
+
chipBox,
|
|
26
|
+
chipCountBox,
|
|
27
|
+
chipShape,
|
|
28
|
+
fitChips,
|
|
29
|
+
INPUT_RESERVE,
|
|
30
|
+
} from '../../lib/chip-fit.ts';
|
|
15
31
|
import { isDevelopment } from '../../lib/env.ts';
|
|
16
|
-
import { useMessages } from '../../lib/format-messages.ts';
|
|
32
|
+
import { fillMessage, useMessages } from '../../lib/format-messages.ts';
|
|
33
|
+
import { multiSelectKeyAction } from '../../lib/multi-select-keys.ts';
|
|
17
34
|
|
|
18
35
|
type ItemValue = string | number;
|
|
19
36
|
|
|
@@ -30,10 +47,13 @@ function resolveAccessor<T, R>(item: T, accessor: Accessor<T, R>): R {
|
|
|
30
47
|
|
|
31
48
|
export interface MultiSelectProps<T> {
|
|
32
49
|
/**
|
|
33
|
-
* The
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
50
|
+
* The options. `undefined` means the first load has not finished: a picked
|
|
51
|
+
* value shows as a skeleton (its label is not known yet), and the open list
|
|
52
|
+
* shows a loading row, one option tall. `[]` means there are no options.
|
|
53
|
+
*
|
|
54
|
+
* With `onSearchChange`, these are the results of the current search, in
|
|
55
|
+
* the order to show them; otherwise MultiSelect filters them by the typed
|
|
56
|
+
* text.
|
|
37
57
|
*/
|
|
38
58
|
items: readonly T[] | undefined;
|
|
39
59
|
/**
|
|
@@ -42,39 +62,68 @@ export interface MultiSelectProps<T> {
|
|
|
42
62
|
*/
|
|
43
63
|
loadingLabel?: string;
|
|
44
64
|
/**
|
|
45
|
-
* How to render each option's label
|
|
46
|
-
* (`itemToLabel="name"`), or a function for
|
|
47
|
-
*
|
|
65
|
+
* How to render each option's label, in the list and in its chip. Pass a
|
|
66
|
+
* property key for the common case (`itemToLabel="name"`), or a function for
|
|
67
|
+
* full control.
|
|
48
68
|
* @default String(item)
|
|
49
69
|
*/
|
|
50
70
|
itemToLabel?: Accessor<T, React.ReactNode>;
|
|
71
|
+
/**
|
|
72
|
+
* The option's label as text: the local search matches it, and the remove
|
|
73
|
+
* button, the "+N" tooltip and the Backspace message read it.
|
|
74
|
+
* @default the label when it is a string or number, else the value
|
|
75
|
+
*/
|
|
76
|
+
itemToString?: Accessor<T, string>;
|
|
51
77
|
/**
|
|
52
78
|
* How to derive the stable value stored in state for each option. Pass a
|
|
53
79
|
* property key (`itemToValue="id"`) or a function. Required when items are
|
|
54
80
|
* objects; for string/number items it defaults to the item itself.
|
|
55
81
|
*/
|
|
56
82
|
itemToValue?: Accessor<T, ItemValue>;
|
|
57
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* The group an option belongs to. Options show under their group's label,
|
|
85
|
+
* groups in the order they first appear in `items`, and the "+N" tooltip
|
|
86
|
+
* names a hidden value as "group · label".
|
|
87
|
+
*/
|
|
88
|
+
itemToGroup?: Accessor<T, string>;
|
|
89
|
+
/** Picked values. Use for controlled usage. */
|
|
58
90
|
value?: readonly ItemValue[];
|
|
59
|
-
/** Initially
|
|
91
|
+
/** Initially picked values for uncontrolled usage. */
|
|
60
92
|
defaultValue?: readonly ItemValue[];
|
|
61
|
-
/** Called with the next array of
|
|
93
|
+
/** Called with the next array of picked values whenever the selection changes. */
|
|
62
94
|
onValueChange?: (value: ItemValue[]) => void;
|
|
63
|
-
/** Text shown in the trigger when nothing is selected. */
|
|
64
|
-
placeholder?: React.ReactNode;
|
|
65
95
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
96
|
+
* The items of picked values that may not be in `items`, such as the saved
|
|
97
|
+
* values of a server-searched field. MultiSelect also remembers every item it
|
|
98
|
+
* has been given, so a value picked from an earlier search keeps its chip.
|
|
99
|
+
*/
|
|
100
|
+
selectedItems?: readonly T[];
|
|
101
|
+
/**
|
|
102
|
+
* Turns on server search: called with the typed text on every change,
|
|
103
|
+
* including the clear after a pick. Pass the results as `items`, and set
|
|
104
|
+
* `isLoading` while they load. Without it, MultiSelect filters `items`.
|
|
105
|
+
*/
|
|
106
|
+
onSearchChange?: (search: string) => void;
|
|
107
|
+
/**
|
|
108
|
+
* A search is loading. The previous results stay in the list, the chevrons
|
|
109
|
+
* turn into a spinner, and Enter picks nothing until the new results arrive.
|
|
110
|
+
*/
|
|
111
|
+
isLoading?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* The search input's placeholder while nothing is picked.
|
|
114
|
+
* @default `messages.multiSelect.placeholder`, "Select…"
|
|
115
|
+
*/
|
|
116
|
+
placeholder?: string;
|
|
117
|
+
/**
|
|
118
|
+
* The list's text when the search matches no option.
|
|
119
|
+
* @default `messages.multiSelect.empty`, "No results."
|
|
69
120
|
*/
|
|
70
|
-
|
|
121
|
+
emptyLabel?: string;
|
|
71
122
|
/** Disable the entire control. */
|
|
72
123
|
disabled?: boolean;
|
|
73
|
-
/** Render the error state (sets `aria-invalid` on the
|
|
124
|
+
/** Render the error state (sets `aria-invalid` on the input). */
|
|
74
125
|
invalid?: boolean;
|
|
75
|
-
/**
|
|
76
|
-
size?: 'sm' | 'default';
|
|
77
|
-
/** Class applied to the trigger. Defaults to a full-width, non-growing trigger; pass e.g. `w-[260px]` to fix the width. */
|
|
126
|
+
/** Class applied to the field. It is full width by default; pass e.g. `w-[260px]` to fix the width. */
|
|
78
127
|
className?: string;
|
|
79
128
|
/** Class applied to the dropdown content. */
|
|
80
129
|
contentClassName?: string;
|
|
@@ -82,29 +131,111 @@ export interface MultiSelectProps<T> {
|
|
|
82
131
|
name?: string;
|
|
83
132
|
/** Whether a selection is required for form submission. */
|
|
84
133
|
required?: boolean;
|
|
85
|
-
/** id forwarded to the
|
|
134
|
+
/** id forwarded to the search input, for a `<Label htmlFor>`. */
|
|
86
135
|
id?: string;
|
|
136
|
+
/** Accessible name of the search input, when no visible label points at it. */
|
|
137
|
+
'aria-label'?: string;
|
|
87
138
|
}
|
|
88
139
|
|
|
140
|
+
type ItemGroup<T> = { value: string; items: T[] };
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Measures every picked chip in a hidden row, in the box of a ComboboxChip, and
|
|
144
|
+
* returns how many fit on the field's one line before a "+N" chip. Counted
|
|
145
|
+
* before paint on a pick or a remove, and again only when the row's width
|
|
146
|
+
* changes, never on focus or while typing.
|
|
147
|
+
*/
|
|
148
|
+
function useChipFit(keys: readonly ItemValue[]) {
|
|
149
|
+
const rowRef = React.useRef<HTMLDivElement | null>(null);
|
|
150
|
+
const measureRef = React.useRef<HTMLDivElement | null>(null);
|
|
151
|
+
const [fit, setFit] = React.useState<ChipFit>({ count: keys.length });
|
|
152
|
+
|
|
153
|
+
const compute = React.useCallback(() => {
|
|
154
|
+
const row = rowRef.current;
|
|
155
|
+
const measure = measureRef.current;
|
|
156
|
+
if (!row || !measure) return;
|
|
157
|
+
const widths = Array.from(measure.querySelectorAll<HTMLElement>('[data-measure=chip]')).map(
|
|
158
|
+
(chip) => chip.offsetWidth,
|
|
159
|
+
);
|
|
160
|
+
const countWidth = measure.querySelector<HTMLElement>('[data-measure=count]')?.offsetWidth ?? 0;
|
|
161
|
+
const next = fitChips(widths, row.clientWidth - INPUT_RESERVE, countWidth, CHIP_GAP);
|
|
162
|
+
setFit((prev) => (prev.count === next.count && prev.firstMax === next.firstMax ? prev : next));
|
|
163
|
+
}, []);
|
|
164
|
+
|
|
165
|
+
// The key list changes on a pick or a remove; its joined form keeps the
|
|
166
|
+
// effect from running on a re-render with the same values.
|
|
167
|
+
const keyList = keys.join('\u0000');
|
|
168
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: recount when the picked values change.
|
|
169
|
+
React.useLayoutEffect(() => {
|
|
170
|
+
compute();
|
|
171
|
+
}, [compute, keyList]);
|
|
172
|
+
|
|
173
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: keyList re-observes the chips of the current picks.
|
|
174
|
+
React.useEffect(() => {
|
|
175
|
+
const row = rowRef.current;
|
|
176
|
+
const measure = measureRef.current;
|
|
177
|
+
if (!row || !measure || typeof ResizeObserver === 'undefined') return;
|
|
178
|
+
// Counts again when the row's width changes (the input in it is flex-1,
|
|
179
|
+
// so only a resize of the field does that) or the chips' widths change
|
|
180
|
+
// without a pick (a web font that loads after the first count).
|
|
181
|
+
let rowWidth = row.clientWidth;
|
|
182
|
+
const chipsWidth = () =>
|
|
183
|
+
Array.from(measure.children).reduce(
|
|
184
|
+
(sum, chip) => sum + (chip as HTMLElement).offsetWidth,
|
|
185
|
+
0,
|
|
186
|
+
);
|
|
187
|
+
let chips = chipsWidth();
|
|
188
|
+
const observer = new ResizeObserver(() => {
|
|
189
|
+
if (row.clientWidth === rowWidth && chipsWidth() === chips) return;
|
|
190
|
+
rowWidth = row.clientWidth;
|
|
191
|
+
chips = chipsWidth();
|
|
192
|
+
compute();
|
|
193
|
+
});
|
|
194
|
+
observer.observe(row);
|
|
195
|
+
for (const chip of Array.from(measure.children)) observer.observe(chip);
|
|
196
|
+
// A change between the count before paint and now.
|
|
197
|
+
compute();
|
|
198
|
+
return () => observer.disconnect();
|
|
199
|
+
}, [compute, keyList]);
|
|
200
|
+
|
|
201
|
+
return { rowRef, measureRef, fit };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* A searchable multi-select: the picked values are chips on one line, the
|
|
206
|
+
* ones that do not fit go behind a "+N" chip with a tooltip, and the list
|
|
207
|
+
* stays open while the user picks.
|
|
208
|
+
*/
|
|
89
209
|
function MultiSelect<T>({
|
|
90
210
|
items,
|
|
91
211
|
loadingLabel,
|
|
92
212
|
itemToLabel = String as unknown as Accessor<T, React.ReactNode>,
|
|
213
|
+
itemToString,
|
|
93
214
|
itemToValue,
|
|
94
|
-
|
|
215
|
+
itemToGroup,
|
|
216
|
+
value: valueProp,
|
|
95
217
|
defaultValue,
|
|
96
218
|
onValueChange,
|
|
97
|
-
|
|
98
|
-
|
|
219
|
+
selectedItems,
|
|
220
|
+
onSearchChange,
|
|
221
|
+
isLoading = false,
|
|
222
|
+
placeholder: placeholderProp,
|
|
223
|
+
emptyLabel,
|
|
99
224
|
disabled,
|
|
100
225
|
invalid,
|
|
101
|
-
size = 'default',
|
|
102
226
|
className,
|
|
103
227
|
contentClassName,
|
|
104
228
|
name,
|
|
105
229
|
required,
|
|
106
230
|
id,
|
|
231
|
+
'aria-label': ariaLabel,
|
|
107
232
|
}: MultiSelectProps<T>) {
|
|
233
|
+
const t = useMessages('multiSelect', {
|
|
234
|
+
loading: loadingLabel,
|
|
235
|
+
placeholder: placeholderProp,
|
|
236
|
+
empty: emptyLabel,
|
|
237
|
+
});
|
|
238
|
+
|
|
108
239
|
const getValue = React.useCallback(
|
|
109
240
|
(item: T): ItemValue =>
|
|
110
241
|
itemToValue == null ? (item as unknown as ItemValue) : resolveAccessor(item, itemToValue),
|
|
@@ -114,12 +245,28 @@ function MultiSelect<T>({
|
|
|
114
245
|
(item: T): React.ReactNode => resolveAccessor(item, itemToLabel),
|
|
115
246
|
[itemToLabel],
|
|
116
247
|
);
|
|
248
|
+
const getString = React.useCallback(
|
|
249
|
+
(item: T): string => {
|
|
250
|
+
if (itemToString != null) return resolveAccessor(item, itemToString);
|
|
251
|
+
const label = resolveAccessor(item, itemToLabel);
|
|
252
|
+
return typeof label === 'string' || typeof label === 'number'
|
|
253
|
+
? String(label)
|
|
254
|
+
: String(getValue(item));
|
|
255
|
+
},
|
|
256
|
+
[itemToString, itemToLabel, getValue],
|
|
257
|
+
);
|
|
258
|
+
const getGroup = React.useCallback(
|
|
259
|
+
(item: T): string | undefined =>
|
|
260
|
+
itemToGroup == null ? undefined : resolveAccessor(item, itemToGroup),
|
|
261
|
+
[itemToGroup],
|
|
262
|
+
);
|
|
117
263
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const
|
|
264
|
+
// Every item seen so far, by value, so a picked value keeps its chip when a
|
|
265
|
+
// later search no longer returns it.
|
|
266
|
+
const known = React.useRef(new Map<ItemValue, T>());
|
|
267
|
+
React.useMemo(() => {
|
|
268
|
+
for (const item of selectedItems ?? []) known.current.set(getValue(item), item);
|
|
269
|
+
const seen = new Set<ItemValue>();
|
|
123
270
|
for (const item of items ?? []) {
|
|
124
271
|
const itemValue = getValue(item);
|
|
125
272
|
if (isDevelopment) {
|
|
@@ -129,90 +276,306 @@ function MultiSelect<T>({
|
|
|
129
276
|
'objects, pass `itemToValue` (e.g. itemToValue="id") so the selection survives the ' +
|
|
130
277
|
'items array being recreated.',
|
|
131
278
|
);
|
|
132
|
-
} else if (
|
|
279
|
+
} else if (seen.has(itemValue)) {
|
|
133
280
|
console.warn(
|
|
134
281
|
`MultiSelect: duplicate item value ${JSON.stringify(itemValue)} — each item must ` +
|
|
135
282
|
'resolve to a unique `itemToValue`; later items override earlier ones.',
|
|
136
283
|
);
|
|
137
284
|
}
|
|
138
285
|
}
|
|
139
|
-
|
|
286
|
+
seen.add(itemValue);
|
|
287
|
+
known.current.set(itemValue, item);
|
|
140
288
|
}
|
|
141
|
-
|
|
142
|
-
|
|
289
|
+
}, [items, selectedItems, getValue]);
|
|
290
|
+
|
|
291
|
+
const [uncontrolled, setUncontrolled] = React.useState<readonly ItemValue[]>(defaultValue ?? []);
|
|
292
|
+
const ids = valueProp ?? uncontrolled;
|
|
293
|
+
const setIds = (next: ItemValue[]) => {
|
|
294
|
+
if (valueProp === undefined) setUncontrolled(next);
|
|
295
|
+
onValueChange?.(next);
|
|
296
|
+
};
|
|
297
|
+
const picked = ids
|
|
298
|
+
.map((itemValue) => known.current.get(itemValue))
|
|
299
|
+
.filter((item): item is T => item !== undefined);
|
|
300
|
+
// The first load has not finished and a picked value has no label yet.
|
|
301
|
+
const labelsPending = items === undefined && picked.length < ids.length;
|
|
302
|
+
|
|
303
|
+
const [open, setOpen] = React.useState(false);
|
|
304
|
+
const [query, setQuery] = React.useState('');
|
|
305
|
+
// The index of the value the next Backspace removes.
|
|
306
|
+
const [armed, setArmed] = React.useState<number | null>(null);
|
|
307
|
+
const [tipOpen, setTipOpen] = React.useState(false);
|
|
308
|
+
// Base UI highlights a row on open (the first picked value) and after a
|
|
309
|
+
// pick. With an empty search that highlight stays hidden, and Enter picks
|
|
310
|
+
// nothing, until the user moves it with an arrow key or the pointer.
|
|
311
|
+
const [hasHighlight, setHasHighlight] = React.useState(false);
|
|
312
|
+
const [navigated, setNavigated] = React.useState(false);
|
|
313
|
+
const quiet = query === '' && !navigated;
|
|
314
|
+
// The search changed and its results have not arrived. Set on a change of
|
|
315
|
+
// the typed text, cleared when new `items` arrive or `isLoading` turns off.
|
|
316
|
+
const [stale, setStale] = React.useState(false);
|
|
317
|
+
const previousItems = React.useRef(items);
|
|
318
|
+
const wasLoading = React.useRef(isLoading);
|
|
319
|
+
if (previousItems.current !== items || (wasLoading.current && !isLoading)) {
|
|
320
|
+
previousItems.current = items;
|
|
321
|
+
if (stale) setStale(false);
|
|
322
|
+
}
|
|
323
|
+
wasLoading.current = isLoading;
|
|
324
|
+
const pending = stale || isLoading;
|
|
325
|
+
// While the rows belong to the previous search, Enter picks nothing, so no
|
|
326
|
+
// row shows as highlighted either.
|
|
327
|
+
const highlightHidden = quiet || pending;
|
|
328
|
+
|
|
329
|
+
// The input needs its own id: without one, Base UI gives the input and the
|
|
330
|
+
// open button the same id.
|
|
331
|
+
const fallbackId = React.useId();
|
|
332
|
+
const anchor = React.useRef<HTMLDivElement | null>(null);
|
|
333
|
+
const inputRef = React.useRef<HTMLInputElement | null>(null);
|
|
334
|
+
// Counted from the chips on screen: a value whose label is still loading
|
|
335
|
+
// has no chip yet, so its arrival counts again.
|
|
336
|
+
const pickedKeys = React.useMemo(() => picked.map(getValue), [picked, getValue]);
|
|
337
|
+
const { rowRef, measureRef, fit } = useChipFit(pickedKeys);
|
|
338
|
+
|
|
339
|
+
// While the list is open, Base UI hides everything but the input and the
|
|
340
|
+
// list from assistive technology, the field's <label> included. A hidden
|
|
341
|
+
// label no longer names the input, so the input points at it by id too.
|
|
342
|
+
const [labelledBy, setLabelledBy] = React.useState<string | undefined>();
|
|
343
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: a new id points another label at the input.
|
|
344
|
+
React.useLayoutEffect(() => {
|
|
345
|
+
const input = inputRef.current;
|
|
346
|
+
if (!input || ariaLabel) return;
|
|
347
|
+
const labelIds = Array.from(input.labels ?? []).map((label, index) => {
|
|
348
|
+
if (!label.id) label.id = `${input.id}-label${index > 0 ? `-${index}` : ''}`;
|
|
349
|
+
return label.id;
|
|
350
|
+
});
|
|
351
|
+
setLabelledBy(labelIds.length > 0 ? labelIds.join(' ') : undefined);
|
|
352
|
+
}, [ariaLabel, id]);
|
|
353
|
+
const shown = picked.slice(0, fit.count);
|
|
354
|
+
const hidden = picked.slice(fit.count);
|
|
355
|
+
const armedItem = armed === null ? undefined : picked[armed];
|
|
356
|
+
const countArmed = armed !== null && armed >= shown.length;
|
|
357
|
+
|
|
358
|
+
const results = React.useMemo(() => {
|
|
359
|
+
if (items === undefined) return [];
|
|
360
|
+
if (onSearchChange) return [...items];
|
|
361
|
+
const search = query.trim().toLowerCase();
|
|
362
|
+
if (!search) return [...items];
|
|
363
|
+
return items.filter(
|
|
364
|
+
(item) =>
|
|
365
|
+
getString(item).toLowerCase().includes(search) ||
|
|
366
|
+
(getGroup(item) ?? '').toLowerCase().includes(search),
|
|
367
|
+
);
|
|
368
|
+
}, [items, onSearchChange, query, getString, getGroup]);
|
|
369
|
+
|
|
370
|
+
// Each option shows one time, in its group, in a fixed order: a pick
|
|
371
|
+
// changes its check, never its place.
|
|
372
|
+
const groups = React.useMemo((): ItemGroup<T>[] | undefined => {
|
|
373
|
+
if (itemToGroup == null) return undefined;
|
|
374
|
+
const byGroup = new Map<string, T[]>();
|
|
375
|
+
for (const item of results) {
|
|
376
|
+
const group = getGroup(item) ?? '';
|
|
377
|
+
const list = byGroup.get(group);
|
|
378
|
+
if (list) list.push(item);
|
|
379
|
+
else byGroup.set(group, [item]);
|
|
380
|
+
}
|
|
381
|
+
return [...byGroup].map(([group, groupItems]) => ({ value: group, items: groupItems }));
|
|
382
|
+
}, [itemToGroup, results, getGroup]);
|
|
383
|
+
|
|
384
|
+
const hiddenLabel = (item: T) => {
|
|
385
|
+
const group = getGroup(item);
|
|
386
|
+
return group ? `${group} · ${getString(item)}` : getString(item);
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const renderItem = (item: T) => (
|
|
390
|
+
<ComboboxItem key={getValue(item)} value={item}>
|
|
391
|
+
{getLabel(item)}
|
|
392
|
+
</ComboboxItem>
|
|
393
|
+
);
|
|
143
394
|
|
|
144
395
|
return (
|
|
145
|
-
<
|
|
396
|
+
<Combobox<T, true>
|
|
146
397
|
multiple
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
398
|
+
items={(groups ?? results) as T[]}
|
|
399
|
+
filter={null}
|
|
400
|
+
value={picked}
|
|
401
|
+
onValueChange={(next) => {
|
|
402
|
+
setIds(next.map(getValue));
|
|
403
|
+
setArmed(null);
|
|
404
|
+
}}
|
|
405
|
+
inputValue={query}
|
|
406
|
+
onInputValueChange={(next, details) => {
|
|
407
|
+
setArmed(null);
|
|
408
|
+
if (onSearchChange) {
|
|
409
|
+
setStale(true);
|
|
410
|
+
onSearchChange(next);
|
|
411
|
+
}
|
|
412
|
+
// Base UI clears the search after a pick ("input-clear"), so the
|
|
413
|
+
// next value can be typed at once. The search is empty again.
|
|
414
|
+
setQuery(next);
|
|
415
|
+
if (details.reason === 'input-clear' || next === '') setNavigated(false);
|
|
416
|
+
}}
|
|
417
|
+
open={open}
|
|
418
|
+
onOpenChange={(next, details) => {
|
|
419
|
+
// A pick keeps the list open, so several values go in one pass.
|
|
420
|
+
if (!next && details.reason === 'item-press') return;
|
|
421
|
+
if (next) setNavigated(false);
|
|
422
|
+
setOpen(next);
|
|
423
|
+
}}
|
|
424
|
+
onItemHighlighted={(item, details) => {
|
|
425
|
+
setHasHighlight(item !== undefined);
|
|
426
|
+
if (item !== undefined && details.reason !== 'none') setNavigated(true);
|
|
427
|
+
}}
|
|
428
|
+
// Typing highlights the first result, so Enter alone picks it.
|
|
429
|
+
autoHighlight
|
|
430
|
+
isItemEqualToValue={(a, b) => getValue(a) === getValue(b)}
|
|
431
|
+
itemToStringLabel={getString}
|
|
432
|
+
itemToStringValue={(item) => String(getValue(item))}
|
|
433
|
+
isLoading={items === undefined}
|
|
434
|
+
loadingLabel={t.loading}
|
|
150
435
|
disabled={disabled}
|
|
151
436
|
name={name}
|
|
152
437
|
required={required}
|
|
153
438
|
>
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
size={size}
|
|
159
|
-
className={cn('w-full', className)}
|
|
160
|
-
aria-invalid={invalid || undefined}
|
|
161
|
-
aria-busy={isLoading || undefined}
|
|
439
|
+
<ComboboxChips
|
|
440
|
+
ref={anchor}
|
|
441
|
+
data-slot="multi-select"
|
|
442
|
+
className={cn('relative w-full flex-nowrap items-start', className)}
|
|
162
443
|
>
|
|
163
|
-
<
|
|
164
|
-
{
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
444
|
+
<div
|
|
445
|
+
ref={measureRef}
|
|
446
|
+
aria-hidden="true"
|
|
447
|
+
className="pointer-events-none invisible absolute start-0 top-0 flex h-0 overflow-hidden"
|
|
448
|
+
>
|
|
449
|
+
{picked.map((item) => (
|
|
450
|
+
<span key={getValue(item)} data-measure="chip" className={cn(chipShape, chipBox)}>
|
|
451
|
+
<span>{getLabel(item)}</span>
|
|
452
|
+
<span className="size-4" />
|
|
453
|
+
</span>
|
|
454
|
+
))}
|
|
455
|
+
<span data-measure="count" className={cn(chipShape, chipCountBox)}>
|
|
456
|
+
+{Math.max(picked.length, 10)}
|
|
457
|
+
</span>
|
|
458
|
+
</div>
|
|
459
|
+
<div
|
|
460
|
+
ref={rowRef}
|
|
461
|
+
className="flex min-w-0 flex-1 flex-nowrap items-center gap-1.5 overflow-hidden"
|
|
462
|
+
>
|
|
463
|
+
{labelsPending ? (
|
|
464
|
+
<span data-slot="multi-select-value-skeleton" className="flex h-6 items-center ps-1.25">
|
|
465
|
+
<SkeletonLine className="w-24" />
|
|
466
|
+
<span className="sr-only">{t.loading}</span>
|
|
467
|
+
</span>
|
|
468
|
+
) : (
|
|
469
|
+
shown.map((item, index) => (
|
|
470
|
+
<ComboboxChip
|
|
471
|
+
key={getValue(item)}
|
|
472
|
+
removeLabel={fillMessage(t.remove, { label: getString(item) })}
|
|
473
|
+
data-armed={armed === index || undefined}
|
|
474
|
+
style={index === 0 && fit.firstMax ? { maxWidth: fit.firstMax } : undefined}
|
|
475
|
+
>
|
|
185
476
|
{getLabel(item)}
|
|
186
|
-
</
|
|
187
|
-
))
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
477
|
+
</ComboboxChip>
|
|
478
|
+
))
|
|
479
|
+
)}
|
|
480
|
+
{!labelsPending && hidden.length > 0 && (
|
|
481
|
+
<Tooltip open={tipOpen || countArmed} onOpenChange={setTipOpen}>
|
|
482
|
+
<TooltipTrigger
|
|
483
|
+
render={
|
|
484
|
+
<ComboboxChipCount
|
|
485
|
+
count={hidden.length}
|
|
486
|
+
data-armed={countArmed || undefined}
|
|
487
|
+
disabled={disabled}
|
|
488
|
+
// Base UI hides the field but the input from assistive
|
|
489
|
+
// technology while the list is open, so the count
|
|
490
|
+
// leaves the tab order too.
|
|
491
|
+
tabIndex={open ? -1 : undefined}
|
|
492
|
+
onClick={() => {
|
|
493
|
+
inputRef.current?.focus();
|
|
494
|
+
setOpen(true);
|
|
495
|
+
}}
|
|
496
|
+
/>
|
|
497
|
+
}
|
|
498
|
+
/>
|
|
499
|
+
<TooltipContent>
|
|
500
|
+
<ul data-slot="multi-select-hidden-values">
|
|
501
|
+
{hidden.map((item) => (
|
|
502
|
+
<li key={getValue(item)}>{hiddenLabel(item)}</li>
|
|
503
|
+
))}
|
|
504
|
+
</ul>
|
|
505
|
+
</TooltipContent>
|
|
506
|
+
</Tooltip>
|
|
507
|
+
)}
|
|
508
|
+
<ComboboxChipsInput
|
|
509
|
+
ref={inputRef}
|
|
510
|
+
id={id ?? fallbackId}
|
|
511
|
+
aria-label={ariaLabel}
|
|
512
|
+
aria-labelledby={labelledBy}
|
|
513
|
+
aria-invalid={invalid || undefined}
|
|
514
|
+
aria-busy={labelsPending || undefined}
|
|
515
|
+
placeholder={ids.length === 0 ? t.placeholder : undefined}
|
|
516
|
+
className="min-w-6"
|
|
517
|
+
// While the highlight is hidden, the input does not point at the
|
|
518
|
+
// row either, so a screen reader does not read it as active.
|
|
519
|
+
render={(props) => (
|
|
520
|
+
<input
|
|
521
|
+
{...props}
|
|
522
|
+
aria-activedescendant={highlightHidden ? undefined : props['aria-activedescendant']}
|
|
523
|
+
/>
|
|
524
|
+
)}
|
|
525
|
+
onClick={() => setArmed(null)}
|
|
526
|
+
onKeyDown={(event) => {
|
|
527
|
+
const action = multiSelectKeyAction({
|
|
528
|
+
key: event.key,
|
|
529
|
+
inputEmpty: event.currentTarget.value === '',
|
|
530
|
+
open,
|
|
531
|
+
quiet,
|
|
532
|
+
hasHighlight,
|
|
533
|
+
stale: pending,
|
|
534
|
+
valueCount: ids.length,
|
|
535
|
+
armed,
|
|
536
|
+
});
|
|
537
|
+
if (action.type === 'pass') return;
|
|
538
|
+
if (action.type === 'disarm') {
|
|
539
|
+
setArmed(null);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
event.preventBaseUIHandler();
|
|
543
|
+
if (action.type !== 'escape') event.preventDefault();
|
|
544
|
+
if (action.type === 'reveal') setNavigated(true);
|
|
545
|
+
else if (action.type === 'arm') setArmed(action.index);
|
|
546
|
+
else if (action.type === 'remove') {
|
|
547
|
+
setIds(ids.filter((_, index) => index !== action.index));
|
|
548
|
+
setArmed(null);
|
|
549
|
+
} else if (action.type === 'escape') setArmed(null);
|
|
550
|
+
}}
|
|
551
|
+
/>
|
|
552
|
+
</div>
|
|
553
|
+
<ComboboxChipsActions
|
|
554
|
+
showClear={ids.length > 0}
|
|
555
|
+
isLoading={isLoading && items !== undefined}
|
|
556
|
+
disabled={disabled}
|
|
557
|
+
/>
|
|
558
|
+
<span aria-live="polite" className="sr-only">
|
|
559
|
+
{armedItem ? fillMessage(t.armed, { label: getString(armedItem) }) : ''}
|
|
560
|
+
</span>
|
|
561
|
+
</ComboboxChips>
|
|
562
|
+
<ComboboxContent anchor={anchor} className={contentClassName}>
|
|
563
|
+
<ComboboxEmpty>{t.empty}</ComboboxEmpty>
|
|
564
|
+
<ComboboxList
|
|
565
|
+
data-quiet={highlightHidden || undefined}
|
|
566
|
+
className="data-quiet:[&_[data-highlighted]]:bg-transparent! data-quiet:[&_[data-highlighted]]:outline-none!"
|
|
567
|
+
>
|
|
568
|
+
{groups
|
|
569
|
+
? (group: ItemGroup<T>) => (
|
|
570
|
+
<ComboboxGroup key={group.value} items={group.items}>
|
|
571
|
+
{group.value ? <ComboboxLabel>{group.value}</ComboboxLabel> : null}
|
|
572
|
+
<ComboboxCollection>{renderItem}</ComboboxCollection>
|
|
573
|
+
</ComboboxGroup>
|
|
574
|
+
)
|
|
575
|
+
: renderItem}
|
|
576
|
+
</ComboboxList>
|
|
577
|
+
</ComboboxContent>
|
|
578
|
+
</Combobox>
|
|
216
579
|
);
|
|
217
580
|
}
|
|
218
581
|
|