@brightlocal/ui-components 0.1.4 → 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/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 -59
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +241 -191
- package/dist/index.cjs +1 -1
- package/dist/index.js +354 -344
- 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/logo.cjs +1 -1
- package/dist/logo.js +13 -13
- package/dist/radio-group.cjs +1 -1
- package/dist/radio-group.d.ts +4 -0
- package/dist/radio-group.d.ts.map +1 -1
- package/dist/radio-group.js +49 -47
- 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/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.cjs +1 -1
- package/dist/toggle.d.ts.map +1 -1
- package/dist/toggle.js +17 -17
- package/package.json +1 -1
- package/src/components/checkbox.tsx +4 -5
- package/src/components/combobox.tsx +482 -294
- package/src/components/input-group.tsx +14 -2
- package/src/components/input-otp.tsx +16 -1
- package/src/components/input-password.tsx +6 -1
- package/src/components/input.tsx +11 -3
- package/src/components/logo.tsx +1 -1
- package/src/components/radio-group.tsx +11 -0
- package/src/components/select.tsx +7 -2
- package/src/components/textarea.tsx +12 -5
- package/src/components/toggle.tsx +13 -15
|
@@ -3,11 +3,9 @@
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import {
|
|
5
5
|
ChevronsUpDownIcon,
|
|
6
|
-
ChevronDownIcon,
|
|
7
6
|
CheckIcon,
|
|
8
|
-
|
|
7
|
+
LoaderIcon,
|
|
9
8
|
} from "@brightlocal/icons";
|
|
10
|
-
import { Checkbox } from "@brightlocal/ui-components/checkbox";
|
|
11
9
|
import { cva, type VariantProps } from "class-variance-authority";
|
|
12
10
|
|
|
13
11
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
@@ -23,54 +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
|
-
* - `withGroupLabel`: Variant with group label showing category above selection
|
|
32
|
-
*/
|
|
33
|
-
export type ComboboxVariant = "default" | "withGroupLabel";
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Menu item type variants for Combobox dropdown items
|
|
37
|
-
* - `simple`: Simple text-only menu item
|
|
38
|
-
* - `checkbox`: Menu item with checkbox
|
|
39
|
-
* - `icon`: Menu item with icon
|
|
40
|
-
* - `avatar`: Menu item with avatar
|
|
41
|
-
*/
|
|
42
|
-
export type ComboboxMenuItemType = "simple" | "checkbox" | "icon" | "avatar";
|
|
27
|
+
/* -------------------------------------------------------------------------------------------------
|
|
28
|
+
* Combobox Context
|
|
29
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
43
30
|
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
interface ComboboxContextValue {
|
|
32
|
+
open: boolean;
|
|
33
|
+
setOpen: (open: boolean) => void;
|
|
46
34
|
value: string;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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;
|
|
41
|
+
}
|
|
42
|
+
|
|
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;
|
|
57
51
|
}
|
|
58
52
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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;
|
|
72
|
+
}
|
|
73
|
+
|
|
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
|
+
);
|
|
88
|
+
|
|
89
|
+
const isValueControlled = controlledValue !== undefined;
|
|
90
|
+
const isOpenControlled = controlledOpen !== undefined;
|
|
91
|
+
|
|
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
|
+
);
|
|
62
146
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
147
|
+
return (
|
|
148
|
+
<ComboboxContext.Provider value={contextValue}>
|
|
149
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
150
|
+
{children}
|
|
151
|
+
</Popover>
|
|
152
|
+
</ComboboxContext.Provider>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
66
155
|
|
|
67
|
-
|
|
156
|
+
/* -------------------------------------------------------------------------------------------------
|
|
157
|
+
* Combobox Trigger
|
|
158
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
68
159
|
|
|
69
160
|
const comboboxTriggerVariants = cva(
|
|
70
161
|
[
|
|
71
162
|
// Base layout
|
|
72
|
-
"relative flex w-full items-center justify-
|
|
73
|
-
"px-4 py-2",
|
|
163
|
+
"relative flex w-full items-center justify-between gap-2",
|
|
164
|
+
"px-4 py-2 h-9",
|
|
74
165
|
|
|
75
166
|
// Base appearance
|
|
76
167
|
"border border-input rounded-md outline-none",
|
|
@@ -86,7 +177,7 @@ const comboboxTriggerVariants = cva(
|
|
|
86
177
|
// Hover state
|
|
87
178
|
"hover:bg-accent",
|
|
88
179
|
|
|
89
|
-
// Focus state
|
|
180
|
+
// Focus state
|
|
90
181
|
"focus-visible:outline-none focus-visible:border-ring dark:focus-visible:border-neutral-500 focus-visible:bg-background",
|
|
91
182
|
"focus-visible:shadow-default",
|
|
92
183
|
|
|
@@ -97,22 +188,17 @@ const comboboxTriggerVariants = cva(
|
|
|
97
188
|
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
98
189
|
"disabled:hover:bg-neutral-50",
|
|
99
190
|
|
|
100
|
-
// Transitions
|
|
191
|
+
// Transitions
|
|
101
192
|
"transition-[background-color,border-color,box-shadow] duration-200 ease-out",
|
|
102
193
|
|
|
103
194
|
// Icon styles
|
|
104
195
|
"[&_svg]:pointer-events-none [&_svg]:shrink-0",
|
|
105
196
|
"[&_svg:not([class*='size-'])]:size-4",
|
|
106
|
-
"[&_svg:not([class*='text-'])]:text-muted-foreground",
|
|
107
197
|
],
|
|
108
198
|
{
|
|
109
199
|
variants: {
|
|
110
200
|
variant: {
|
|
111
|
-
default: [
|
|
112
|
-
withGroupLabel: [
|
|
113
|
-
// Only variant-specific styles (removed duplicates from base)
|
|
114
|
-
"self-stretch",
|
|
115
|
-
],
|
|
201
|
+
default: [],
|
|
116
202
|
},
|
|
117
203
|
},
|
|
118
204
|
defaultVariants: {
|
|
@@ -121,291 +207,393 @@ const comboboxTriggerVariants = cva(
|
|
|
121
207
|
}
|
|
122
208
|
);
|
|
123
209
|
|
|
124
|
-
interface
|
|
125
|
-
|
|
126
|
-
|
|
210
|
+
export interface ComboboxTriggerProps
|
|
211
|
+
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
212
|
+
VariantProps<typeof comboboxTriggerVariants> {
|
|
127
213
|
/** Data hook for automated testing (REQUIRED) */
|
|
128
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;
|
|
129
219
|
/** Tracking element identifier for analytics */
|
|
130
220
|
trackingEl?: string;
|
|
131
221
|
/** Tracking label for analytics context */
|
|
132
222
|
trackingLabel?: string;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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 {
|
|
139
281
|
/** Placeholder text when no value is selected */
|
|
140
282
|
placeholder?: string;
|
|
141
|
-
/**
|
|
142
|
-
groupLabel?: string;
|
|
143
|
-
/** Disabled state */
|
|
144
|
-
disabled?: boolean;
|
|
145
|
-
/** Empty state message when no results found */
|
|
146
|
-
emptyMessage?: string;
|
|
147
|
-
/** Search placeholder text */
|
|
148
|
-
searchPlaceholder?: string;
|
|
149
|
-
/** Whether to show the avatar */
|
|
150
|
-
showAvatar?: boolean;
|
|
151
|
-
/** Avatar image source URL */
|
|
152
|
-
avatarSrc?: string;
|
|
153
|
-
/** Avatar fallback text/initials */
|
|
154
|
-
avatarFallback?: string;
|
|
155
|
-
/** Custom className for the trigger button */
|
|
283
|
+
/** Custom className */
|
|
156
284
|
className?: string;
|
|
157
|
-
/** Menu item type for dropdown items */
|
|
158
|
-
menuItemType?: ComboboxMenuItemType;
|
|
159
|
-
/** Accessible label for the combobox (used when no visible label is provided) */
|
|
160
|
-
"aria-label"?: string;
|
|
161
285
|
}
|
|
162
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
|
+
|
|
163
411
|
/**
|
|
164
|
-
*
|
|
412
|
+
* Highlights matching text within a string
|
|
165
413
|
*/
|
|
166
|
-
function
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
+
|
|
177
426
|
return (
|
|
178
|
-
<span
|
|
179
|
-
{
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
+
)
|
|
183
436
|
)}
|
|
184
437
|
</span>
|
|
185
438
|
);
|
|
186
439
|
}
|
|
187
440
|
|
|
188
|
-
|
|
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
|
+
>(
|
|
189
459
|
(
|
|
190
460
|
{
|
|
191
|
-
|
|
192
|
-
|
|
461
|
+
value: itemValue,
|
|
462
|
+
label,
|
|
193
463
|
dataHook,
|
|
194
|
-
|
|
195
|
-
trackingLabel,
|
|
196
|
-
items,
|
|
197
|
-
value,
|
|
198
|
-
onValueChange,
|
|
199
|
-
placeholder = "Select an option...",
|
|
200
|
-
groupLabel,
|
|
201
|
-
disabled,
|
|
202
|
-
emptyMessage = "No results found.",
|
|
203
|
-
searchPlaceholder = "Search",
|
|
204
|
-
showAvatar = false,
|
|
205
|
-
avatarSrc,
|
|
206
|
-
avatarFallback,
|
|
464
|
+
children,
|
|
207
465
|
className,
|
|
208
|
-
|
|
209
|
-
|
|
466
|
+
highlight,
|
|
467
|
+
highlightClassName,
|
|
468
|
+
...props
|
|
210
469
|
},
|
|
211
470
|
ref
|
|
212
471
|
) => {
|
|
213
|
-
const
|
|
214
|
-
const
|
|
472
|
+
const { value, onValueChange, setOpen, registerItem } = useComboboxContext();
|
|
473
|
+
const isSelected = value === itemValue;
|
|
215
474
|
|
|
216
|
-
|
|
217
|
-
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]);
|
|
218
482
|
|
|
219
|
-
//
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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]);
|
|
224
493
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (!acc[group]) {
|
|
229
|
-
acc[group] = [];
|
|
230
|
-
}
|
|
231
|
-
acc[group].push(item);
|
|
232
|
-
return acc;
|
|
233
|
-
},
|
|
234
|
-
{} as Record<string, ComboboxItem[]>
|
|
235
|
-
);
|
|
236
|
-
}, [items, variant]);
|
|
237
|
-
|
|
238
|
-
const handleSelect = (currentValue: string) => {
|
|
239
|
-
const newValue = currentValue === value ? "" : currentValue;
|
|
240
|
-
onValueChange?.(newValue);
|
|
494
|
+
const handleSelect = () => {
|
|
495
|
+
const newValue = itemValue === value ? "" : itemValue;
|
|
496
|
+
onValueChange(newValue);
|
|
241
497
|
setOpen(false);
|
|
242
498
|
};
|
|
243
499
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
return (
|
|
253
|
-
<>
|
|
254
|
-
<Checkbox
|
|
255
|
-
checked={isSelected}
|
|
256
|
-
className="pointer-events-none self-center"
|
|
257
|
-
dataHook={`${dataHook}-checkbox-${item.value}`}
|
|
258
|
-
/>
|
|
259
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
260
|
-
</>
|
|
261
|
-
);
|
|
262
|
-
case "icon":
|
|
263
|
-
return (
|
|
264
|
-
<>
|
|
265
|
-
{ItemIcon && (
|
|
266
|
-
<ItemIcon className="text-muted-foreground size-4 shrink-0" />
|
|
267
|
-
)}
|
|
268
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
269
|
-
{checkIcon}
|
|
270
|
-
</>
|
|
271
|
-
);
|
|
272
|
-
case "avatar":
|
|
273
|
-
return (
|
|
274
|
-
<>
|
|
275
|
-
<ComboboxAvatar
|
|
276
|
-
alt={item.label}
|
|
277
|
-
dataSlot="menu-item-avatar"
|
|
278
|
-
fallback={item.avatarFallback}
|
|
279
|
-
src={item.avatarSrc}
|
|
280
|
-
/>
|
|
281
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
282
|
-
{checkIcon}
|
|
283
|
-
</>
|
|
284
|
-
);
|
|
285
|
-
case "simple":
|
|
286
|
-
default:
|
|
287
|
-
return (
|
|
288
|
-
<>
|
|
289
|
-
<span className={MENU_ITEM_LABEL_CLASS}>{item.label}</span>
|
|
290
|
-
{checkIcon}
|
|
291
|
-
</>
|
|
292
|
-
);
|
|
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
|
+
);
|
|
293
508
|
}
|
|
509
|
+
return children;
|
|
294
510
|
};
|
|
295
511
|
|
|
296
512
|
return (
|
|
297
|
-
<
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
data-tracking-el={trackingEl || `combobox-${variant}`}
|
|
315
|
-
data-tracking-label={trackingLabel}
|
|
316
|
-
disabled={disabled}
|
|
317
|
-
id={id}
|
|
318
|
-
role="combobox"
|
|
319
|
-
type="button"
|
|
320
|
-
>
|
|
321
|
-
{variant === "withGroupLabel" ? (
|
|
322
|
-
<>
|
|
323
|
-
<div className="flex w-full flex-col items-start pr-6">
|
|
324
|
-
{groupLabel && (
|
|
325
|
-
<span className="text-muted-foreground overflow-hidden font-sans text-xs leading-4 font-normal text-ellipsis">
|
|
326
|
-
{groupLabel}
|
|
327
|
-
</span>
|
|
328
|
-
)}
|
|
329
|
-
<span className="text-foreground overflow-hidden font-sans text-sm leading-5 font-medium text-ellipsis whitespace-nowrap">
|
|
330
|
-
{selectedItem ? selectedItem.label : placeholder}
|
|
331
|
-
</span>
|
|
332
|
-
</div>
|
|
333
|
-
<ChevronDownIcon className="text-muted-foreground absolute top-1/2 right-4 size-4 shrink-0 -translate-y-1/2" />
|
|
334
|
-
</>
|
|
335
|
-
) : (
|
|
336
|
-
<>
|
|
337
|
-
{showAvatar && (
|
|
338
|
-
<ComboboxAvatar
|
|
339
|
-
alt={selectedItem?.label}
|
|
340
|
-
dataSlot="combobox-avatar"
|
|
341
|
-
fallback={avatarFallback}
|
|
342
|
-
src={avatarSrc}
|
|
343
|
-
/>
|
|
344
|
-
)}
|
|
345
|
-
<span className="text-foreground overflow-hidden text-sm leading-normal font-medium text-ellipsis whitespace-nowrap">
|
|
346
|
-
{selectedItem ? selectedItem.label : placeholder}
|
|
347
|
-
</span>
|
|
348
|
-
<ChevronsUpDownIcon className="text-muted-foreground ml-auto size-4 shrink-0" />
|
|
349
|
-
</>
|
|
350
|
-
)}
|
|
351
|
-
</button>
|
|
352
|
-
</PopoverTrigger>
|
|
353
|
-
|
|
354
|
-
<PopoverContent
|
|
355
|
-
align="start"
|
|
356
|
-
className="w-[var(--radix-popover-trigger-width)] p-0"
|
|
357
|
-
data-slot="combobox-popover"
|
|
358
|
-
>
|
|
359
|
-
<Command
|
|
360
|
-
className="border-0 shadow-none"
|
|
361
|
-
data-slot="combobox-command"
|
|
362
|
-
dataHook={`${dataHook}-command`}
|
|
363
|
-
id={listboxId}
|
|
364
|
-
>
|
|
365
|
-
<CommandInput
|
|
366
|
-
dataHook={`${dataHook}-search`}
|
|
367
|
-
placeholder={searchPlaceholder}
|
|
368
|
-
/>
|
|
369
|
-
<CommandList>
|
|
370
|
-
<CommandEmpty>{emptyMessage}</CommandEmpty>
|
|
371
|
-
{variant === "withGroupLabel" &&
|
|
372
|
-
Object.keys(groupedItems).length > 1 ? (
|
|
373
|
-
Object.entries(groupedItems).map(([group, groupItems]) => (
|
|
374
|
-
<CommandGroup key={group} heading={group}>
|
|
375
|
-
{groupItems.map((item) => (
|
|
376
|
-
<CommandItem
|
|
377
|
-
key={item.value}
|
|
378
|
-
dataHook={`${dataHook}-item-${item.value}`}
|
|
379
|
-
value={item.value}
|
|
380
|
-
onSelect={handleSelect}
|
|
381
|
-
>
|
|
382
|
-
{renderMenuItem(item, value === item.value)}
|
|
383
|
-
</CommandItem>
|
|
384
|
-
))}
|
|
385
|
-
</CommandGroup>
|
|
386
|
-
))
|
|
387
|
-
) : (
|
|
388
|
-
<CommandGroup>
|
|
389
|
-
{items.map((item) => (
|
|
390
|
-
<CommandItem
|
|
391
|
-
key={item.value}
|
|
392
|
-
dataHook={`${dataHook}-item-${item.value}`}
|
|
393
|
-
value={item.value}
|
|
394
|
-
onSelect={handleSelect}
|
|
395
|
-
>
|
|
396
|
-
{renderMenuItem(item, value === item.value)}
|
|
397
|
-
</CommandItem>
|
|
398
|
-
))}
|
|
399
|
-
</CommandGroup>
|
|
400
|
-
)}
|
|
401
|
-
</CommandList>
|
|
402
|
-
</Command>
|
|
403
|
-
</PopoverContent>
|
|
404
|
-
</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>
|
|
405
530
|
);
|
|
406
531
|
}
|
|
407
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
|
+
}
|
|
408
582
|
|
|
409
|
-
|
|
583
|
+
/* -------------------------------------------------------------------------------------------------
|
|
584
|
+
* Exports
|
|
585
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
410
586
|
|
|
411
|
-
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
|
+
};
|