@marimo-team/islands 0.23.10-dev26 → 0.23.10-dev28
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/{code-visibility-Rcdlclvw.js → code-visibility-Dy2BhYTf.js} +1334 -1044
- package/dist/main.js +7143 -7119
- package/dist/{reveal-component-xsFYQVKT.js → reveal-component-B5af53Y1.js} +16 -16
- package/package.json +1 -1
- package/src/components/data-table/filter-by-values-picker.tsx +39 -17
- package/src/components/data-table/filter-pills.tsx +1 -1
- package/src/components/ui/combobox.tsx +51 -32
- package/src/components/ui/select-core/__tests__/use-select-list.test.ts +294 -0
- package/src/components/ui/select-core/__tests__/utils.test.ts +222 -0
- package/src/components/ui/select-core/index.ts +16 -0
- package/src/components/ui/select-core/option-row.tsx +33 -0
- package/src/components/ui/select-core/render-slot.ts +20 -0
- package/src/components/ui/select-core/select-list.tsx +248 -0
- package/src/components/ui/select-core/types.ts +44 -0
- package/src/components/ui/select-core/use-select-list.ts +347 -0
- package/src/components/ui/select-core/utils.ts +121 -0
- package/src/plugins/impl/MultiselectPlugin.tsx +19 -142
- package/src/plugins/impl/SearchableSelect.tsx +16 -97
- package/src/plugins/impl/__tests__/DropdownPlugin.test.tsx +5 -2
- package/src/plugins/impl/__tests__/MultiSelectPlugin.test.ts +1 -1
- package/src/plugins/impl/multiselectFilterFn.tsx +0 -22
- /package/src/components/{data-table → ui}/value-chips.tsx +0 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import { useMemo, useState } from "react";
|
|
3
|
+
import { assertNever } from "@/utils/assertNever";
|
|
4
|
+
import type { BulkAction, Option } from "./types";
|
|
5
|
+
import {
|
|
6
|
+
deselectMatching,
|
|
7
|
+
getBulkActions,
|
|
8
|
+
getVisibleOptions,
|
|
9
|
+
multiselectFilterFn,
|
|
10
|
+
selectMatching,
|
|
11
|
+
} from "./utils";
|
|
12
|
+
|
|
13
|
+
/** cmdk-style relevance score for `(label, query)`; any positive score matches. */
|
|
14
|
+
type FilterFn = (label: string, query: string) => number;
|
|
15
|
+
|
|
16
|
+
interface UseSelectListParams<V> {
|
|
17
|
+
options: Array<Option<V>>;
|
|
18
|
+
/** Current selection: an array when `multiple`, otherwise a single value or null. */
|
|
19
|
+
value: V[] | V | null;
|
|
20
|
+
onChange: (next: V[] | V | null) => void;
|
|
21
|
+
/** Multi-select when true; single-select (replace-on-pick) when false. */
|
|
22
|
+
multiple: boolean;
|
|
23
|
+
/** Cap on multi-select size. At the cap, picking another drops the oldest. */
|
|
24
|
+
maxSelections?: number;
|
|
25
|
+
/** Single-select only: re-picking the current value clears it to null. */
|
|
26
|
+
allowSelectNone?: boolean;
|
|
27
|
+
/** Match predicate over `(label, query)`; defaults to the strict word match. */
|
|
28
|
+
filterFn?: FilterFn;
|
|
29
|
+
/** Float the (frozen) selection to the top of the idle menu. */
|
|
30
|
+
pinSelected?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface UseSelectListResult<V> {
|
|
34
|
+
searchQuery: string;
|
|
35
|
+
setSearchQuery: (query: string) => void;
|
|
36
|
+
open: boolean;
|
|
37
|
+
setOpen: (open: boolean) => void;
|
|
38
|
+
/** Filtered, and (when `pinSelected` and idle) selected-first ordered. */
|
|
39
|
+
visibleOptions: Array<Option<V>>;
|
|
40
|
+
/** Count of pinned options at the head of `visibleOptions` (0 unless pinned + idle). */
|
|
41
|
+
pinnedCount: number;
|
|
42
|
+
isChecked: (value: V) => boolean;
|
|
43
|
+
toggle: (value: V) => void;
|
|
44
|
+
/**
|
|
45
|
+
* Renderable bulk rows for the current search/cap state, in display order.
|
|
46
|
+
* Empty for single-select. Each entry carries its data (`enabled` or `items`)
|
|
47
|
+
* and a `run` closure that applies it.
|
|
48
|
+
*/
|
|
49
|
+
bulkActions: Array<BulkAction<V>>;
|
|
50
|
+
/** Map a value back to its option label; falls back to `String(value)`. */
|
|
51
|
+
labelOf: (value: V) => string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function asArray<V>(value: V[] | V | null): V[] {
|
|
55
|
+
if (value == null) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
return Array.isArray(value) ? value : [value];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface SearchParams<V> {
|
|
62
|
+
options: Array<Option<V>>;
|
|
63
|
+
filterFn: FilterFn;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Search query and the options that currently match it. */
|
|
67
|
+
function useSearch<V>({ options, filterFn }: SearchParams<V>): {
|
|
68
|
+
searchQuery: string;
|
|
69
|
+
setSearchQuery: (query: string) => void;
|
|
70
|
+
filteredOptions: Array<Option<V>>;
|
|
71
|
+
} {
|
|
72
|
+
const [searchQuery, setSearchQuery] = useState("");
|
|
73
|
+
const filteredOptions = useMemo(() => {
|
|
74
|
+
if (!searchQuery) {
|
|
75
|
+
return options;
|
|
76
|
+
}
|
|
77
|
+
return options.filter((o) => filterFn(o.label, searchQuery) > 0);
|
|
78
|
+
}, [options, searchQuery, filterFn]);
|
|
79
|
+
return { searchQuery, setSearchQuery, filteredOptions };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface PinningParams<V> {
|
|
83
|
+
value: V[] | V | null;
|
|
84
|
+
pinSelected: boolean;
|
|
85
|
+
options: Array<Option<V>>;
|
|
86
|
+
searchQuery: string;
|
|
87
|
+
filteredOptions: Array<Option<V>>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Open state plus the frozen selection snapshot that orders the idle menu. The
|
|
92
|
+
* snapshot is retaken (`repin`) when the menu opens or the search clears, so a row
|
|
93
|
+
* toggled mid-session keeps its place instead of jumping to the top under the cursor.
|
|
94
|
+
*/
|
|
95
|
+
function usePinning<V>({
|
|
96
|
+
value,
|
|
97
|
+
pinSelected,
|
|
98
|
+
options,
|
|
99
|
+
searchQuery,
|
|
100
|
+
filteredOptions,
|
|
101
|
+
}: PinningParams<V>): {
|
|
102
|
+
open: boolean;
|
|
103
|
+
setOpen: (open: boolean) => void;
|
|
104
|
+
repin: () => void;
|
|
105
|
+
visibleOptions: Array<Option<V>>;
|
|
106
|
+
pinnedCount: number;
|
|
107
|
+
} {
|
|
108
|
+
const [open, setOpenState] = useState(false);
|
|
109
|
+
const [pinnedSelection, setPinnedSelection] = useState<Set<V>>(
|
|
110
|
+
() => new Set(asArray(value)),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const repin = (): void => setPinnedSelection(new Set(asArray(value)));
|
|
114
|
+
|
|
115
|
+
const setOpen = (nextOpen: boolean): void => {
|
|
116
|
+
setOpenState(nextOpen);
|
|
117
|
+
if (nextOpen) {
|
|
118
|
+
repin();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const { visibleOptions, pinnedCount } = useMemo(() => {
|
|
123
|
+
if (searchQuery || !pinSelected) {
|
|
124
|
+
return { visibleOptions: filteredOptions, pinnedCount: 0 };
|
|
125
|
+
}
|
|
126
|
+
return getVisibleOptions(options, pinnedSelection);
|
|
127
|
+
}, [searchQuery, pinSelected, filteredOptions, options, pinnedSelection]);
|
|
128
|
+
|
|
129
|
+
return { open, setOpen, repin, visibleOptions, pinnedCount };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
interface ToggleParams<V> {
|
|
133
|
+
value: V[] | V | null;
|
|
134
|
+
onChange: (next: V[] | V | null) => void;
|
|
135
|
+
multiple: boolean;
|
|
136
|
+
maxSelections: number | undefined;
|
|
137
|
+
allowSelectNone: boolean | undefined;
|
|
138
|
+
selected: ReadonlySet<V>;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Membership test and the cap/cardinality-aware single-item toggle. */
|
|
142
|
+
function useToggle<V>({
|
|
143
|
+
value,
|
|
144
|
+
onChange,
|
|
145
|
+
multiple,
|
|
146
|
+
maxSelections,
|
|
147
|
+
allowSelectNone,
|
|
148
|
+
selected,
|
|
149
|
+
}: ToggleParams<V>): {
|
|
150
|
+
isChecked: (value: V) => boolean;
|
|
151
|
+
toggle: (value: V) => void;
|
|
152
|
+
} {
|
|
153
|
+
const isChecked = (candidate: V): boolean => selected.has(candidate);
|
|
154
|
+
|
|
155
|
+
const toggle = (candidate: V): void => {
|
|
156
|
+
if (!multiple) {
|
|
157
|
+
if (allowSelectNone && value === candidate) {
|
|
158
|
+
onChange(null);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
onChange(candidate);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const current = asArray(value);
|
|
166
|
+
if (selected.has(candidate)) {
|
|
167
|
+
onChange(current.filter((v) => v !== candidate));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let next = [...current, candidate];
|
|
172
|
+
if (maxSelections != null && next.length > maxSelections) {
|
|
173
|
+
next = next.slice(-maxSelections);
|
|
174
|
+
}
|
|
175
|
+
onChange(next);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
return { isChecked, toggle };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface BulkParams<V> {
|
|
182
|
+
value: V[] | V | null;
|
|
183
|
+
onChange: (next: V[] | V | null) => void;
|
|
184
|
+
multiple: boolean;
|
|
185
|
+
options: Array<Option<V>>;
|
|
186
|
+
filteredOptions: Array<Option<V>>;
|
|
187
|
+
searchQuery: string;
|
|
188
|
+
maxSelections: number | undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Bulk-row specs paired with run closures; inert for single-select. */
|
|
192
|
+
function useBulk<V>({
|
|
193
|
+
value,
|
|
194
|
+
onChange,
|
|
195
|
+
multiple,
|
|
196
|
+
options,
|
|
197
|
+
filteredOptions,
|
|
198
|
+
searchQuery,
|
|
199
|
+
maxSelections,
|
|
200
|
+
}: BulkParams<V>): { bulkActions: Array<BulkAction<V>> } {
|
|
201
|
+
const bulkActions = useMemo<Array<BulkAction<V>>>(() => {
|
|
202
|
+
if (!multiple) {
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
const specs = getBulkActions({
|
|
206
|
+
options,
|
|
207
|
+
filteredOptions,
|
|
208
|
+
value: asArray(value),
|
|
209
|
+
searchQuery,
|
|
210
|
+
maxSelections,
|
|
211
|
+
});
|
|
212
|
+
return specs.map((spec): BulkAction<V> => {
|
|
213
|
+
switch (spec.kind) {
|
|
214
|
+
case "select-all":
|
|
215
|
+
return {
|
|
216
|
+
...spec,
|
|
217
|
+
run: () =>
|
|
218
|
+
onChange(
|
|
219
|
+
selectMatching(
|
|
220
|
+
asArray(value),
|
|
221
|
+
options.filter((o) => !o.disabled).map((o) => o.value),
|
|
222
|
+
),
|
|
223
|
+
),
|
|
224
|
+
};
|
|
225
|
+
case "deselect-all":
|
|
226
|
+
return { ...spec, run: () => onChange([]) };
|
|
227
|
+
case "select-matching":
|
|
228
|
+
return {
|
|
229
|
+
...spec,
|
|
230
|
+
run: () =>
|
|
231
|
+
onChange(
|
|
232
|
+
selectMatching(
|
|
233
|
+
asArray(value),
|
|
234
|
+
spec.items.map((o) => o.value),
|
|
235
|
+
),
|
|
236
|
+
),
|
|
237
|
+
};
|
|
238
|
+
case "deselect-matching":
|
|
239
|
+
return {
|
|
240
|
+
...spec,
|
|
241
|
+
run: () =>
|
|
242
|
+
onChange(
|
|
243
|
+
deselectMatching(
|
|
244
|
+
asArray(value),
|
|
245
|
+
spec.items.map((o) => o.value),
|
|
246
|
+
),
|
|
247
|
+
),
|
|
248
|
+
};
|
|
249
|
+
default:
|
|
250
|
+
return assertNever(spec);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}, [
|
|
254
|
+
multiple,
|
|
255
|
+
options,
|
|
256
|
+
filteredOptions,
|
|
257
|
+
value,
|
|
258
|
+
searchQuery,
|
|
259
|
+
maxSelections,
|
|
260
|
+
onChange,
|
|
261
|
+
]);
|
|
262
|
+
|
|
263
|
+
return { bulkActions };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Headless state for a searchable select list, shared across the multiselect,
|
|
268
|
+
* dropdown, and top-K filter facades. Composes four focused concerns — search,
|
|
269
|
+
* pinning/freeze, membership/toggle, and bulk actions — behind one entry point.
|
|
270
|
+
* Pinning and bulk rows are opt-in so the single-select and top-K facades share
|
|
271
|
+
* only what they need.
|
|
272
|
+
*/
|
|
273
|
+
export function useSelectList<V>({
|
|
274
|
+
options,
|
|
275
|
+
value,
|
|
276
|
+
onChange,
|
|
277
|
+
multiple,
|
|
278
|
+
maxSelections,
|
|
279
|
+
allowSelectNone,
|
|
280
|
+
filterFn = multiselectFilterFn,
|
|
281
|
+
pinSelected = false,
|
|
282
|
+
}: UseSelectListParams<V>): UseSelectListResult<V> {
|
|
283
|
+
const selected = useMemo(() => new Set(asArray(value)), [value]);
|
|
284
|
+
|
|
285
|
+
const labelByValue = useMemo(() => {
|
|
286
|
+
const map = new Map<V, string>();
|
|
287
|
+
for (const option of options) {
|
|
288
|
+
map.set(option.value, option.label);
|
|
289
|
+
}
|
|
290
|
+
return map;
|
|
291
|
+
}, [options]);
|
|
292
|
+
const labelOf = (candidate: V): string =>
|
|
293
|
+
labelByValue.get(candidate) ?? String(candidate);
|
|
294
|
+
|
|
295
|
+
const {
|
|
296
|
+
searchQuery,
|
|
297
|
+
setSearchQuery: setSearchQueryState,
|
|
298
|
+
filteredOptions,
|
|
299
|
+
} = useSearch({ options, filterFn });
|
|
300
|
+
|
|
301
|
+
const { open, setOpen, repin, visibleOptions, pinnedCount } = usePinning({
|
|
302
|
+
value,
|
|
303
|
+
pinSelected,
|
|
304
|
+
options,
|
|
305
|
+
searchQuery,
|
|
306
|
+
filteredOptions,
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const { isChecked, toggle } = useToggle({
|
|
310
|
+
value,
|
|
311
|
+
onChange,
|
|
312
|
+
multiple,
|
|
313
|
+
maxSelections,
|
|
314
|
+
allowSelectNone,
|
|
315
|
+
selected,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
const { bulkActions } = useBulk({
|
|
319
|
+
value,
|
|
320
|
+
onChange,
|
|
321
|
+
multiple,
|
|
322
|
+
options,
|
|
323
|
+
filteredOptions,
|
|
324
|
+
searchQuery,
|
|
325
|
+
maxSelections,
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
const setSearchQuery = (query: string): void => {
|
|
329
|
+
setSearchQueryState(query);
|
|
330
|
+
if (query === "") {
|
|
331
|
+
repin();
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
return {
|
|
336
|
+
searchQuery,
|
|
337
|
+
setSearchQuery,
|
|
338
|
+
open,
|
|
339
|
+
setOpen,
|
|
340
|
+
visibleOptions,
|
|
341
|
+
pinnedCount,
|
|
342
|
+
isChecked,
|
|
343
|
+
toggle,
|
|
344
|
+
bulkActions,
|
|
345
|
+
labelOf,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import type { BulkActionSpec, Option } from "./types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Override cmdk's relevance-sorting filter with a stricter membership test: an
|
|
6
|
+
* option matches only when every whitespace-separated query word appears in it
|
|
7
|
+
* (case-insensitive), in any order. More lenient than exact match, stricter than
|
|
8
|
+
* fuzzy.
|
|
9
|
+
*
|
|
10
|
+
* Examples:
|
|
11
|
+
* - "foo bar" matches "foo bar"
|
|
12
|
+
* - "bar foo" matches "foo bar"
|
|
13
|
+
* - "foob" does not match "foo bar"
|
|
14
|
+
*/
|
|
15
|
+
export function multiselectFilterFn(option: string, value: string): number {
|
|
16
|
+
const words = value.split(/\s+/);
|
|
17
|
+
const match = words.every((word) =>
|
|
18
|
+
option.toLowerCase().includes(word.toLowerCase()),
|
|
19
|
+
);
|
|
20
|
+
return match ? 1 : 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Union: append the not-yet-selected members of `toAdd`, preserving order. */
|
|
24
|
+
export function selectMatching<V>(selected: V[], toAdd: V[]): V[] {
|
|
25
|
+
const set = new Set(selected);
|
|
26
|
+
return [...set, ...toAdd.filter((v) => !set.has(v))];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Difference: drop every member of `toRemove` from `selected`. */
|
|
30
|
+
export function deselectMatching<V>(selected: V[], toRemove: V[]): V[] {
|
|
31
|
+
const set = new Set(toRemove);
|
|
32
|
+
return selected.filter((v) => !set.has(v));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Order options for the idle (no active search) menu: pinned selections first in
|
|
37
|
+
* pin insertion order (so a freshly added selection appears after earlier ones),
|
|
38
|
+
* then everything else in option order. `pinnedSelection` is a frozen snapshot
|
|
39
|
+
* taken when the menu opens, so toggling an item does not reorder the list under
|
|
40
|
+
* the cursor. Returns the count of pinned options alongside the list so callers
|
|
41
|
+
* don't have to re-scan to find where the pinned block ends.
|
|
42
|
+
*/
|
|
43
|
+
export function getVisibleOptions<V>(
|
|
44
|
+
options: Array<Option<V>>,
|
|
45
|
+
pinnedSelection: ReadonlySet<V>,
|
|
46
|
+
): { visibleOptions: Array<Option<V>>; pinnedCount: number } {
|
|
47
|
+
const byValue = new Map(options.map((o) => [o.value, o] as const));
|
|
48
|
+
const pinned: Array<Option<V>> = [];
|
|
49
|
+
for (const value of pinnedSelection) {
|
|
50
|
+
const option = byValue.get(value);
|
|
51
|
+
if (option) {
|
|
52
|
+
pinned.push(option);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const rest = options.filter((o) => !pinnedSelection.has(o.value));
|
|
56
|
+
return { visibleOptions: [...pinned, ...rest], pinnedCount: pinned.length };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Decide which bulk rows the menu shows for the current search/cap state.
|
|
61
|
+
*
|
|
62
|
+
* When searching, the select-side spec carries the unselected matches and the
|
|
63
|
+
* deselect-side spec carries the selected matches (each omitted when empty).
|
|
64
|
+
* When idle, the spec is just `select-all` / `deselect-all` with an `enabled`
|
|
65
|
+
* flag for the disabled-but-visible state. `maxSelections` hides the select-
|
|
66
|
+
* side everywhere (a bulk select could exceed the cap); `maxSelections === 1`
|
|
67
|
+
* suppresses bulk rows entirely. Returns `[]` when bulk rows shouldn't show.
|
|
68
|
+
*
|
|
69
|
+
* Specs come in select-then-deselect order so the facade can render them as-is.
|
|
70
|
+
*/
|
|
71
|
+
export function getBulkActions<V>(params: {
|
|
72
|
+
options: Array<Option<V>>;
|
|
73
|
+
filteredOptions: Array<Option<V>>;
|
|
74
|
+
value: V[];
|
|
75
|
+
searchQuery: string;
|
|
76
|
+
maxSelections: number | undefined;
|
|
77
|
+
}): Array<BulkActionSpec<V>> {
|
|
78
|
+
const { options, filteredOptions, value, searchQuery, maxSelections } =
|
|
79
|
+
params;
|
|
80
|
+
|
|
81
|
+
if (options.length <= 2 || maxSelections === 1) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const capped = maxSelections != null;
|
|
86
|
+
const specs: Array<BulkActionSpec<V>> = [];
|
|
87
|
+
|
|
88
|
+
if (searchQuery !== "") {
|
|
89
|
+
if (filteredOptions.length === 0) {
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
const selected = new Set(value);
|
|
93
|
+
const selectedMatches: Array<Option<V>> = [];
|
|
94
|
+
const unselectedMatches: Array<Option<V>> = [];
|
|
95
|
+
for (const option of filteredOptions) {
|
|
96
|
+
if (selected.has(option.value)) {
|
|
97
|
+
selectedMatches.push(option);
|
|
98
|
+
} else if (!option.disabled) {
|
|
99
|
+
// Disabled rows can't be picked individually, so bulk select skips them too.
|
|
100
|
+
unselectedMatches.push(option);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!capped && unselectedMatches.length > 0) {
|
|
104
|
+
specs.push({ kind: "select-matching", items: unselectedMatches });
|
|
105
|
+
}
|
|
106
|
+
if (selectedMatches.length > 0) {
|
|
107
|
+
specs.push({ kind: "deselect-matching", items: selectedMatches });
|
|
108
|
+
}
|
|
109
|
+
return specs;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!capped) {
|
|
113
|
+
const selected = new Set(value);
|
|
114
|
+
specs.push({
|
|
115
|
+
kind: "select-all",
|
|
116
|
+
enabled: options.some((o) => !o.disabled && !selected.has(o.value)),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
specs.push({ kind: "deselect-all", enabled: value.length > 0 });
|
|
120
|
+
return specs;
|
|
121
|
+
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
-
import { type JSX, useId, useMemo
|
|
3
|
-
import { Virtuoso } from "react-virtuoso";
|
|
2
|
+
import { type JSX, useId, useMemo } from "react";
|
|
4
3
|
import { z } from "zod";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { CommandSeparator } from "../../components/ui/command";
|
|
4
|
+
import type { Option } from "@/components/ui/select-core";
|
|
5
|
+
import { SelectList } from "@/components/ui/select-core";
|
|
8
6
|
import type { IPlugin, IPluginProps, Setter } from "../types";
|
|
9
7
|
import { Labeled } from "./common/labeled";
|
|
10
|
-
import { multiselectFilterFn } from "./multiselectFilterFn";
|
|
11
8
|
|
|
12
9
|
interface Data {
|
|
13
10
|
label: string | null;
|
|
@@ -55,9 +52,6 @@ interface MultiselectProps extends Data {
|
|
|
55
52
|
setValue: Setter<T>;
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
const SELECT_ALL_KEY = "__select_all__";
|
|
59
|
-
const DESELECT_ALL_KEY = "__deselect_all__";
|
|
60
|
-
|
|
61
55
|
export const Multiselect = ({
|
|
62
56
|
options,
|
|
63
57
|
label,
|
|
@@ -68,144 +62,27 @@ export const Multiselect = ({
|
|
|
68
62
|
disabled,
|
|
69
63
|
}: MultiselectProps): JSX.Element => {
|
|
70
64
|
const id = useId();
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return options;
|
|
76
|
-
}
|
|
77
|
-
return options.filter(
|
|
78
|
-
(option) => multiselectFilterFn(option, searchQuery) === 1,
|
|
79
|
-
);
|
|
80
|
-
}, [options, searchQuery]);
|
|
81
|
-
|
|
82
|
-
const handleValueChange = (newValues: string[] | null) => {
|
|
83
|
-
if (!newValues || newValues.length === 0) {
|
|
84
|
-
setValue([]);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Remove select all and deselect all from the new values
|
|
89
|
-
newValues = newValues.filter(
|
|
90
|
-
(value) => value !== SELECT_ALL_KEY && value !== DESELECT_ALL_KEY,
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
if (maxSelections === 1) {
|
|
94
|
-
// For single selection, just take the last selected value
|
|
95
|
-
setValue([newValues[newValues.length - 1]]);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (maxSelections != null && newValues.length > maxSelections) {
|
|
100
|
-
// When over max selections, remove oldest selections
|
|
101
|
-
newValues = newValues.slice(-maxSelections);
|
|
102
|
-
}
|
|
103
|
-
setValue(newValues);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const handleSelectAll = () => {
|
|
107
|
-
setValue(options);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const handleDeselectAll = () => {
|
|
111
|
-
setValue([]);
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const extraOptions: React.ReactNode[] = [];
|
|
115
|
-
const selectAllEnabled = options.length > 0 && value.length < options.length;
|
|
116
|
-
const deselectAllEnabled = options.length > 0 && value.length > 0;
|
|
117
|
-
|
|
118
|
-
// Only show when more than 2 options
|
|
119
|
-
// Only show select all when maxSelections is not set
|
|
120
|
-
if (options.length > 2 && maxSelections == null) {
|
|
121
|
-
extraOptions.push(
|
|
122
|
-
<ComboboxItem
|
|
123
|
-
key={SELECT_ALL_KEY}
|
|
124
|
-
value={SELECT_ALL_KEY}
|
|
125
|
-
onSelect={handleSelectAll}
|
|
126
|
-
disabled={!selectAllEnabled}
|
|
127
|
-
>
|
|
128
|
-
Select all
|
|
129
|
-
</ComboboxItem>,
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (options.length > 2) {
|
|
134
|
-
extraOptions.push(
|
|
135
|
-
<ComboboxItem
|
|
136
|
-
key={DESELECT_ALL_KEY}
|
|
137
|
-
value={DESELECT_ALL_KEY}
|
|
138
|
-
onSelect={handleDeselectAll}
|
|
139
|
-
disabled={!deselectAllEnabled}
|
|
140
|
-
>
|
|
141
|
-
{maxSelections === 1 ? "Deselect" : "Deselect all"}
|
|
142
|
-
</ComboboxItem>,
|
|
143
|
-
<CommandSeparator key="_separator" />,
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const renderList = () => {
|
|
148
|
-
// List virtualization
|
|
149
|
-
if (filteredOptions.length > 200) {
|
|
150
|
-
return (
|
|
151
|
-
<Virtuoso
|
|
152
|
-
style={{ height: "200px" }}
|
|
153
|
-
totalCount={filteredOptions.length}
|
|
154
|
-
overscan={50}
|
|
155
|
-
itemContent={(i: number) => {
|
|
156
|
-
const comboboxItem = (
|
|
157
|
-
<ComboboxItem key={filteredOptions[i]} value={filteredOptions[i]}>
|
|
158
|
-
{filteredOptions[i]}
|
|
159
|
-
</ComboboxItem>
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
if (i === 0) {
|
|
163
|
-
return (
|
|
164
|
-
<>
|
|
165
|
-
{extraOptions}
|
|
166
|
-
{comboboxItem}
|
|
167
|
-
</>
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return comboboxItem;
|
|
172
|
-
}}
|
|
173
|
-
/>
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const list = filteredOptions.map((option) => (
|
|
178
|
-
<ComboboxItem key={option} value={option}>
|
|
179
|
-
{option}
|
|
180
|
-
</ComboboxItem>
|
|
181
|
-
));
|
|
182
|
-
|
|
183
|
-
return (
|
|
184
|
-
<>
|
|
185
|
-
{extraOptions}
|
|
186
|
-
{list}
|
|
187
|
-
</>
|
|
188
|
-
);
|
|
189
|
-
};
|
|
65
|
+
const items = useMemo<Array<Option<string>>>(
|
|
66
|
+
() => options.map((option) => ({ value: option, label: option })),
|
|
67
|
+
[options],
|
|
68
|
+
);
|
|
190
69
|
|
|
191
70
|
return (
|
|
192
71
|
<Labeled label={label} id={id} fullWidth={fullWidth}>
|
|
193
|
-
<
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
multiple={true}
|
|
197
|
-
className={cn({
|
|
198
|
-
"w-full": fullWidth,
|
|
199
|
-
})}
|
|
72
|
+
<SelectList<string>
|
|
73
|
+
id={id}
|
|
74
|
+
options={items}
|
|
200
75
|
value={value}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
76
|
+
onChange={(next: string[] | string | null) =>
|
|
77
|
+
setValue((next as string[] | null) ?? [])
|
|
78
|
+
}
|
|
79
|
+
multiple={true}
|
|
80
|
+
maxSelections={maxSelections}
|
|
81
|
+
pinSelected={true}
|
|
82
|
+
compactChipTrigger={true}
|
|
83
|
+
fullWidth={fullWidth}
|
|
205
84
|
disabled={disabled}
|
|
206
|
-
|
|
207
|
-
{renderList()}
|
|
208
|
-
</Combobox>
|
|
85
|
+
/>
|
|
209
86
|
</Labeled>
|
|
210
87
|
);
|
|
211
88
|
};
|