@brightlocal/ui-components 3.1.0 → 3.2.0
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 +6 -1
- package/component-meta.json +663 -38
- package/deprecations.json +1 -1
- package/dist/alert-dialog.cjs +1 -1
- package/dist/alert-dialog.d.ts +32 -3
- package/dist/alert-dialog.d.ts.map +1 -1
- package/dist/alert-dialog.js +61 -58
- package/dist/alert.cjs +1 -1
- package/dist/alert.d.ts.map +1 -1
- package/dist/alert.js +58 -57
- package/dist/badge.cjs +1 -1
- package/dist/badge.d.ts +9 -0
- package/dist/badge.d.ts.map +1 -1
- package/dist/badge.js +50 -30
- package/dist/banner.cjs +1 -0
- package/dist/banner.d.ts +81 -0
- package/dist/banner.d.ts.map +1 -0
- package/dist/banner.js +176 -0
- package/dist/blocks/empty-state.cjs +1 -1
- package/dist/blocks/empty-state.d.ts +4 -3
- package/dist/blocks/empty-state.d.ts.map +1 -1
- package/dist/blocks/empty-state.js +21 -20
- package/dist/blocks/progress-state.d.ts +2 -2
- package/dist/blocks/sidebar.cjs +1 -1
- package/dist/blocks/sidebar.d.ts +17 -0
- package/dist/blocks/sidebar.d.ts.map +1 -1
- package/dist/blocks/sidebar.js +8 -2
- package/dist/brand-illustration-motif.d.ts +1 -1
- package/dist/card.cjs +1 -1
- package/dist/card.d.ts +86 -6
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +185 -96
- package/dist/chart.cjs +3 -3
- package/dist/chart.d.ts +231 -13
- package/dist/chart.d.ts.map +1 -1
- package/dist/chart.js +466 -306
- package/dist/checkbox.cjs +1 -1
- package/dist/checkbox.d.ts +1 -1
- package/dist/checkbox.d.ts.map +1 -1
- package/dist/checkbox.js +2 -1
- package/dist/combobox.cjs +1 -1
- package/dist/combobox.js +2 -2
- package/dist/command.cjs +1 -1
- package/dist/command.js +1 -1
- package/dist/data-table.cjs +1 -1
- package/dist/data-table.d.ts +169 -22
- package/dist/data-table.d.ts.map +1 -1
- package/dist/data-table.js +695 -561
- package/dist/grid.d.ts +24 -1
- package/dist/grid.d.ts.map +1 -1
- package/dist/hooks/use-breakpoint.cjs +1 -1
- package/dist/hooks/use-breakpoint.d.ts +1 -1
- package/dist/hooks/use-breakpoint.d.ts.map +1 -1
- package/dist/hooks/use-breakpoint.js +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +593 -561
- package/dist/info-tooltip.cjs +1 -0
- package/dist/info-tooltip.d.ts +39 -0
- package/dist/info-tooltip.d.ts.map +1 -0
- package/dist/info-tooltip.js +41 -0
- package/dist/input-chip.cjs +1 -1
- package/dist/input-chip.js +1 -1
- package/dist/input-group.cjs +1 -1
- package/dist/input-group.d.ts +33 -2
- package/dist/input-group.d.ts.map +1 -1
- package/dist/input-group.js +167 -54
- package/dist/input-otp.cjs +1 -1
- package/dist/input-otp.js +9 -9
- package/dist/input-password.cjs +1 -1
- package/dist/input-password.d.ts +28 -6
- package/dist/input-password.d.ts.map +1 -1
- package/dist/input-password.js +5 -5
- package/dist/input.cjs +1 -1
- package/dist/input.js +10 -10
- package/dist/lib/block-illustration.cjs +1 -1
- package/dist/lib/block-illustration.d.ts +4 -1
- package/dist/lib/block-illustration.d.ts.map +1 -1
- package/dist/lib/block-illustration.js +12 -11
- package/dist/lib/utils.cjs +1 -1
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +4 -3
- package/dist/list.cjs +1 -1
- package/dist/list.js +1 -1
- package/dist/multi-select-filter.cjs +1 -0
- package/dist/multi-select-filter.d.ts +178 -0
- package/dist/multi-select-filter.d.ts.map +1 -0
- package/dist/multi-select-filter.js +463 -0
- package/dist/progress.cjs +1 -1
- package/dist/progress.d.ts +123 -16
- package/dist/progress.d.ts.map +1 -1
- package/dist/progress.js +89 -75
- package/dist/rating.cjs +1 -1
- package/dist/rating.js +18 -18
- package/dist/relative-date.cjs +1 -0
- package/dist/relative-date.d.ts +65 -0
- package/dist/relative-date.d.ts.map +1 -0
- package/dist/relative-date.js +128 -0
- package/dist/select.cjs +2 -2
- package/dist/select.d.ts +11 -1
- package/dist/select.d.ts.map +1 -1
- package/dist/select.js +97 -81
- package/dist/sheet.cjs +1 -1
- package/dist/sheet.d.ts +78 -2
- package/dist/sheet.d.ts.map +1 -1
- package/dist/sheet.js +192 -85
- package/dist/single-select-filter.cjs +1 -0
- package/dist/single-select-filter.d.ts +81 -0
- package/dist/single-select-filter.d.ts.map +1 -0
- package/dist/single-select-filter.js +226 -0
- package/dist/sonner.cjs +1 -1
- package/dist/sonner.d.ts.map +1 -1
- package/dist/sonner.js +22 -22
- package/dist/split-layout.cjs +1 -1
- package/dist/split-layout.d.ts +1 -1
- package/dist/split-layout.js +13 -13
- package/dist/table.cjs +1 -1
- package/dist/table.d.ts +45 -4
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +90 -73
- package/dist/tabs.cjs +1 -1
- package/dist/tabs.d.ts +12 -1
- package/dist/tabs.d.ts.map +1 -1
- package/dist/tabs.js +148 -126
- package/dist/textarea.cjs +1 -1
- package/dist/textarea.js +1 -1
- package/dist/tooltip.cjs +1 -1
- package/dist/tooltip.d.ts +38 -36
- package/dist/tooltip.d.ts.map +1 -1
- package/dist/tooltip.js +262 -73
- package/dist/typography-text.d.ts +1 -2
- package/dist/typography-text.d.ts.map +1 -1
- package/dist/typography.d.ts +1 -1
- package/package.json +5 -6
- package/src/blocks/empty-state.tsx +8 -3
- package/src/blocks/progress-state.tsx +2 -2
- package/src/blocks/sidebar.tsx +26 -2
- package/src/components/alert-dialog.tsx +50 -5
- package/src/components/alert.tsx +16 -8
- package/src/components/badge.tsx +40 -3
- package/src/components/banner.tsx +288 -0
- package/src/components/brand-illustration-motif.tsx +1 -1
- package/src/components/card.tsx +211 -24
- package/src/components/chart.tsx +523 -35
- package/src/components/checkbox.tsx +1 -1
- package/src/components/combobox.tsx +2 -2
- package/src/components/command.tsx +1 -1
- package/src/components/data-table.tsx +778 -195
- package/src/components/grid.tsx +32 -1
- package/src/components/info-tooltip.tsx +94 -0
- package/src/components/input-chip.tsx +1 -1
- package/src/components/input-group.tsx +246 -11
- package/src/components/input-otp.tsx +1 -1
- package/src/components/input-password.tsx +33 -11
- package/src/components/input.tsx +1 -1
- package/src/components/list.tsx +1 -1
- package/src/components/multi-select-filter.tsx +912 -0
- package/src/components/progress.tsx +190 -31
- package/src/components/rating.tsx +1 -1
- package/src/components/relative-date.tsx +230 -0
- package/src/components/select.tsx +35 -5
- package/src/components/sheet.tsx +240 -4
- package/src/components/single-select-filter.tsx +443 -0
- package/src/components/sonner.tsx +16 -5
- package/src/components/split-layout.tsx +2 -2
- package/src/components/table.tsx +71 -11
- package/src/components/tabs.tsx +93 -46
- package/src/components/textarea.tsx +1 -1
- package/src/components/tooltip.tsx +440 -68
- package/src/components/typography-text.tsx +1 -2
- package/src/components/typography.tsx +1 -1
- package/src/hooks/use-breakpoint.ts +2 -1
- package/src/index.ts +5 -0
- package/src/lib/block-illustration.tsx +5 -1
- package/src/lib/utils.ts +1 -0
|
@@ -0,0 +1,912 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
5
|
+
|
|
6
|
+
import { Check, ChevronDown, SearchX } from "@brightlocal/icons";
|
|
7
|
+
import { Button } from "@brightlocal/ui-components/button";
|
|
8
|
+
import {
|
|
9
|
+
Checkbox,
|
|
10
|
+
checkboxVariants,
|
|
11
|
+
} from "@brightlocal/ui-components/checkbox";
|
|
12
|
+
import {
|
|
13
|
+
Command,
|
|
14
|
+
CommandEmpty,
|
|
15
|
+
CommandGroup,
|
|
16
|
+
CommandInput,
|
|
17
|
+
CommandItem,
|
|
18
|
+
CommandList,
|
|
19
|
+
} from "@brightlocal/ui-components/command";
|
|
20
|
+
import {
|
|
21
|
+
Drawer,
|
|
22
|
+
DrawerBody,
|
|
23
|
+
DrawerClose,
|
|
24
|
+
DrawerContent,
|
|
25
|
+
DrawerDescription,
|
|
26
|
+
DrawerFooter,
|
|
27
|
+
DrawerHeader,
|
|
28
|
+
DrawerTitle,
|
|
29
|
+
DrawerTrigger,
|
|
30
|
+
} from "@brightlocal/ui-components/drawer";
|
|
31
|
+
import { useIsBelowBreakpoint } from "@brightlocal/ui-components/hooks/use-breakpoint";
|
|
32
|
+
import { Input } from "@brightlocal/ui-components/input";
|
|
33
|
+
import { Label } from "@brightlocal/ui-components/label";
|
|
34
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
35
|
+
import {
|
|
36
|
+
Popover,
|
|
37
|
+
PopoverContent,
|
|
38
|
+
PopoverTrigger,
|
|
39
|
+
} from "@brightlocal/ui-components/popover";
|
|
40
|
+
import { emptyChipStyles } from "../lib/empty-chip";
|
|
41
|
+
|
|
42
|
+
/* MultiSelectFilter — the Filtering pattern's faceted multi-select
|
|
43
|
+
(Patterns/Data Manipulation/Filtering → Which control → Faceted
|
|
44
|
+
multi-select): several values from one dimension, combining as OR.
|
|
45
|
+
|
|
46
|
+
Trigger. The trigger hugs its label in a toolbar and wears the field look
|
|
47
|
+
(SelectTrigger's border, radius, focus ring and chevron), because a filter
|
|
48
|
+
trigger holds a value rather than performing an action — which is also why
|
|
49
|
+
it is a native button, not the DS Button. Stacked in a panel, it takes the
|
|
50
|
+
row's width from a `w-full` layout class. The trigger reads its
|
|
51
|
+
picks like a Select shows its value: the All label (or the dimension's
|
|
52
|
+
name, muted, as a placeholder) at zero, then the picked labels in pick
|
|
53
|
+
order, collapsing past `maxVisibleLabels` into `summaryLabel` ("Google
|
|
54
|
+
+2"). The accessible name is computed here with every pick spelled out.
|
|
55
|
+
Glyphs are the consumer's: a pinned `icon`, or the selection's own —
|
|
56
|
+
`allIcon` at zero, then the first pick's `option.icon`; every option's
|
|
57
|
+
glyph sits before its label in the menu and the drawer.
|
|
58
|
+
|
|
59
|
+
Menu. The menu stays open across picks; selected values pin to the top on
|
|
60
|
+
reopen (never mid-pick); a zero-count option is disabled, never hidden,
|
|
61
|
+
and stays enabled while picked so it can be unticked; Clear filters lives
|
|
62
|
+
inside the menu. Past `searchThreshold` options (or with `searchable`) an
|
|
63
|
+
autofocused search input narrows the options by a case-insensitive
|
|
64
|
+
substring match on label and value — never on the count. While a query
|
|
65
|
+
is typed the All row and Clear step aside; a query that matches nothing
|
|
66
|
+
shows the compact empty form (muted chip, `emptyLabel`,
|
|
67
|
+
`emptyDescription`).
|
|
68
|
+
|
|
69
|
+
All. The optional All row is a reset, not a select-all: an empty
|
|
70
|
+
selection narrows nothing (the API's own rule for an omitted filter), so
|
|
71
|
+
the row is checked when nothing is picked and picking it clears. For the
|
|
72
|
+
same reason a pick that completes the set collapses to `[]`
|
|
73
|
+
(`collapseFullSelection`, default on) — every pickable option ticked is
|
|
74
|
+
"All", and must not read "Google +3".
|
|
75
|
+
|
|
76
|
+
Presentations. `presentation="menu"` is the Popover + Command menu;
|
|
77
|
+
`presentation="drawer"` is the phone form — the same trigger opens a
|
|
78
|
+
bottom Drawer titled with the dimension, with the All row, the options as
|
|
79
|
+
Checkboxes in two columns, the search field past the threshold, Clear
|
|
80
|
+
while something is picked, and a Done button that only closes (every tick
|
|
81
|
+
applies at once; an Apply button on a per-filter surface reads as "nothing
|
|
82
|
+
happened"). `presentation="auto"` switches at the `md` breakpoint. Both
|
|
83
|
+
render from one state, so the rules above cannot drift between them.
|
|
84
|
+
|
|
85
|
+
Commit model is per filter: `onValueChange` fires on every pick so client
|
|
86
|
+
data can reflect immediately; defer an expensive refetch to
|
|
87
|
+
`onOpenChange(false)`. */
|
|
88
|
+
|
|
89
|
+
/* ------------------------------------------------------------------------ */
|
|
90
|
+
/* Trigger styles */
|
|
91
|
+
/* ------------------------------------------------------------------------ */
|
|
92
|
+
|
|
93
|
+
/* The field look of SelectTrigger — the same border, radius, focus ring and
|
|
94
|
+
open state, written in the semantic tokens the Figma Select is bound to
|
|
95
|
+
(`background` / `input/30` fill, `input` border, `ring` focus). The
|
|
96
|
+
differences are structural, not decorative: the trigger hugs its label
|
|
97
|
+
where a form field fills its row, there is no `data-[placeholder]` state
|
|
98
|
+
because a trigger always has text to show, and there is no error state
|
|
99
|
+
because a filter does not validate. Exported so the Filtering pattern's "Filters (n)" recipe
|
|
100
|
+
— a trigger that opens a Sheet or Drawer of hidden filters — can wear the
|
|
101
|
+
same look. */
|
|
102
|
+
const multiSelectFilterTriggerVariants = cva(
|
|
103
|
+
[
|
|
104
|
+
// Base layout
|
|
105
|
+
"relative inline-flex items-center justify-between gap-2 py-2",
|
|
106
|
+
|
|
107
|
+
// Base appearance — the Figma Select's surface: background (input/30 in
|
|
108
|
+
// dark) fill and the input border
|
|
109
|
+
"border border-input rounded-sm outline-none transition-[border-color,box-shadow,background-color] duration-fast ease-out",
|
|
110
|
+
"bg-background dark:bg-input/30",
|
|
111
|
+
"text-foreground",
|
|
112
|
+
|
|
113
|
+
// Focus state (keyboard navigation only — not on click)
|
|
114
|
+
"focus-visible:outline-none focus-visible:border-ring focus-visible:bg-background",
|
|
115
|
+
"focus-visible:shadow-default",
|
|
116
|
+
|
|
117
|
+
// Open state — the surface this trigger owns is showing
|
|
118
|
+
"data-[state=open]:border-ring",
|
|
119
|
+
|
|
120
|
+
// Disabled state
|
|
121
|
+
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",
|
|
122
|
+
|
|
123
|
+
// Icon styles (chevron and leading icons)
|
|
124
|
+
"[&_svg]:pointer-events-none [&_svg]:shrink-0",
|
|
125
|
+
],
|
|
126
|
+
{
|
|
127
|
+
variants: {
|
|
128
|
+
size: {
|
|
129
|
+
/* A filter trigger is a field-like control holding a value, so it
|
|
130
|
+
wears the field's type role, exactly like SelectTrigger (DS-992):
|
|
131
|
+
14px/20 on desktop, 16px/24 on touch devices so it never differs
|
|
132
|
+
from the Select beside it in a toolbar. */
|
|
133
|
+
default: "h-9 px-3 text-field",
|
|
134
|
+
/* The DS Button's `sm` height (h-8) with text-label (14px everywhere),
|
|
135
|
+
so the same trigger sits in a phone card header without the desktop
|
|
136
|
+
height or the touch bump crowding the row. Icons stay 16px unless
|
|
137
|
+
the consumer's glyph sizes itself. */
|
|
138
|
+
sm: "h-8 px-2.5 text-label [&_svg:not([class*='size-'])]:size-4",
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
defaultVariants: {
|
|
142
|
+
size: "default",
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
/* ------------------------------------------------------------------------ */
|
|
148
|
+
/* Types */
|
|
149
|
+
/* ------------------------------------------------------------------------ */
|
|
150
|
+
|
|
151
|
+
/** One selectable option of a multi-select filter. */
|
|
152
|
+
interface MultiSelectFilterOption {
|
|
153
|
+
/** Unique value of the option. */
|
|
154
|
+
value: string;
|
|
155
|
+
/** Visible label of the option. */
|
|
156
|
+
label: string;
|
|
157
|
+
/**
|
|
158
|
+
* Result count for this option, shown beside the label in the menu (the
|
|
159
|
+
* drawer's two-column rows carry only the All total). A count of 0
|
|
160
|
+
* disables the option in both — disabled, never hidden, so the dimension
|
|
161
|
+
* keeps its full shape.
|
|
162
|
+
*/
|
|
163
|
+
count?: number;
|
|
164
|
+
/** Disables the option explicitly. */
|
|
165
|
+
disabled?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* The option's own glyph (a search-engine logo, a rating star), 16px.
|
|
168
|
+
* Shown before the label in the menu and the drawer; the trigger shows the
|
|
169
|
+
* first pick's glyph when no `icon` is set.
|
|
170
|
+
*/
|
|
171
|
+
icon?: React.ReactNode;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
interface MultiSelectFilterProps
|
|
175
|
+
extends
|
|
176
|
+
Omit<
|
|
177
|
+
React.ComponentPropsWithoutRef<"button">,
|
|
178
|
+
"value" | "defaultValue" | "onChange" | "children"
|
|
179
|
+
>,
|
|
180
|
+
VariantProps<typeof multiSelectFilterTriggerVariants> {
|
|
181
|
+
/**
|
|
182
|
+
* Data hook for automated testing (REQUIRED). Every part of the menu
|
|
183
|
+
* derives its own hook from it (`-popover`, `-search`, `-option-all`,
|
|
184
|
+
* `-option-<value>`, `-clear`, `-summary`, `-icon`).
|
|
185
|
+
* @example "locations-status-filter"
|
|
186
|
+
*/
|
|
187
|
+
dataHook: string;
|
|
188
|
+
/**
|
|
189
|
+
* The dimension's name — use the product's words ("Status", "Source"),
|
|
190
|
+
* never an internal field name. Always the start of the accessible name;
|
|
191
|
+
* also the trigger's muted placeholder at zero when there is no `allLabel`.
|
|
192
|
+
*/
|
|
193
|
+
label: string;
|
|
194
|
+
/** The selectable options, in the dimension's own order. */
|
|
195
|
+
options: readonly MultiSelectFilterOption[];
|
|
196
|
+
/** Controlled selected values. Values combine as OR. */
|
|
197
|
+
value?: readonly string[];
|
|
198
|
+
/**
|
|
199
|
+
* Initial selection for uncontrolled mode.
|
|
200
|
+
* @default []
|
|
201
|
+
*/
|
|
202
|
+
defaultValue?: readonly string[];
|
|
203
|
+
/**
|
|
204
|
+
* Callback on every pick (the per-filter commit model: reflect
|
|
205
|
+
* immediately on client data; defer expensive refetches to
|
|
206
|
+
* `onOpenChange(false)`).
|
|
207
|
+
* @param value - The full selection after the pick
|
|
208
|
+
*/
|
|
209
|
+
onValueChange?: (value: string[]) => void;
|
|
210
|
+
/** Controlled open state of the menu. */
|
|
211
|
+
open?: boolean;
|
|
212
|
+
/**
|
|
213
|
+
* Callback when the menu opens or closes — `false` is the moment to run
|
|
214
|
+
* an expensive refetch for everything picked while it was open.
|
|
215
|
+
*/
|
|
216
|
+
onOpenChange?: (open: boolean) => void;
|
|
217
|
+
/**
|
|
218
|
+
* Fixed glyph leading the trigger — the dimension's own icon (a funnel, a
|
|
219
|
+
* calendar), 16px. Without it the trigger follows the selection: `allIcon`
|
|
220
|
+
* at zero, then the first pick's `option.icon`. Hidden from assistive
|
|
221
|
+
* tech; the accessible name comes from `label` and the picks.
|
|
222
|
+
*/
|
|
223
|
+
icon?: React.ReactNode;
|
|
224
|
+
/**
|
|
225
|
+
* How many picked labels to spell out before collapsing into
|
|
226
|
+
* `summaryLabel` ("Google +2" at 1, "London, Brighton +1" at 2). Values
|
|
227
|
+
* below 1 are treated as 1.
|
|
228
|
+
* @default 1
|
|
229
|
+
*/
|
|
230
|
+
maxVisibleLabels?: number;
|
|
231
|
+
/**
|
|
232
|
+
* Label of the leading reset row, e.g. "All sources". Omit to hide the
|
|
233
|
+
* row. Checked when nothing is picked; picking it clears the selection.
|
|
234
|
+
* Also the trigger's text at zero.
|
|
235
|
+
*/
|
|
236
|
+
allLabel?: string;
|
|
237
|
+
/**
|
|
238
|
+
* Glyph of the All row, and of the trigger while nothing is picked (when
|
|
239
|
+
* no `icon` is set) — the dimension's "everything" mark, e.g. a globe.
|
|
240
|
+
*/
|
|
241
|
+
allIcon?: React.ReactNode;
|
|
242
|
+
/**
|
|
243
|
+
* Total beside the All row. Defaults to the sum of the options' counts
|
|
244
|
+
* when every option has one — per-option counts leave out their own
|
|
245
|
+
* filter, so together they are the unfiltered total.
|
|
246
|
+
*/
|
|
247
|
+
allCount?: number;
|
|
248
|
+
/**
|
|
249
|
+
* Show a search input inside the menu once the option list grows past
|
|
250
|
+
* this many options. Scanning stops working past ~10–15.
|
|
251
|
+
* @default 10
|
|
252
|
+
*/
|
|
253
|
+
searchThreshold?: number;
|
|
254
|
+
/** Force the search input on or off, overriding `searchThreshold`. */
|
|
255
|
+
searchable?: boolean;
|
|
256
|
+
/**
|
|
257
|
+
* When a pick completes the set — every option that can be picked (not
|
|
258
|
+
* disabled, count not 0) is ticked — the selection collapses to `[]`,
|
|
259
|
+
* i.e. "All": the same records, read as no filter instead of "Google +3".
|
|
260
|
+
* @default true
|
|
261
|
+
*/
|
|
262
|
+
collapseFullSelection?: boolean;
|
|
263
|
+
/** Disables the trigger. */
|
|
264
|
+
disabled?: boolean;
|
|
265
|
+
/**
|
|
266
|
+
* Label of the clear action inside the menu.
|
|
267
|
+
* @default "Clear filters"
|
|
268
|
+
*/
|
|
269
|
+
clearLabel?: string;
|
|
270
|
+
/**
|
|
271
|
+
* Trigger summary once more values are picked than `maxVisibleLabels`.
|
|
272
|
+
* Placeholders: `{labels}` — the first `maxVisibleLabels` labels,
|
|
273
|
+
* `{rest}` — the picks beyond them, `{count}` — every pick.
|
|
274
|
+
* `"{count} selected"` reads "3 selected" instead.
|
|
275
|
+
* @default "{labels} +{rest}"
|
|
276
|
+
*/
|
|
277
|
+
summaryLabel?: string;
|
|
278
|
+
/**
|
|
279
|
+
* Placeholder of the search input inside the menu.
|
|
280
|
+
* @default "Search options..."
|
|
281
|
+
*/
|
|
282
|
+
searchPlaceholder?: string;
|
|
283
|
+
/**
|
|
284
|
+
* Message when the search matches no options.
|
|
285
|
+
* @default "No options found."
|
|
286
|
+
*/
|
|
287
|
+
emptyLabel?: string;
|
|
288
|
+
/**
|
|
289
|
+
* The quiet hint under the no-match message.
|
|
290
|
+
* @default "Try another search."
|
|
291
|
+
*/
|
|
292
|
+
emptyDescription?: string;
|
|
293
|
+
/**
|
|
294
|
+
* Accessible label of the menu search input's clear button (the drawer's
|
|
295
|
+
* search field is a plain Input without one).
|
|
296
|
+
* @default "Clear search"
|
|
297
|
+
*/
|
|
298
|
+
clearSearchAriaLabel?: string;
|
|
299
|
+
/**
|
|
300
|
+
* How the options open: the Popover + Command `menu`, a bottom `drawer`
|
|
301
|
+
* (the phone form: Checkbox rows in two columns, Clear, Done), or `auto`
|
|
302
|
+
* — the menu from the `md` breakpoint up and the drawer below it.
|
|
303
|
+
* @default "menu"
|
|
304
|
+
*/
|
|
305
|
+
presentation?: "menu" | "drawer" | "auto";
|
|
306
|
+
/**
|
|
307
|
+
* Label of the drawer's Done button — it only closes; picks apply at once.
|
|
308
|
+
* @default "Done"
|
|
309
|
+
*/
|
|
310
|
+
doneLabel?: string;
|
|
311
|
+
/**
|
|
312
|
+
* Screen-reader description of the drawer (the Drawer contract needs a
|
|
313
|
+
* title and a description; the title is `label`).
|
|
314
|
+
* @default "Picks apply as you make them."
|
|
315
|
+
*/
|
|
316
|
+
drawerDescription?: string;
|
|
317
|
+
/** Analytics element identifier. @default "multi-select-filter" */
|
|
318
|
+
trackingEl?: string;
|
|
319
|
+
/** Analytics label */
|
|
320
|
+
trackingLabel?: string;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/* ------------------------------------------------------------------------ */
|
|
324
|
+
/* Engine */
|
|
325
|
+
/* ------------------------------------------------------------------------ */
|
|
326
|
+
|
|
327
|
+
interface UseMultiSelectFilterStateOptions {
|
|
328
|
+
value?: readonly string[];
|
|
329
|
+
defaultValue: readonly string[];
|
|
330
|
+
onValueChange?: (value: string[]) => void;
|
|
331
|
+
open?: boolean;
|
|
332
|
+
onOpenChange?: (open: boolean) => void;
|
|
333
|
+
options: readonly MultiSelectFilterOption[];
|
|
334
|
+
collapseFullSelection: boolean;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/* The shared multi-select state: value, open state, pinned order and the
|
|
338
|
+
toggle-accumulate handler. Private — the component is the only consumer. */
|
|
339
|
+
function useMultiSelectFilterState({
|
|
340
|
+
value: controlledValue,
|
|
341
|
+
defaultValue,
|
|
342
|
+
onValueChange,
|
|
343
|
+
open: controlledOpen,
|
|
344
|
+
onOpenChange,
|
|
345
|
+
options,
|
|
346
|
+
collapseFullSelection,
|
|
347
|
+
}: UseMultiSelectFilterStateOptions) {
|
|
348
|
+
const [uncontrolledValue, setUncontrolledValue] =
|
|
349
|
+
React.useState<readonly string[]>(defaultValue);
|
|
350
|
+
const isControlled = controlledValue !== undefined;
|
|
351
|
+
const value = isControlled ? controlledValue : uncontrolledValue;
|
|
352
|
+
const selectedSet = React.useMemo(() => new Set(value), [value]);
|
|
353
|
+
|
|
354
|
+
const [uncontrolledOpen, setUncontrolledOpen] = React.useState(false);
|
|
355
|
+
const isOpenControlled = controlledOpen !== undefined;
|
|
356
|
+
const open = isOpenControlled ? controlledOpen : uncontrolledOpen;
|
|
357
|
+
|
|
358
|
+
/* Pinned order snapshots when the menu opens — selected values pin to the
|
|
359
|
+
top on reopen, never mid-pick, so the list doesn't reshuffle under the
|
|
360
|
+
pointer. Keyed on `open` itself (not the handler) so a parent flipping a
|
|
361
|
+
controlled `open` pins too; the value is read through a ref so a pick
|
|
362
|
+
while open never re-snapshots. */
|
|
363
|
+
const [pinned, setPinned] = React.useState<Set<string>>(new Set());
|
|
364
|
+
const wasOpen = React.useRef(open);
|
|
365
|
+
React.useEffect(() => {
|
|
366
|
+
if (open && !wasOpen.current) setPinned(new Set(value));
|
|
367
|
+
wasOpen.current = open;
|
|
368
|
+
}, [open, value]);
|
|
369
|
+
|
|
370
|
+
const setValue = (next: string[]) => {
|
|
371
|
+
if (!isControlled) setUncontrolledValue(next);
|
|
372
|
+
onValueChange?.(next);
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const toggleOption = (optionValue: string) => {
|
|
376
|
+
if (selectedSet.has(optionValue)) {
|
|
377
|
+
setValue(value.filter((item) => item !== optionValue));
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const next = [...value, optionValue];
|
|
381
|
+
if (collapseFullSelection) {
|
|
382
|
+
/* Every pickable option ticked means "All" — the empty selection. */
|
|
383
|
+
const pickable = options.filter(
|
|
384
|
+
(option) => !option.disabled && option.count !== 0
|
|
385
|
+
);
|
|
386
|
+
const nextSet = new Set(next);
|
|
387
|
+
if (
|
|
388
|
+
pickable.length > 0 &&
|
|
389
|
+
pickable.every((option) => nextSet.has(option.value))
|
|
390
|
+
) {
|
|
391
|
+
setValue([]);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
setValue(next);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const handleOpenChange = (next: boolean) => {
|
|
399
|
+
if (!isOpenControlled) setUncontrolledOpen(next);
|
|
400
|
+
onOpenChange?.(next);
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const orderedOptions = React.useMemo(
|
|
404
|
+
() => [
|
|
405
|
+
...options.filter((option) => pinned.has(option.value)),
|
|
406
|
+
...options.filter((option) => !pinned.has(option.value)),
|
|
407
|
+
],
|
|
408
|
+
[options, pinned]
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
clear: () => setValue([]),
|
|
413
|
+
onOpenChange: handleOpenChange,
|
|
414
|
+
open,
|
|
415
|
+
orderedOptions,
|
|
416
|
+
selectedSet,
|
|
417
|
+
toggleOption,
|
|
418
|
+
value,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/* ------------------------------------------------------------------------ */
|
|
423
|
+
/* Pieces */
|
|
424
|
+
/* ------------------------------------------------------------------------ */
|
|
425
|
+
|
|
426
|
+
/* The option's check mark wears the DS Checkbox look without being a
|
|
427
|
+
Checkbox: the CommandItem owns the click and carries aria-checked, so a
|
|
428
|
+
nested button would be a second control inside an option. `data-state`
|
|
429
|
+
drives checkboxVariants' checked treatment exactly as Radix does. */
|
|
430
|
+
function OptionCheck({ checked }: { checked: boolean }) {
|
|
431
|
+
return (
|
|
432
|
+
<span
|
|
433
|
+
aria-hidden="true"
|
|
434
|
+
className={cn(
|
|
435
|
+
checkboxVariants(),
|
|
436
|
+
"mt-0 flex items-center justify-center self-center"
|
|
437
|
+
)}
|
|
438
|
+
data-state={checked ? "checked" : "unchecked"}
|
|
439
|
+
>
|
|
440
|
+
{checked ? <Check /> : null}
|
|
441
|
+
</span>
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function OptionCount({ count }: { count: number }) {
|
|
446
|
+
return (
|
|
447
|
+
<span className="text-muted-foreground text-label-sm ml-auto tabular-nums">
|
|
448
|
+
{count}
|
|
449
|
+
</span>
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/* cmdk values for the two rows that are not options — unique, so an option
|
|
454
|
+
labelled "All" or "Clear" never shares a value with them. */
|
|
455
|
+
const ALL_ROW_VALUE = "__all__";
|
|
456
|
+
const CLEAR_ROW_VALUE = "__clear__";
|
|
457
|
+
|
|
458
|
+
/* ------------------------------------------------------------------------ */
|
|
459
|
+
/* Component */
|
|
460
|
+
/* ------------------------------------------------------------------------ */
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Multi-select filter for one dimension: a field-look trigger that reads its
|
|
464
|
+
* picks, over a Popover + Command menu that stays open across picks. Values
|
|
465
|
+
* combine as OR.
|
|
466
|
+
*/
|
|
467
|
+
const MultiSelectFilter = React.forwardRef<
|
|
468
|
+
HTMLButtonElement,
|
|
469
|
+
MultiSelectFilterProps
|
|
470
|
+
>(
|
|
471
|
+
(
|
|
472
|
+
{
|
|
473
|
+
className,
|
|
474
|
+
dataHook,
|
|
475
|
+
label,
|
|
476
|
+
options,
|
|
477
|
+
value: controlledValue,
|
|
478
|
+
defaultValue = [],
|
|
479
|
+
onValueChange,
|
|
480
|
+
open: controlledOpen,
|
|
481
|
+
onOpenChange,
|
|
482
|
+
size = "default",
|
|
483
|
+
icon,
|
|
484
|
+
maxVisibleLabels = 1,
|
|
485
|
+
allLabel,
|
|
486
|
+
allIcon,
|
|
487
|
+
allCount,
|
|
488
|
+
searchThreshold = 10,
|
|
489
|
+
searchable,
|
|
490
|
+
collapseFullSelection = true,
|
|
491
|
+
disabled = false,
|
|
492
|
+
clearLabel,
|
|
493
|
+
summaryLabel,
|
|
494
|
+
searchPlaceholder,
|
|
495
|
+
emptyLabel,
|
|
496
|
+
emptyDescription,
|
|
497
|
+
clearSearchAriaLabel,
|
|
498
|
+
presentation = "menu",
|
|
499
|
+
doneLabel,
|
|
500
|
+
drawerDescription,
|
|
501
|
+
trackingEl,
|
|
502
|
+
trackingLabel,
|
|
503
|
+
"aria-label": ariaLabelProp,
|
|
504
|
+
...props
|
|
505
|
+
},
|
|
506
|
+
ref
|
|
507
|
+
) => {
|
|
508
|
+
const state = useMultiSelectFilterState({
|
|
509
|
+
collapseFullSelection,
|
|
510
|
+
defaultValue,
|
|
511
|
+
onOpenChange,
|
|
512
|
+
onValueChange,
|
|
513
|
+
open: controlledOpen,
|
|
514
|
+
options,
|
|
515
|
+
value: controlledValue,
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
const belowMd = useIsBelowBreakpoint("md");
|
|
519
|
+
const useDrawer =
|
|
520
|
+
presentation === "drawer" || (presentation === "auto" && belowMd);
|
|
521
|
+
const idPrefix = React.useId();
|
|
522
|
+
|
|
523
|
+
/* The search query, so the All row and Clear can step aside while the
|
|
524
|
+
person types. It resets with every open. */
|
|
525
|
+
const [query, setQuery] = React.useState("");
|
|
526
|
+
const normalizedQuery = query.trim().toLowerCase();
|
|
527
|
+
const searching = normalizedQuery.length > 0;
|
|
528
|
+
/* Case-insensitive substring match on label and value — predictable for
|
|
529
|
+
a list of names, unlike a fuzzy score. */
|
|
530
|
+
const visibleOptions = searching
|
|
531
|
+
? state.orderedOptions.filter(
|
|
532
|
+
(option) =>
|
|
533
|
+
option.label.toLowerCase().includes(normalizedQuery) ||
|
|
534
|
+
option.value.toLowerCase().includes(normalizedQuery)
|
|
535
|
+
)
|
|
536
|
+
: state.orderedOptions;
|
|
537
|
+
const commandRef = React.useRef<HTMLDivElement>(null);
|
|
538
|
+
const showSearch = searchable ?? options.length > searchThreshold;
|
|
539
|
+
|
|
540
|
+
/* The trigger's own record of the selection, in pick order — the first
|
|
541
|
+
label stays put while more are added. A value with no option (stale
|
|
542
|
+
after the options changed) is not a pick — it neither reads on the
|
|
543
|
+
trigger nor counts. */
|
|
544
|
+
const labelByValue = new Map(
|
|
545
|
+
options.map((option) => [option.value, option.label])
|
|
546
|
+
);
|
|
547
|
+
const pickedLabels = state.value.flatMap((picked) => {
|
|
548
|
+
const pickedLabel = labelByValue.get(picked);
|
|
549
|
+
return pickedLabel === undefined ? [] : [pickedLabel];
|
|
550
|
+
});
|
|
551
|
+
const count = pickedLabels.length;
|
|
552
|
+
/* "All" means no narrowing, which is an empty selection — so the row is
|
|
553
|
+
checked when nothing is picked, not when everything is. */
|
|
554
|
+
const allSelected = state.value.length === 0;
|
|
555
|
+
const resolvedAllCount =
|
|
556
|
+
allCount ??
|
|
557
|
+
(options.length > 0 &&
|
|
558
|
+
options.every((option) => option.count !== undefined)
|
|
559
|
+
? options.reduce((sum, option) => sum + (option.count ?? 0), 0)
|
|
560
|
+
: undefined);
|
|
561
|
+
const visibleLabels = Math.max(1, maxVisibleLabels);
|
|
562
|
+
|
|
563
|
+
let triggerText: string;
|
|
564
|
+
let mutedText = false;
|
|
565
|
+
if (count === 0) {
|
|
566
|
+
triggerText = allLabel ?? label;
|
|
567
|
+
/* Without an All label the dimension name stands in as a placeholder —
|
|
568
|
+
muted, like a Select with nothing chosen. With one, "All sources" is
|
|
569
|
+
a real state and reads at full strength. */
|
|
570
|
+
mutedText = allLabel === undefined;
|
|
571
|
+
} else if (count <= visibleLabels) {
|
|
572
|
+
triggerText = pickedLabels.join(", ");
|
|
573
|
+
} else {
|
|
574
|
+
/* One pass with a function replacer: a label containing "$&" or a
|
|
575
|
+
placeholder is inserted verbatim, never re-expanded. */
|
|
576
|
+
const fills: Record<string, string> = {
|
|
577
|
+
"{count}": String(count),
|
|
578
|
+
"{labels}": pickedLabels.slice(0, visibleLabels).join(", "),
|
|
579
|
+
"{rest}": String(count - visibleLabels),
|
|
580
|
+
};
|
|
581
|
+
triggerText = (summaryLabel ?? "{labels} +{rest}").replace(
|
|
582
|
+
/\{(count|labels|rest)\}/g,
|
|
583
|
+
(token) => fills[token] ?? token
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/* The accessible name always starts with the dimension; the summary may
|
|
588
|
+
hide picks ("Google +2"), so every pick is spelled out. */
|
|
589
|
+
const computedAriaLabel =
|
|
590
|
+
count > 0
|
|
591
|
+
? `${label}: ${pickedLabels.join(", ")}`
|
|
592
|
+
: allLabel !== undefined
|
|
593
|
+
? `${label}: ${allLabel}`
|
|
594
|
+
: label;
|
|
595
|
+
|
|
596
|
+
/* The glyph follows the selection unless the consumer pins one: the All
|
|
597
|
+
mark at zero, then the first pick's own. */
|
|
598
|
+
const firstPicked = state.value
|
|
599
|
+
.map((picked) => options.find((option) => option.value === picked))
|
|
600
|
+
.find((option) => option !== undefined);
|
|
601
|
+
const resolvedIcon =
|
|
602
|
+
icon ?? (count > 0 ? firstPicked?.icon : allIcon) ?? undefined;
|
|
603
|
+
|
|
604
|
+
const handleOpenChange = (next: boolean) => {
|
|
605
|
+
if (!next) setQuery("");
|
|
606
|
+
state.onOpenChange(next);
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
const triggerNode = (
|
|
610
|
+
<button
|
|
611
|
+
ref={ref}
|
|
612
|
+
aria-label={ariaLabelProp ?? computedAriaLabel}
|
|
613
|
+
className={cn(multiSelectFilterTriggerVariants({ size }), className)}
|
|
614
|
+
data-hook={dataHook}
|
|
615
|
+
data-slot="multi-select-filter"
|
|
616
|
+
data-tracking-el={trackingEl ?? "multi-select-filter"}
|
|
617
|
+
data-tracking-label={trackingLabel}
|
|
618
|
+
disabled={disabled}
|
|
619
|
+
type="button"
|
|
620
|
+
{...props}
|
|
621
|
+
>
|
|
622
|
+
{resolvedIcon ? (
|
|
623
|
+
<span
|
|
624
|
+
aria-hidden="true"
|
|
625
|
+
className="flex shrink-0 items-center"
|
|
626
|
+
data-hook={`${dataHook}-icon`}
|
|
627
|
+
>
|
|
628
|
+
{resolvedIcon}
|
|
629
|
+
</span>
|
|
630
|
+
) : null}
|
|
631
|
+
<span
|
|
632
|
+
className={cn(mutedText && "text-muted-foreground")}
|
|
633
|
+
data-hook={`${dataHook}-summary`}
|
|
634
|
+
>
|
|
635
|
+
{triggerText}
|
|
636
|
+
</span>
|
|
637
|
+
{/* The affordance the field look promises: a menu opens here. */}
|
|
638
|
+
<ChevronDown aria-hidden="true" className="text-muted-foreground" />
|
|
639
|
+
</button>
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
if (useDrawer) {
|
|
643
|
+
const allId = `${idPrefix}-all`;
|
|
644
|
+
return (
|
|
645
|
+
<Drawer open={state.open} onOpenChange={handleOpenChange}>
|
|
646
|
+
<DrawerTrigger asChild>{triggerNode}</DrawerTrigger>
|
|
647
|
+
<DrawerContent dataHook={`${dataHook}-drawer`}>
|
|
648
|
+
<DrawerHeader>
|
|
649
|
+
<DrawerTitle>{label}</DrawerTitle>
|
|
650
|
+
<DrawerDescription className="sr-only">
|
|
651
|
+
{drawerDescription ?? "Picks apply as you make them."}
|
|
652
|
+
</DrawerDescription>
|
|
653
|
+
</DrawerHeader>
|
|
654
|
+
{/* The search field sits outside the scroll area: an overflow
|
|
655
|
+
container clips the input's 3px focus ring, and the field
|
|
656
|
+
stays in reach while the list scrolls. */}
|
|
657
|
+
{showSearch ? (
|
|
658
|
+
<DrawerBody className="shrink-0 pb-4">
|
|
659
|
+
<Input
|
|
660
|
+
aria-label={searchPlaceholder ?? "Search options..."}
|
|
661
|
+
dataHook={`${dataHook}-search`}
|
|
662
|
+
placeholder={searchPlaceholder ?? "Search options..."}
|
|
663
|
+
type="search"
|
|
664
|
+
value={query}
|
|
665
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
666
|
+
/>
|
|
667
|
+
</DrawerBody>
|
|
668
|
+
) : null}
|
|
669
|
+
{/* min-h-0 + overflow: the list scrolls inside the drawer's
|
|
670
|
+
height cap; pt-1 keeps the first row's focus ring inside the
|
|
671
|
+
clip. The All row and Clear scroll with the list. */}
|
|
672
|
+
<DrawerBody className="flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto pt-1 pb-2">
|
|
673
|
+
{searching && visibleOptions.length === 0 ? (
|
|
674
|
+
/* The compact no-match form of Patterns → Empty States: the
|
|
675
|
+
muted chip, the message line, one quiet hint — the same
|
|
676
|
+
form CommandEmpty draws in the menu. */
|
|
677
|
+
<div
|
|
678
|
+
className="text-body-sm flex flex-col items-center gap-1 py-6 text-center"
|
|
679
|
+
data-hook={`${dataHook}-empty`}
|
|
680
|
+
>
|
|
681
|
+
<span
|
|
682
|
+
aria-hidden="true"
|
|
683
|
+
className={cn(emptyChipStyles(), "mb-1")}
|
|
684
|
+
>
|
|
685
|
+
<SearchX />
|
|
686
|
+
</span>
|
|
687
|
+
<p>{emptyLabel ?? "No options found."}</p>
|
|
688
|
+
<p className="text-muted-foreground">
|
|
689
|
+
{emptyDescription ?? "Try another search."}
|
|
690
|
+
</p>
|
|
691
|
+
</div>
|
|
692
|
+
) : (
|
|
693
|
+
<>
|
|
694
|
+
{allLabel !== undefined && !searching ? (
|
|
695
|
+
<div className="flex items-center gap-2">
|
|
696
|
+
<Checkbox
|
|
697
|
+
checked={allSelected}
|
|
698
|
+
dataHook={`${dataHook}-option-all`}
|
|
699
|
+
id={allId}
|
|
700
|
+
onCheckedChange={state.clear}
|
|
701
|
+
/>
|
|
702
|
+
<Label className="flex-1 gap-2" htmlFor={allId}>
|
|
703
|
+
{allIcon ? (
|
|
704
|
+
<span
|
|
705
|
+
aria-hidden="true"
|
|
706
|
+
className="flex shrink-0 items-center [&_svg]:size-4"
|
|
707
|
+
>
|
|
708
|
+
{allIcon}
|
|
709
|
+
</span>
|
|
710
|
+
) : null}
|
|
711
|
+
{allLabel}
|
|
712
|
+
</Label>
|
|
713
|
+
{resolvedAllCount !== undefined ? (
|
|
714
|
+
<OptionCount count={resolvedAllCount} />
|
|
715
|
+
) : null}
|
|
716
|
+
</div>
|
|
717
|
+
) : null}
|
|
718
|
+
<div className="grid grid-cols-2 gap-x-4 gap-y-3">
|
|
719
|
+
{visibleOptions.map((option, index) => {
|
|
720
|
+
const active = state.selectedSet.has(option.value);
|
|
721
|
+
const optionDisabled =
|
|
722
|
+
option.disabled || (option.count === 0 && !active);
|
|
723
|
+
/* Keyed on the index, not the value — an id may not
|
|
724
|
+
contain whitespace, and a value may. */
|
|
725
|
+
const optionId = `${idPrefix}-${index}`;
|
|
726
|
+
return (
|
|
727
|
+
<div
|
|
728
|
+
key={option.value}
|
|
729
|
+
className="flex items-center gap-2"
|
|
730
|
+
>
|
|
731
|
+
<Checkbox
|
|
732
|
+
checked={active}
|
|
733
|
+
dataHook={`${dataHook}-option-${option.value}`}
|
|
734
|
+
disabled={optionDisabled}
|
|
735
|
+
id={optionId}
|
|
736
|
+
onCheckedChange={() =>
|
|
737
|
+
state.toggleOption(option.value)
|
|
738
|
+
}
|
|
739
|
+
/>
|
|
740
|
+
<Label className="flex-1 gap-2" htmlFor={optionId}>
|
|
741
|
+
{option.icon ? (
|
|
742
|
+
<span
|
|
743
|
+
aria-hidden="true"
|
|
744
|
+
className="flex shrink-0 items-center [&_svg]:size-4"
|
|
745
|
+
data-hook={`${dataHook}-option-${option.value}-icon`}
|
|
746
|
+
>
|
|
747
|
+
{option.icon}
|
|
748
|
+
</span>
|
|
749
|
+
) : null}
|
|
750
|
+
{option.label}
|
|
751
|
+
</Label>
|
|
752
|
+
</div>
|
|
753
|
+
);
|
|
754
|
+
})}
|
|
755
|
+
</div>
|
|
756
|
+
</>
|
|
757
|
+
)}
|
|
758
|
+
{state.value.length > 0 && !searching ? (
|
|
759
|
+
<Button
|
|
760
|
+
dataHook={`${dataHook}-clear`}
|
|
761
|
+
variant="ghost"
|
|
762
|
+
onClick={state.clear}
|
|
763
|
+
>
|
|
764
|
+
{clearLabel ?? "Clear filters"}
|
|
765
|
+
</Button>
|
|
766
|
+
) : null}
|
|
767
|
+
</DrawerBody>
|
|
768
|
+
<DrawerFooter>
|
|
769
|
+
<DrawerClose asChild>
|
|
770
|
+
<Button dataHook={`${dataHook}-done`}>
|
|
771
|
+
{doneLabel ?? "Done"}
|
|
772
|
+
</Button>
|
|
773
|
+
</DrawerClose>
|
|
774
|
+
</DrawerFooter>
|
|
775
|
+
</DrawerContent>
|
|
776
|
+
</Drawer>
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
return (
|
|
781
|
+
<Popover open={state.open} onOpenChange={handleOpenChange}>
|
|
782
|
+
<PopoverTrigger asChild>{triggerNode}</PopoverTrigger>
|
|
783
|
+
{/* p-0: the Command surface owns the popover's inner spacing. The
|
|
784
|
+
menu hugs its widest option between a floor and a ceiling rather
|
|
785
|
+
than following the trigger, whose width is only its text. */}
|
|
786
|
+
<PopoverContent
|
|
787
|
+
align="start"
|
|
788
|
+
aria-label={label}
|
|
789
|
+
className="w-max max-w-80 min-w-60 p-0"
|
|
790
|
+
dataHook={`${dataHook}-popover`}
|
|
791
|
+
/* cmdk listens for the arrow keys on its root, so without a search
|
|
792
|
+
input to take focus the root itself must — otherwise the menu
|
|
793
|
+
opens but the keyboard goes nowhere. */
|
|
794
|
+
onOpenAutoFocus={(event) => {
|
|
795
|
+
if (showSearch) return;
|
|
796
|
+
event.preventDefault();
|
|
797
|
+
commandRef.current?.focus();
|
|
798
|
+
}}
|
|
799
|
+
>
|
|
800
|
+
{/* shouldFilter off: the list is narrowed above, so cmdk only
|
|
801
|
+
provides the roving highlight and the keyboard. */}
|
|
802
|
+
<Command
|
|
803
|
+
ref={commandRef}
|
|
804
|
+
dataHook={`${dataHook}-command`}
|
|
805
|
+
shouldFilter={false}
|
|
806
|
+
>
|
|
807
|
+
{showSearch ? (
|
|
808
|
+
/* Autofocused on open — the person came to type, not scroll;
|
|
809
|
+
focus moves into a menu they deliberately opened. */
|
|
810
|
+
<CommandInput
|
|
811
|
+
// eslint-disable-next-line jsx-a11y/no-autofocus
|
|
812
|
+
autoFocus
|
|
813
|
+
clearSearchAriaLabel={clearSearchAriaLabel ?? "Clear search"}
|
|
814
|
+
dataHook={`${dataHook}-search`}
|
|
815
|
+
placeholder={searchPlaceholder ?? "Search options..."}
|
|
816
|
+
onValueChange={setQuery}
|
|
817
|
+
/>
|
|
818
|
+
) : null}
|
|
819
|
+
{/* aria-multiselectable + per-option aria-checked: cmdk's
|
|
820
|
+
aria-selected tracks the highlight, so checkedness needs its
|
|
821
|
+
own attribute for assistive tech. */}
|
|
822
|
+
<CommandList aria-multiselectable="true">
|
|
823
|
+
<CommandEmpty
|
|
824
|
+
description={emptyDescription ?? "Try another search."}
|
|
825
|
+
icon={<SearchX />}
|
|
826
|
+
>
|
|
827
|
+
{emptyLabel ?? "No options found."}
|
|
828
|
+
</CommandEmpty>
|
|
829
|
+
<CommandGroup>
|
|
830
|
+
{allLabel !== undefined && !searching ? (
|
|
831
|
+
<CommandItem
|
|
832
|
+
aria-checked={allSelected}
|
|
833
|
+
dataHook={`${dataHook}-option-all`}
|
|
834
|
+
value={ALL_ROW_VALUE}
|
|
835
|
+
onSelect={state.clear}
|
|
836
|
+
>
|
|
837
|
+
<OptionCheck checked={allSelected} />
|
|
838
|
+
{allIcon ? (
|
|
839
|
+
<span
|
|
840
|
+
aria-hidden="true"
|
|
841
|
+
className="flex shrink-0 items-center [&_svg]:size-4"
|
|
842
|
+
>
|
|
843
|
+
{allIcon}
|
|
844
|
+
</span>
|
|
845
|
+
) : null}
|
|
846
|
+
{allLabel}
|
|
847
|
+
{resolvedAllCount !== undefined ? (
|
|
848
|
+
<OptionCount count={resolvedAllCount} />
|
|
849
|
+
) : null}
|
|
850
|
+
</CommandItem>
|
|
851
|
+
) : null}
|
|
852
|
+
{visibleOptions.map((option) => {
|
|
853
|
+
const active = state.selectedSet.has(option.value);
|
|
854
|
+
/* A zero-count option disables, never hides — the dimension
|
|
855
|
+
keeps its full shape on screen. A pick whose count dropped
|
|
856
|
+
to zero under another filter stays enabled, so it can
|
|
857
|
+
still be unticked. */
|
|
858
|
+
const optionDisabled =
|
|
859
|
+
option.disabled || (option.count === 0 && !active);
|
|
860
|
+
return (
|
|
861
|
+
<CommandItem
|
|
862
|
+
key={option.value}
|
|
863
|
+
aria-checked={active}
|
|
864
|
+
dataHook={`${dataHook}-option-${option.value}`}
|
|
865
|
+
disabled={optionDisabled}
|
|
866
|
+
value={option.value}
|
|
867
|
+
onSelect={() => state.toggleOption(option.value)}
|
|
868
|
+
>
|
|
869
|
+
<OptionCheck checked={active} />
|
|
870
|
+
{option.icon ? (
|
|
871
|
+
<span
|
|
872
|
+
aria-hidden="true"
|
|
873
|
+
className="flex shrink-0 items-center [&_svg]:size-4"
|
|
874
|
+
data-hook={`${dataHook}-option-${option.value}-icon`}
|
|
875
|
+
>
|
|
876
|
+
{option.icon}
|
|
877
|
+
</span>
|
|
878
|
+
) : null}
|
|
879
|
+
{option.label}
|
|
880
|
+
{option.count !== undefined ? (
|
|
881
|
+
<OptionCount count={option.count} />
|
|
882
|
+
) : null}
|
|
883
|
+
</CommandItem>
|
|
884
|
+
);
|
|
885
|
+
})}
|
|
886
|
+
</CommandGroup>
|
|
887
|
+
{state.value.length > 0 && !searching ? (
|
|
888
|
+
/* A bordered group, not CommandSeparator — role="separator"
|
|
889
|
+
is not a permitted child of the cmdk listbox. */
|
|
890
|
+
<CommandGroup className="border-border border-t">
|
|
891
|
+
<CommandItem
|
|
892
|
+
className="justify-center"
|
|
893
|
+
dataHook={`${dataHook}-clear`}
|
|
894
|
+
value={CLEAR_ROW_VALUE}
|
|
895
|
+
onSelect={state.clear}
|
|
896
|
+
>
|
|
897
|
+
{clearLabel ?? "Clear filters"}
|
|
898
|
+
</CommandItem>
|
|
899
|
+
</CommandGroup>
|
|
900
|
+
) : null}
|
|
901
|
+
</CommandList>
|
|
902
|
+
</Command>
|
|
903
|
+
</PopoverContent>
|
|
904
|
+
</Popover>
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
);
|
|
908
|
+
|
|
909
|
+
MultiSelectFilter.displayName = "MultiSelectFilter";
|
|
910
|
+
|
|
911
|
+
export { MultiSelectFilter, multiSelectFilterTriggerVariants };
|
|
912
|
+
export type { MultiSelectFilterOption, MultiSelectFilterProps };
|