@brightlocal/ui-components 0.1.3 → 0.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 +2 -1
- package/dist/accordion.cjs +1 -1
- package/dist/accordion.js +1 -1
- package/dist/alert.cjs +1 -1
- package/dist/alert.js +1 -1
- package/dist/blocks/data-table.cjs +1 -1
- package/dist/blocks/data-table.js +1 -1
- package/dist/blocks/sidebar.cjs +1 -1
- package/dist/blocks/sidebar.d.ts +37 -306
- package/dist/blocks/sidebar.d.ts.map +1 -1
- package/dist/blocks/sidebar.js +392 -489
- package/dist/breadcrumb.cjs +1 -1
- package/dist/breadcrumb.js +1 -1
- package/dist/button.cjs +1 -1
- package/dist/button.js +1 -1
- package/dist/calendar.cjs +1 -1
- package/dist/calendar.d.ts +4 -1
- package/dist/calendar.d.ts.map +1 -1
- package/dist/calendar.js +33 -33
- package/dist/centred-layout.cjs +1 -1
- package/dist/centred-layout.d.ts +5 -18
- package/dist/centred-layout.d.ts.map +1 -1
- package/dist/centred-layout.js +21 -23
- package/dist/checkbox.cjs +1 -1
- package/dist/checkbox.d.ts.map +1 -1
- package/dist/checkbox.js +19 -19
- package/dist/combobox.cjs +1 -1
- package/dist/combobox.d.ts +75 -67
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +241 -194
- package/dist/command.cjs +1 -1
- package/dist/command.js +1 -1
- package/dist/context-menu.cjs +1 -1
- package/dist/context-menu.js +1 -1
- package/dist/date-picker.cjs +1 -1
- package/dist/date-picker.js +1 -1
- package/dist/dialog.cjs +1 -1
- package/dist/dialog.js +1 -1
- package/dist/dropdown-menu.cjs +1 -1
- package/dist/dropdown-menu.js +1 -1
- package/dist/field.cjs +1 -1
- package/dist/field.d.ts +3 -0
- package/dist/field.d.ts.map +1 -1
- package/dist/field.js +5 -4
- package/dist/index.cjs +1 -1
- package/dist/index.js +361 -366
- package/dist/input-group.cjs +1 -1
- package/dist/input-group.d.ts +5 -0
- package/dist/input-group.d.ts.map +1 -1
- package/dist/input-group.js +47 -39
- package/dist/input-otp.cjs +1 -1
- package/dist/input-otp.d.ts +6 -0
- package/dist/input-otp.d.ts.map +1 -1
- package/dist/input-otp.js +79 -76
- package/dist/input-password.cjs +1 -1
- package/dist/input-password.d.ts +4 -0
- package/dist/input-password.d.ts.map +1 -1
- package/dist/input-password.js +32 -31
- package/dist/input.cjs +1 -1
- package/dist/input.d.ts +4 -0
- package/dist/input.d.ts.map +1 -1
- package/dist/input.js +26 -24
- package/dist/link.cjs +1 -1
- package/dist/link.js +1 -1
- package/dist/logo.cjs +1 -1
- package/dist/logo.d.ts +6 -4
- package/dist/logo.d.ts.map +1 -1
- package/dist/logo.js +17 -20
- package/dist/menubar.cjs +1 -1
- package/dist/menubar.d.ts +3 -0
- package/dist/menubar.d.ts.map +1 -1
- package/dist/menubar.js +31 -30
- package/dist/navigation-menu.cjs +1 -1
- package/dist/navigation-menu.d.ts +6 -54
- package/dist/navigation-menu.d.ts.map +1 -1
- package/dist/navigation-menu.js +73 -84
- package/dist/pagination.cjs +1 -1
- package/dist/pagination.d.ts +8 -10
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +78 -86
- package/dist/radio-group.cjs +1 -1
- package/dist/radio-group.d.ts +10 -23
- package/dist/radio-group.d.ts.map +1 -1
- package/dist/radio-group.js +47 -55
- package/dist/resizable.cjs +1 -1
- package/dist/resizable.d.ts +19 -28
- package/dist/resizable.d.ts.map +1 -1
- package/dist/resizable.js +24 -40
- package/dist/select.cjs +1 -1
- package/dist/select.d.ts +3 -1
- package/dist/select.d.ts.map +1 -1
- package/dist/select.js +57 -56
- package/dist/separator.cjs +1 -1
- package/dist/separator.d.ts +4 -4
- package/dist/separator.d.ts.map +1 -1
- package/dist/separator.js +19 -21
- package/dist/sheet.cjs +1 -1
- package/dist/sheet.d.ts +5 -10
- package/dist/sheet.d.ts.map +1 -1
- package/dist/sheet.js +24 -26
- package/dist/sonner.cjs +1 -1
- package/dist/sonner.d.ts +17 -20
- package/dist/sonner.d.ts.map +1 -1
- package/dist/sonner.js +42 -45
- package/dist/switch.cjs +1 -1
- package/dist/switch.d.ts +2 -12
- package/dist/switch.d.ts.map +1 -1
- package/dist/switch.js +37 -41
- package/dist/table.cjs +1 -1
- package/dist/table.d.ts +16 -12
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +56 -58
- package/dist/tabs.d.ts +27 -3
- package/dist/tabs.d.ts.map +1 -1
- package/dist/textarea.cjs +1 -1
- package/dist/textarea.d.ts +4 -0
- package/dist/textarea.d.ts.map +1 -1
- package/dist/textarea.js +19 -18
- package/dist/toggle-group.cjs +1 -1
- package/dist/toggle-group.d.ts +57 -7
- package/dist/toggle-group.d.ts.map +1 -1
- package/dist/toggle-group.js +60 -67
- package/dist/toggle.cjs +1 -1
- package/dist/toggle.d.ts +12 -26
- package/dist/toggle.d.ts.map +1 -1
- package/dist/toggle.js +18 -22
- package/package.json +2 -2
- package/src/blocks/data-table.tsx +1 -1
- package/src/blocks/sidebar.tsx +157 -532
- package/src/components/accordion.tsx +1 -1
- package/src/components/alert.tsx +1 -1
- package/src/components/breadcrumb.tsx +1 -1
- package/src/components/button.tsx +1 -1
- package/src/components/calendar.tsx +6 -4
- package/src/components/centred-layout.tsx +11 -24
- package/src/components/checkbox.tsx +5 -6
- package/src/components/combobox.tsx +481 -301
- package/src/components/command.tsx +1 -1
- package/src/components/context-menu.tsx +1 -1
- package/src/components/date-picker.tsx +1 -1
- package/src/components/dialog.tsx +1 -1
- package/src/components/dropdown-menu.tsx +1 -1
- package/src/components/field.tsx +2 -0
- package/src/components/input-group.tsx +14 -2
- package/src/components/input-otp.tsx +17 -2
- package/src/components/input-password.tsx +7 -2
- package/src/components/input.tsx +11 -3
- package/src/components/link.tsx +1 -1
- package/src/components/logo.tsx +10 -8
- package/src/components/menubar.tsx +3 -1
- package/src/components/navigation-menu.tsx +37 -99
- package/src/components/pagination.tsx +13 -19
- package/src/components/radio-group.tsx +28 -38
- package/src/components/resizable.tsx +5 -43
- package/src/components/select.tsx +8 -3
- package/src/components/separator.tsx +5 -5
- package/src/components/sheet.tsx +6 -11
- package/src/components/sonner.tsx +24 -27
- package/src/components/switch.figma.tsx +7 -11
- package/src/components/switch.tsx +9 -19
- package/src/components/table.tsx +23 -18
- package/src/components/tabs.tsx +27 -3
- package/src/components/textarea.tsx +12 -5
- package/src/components/toggle-group.tsx +115 -86
- package/src/components/toggle.figma.tsx +13 -19
- package/src/components/toggle.tsx +32 -48
|
@@ -3,11 +3,9 @@
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import {
|
|
5
5
|
ChevronsUpDownIcon,
|
|
6
|
-
ChevronDownIcon,
|
|
7
6
|
CheckIcon,
|
|
8
|
-
|
|
9
|
-
} from "
|
|
10
|
-
import { Checkbox } from "@brightlocal/ui-components/checkbox";
|
|
7
|
+
LoaderIcon,
|
|
8
|
+
} from "@brightlocal/icons";
|
|
11
9
|
import { cva, type VariantProps } from "class-variance-authority";
|
|
12
10
|
|
|
13
11
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
@@ -23,62 +21,147 @@ import {
|
|
|
23
21
|
CommandInput,
|
|
24
22
|
CommandItem,
|
|
25
23
|
CommandList,
|
|
24
|
+
CommandSeparator,
|
|
26
25
|
} from "@brightlocal/ui-components/command";
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
/* -------------------------------------------------------------------------------------------------
|
|
28
|
+
* Combobox Context
|
|
29
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
30
|
+
|
|
31
|
+
interface ComboboxContextValue {
|
|
32
|
+
open: boolean;
|
|
33
|
+
setOpen: (open: boolean) => void;
|
|
34
|
+
value: string;
|
|
35
|
+
onValueChange: (value: string) => void;
|
|
36
|
+
shouldFilter: boolean;
|
|
37
|
+
disabled: boolean;
|
|
38
|
+
/** Map of value -> label for displaying selected value */
|
|
39
|
+
itemLabels: Map<string, string>;
|
|
40
|
+
registerItem: (value: string, label: string) => void;
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
/** Menu item with icon */
|
|
47
|
-
ICON = "icon",
|
|
48
|
-
/** Menu item with avatar */
|
|
49
|
-
AVATAR = "avatar",
|
|
43
|
+
const ComboboxContext = React.createContext<ComboboxContextValue | null>(null);
|
|
44
|
+
|
|
45
|
+
function useComboboxContext() {
|
|
46
|
+
const context = React.useContext(ComboboxContext);
|
|
47
|
+
if (!context) {
|
|
48
|
+
throw new Error("Combobox components must be used within a <Combobox>");
|
|
49
|
+
}
|
|
50
|
+
return context;
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
|
|
53
|
+
/* -------------------------------------------------------------------------------------------------
|
|
54
|
+
* Combobox Root
|
|
55
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
56
|
+
|
|
57
|
+
export interface ComboboxProps {
|
|
58
|
+
/** Controlled selected value */
|
|
59
|
+
value?: string;
|
|
60
|
+
/** Callback when value changes */
|
|
61
|
+
onValueChange?: (value: string) => void;
|
|
62
|
+
/** Controlled open state */
|
|
63
|
+
open?: boolean;
|
|
64
|
+
/** Callback when open state changes */
|
|
65
|
+
onOpenChange?: (open: boolean) => void;
|
|
66
|
+
/** Whether to use cmdk's built-in filtering. Set to false for async search. */
|
|
67
|
+
shouldFilter?: boolean;
|
|
68
|
+
/** Disabled state */
|
|
69
|
+
disabled?: boolean;
|
|
70
|
+
/** Children */
|
|
71
|
+
children: React.ReactNode;
|
|
65
72
|
}
|
|
66
73
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
74
|
+
function Combobox({
|
|
75
|
+
value: controlledValue,
|
|
76
|
+
onValueChange: controlledOnValueChange,
|
|
77
|
+
open: controlledOpen,
|
|
78
|
+
onOpenChange: controlledOnOpenChange,
|
|
79
|
+
shouldFilter = true,
|
|
80
|
+
disabled = false,
|
|
81
|
+
children,
|
|
82
|
+
}: ComboboxProps) {
|
|
83
|
+
const [uncontrolledValue, setUncontrolledValue] = React.useState("");
|
|
84
|
+
const [uncontrolledOpen, setUncontrolledOpen] = React.useState(false);
|
|
85
|
+
const [itemLabels, setItemLabels] = React.useState<Map<string, string>>(
|
|
86
|
+
() => new Map()
|
|
87
|
+
);
|
|
70
88
|
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
"bg-accent flex size-5 shrink-0 items-center justify-center rounded-full text-xs font-medium";
|
|
89
|
+
const isValueControlled = controlledValue !== undefined;
|
|
90
|
+
const isOpenControlled = controlledOpen !== undefined;
|
|
74
91
|
|
|
75
|
-
const
|
|
92
|
+
const value = isValueControlled ? controlledValue : uncontrolledValue;
|
|
93
|
+
const open = isOpenControlled ? controlledOpen : uncontrolledOpen;
|
|
94
|
+
|
|
95
|
+
const setOpen = React.useCallback(
|
|
96
|
+
(newOpen: boolean) => {
|
|
97
|
+
if (!isOpenControlled) {
|
|
98
|
+
setUncontrolledOpen(newOpen);
|
|
99
|
+
}
|
|
100
|
+
controlledOnOpenChange?.(newOpen);
|
|
101
|
+
},
|
|
102
|
+
[isOpenControlled, controlledOnOpenChange]
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const onValueChange = React.useCallback(
|
|
106
|
+
(newValue: string) => {
|
|
107
|
+
if (!isValueControlled) {
|
|
108
|
+
setUncontrolledValue(newValue);
|
|
109
|
+
}
|
|
110
|
+
controlledOnValueChange?.(newValue);
|
|
111
|
+
},
|
|
112
|
+
[isValueControlled, controlledOnValueChange]
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const registerItem = React.useCallback((itemValue: string, label: string) => {
|
|
116
|
+
setItemLabels((prev) => {
|
|
117
|
+
if (prev.get(itemValue) === label) return prev;
|
|
118
|
+
const next = new Map(prev);
|
|
119
|
+
next.set(itemValue, label);
|
|
120
|
+
return next;
|
|
121
|
+
});
|
|
122
|
+
}, []);
|
|
123
|
+
|
|
124
|
+
const contextValue = React.useMemo<ComboboxContextValue>(
|
|
125
|
+
() => ({
|
|
126
|
+
open,
|
|
127
|
+
setOpen,
|
|
128
|
+
value,
|
|
129
|
+
onValueChange,
|
|
130
|
+
shouldFilter,
|
|
131
|
+
disabled,
|
|
132
|
+
itemLabels,
|
|
133
|
+
registerItem,
|
|
134
|
+
}),
|
|
135
|
+
[
|
|
136
|
+
open,
|
|
137
|
+
setOpen,
|
|
138
|
+
value,
|
|
139
|
+
onValueChange,
|
|
140
|
+
shouldFilter,
|
|
141
|
+
disabled,
|
|
142
|
+
itemLabels,
|
|
143
|
+
registerItem,
|
|
144
|
+
]
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<ComboboxContext.Provider value={contextValue}>
|
|
149
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
150
|
+
{children}
|
|
151
|
+
</Popover>
|
|
152
|
+
</ComboboxContext.Provider>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* -------------------------------------------------------------------------------------------------
|
|
157
|
+
* Combobox Trigger
|
|
158
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
76
159
|
|
|
77
160
|
const comboboxTriggerVariants = cva(
|
|
78
161
|
[
|
|
79
162
|
// Base layout
|
|
80
|
-
"relative flex w-full items-center justify-
|
|
81
|
-
"px-4 py-2",
|
|
163
|
+
"relative flex w-full items-center justify-between gap-2",
|
|
164
|
+
"px-4 py-2 h-9",
|
|
82
165
|
|
|
83
166
|
// Base appearance
|
|
84
167
|
"border border-input rounded-md outline-none",
|
|
@@ -94,7 +177,7 @@ const comboboxTriggerVariants = cva(
|
|
|
94
177
|
// Hover state
|
|
95
178
|
"hover:bg-accent",
|
|
96
179
|
|
|
97
|
-
// Focus state
|
|
180
|
+
// Focus state
|
|
98
181
|
"focus-visible:outline-none focus-visible:border-ring dark:focus-visible:border-neutral-500 focus-visible:bg-background",
|
|
99
182
|
"focus-visible:shadow-default",
|
|
100
183
|
|
|
@@ -105,22 +188,17 @@ const comboboxTriggerVariants = cva(
|
|
|
105
188
|
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
106
189
|
"disabled:hover:bg-neutral-50",
|
|
107
190
|
|
|
108
|
-
// Transitions
|
|
191
|
+
// Transitions
|
|
109
192
|
"transition-[background-color,border-color,box-shadow] duration-200 ease-out",
|
|
110
193
|
|
|
111
194
|
// Icon styles
|
|
112
195
|
"[&_svg]:pointer-events-none [&_svg]:shrink-0",
|
|
113
196
|
"[&_svg:not([class*='size-'])]:size-4",
|
|
114
|
-
"[&_svg:not([class*='text-'])]:text-muted-foreground",
|
|
115
197
|
],
|
|
116
198
|
{
|
|
117
199
|
variants: {
|
|
118
200
|
variant: {
|
|
119
|
-
default: [
|
|
120
|
-
withGroupLabel: [
|
|
121
|
-
// Only variant-specific styles (removed duplicates from base)
|
|
122
|
-
"self-stretch",
|
|
123
|
-
],
|
|
201
|
+
default: [],
|
|
124
202
|
},
|
|
125
203
|
},
|
|
126
204
|
defaultVariants: {
|
|
@@ -129,291 +207,393 @@ const comboboxTriggerVariants = cva(
|
|
|
129
207
|
}
|
|
130
208
|
);
|
|
131
209
|
|
|
132
|
-
interface
|
|
133
|
-
|
|
134
|
-
|
|
210
|
+
export interface ComboboxTriggerProps
|
|
211
|
+
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
212
|
+
VariantProps<typeof comboboxTriggerVariants> {
|
|
135
213
|
/** Data hook for automated testing (REQUIRED) */
|
|
136
214
|
dataHook: string;
|
|
215
|
+
/** Accessible label for the combobox trigger. Use either aria-label or aria-labelledby for accessibility. */
|
|
216
|
+
"aria-label"?: string;
|
|
217
|
+
/** ID reference to a label element that provides the accessible name */
|
|
218
|
+
"aria-labelledby"?: string;
|
|
137
219
|
/** Tracking element identifier for analytics */
|
|
138
220
|
trackingEl?: string;
|
|
139
221
|
/** Tracking label for analytics context */
|
|
140
222
|
trackingLabel?: string;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const ComboboxTrigger = React.forwardRef<
|
|
226
|
+
HTMLButtonElement,
|
|
227
|
+
ComboboxTriggerProps
|
|
228
|
+
>(
|
|
229
|
+
(
|
|
230
|
+
{
|
|
231
|
+
className,
|
|
232
|
+
variant,
|
|
233
|
+
dataHook,
|
|
234
|
+
trackingEl,
|
|
235
|
+
trackingLabel,
|
|
236
|
+
children,
|
|
237
|
+
...props
|
|
238
|
+
},
|
|
239
|
+
ref
|
|
240
|
+
) => {
|
|
241
|
+
const { open, disabled, value } = useComboboxContext();
|
|
242
|
+
const listboxId = React.useId();
|
|
243
|
+
const isPlaceholder = !value;
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
<PopoverTrigger asChild>
|
|
247
|
+
<button
|
|
248
|
+
ref={ref}
|
|
249
|
+
aria-controls={open ? listboxId : undefined}
|
|
250
|
+
aria-expanded={open}
|
|
251
|
+
aria-haspopup="listbox"
|
|
252
|
+
className={cn(comboboxTriggerVariants({ variant }), className)}
|
|
253
|
+
data-hook={dataHook}
|
|
254
|
+
data-placeholder={isPlaceholder}
|
|
255
|
+
data-slot="combobox-trigger"
|
|
256
|
+
data-state={open ? "open" : "closed"}
|
|
257
|
+
data-tracking-el={trackingEl || "combobox-trigger"}
|
|
258
|
+
data-tracking-label={trackingLabel}
|
|
259
|
+
disabled={disabled || props.disabled}
|
|
260
|
+
role="combobox"
|
|
261
|
+
type="button"
|
|
262
|
+
{...props}
|
|
263
|
+
>
|
|
264
|
+
{children}
|
|
265
|
+
<ChevronsUpDownIcon
|
|
266
|
+
aria-hidden="true"
|
|
267
|
+
className="text-muted-foreground ml-auto size-4 shrink-0"
|
|
268
|
+
/>
|
|
269
|
+
</button>
|
|
270
|
+
</PopoverTrigger>
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
);
|
|
274
|
+
ComboboxTrigger.displayName = "ComboboxTrigger";
|
|
275
|
+
|
|
276
|
+
/* -------------------------------------------------------------------------------------------------
|
|
277
|
+
* Combobox Value
|
|
278
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
279
|
+
|
|
280
|
+
export interface ComboboxValueProps {
|
|
147
281
|
/** Placeholder text when no value is selected */
|
|
148
282
|
placeholder?: string;
|
|
149
|
-
/**
|
|
150
|
-
groupLabel?: string;
|
|
151
|
-
/** Disabled state */
|
|
152
|
-
disabled?: boolean;
|
|
153
|
-
/** Empty state message when no results found */
|
|
154
|
-
emptyMessage?: string;
|
|
155
|
-
/** Search placeholder text */
|
|
156
|
-
searchPlaceholder?: string;
|
|
157
|
-
/** Whether to show the avatar */
|
|
158
|
-
showAvatar?: boolean;
|
|
159
|
-
/** Avatar image source URL */
|
|
160
|
-
avatarSrc?: string;
|
|
161
|
-
/** Avatar fallback text/initials */
|
|
162
|
-
avatarFallback?: string;
|
|
163
|
-
/** Custom className for the trigger button */
|
|
283
|
+
/** Custom className */
|
|
164
284
|
className?: string;
|
|
165
|
-
/** Menu item type for dropdown items */
|
|
166
|
-
menuItemType?: ComboboxMenuItemType;
|
|
167
|
-
/** Accessible label for the combobox (used when no visible label is provided) */
|
|
168
|
-
"aria-label"?: string;
|
|
169
285
|
}
|
|
170
286
|
|
|
287
|
+
function ComboboxValue({ placeholder, className }: ComboboxValueProps) {
|
|
288
|
+
const { value, itemLabels } = useComboboxContext();
|
|
289
|
+
const displayText = value ? itemLabels.get(value) || value : placeholder;
|
|
290
|
+
|
|
291
|
+
return (
|
|
292
|
+
<span
|
|
293
|
+
className={cn(
|
|
294
|
+
"text-foreground overflow-hidden text-sm leading-normal font-medium text-ellipsis whitespace-nowrap",
|
|
295
|
+
className
|
|
296
|
+
)}
|
|
297
|
+
data-slot="combobox-value"
|
|
298
|
+
>
|
|
299
|
+
{displayText}
|
|
300
|
+
</span>
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/* -------------------------------------------------------------------------------------------------
|
|
305
|
+
* Combobox Content
|
|
306
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
307
|
+
|
|
308
|
+
export interface ComboboxContentProps {
|
|
309
|
+
/** Data hook for automated testing */
|
|
310
|
+
dataHook?: string;
|
|
311
|
+
/** Custom className */
|
|
312
|
+
className?: string;
|
|
313
|
+
/** Children */
|
|
314
|
+
children: React.ReactNode;
|
|
315
|
+
/** Alignment of the popover relative to trigger */
|
|
316
|
+
align?: "start" | "center" | "end";
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function ComboboxContent({
|
|
320
|
+
dataHook,
|
|
321
|
+
className,
|
|
322
|
+
children,
|
|
323
|
+
align = "start",
|
|
324
|
+
}: ComboboxContentProps) {
|
|
325
|
+
const { shouldFilter } = useComboboxContext();
|
|
326
|
+
|
|
327
|
+
return (
|
|
328
|
+
<PopoverContent
|
|
329
|
+
align={align}
|
|
330
|
+
className={cn("w-[var(--radix-popover-trigger-width)] p-0", className)}
|
|
331
|
+
data-slot="combobox-popover"
|
|
332
|
+
>
|
|
333
|
+
<Command
|
|
334
|
+
className="border-0 shadow-none"
|
|
335
|
+
data-slot="combobox-command"
|
|
336
|
+
dataHook={dataHook || "combobox-command"}
|
|
337
|
+
shouldFilter={shouldFilter}
|
|
338
|
+
>
|
|
339
|
+
{children}
|
|
340
|
+
</Command>
|
|
341
|
+
</PopoverContent>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/* -------------------------------------------------------------------------------------------------
|
|
346
|
+
* Combobox Input
|
|
347
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
348
|
+
|
|
349
|
+
export interface ComboboxInputProps
|
|
350
|
+
extends React.ComponentProps<typeof CommandInput> {
|
|
351
|
+
/** Current input value. Use for controlled/async search. */
|
|
352
|
+
value?: string;
|
|
353
|
+
/** Called when input value changes. Use to trigger async search requests. */
|
|
354
|
+
onValueChange?: (value: string) => void;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const ComboboxInput = React.forwardRef<
|
|
358
|
+
React.ComponentRef<typeof CommandInput>,
|
|
359
|
+
ComboboxInputProps
|
|
360
|
+
>((props, ref) => {
|
|
361
|
+
return <CommandInput ref={ref} data-slot="combobox-input" {...props} />;
|
|
362
|
+
});
|
|
363
|
+
ComboboxInput.displayName = "ComboboxInput";
|
|
364
|
+
|
|
365
|
+
/* -------------------------------------------------------------------------------------------------
|
|
366
|
+
* Combobox List
|
|
367
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
368
|
+
|
|
369
|
+
export type ComboboxListProps = React.ComponentProps<typeof CommandList>;
|
|
370
|
+
|
|
371
|
+
const ComboboxList = React.forwardRef<
|
|
372
|
+
React.ComponentRef<typeof CommandList>,
|
|
373
|
+
ComboboxListProps
|
|
374
|
+
>((props, ref) => {
|
|
375
|
+
return <CommandList ref={ref} data-slot="combobox-list" {...props} />;
|
|
376
|
+
});
|
|
377
|
+
ComboboxList.displayName = "ComboboxList";
|
|
378
|
+
|
|
379
|
+
/* -------------------------------------------------------------------------------------------------
|
|
380
|
+
* Combobox Empty
|
|
381
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
382
|
+
|
|
383
|
+
export type ComboboxEmptyProps = React.ComponentProps<typeof CommandEmpty>;
|
|
384
|
+
|
|
385
|
+
const ComboboxEmpty = React.forwardRef<
|
|
386
|
+
React.ComponentRef<typeof CommandEmpty>,
|
|
387
|
+
ComboboxEmptyProps
|
|
388
|
+
>((props, ref) => {
|
|
389
|
+
return <CommandEmpty ref={ref} data-slot="combobox-empty" {...props} />;
|
|
390
|
+
});
|
|
391
|
+
ComboboxEmpty.displayName = "ComboboxEmpty";
|
|
392
|
+
|
|
393
|
+
/* -------------------------------------------------------------------------------------------------
|
|
394
|
+
* Combobox Group
|
|
395
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
396
|
+
|
|
397
|
+
export type ComboboxGroupProps = React.ComponentProps<typeof CommandGroup>;
|
|
398
|
+
|
|
399
|
+
const ComboboxGroup = React.forwardRef<
|
|
400
|
+
React.ComponentRef<typeof CommandGroup>,
|
|
401
|
+
ComboboxGroupProps
|
|
402
|
+
>((props, ref) => {
|
|
403
|
+
return <CommandGroup ref={ref} data-slot="combobox-group" {...props} />;
|
|
404
|
+
});
|
|
405
|
+
ComboboxGroup.displayName = "ComboboxGroup";
|
|
406
|
+
|
|
407
|
+
/* -------------------------------------------------------------------------------------------------
|
|
408
|
+
* Combobox Item
|
|
409
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
410
|
+
|
|
171
411
|
/**
|
|
172
|
-
*
|
|
412
|
+
* Highlights matching text within a string
|
|
173
413
|
*/
|
|
174
|
-
function
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
414
|
+
function highlightText(
|
|
415
|
+
text: string,
|
|
416
|
+
highlight: string,
|
|
417
|
+
highlightClassName: string
|
|
418
|
+
): React.ReactNode {
|
|
419
|
+
if (!highlight || !highlight.trim()) {
|
|
420
|
+
return text;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const regex = new RegExp(`(${highlight.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})`, "gi");
|
|
424
|
+
const parts = text.split(regex);
|
|
425
|
+
|
|
185
426
|
return (
|
|
186
|
-
<span
|
|
187
|
-
{
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
427
|
+
<span>
|
|
428
|
+
{parts.map((part, index) =>
|
|
429
|
+
index % 2 === 1 ? (
|
|
430
|
+
<span key={index} className={highlightClassName}>
|
|
431
|
+
{part}
|
|
432
|
+
</span>
|
|
433
|
+
) : (
|
|
434
|
+
<React.Fragment key={index}>{part}</React.Fragment>
|
|
435
|
+
)
|
|
191
436
|
)}
|
|
192
437
|
</span>
|
|
193
438
|
);
|
|
194
439
|
}
|
|
195
440
|
|
|
196
|
-
|
|
441
|
+
export interface ComboboxItemProps
|
|
442
|
+
extends Omit<React.ComponentProps<typeof CommandItem>, "onSelect"> {
|
|
443
|
+
/** The value of the item (required) */
|
|
444
|
+
value: string;
|
|
445
|
+
/** Display label - if not provided, uses children text content */
|
|
446
|
+
label?: string;
|
|
447
|
+
/** Data hook for automated testing */
|
|
448
|
+
dataHook?: string;
|
|
449
|
+
/** Text to highlight in the item (typically the search query) */
|
|
450
|
+
highlight?: string;
|
|
451
|
+
/** Class name for highlighted text */
|
|
452
|
+
highlightClassName?: string;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const ComboboxItem = React.forwardRef<
|
|
456
|
+
React.ComponentRef<typeof CommandItem>,
|
|
457
|
+
ComboboxItemProps
|
|
458
|
+
>(
|
|
197
459
|
(
|
|
198
460
|
{
|
|
199
|
-
|
|
200
|
-
|
|
461
|
+
value: itemValue,
|
|
462
|
+
label,
|
|
201
463
|
dataHook,
|
|
202
|
-
|
|
203
|
-
trackingLabel,
|
|
204
|
-
items,
|
|
205
|
-
value,
|
|
206
|
-
onValueChange,
|
|
207
|
-
placeholder = "Select an option...",
|
|
208
|
-
groupLabel,
|
|
209
|
-
disabled,
|
|
210
|
-
emptyMessage = "No results found.",
|
|
211
|
-
searchPlaceholder = "Search",
|
|
212
|
-
showAvatar = false,
|
|
213
|
-
avatarSrc,
|
|
214
|
-
avatarFallback,
|
|
464
|
+
children,
|
|
215
465
|
className,
|
|
216
|
-
|
|
217
|
-
|
|
466
|
+
highlight,
|
|
467
|
+
highlightClassName,
|
|
468
|
+
...props
|
|
218
469
|
},
|
|
219
470
|
ref
|
|
220
471
|
) => {
|
|
221
|
-
const
|
|
222
|
-
const
|
|
472
|
+
const { value, onValueChange, setOpen, registerItem } = useComboboxContext();
|
|
473
|
+
const isSelected = value === itemValue;
|
|
223
474
|
|
|
224
|
-
|
|
225
|
-
const
|
|
475
|
+
// Extract text content from children for label registration
|
|
476
|
+
const childText = React.useMemo(() => {
|
|
477
|
+
if (label) return label;
|
|
478
|
+
if (typeof children === "string") return children;
|
|
479
|
+
if (typeof children === "number") return String(children);
|
|
480
|
+
return itemValue;
|
|
481
|
+
}, [label, children, itemValue]);
|
|
226
482
|
|
|
227
|
-
//
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
483
|
+
// Keep latest registerItem in a ref to avoid effect dependency churn
|
|
484
|
+
const registerItemRef = React.useRef(registerItem);
|
|
485
|
+
React.useEffect(() => {
|
|
486
|
+
registerItemRef.current = registerItem;
|
|
487
|
+
}, [registerItem]);
|
|
488
|
+
|
|
489
|
+
// Register item label for ComboboxValue display
|
|
490
|
+
React.useEffect(() => {
|
|
491
|
+
registerItemRef.current(itemValue, childText);
|
|
492
|
+
}, [itemValue, childText]);
|
|
232
493
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (!acc[group]) {
|
|
237
|
-
acc[group] = [];
|
|
238
|
-
}
|
|
239
|
-
acc[group].push(item);
|
|
240
|
-
return acc;
|
|
241
|
-
},
|
|
242
|
-
{} as Record<string, ComboboxItem[]>
|
|
243
|
-
);
|
|
244
|
-
}, [items, variant]);
|
|
245
|
-
|
|
246
|
-
const handleSelect = (currentValue: string) => {
|
|
247
|
-
const newValue = currentValue === value ? "" : currentValue;
|
|
248
|
-
onValueChange?.(newValue);
|
|
494
|
+
const handleSelect = () => {
|
|
495
|
+
const newValue = itemValue === value ? "" : itemValue;
|
|
496
|
+
onValueChange(newValue);
|
|
249
497
|
setOpen(false);
|
|
250
498
|
};
|
|
251
499
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
return (
|
|
261
|
-
<>
|
|
262
|
-
<Checkbox
|
|
263
|
-
checked={isSelected}
|
|
264
|
-
className="pointer-events-none self-center"
|
|
265
|
-
dataHook={`${dataHook}-checkbox-${item.value}`}
|
|
266
|
-
/>
|
|
267
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
268
|
-
</>
|
|
269
|
-
);
|
|
270
|
-
case ComboboxMenuItemType.ICON:
|
|
271
|
-
return (
|
|
272
|
-
<>
|
|
273
|
-
{ItemIcon && (
|
|
274
|
-
<ItemIcon className="text-muted-foreground size-4 shrink-0" />
|
|
275
|
-
)}
|
|
276
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
277
|
-
{checkIcon}
|
|
278
|
-
</>
|
|
279
|
-
);
|
|
280
|
-
case ComboboxMenuItemType.AVATAR:
|
|
281
|
-
return (
|
|
282
|
-
<>
|
|
283
|
-
<ComboboxAvatar
|
|
284
|
-
alt={item.label}
|
|
285
|
-
dataSlot="menu-item-avatar"
|
|
286
|
-
fallback={item.avatarFallback}
|
|
287
|
-
src={item.avatarSrc}
|
|
288
|
-
/>
|
|
289
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
290
|
-
{checkIcon}
|
|
291
|
-
</>
|
|
292
|
-
);
|
|
293
|
-
case ComboboxMenuItemType.SIMPLE:
|
|
294
|
-
default:
|
|
295
|
-
return (
|
|
296
|
-
<>
|
|
297
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
298
|
-
{checkIcon}
|
|
299
|
-
</>
|
|
300
|
-
);
|
|
500
|
+
// Render children with optional highlighting
|
|
501
|
+
const renderContent = () => {
|
|
502
|
+
if (highlight && typeof children === "string") {
|
|
503
|
+
return highlightText(
|
|
504
|
+
children,
|
|
505
|
+
highlight,
|
|
506
|
+
highlightClassName || "text-sm leading-normal font-semibold"
|
|
507
|
+
);
|
|
301
508
|
}
|
|
509
|
+
return children;
|
|
302
510
|
};
|
|
303
511
|
|
|
304
512
|
return (
|
|
305
|
-
<
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
data-tracking-el={trackingEl || `combobox-${variant}`}
|
|
323
|
-
data-tracking-label={trackingLabel}
|
|
324
|
-
disabled={disabled}
|
|
325
|
-
id={id}
|
|
326
|
-
role="combobox"
|
|
327
|
-
type="button"
|
|
328
|
-
>
|
|
329
|
-
{variant === ComboboxVariant.WITH_GROUP_LABEL ? (
|
|
330
|
-
<>
|
|
331
|
-
<div className="flex w-full flex-col items-start pr-6">
|
|
332
|
-
{groupLabel && (
|
|
333
|
-
<span className="text-muted-foreground overflow-hidden font-sans text-xs leading-4 font-normal text-ellipsis">
|
|
334
|
-
{groupLabel}
|
|
335
|
-
</span>
|
|
336
|
-
)}
|
|
337
|
-
<span className="text-foreground overflow-hidden font-sans text-sm leading-5 font-medium text-ellipsis whitespace-nowrap">
|
|
338
|
-
{selectedItem ? selectedItem.label : placeholder}
|
|
339
|
-
</span>
|
|
340
|
-
</div>
|
|
341
|
-
<ChevronDownIcon className="text-muted-foreground absolute top-1/2 right-4 size-4 shrink-0 -translate-y-1/2" />
|
|
342
|
-
</>
|
|
343
|
-
) : (
|
|
344
|
-
<>
|
|
345
|
-
{showAvatar && (
|
|
346
|
-
<ComboboxAvatar
|
|
347
|
-
alt={selectedItem?.label}
|
|
348
|
-
dataSlot="combobox-avatar"
|
|
349
|
-
fallback={avatarFallback}
|
|
350
|
-
src={avatarSrc}
|
|
351
|
-
/>
|
|
352
|
-
)}
|
|
353
|
-
<span className="text-foreground overflow-hidden text-sm leading-normal font-medium text-ellipsis whitespace-nowrap">
|
|
354
|
-
{selectedItem ? selectedItem.label : placeholder}
|
|
355
|
-
</span>
|
|
356
|
-
<ChevronsUpDownIcon className="text-muted-foreground ml-auto size-4 shrink-0" />
|
|
357
|
-
</>
|
|
358
|
-
)}
|
|
359
|
-
</button>
|
|
360
|
-
</PopoverTrigger>
|
|
361
|
-
|
|
362
|
-
<PopoverContent
|
|
363
|
-
align="start"
|
|
364
|
-
className="w-[var(--radix-popover-trigger-width)] p-0"
|
|
365
|
-
data-slot="combobox-popover"
|
|
366
|
-
>
|
|
367
|
-
<Command
|
|
368
|
-
className="border-0 shadow-none"
|
|
369
|
-
data-slot="combobox-command"
|
|
370
|
-
dataHook={`${dataHook}-command`}
|
|
371
|
-
id={listboxId}
|
|
372
|
-
>
|
|
373
|
-
<CommandInput
|
|
374
|
-
dataHook={`${dataHook}-search`}
|
|
375
|
-
placeholder={searchPlaceholder}
|
|
376
|
-
/>
|
|
377
|
-
<CommandList>
|
|
378
|
-
<CommandEmpty>{emptyMessage}</CommandEmpty>
|
|
379
|
-
{variant === ComboboxVariant.WITH_GROUP_LABEL &&
|
|
380
|
-
Object.keys(groupedItems).length > 1 ? (
|
|
381
|
-
Object.entries(groupedItems).map(([group, groupItems]) => (
|
|
382
|
-
<CommandGroup key={group} heading={group}>
|
|
383
|
-
{groupItems.map((item) => (
|
|
384
|
-
<CommandItem
|
|
385
|
-
key={item.value}
|
|
386
|
-
dataHook={`${dataHook}-item-${item.value}`}
|
|
387
|
-
value={item.value}
|
|
388
|
-
onSelect={handleSelect}
|
|
389
|
-
>
|
|
390
|
-
{renderMenuItem(item, value === item.value)}
|
|
391
|
-
</CommandItem>
|
|
392
|
-
))}
|
|
393
|
-
</CommandGroup>
|
|
394
|
-
))
|
|
395
|
-
) : (
|
|
396
|
-
<CommandGroup>
|
|
397
|
-
{items.map((item) => (
|
|
398
|
-
<CommandItem
|
|
399
|
-
key={item.value}
|
|
400
|
-
dataHook={`${dataHook}-item-${item.value}`}
|
|
401
|
-
value={item.value}
|
|
402
|
-
onSelect={handleSelect}
|
|
403
|
-
>
|
|
404
|
-
{renderMenuItem(item, value === item.value)}
|
|
405
|
-
</CommandItem>
|
|
406
|
-
))}
|
|
407
|
-
</CommandGroup>
|
|
408
|
-
)}
|
|
409
|
-
</CommandList>
|
|
410
|
-
</Command>
|
|
411
|
-
</PopoverContent>
|
|
412
|
-
</Popover>
|
|
513
|
+
<CommandItem
|
|
514
|
+
ref={ref}
|
|
515
|
+
className={cn("justify-between", className)}
|
|
516
|
+
data-slot="combobox-item"
|
|
517
|
+
dataHook={dataHook}
|
|
518
|
+
value={itemValue}
|
|
519
|
+
onSelect={handleSelect}
|
|
520
|
+
{...props}
|
|
521
|
+
>
|
|
522
|
+
{renderContent()}
|
|
523
|
+
{isSelected && (
|
|
524
|
+
<CheckIcon
|
|
525
|
+
aria-hidden="true"
|
|
526
|
+
className="text-muted-foreground size-4 shrink-0"
|
|
527
|
+
/>
|
|
528
|
+
)}
|
|
529
|
+
</CommandItem>
|
|
413
530
|
);
|
|
414
531
|
}
|
|
415
532
|
);
|
|
533
|
+
ComboboxItem.displayName = "ComboboxItem";
|
|
534
|
+
|
|
535
|
+
/* -------------------------------------------------------------------------------------------------
|
|
536
|
+
* Combobox Separator
|
|
537
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
538
|
+
|
|
539
|
+
export type ComboboxSeparatorProps = React.ComponentProps<
|
|
540
|
+
typeof CommandSeparator
|
|
541
|
+
>;
|
|
542
|
+
|
|
543
|
+
const ComboboxSeparator = React.forwardRef<
|
|
544
|
+
React.ComponentRef<typeof CommandSeparator>,
|
|
545
|
+
ComboboxSeparatorProps
|
|
546
|
+
>((props, ref) => {
|
|
547
|
+
return (
|
|
548
|
+
<CommandSeparator ref={ref} data-slot="combobox-separator" {...props} />
|
|
549
|
+
);
|
|
550
|
+
});
|
|
551
|
+
ComboboxSeparator.displayName = "ComboboxSeparator";
|
|
552
|
+
|
|
553
|
+
/* -------------------------------------------------------------------------------------------------
|
|
554
|
+
* Combobox Loading
|
|
555
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
556
|
+
|
|
557
|
+
export interface ComboboxLoadingProps {
|
|
558
|
+
/** Custom className */
|
|
559
|
+
className?: string;
|
|
560
|
+
/** Children - custom loading content */
|
|
561
|
+
children?: React.ReactNode;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function ComboboxLoading({ className, children }: ComboboxLoadingProps) {
|
|
565
|
+
return (
|
|
566
|
+
<div
|
|
567
|
+
className={cn(
|
|
568
|
+
"flex items-center justify-center gap-2 py-6 text-sm text-muted-foreground",
|
|
569
|
+
className
|
|
570
|
+
)}
|
|
571
|
+
data-slot="combobox-loading"
|
|
572
|
+
>
|
|
573
|
+
{children || (
|
|
574
|
+
<>
|
|
575
|
+
<LoaderIcon aria-hidden="true" className="size-4 animate-spin" />
|
|
576
|
+
Loading...
|
|
577
|
+
</>
|
|
578
|
+
)}
|
|
579
|
+
</div>
|
|
580
|
+
);
|
|
581
|
+
}
|
|
416
582
|
|
|
417
|
-
|
|
583
|
+
/* -------------------------------------------------------------------------------------------------
|
|
584
|
+
* Exports
|
|
585
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
418
586
|
|
|
419
|
-
export {
|
|
587
|
+
export {
|
|
588
|
+
Combobox,
|
|
589
|
+
ComboboxTrigger,
|
|
590
|
+
ComboboxValue,
|
|
591
|
+
ComboboxContent,
|
|
592
|
+
ComboboxInput,
|
|
593
|
+
ComboboxList,
|
|
594
|
+
ComboboxEmpty,
|
|
595
|
+
ComboboxGroup,
|
|
596
|
+
ComboboxItem,
|
|
597
|
+
ComboboxSeparator,
|
|
598
|
+
ComboboxLoading,
|
|
599
|
+
};
|