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