@brightlocal/ui-components 0.3.1 → 0.4.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 (51) hide show
  1. package/dist/alert.d.ts +2 -2
  2. package/dist/alert.js +3 -3
  3. package/dist/blocks/sidebar.cjs +1 -1
  4. package/dist/blocks/sidebar.d.ts +2 -2
  5. package/dist/blocks/sidebar.js +0 -1
  6. package/dist/breadcrumb.js +3 -3
  7. package/dist/button.d.ts +1 -1
  8. package/dist/carousel.js +3 -3
  9. package/dist/centred-layout.cjs +1 -1
  10. package/dist/centred-layout.d.ts +1 -1
  11. package/dist/centred-layout.js +6 -6
  12. package/dist/checkbox.cjs +1 -1
  13. package/dist/checkbox.js +1 -1
  14. package/dist/combobox.d.ts +6 -6
  15. package/dist/combobox.d.ts.map +1 -1
  16. package/dist/combobox.js +13 -13
  17. package/dist/date-picker.js +4 -4
  18. package/dist/field.cjs +1 -1
  19. package/dist/field.d.ts +103 -10
  20. package/dist/field.d.ts.map +1 -1
  21. package/dist/field.js +118 -58
  22. package/dist/index.cjs +1 -1
  23. package/dist/index.js +351 -349
  24. package/dist/input-chip.cjs +1 -1
  25. package/dist/input-chip.js +1 -1
  26. package/dist/input-group.d.ts +1 -1
  27. package/dist/input-otp.js +3 -3
  28. package/dist/input-password.js +1 -1
  29. package/dist/label.cjs +1 -1
  30. package/dist/label.js +1 -1
  31. package/dist/link.cjs +1 -1
  32. package/dist/link.d.ts +5 -0
  33. package/dist/link.d.ts.map +1 -1
  34. package/dist/link.js +24 -23
  35. package/dist/pagination.d.ts +1 -1
  36. package/dist/pagination.js +3 -3
  37. package/dist/radio-group.d.ts +2 -2
  38. package/dist/resizable.d.ts +1 -1
  39. package/dist/select.cjs +1 -1
  40. package/dist/select.d.ts +19 -14
  41. package/dist/select.d.ts.map +1 -1
  42. package/dist/select.js +102 -133
  43. package/dist/sonner.js +1 -1
  44. package/dist/switch.d.ts +1 -1
  45. package/package.json +9 -9
  46. package/src/components/centred-layout.tsx +3 -3
  47. package/src/components/checkbox.tsx +1 -1
  48. package/src/components/field.tsx +187 -15
  49. package/src/components/label.tsx +1 -1
  50. package/src/components/link.tsx +7 -1
  51. package/src/components/select.tsx +22 -86
@@ -1,5 +1,6 @@
1
1
  import { cva, type VariantProps } from "class-variance-authority";
2
2
 
3
+ import { AlertCircle, type LucideProps } from "@brightlocal/icons";
3
4
  import { cn } from "@brightlocal/ui-components/lib/utils";
4
5
  import { Description } from "./description";
5
6
  import { Label } from "@brightlocal/ui-components/label";
@@ -68,13 +69,13 @@ const fieldVariants = cva("group/field flex w-full gap-2", {
68
69
  orientation: {
69
70
  vertical: ["flex-col [&>*]:w-full [&>.sr-only]:w-auto"],
70
71
  horizontal: [
71
- "flex-row items-center",
72
- "[&>[data-slot=field-label]]:flex-auto",
72
+ "grid grid-cols-[auto_1fr] items-center",
73
+ "[&>[data-slot=field-description]]:col-start-2 [&>[data-slot=field-error]]:col-start-2",
73
74
  "has-[>[data-slot=field-content]]:items-start has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
74
75
  ],
75
76
  responsive: [
76
- "flex-col [&>*]:w-full [&>.sr-only]:w-auto @md/field-group:flex-row @md/field-group:items-center @md/field-group:[&>*]:w-auto",
77
- "@md/field-group:[&>[data-slot=field-label]]:flex-auto",
77
+ "flex-col [&>*]:w-full [&>.sr-only]:w-auto @md/field-group:grid @md/field-group:grid-cols-[auto_1fr] @md/field-group:items-center @md/field-group:[&>*]:w-auto",
78
+ "@md/field-group:[&>[data-slot=field-description]]:col-start-2 @md/field-group:[&>[data-slot=field-error]]:col-start-2",
78
79
  "@md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
79
80
  ],
80
81
  },
@@ -88,23 +89,17 @@ const fieldVariants = cva("group/field flex w-full gap-2", {
88
89
  * Props for the Field component
89
90
  */
90
91
  interface FieldProps
91
- extends React.ComponentProps<"div">, VariantProps<typeof fieldVariants> {
92
- /**
93
- * Data hook for automated testing
94
- */
95
- dataHook?: string;
96
- }
92
+ extends React.ComponentProps<"div">,
93
+ VariantProps<typeof fieldVariants> {}
97
94
 
98
95
  function Field({
99
96
  className,
100
97
  orientation = "vertical",
101
- dataHook,
102
98
  ...props
103
99
  }: FieldProps) {
104
100
  return (
105
101
  <div
106
102
  className={cn(fieldVariants({ orientation }), className)}
107
- data-hook={dataHook}
108
103
  data-orientation={orientation}
109
104
  data-slot="field"
110
105
  role="group"
@@ -131,9 +126,18 @@ interface FieldLabelProps extends React.ComponentProps<typeof Label> {
131
126
  * Required htmlFor attribute for linking label with input
132
127
  */
133
128
  htmlFor: string;
129
+ /**
130
+ * Data hook for automated testing (REQUIRED)
131
+ */
132
+ dataHook: string;
134
133
  }
135
134
 
136
- function FieldLabel({ className, htmlFor, ...props }: FieldLabelProps) {
135
+ function FieldLabel({
136
+ className,
137
+ htmlFor,
138
+ dataHook,
139
+ ...props
140
+ }: FieldLabelProps) {
137
141
  return (
138
142
  <Label
139
143
  className={cn(
@@ -145,6 +149,7 @@ function FieldLabel({ className, htmlFor, ...props }: FieldLabelProps) {
145
149
  "has-data-[state=checked]:bg-primary/5 has-data-[state=checked]:border-primary dark:has-data-[state=checked]:bg-primary/10",
146
150
  className
147
151
  )}
152
+ data-hook={dataHook}
148
153
  data-slot="field-label"
149
154
  htmlFor={htmlFor}
150
155
  {...props}
@@ -152,7 +157,18 @@ function FieldLabel({ className, htmlFor, ...props }: FieldLabelProps) {
152
157
  );
153
158
  }
154
159
 
155
- function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
160
+ interface FieldDescriptionProps extends React.ComponentProps<"p"> {
161
+ /**
162
+ * Data hook for automated testing (REQUIRED)
163
+ */
164
+ dataHook: string;
165
+ }
166
+
167
+ function FieldDescription({
168
+ className,
169
+ dataHook,
170
+ ...props
171
+ }: FieldDescriptionProps) {
156
172
  return (
157
173
  <Description
158
174
  className={cn(
@@ -162,16 +178,172 @@ function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
162
178
  "last:mt-0 nth-last-2:-mt-1 [[data-variant=legend]+&]:-mt-1.5",
163
179
  className
164
180
  )}
181
+ data-hook={dataHook}
165
182
  data-slot="field-description"
166
183
  {...props}
167
184
  />
168
185
  );
169
186
  }
170
187
 
188
+ /**
189
+ * Error object type for form library integration
190
+ */
191
+ interface FieldErrorItem {
192
+ message?: string;
193
+ }
194
+
195
+ /**
196
+ * Props for the FieldError component
197
+ */
198
+ interface FieldErrorProps extends Omit<React.ComponentProps<"div">, "children"> {
199
+ /**
200
+ * Data hook for automated testing (REQUIRED)
201
+ */
202
+ dataHook: string;
203
+ /**
204
+ * Error message to display as children
205
+ */
206
+ children?: React.ReactNode;
207
+ /**
208
+ * Array of error objects from form libraries (e.g., react-hook-form)
209
+ * Each error object should have a message property
210
+ */
211
+ errors?: Array<FieldErrorItem | undefined>;
212
+ }
213
+
214
+ interface FieldErrorIconProps extends Omit<LucideProps, "ref"> {
215
+ /**
216
+ * Custom icon component to render instead of the default AlertCircle.
217
+ * Pass a BrightLocal icon component (not an element).
218
+ * @default AlertCircle
219
+ */
220
+ icon?: React.ComponentType<LucideProps>;
221
+ }
222
+
223
+ /**
224
+ * FieldErrorIcon displays an error icon with proper styling.
225
+ * Use this component inside FieldError for composition - place it before or after
226
+ * the error text to control icon position.
227
+ *
228
+ * @example
229
+ * ```tsx
230
+ * // Default AlertCircle icon
231
+ * <FieldError>
232
+ * <FieldErrorIcon /> Enter a valid email address.
233
+ * </FieldError>
234
+ *
235
+ * // Custom icon
236
+ * <FieldError>
237
+ * <FieldErrorIcon icon={XCircle} /> Enter a valid email address.
238
+ * </FieldError>
239
+ *
240
+ * // Icon after text
241
+ * <FieldError>
242
+ * Enter a valid email address. <FieldErrorIcon />
243
+ * </FieldError>
244
+ * ```
245
+ */
246
+ function FieldErrorIcon({
247
+ icon: Icon = AlertCircle,
248
+ className,
249
+ ...props
250
+ }: FieldErrorIconProps) {
251
+ return (
252
+ <Icon
253
+ aria-hidden="true"
254
+ className={cn(
255
+ "inline-block size-4 shrink-0 align-text-bottom",
256
+ className
257
+ )}
258
+ {...props}
259
+ />
260
+ );
261
+ }
262
+
263
+ /**
264
+ * FieldError displays validation error messages within a Field.
265
+ *
266
+ * Supports both direct children content and an errors array for form library integration.
267
+ * Uses role="alert" for accessibility, ensuring screen readers announce error messages.
268
+ * No icon is displayed by default - use FieldErrorIcon for composition.
269
+ *
270
+ * @example
271
+ * ```tsx
272
+ * // Simple usage without icon
273
+ * <FieldError>Enter a valid email address.</FieldError>
274
+ *
275
+ * // With icon (composition pattern)
276
+ * <FieldError>
277
+ * <FieldErrorIcon /> Enter a valid email address.
278
+ * </FieldError>
279
+ *
280
+ * // With custom icon
281
+ * <FieldError>
282
+ * <FieldErrorIcon icon={XCircle} /> Error message
283
+ * </FieldError>
284
+ *
285
+ * // With react-hook-form errors array
286
+ * <FieldError errors={[fieldState.error]} />
287
+ *
288
+ * // Multiple errors rendered as list
289
+ * <FieldError errors={[{ message: "Too short" }, { message: "Invalid format" }]} />
290
+ * ```
291
+ */
292
+ function FieldError({
293
+ className,
294
+ dataHook,
295
+ children,
296
+ errors,
297
+ ...props
298
+ }: FieldErrorProps) {
299
+ // Filter out undefined errors and those without messages
300
+ const errorMessages = errors?.filter(
301
+ (e): e is FieldErrorItem => Boolean(e?.message)
302
+ );
303
+
304
+ // If no children and no valid errors, don't render anything
305
+ if (!children && (!errorMessages || errorMessages.length === 0)) {
306
+ return null;
307
+ }
308
+
309
+ const baseStyles = cn(
310
+ "text-destructive-foreground text-sm font-normal leading-5",
311
+ "mt-2",
312
+ "group-has-[[data-orientation=horizontal]]/field:text-balance",
313
+ "last:mt-0 nth-last-2:-mt-1 [[data-variant=legend]+&]:-mt-1.5",
314
+ className
315
+ );
316
+
317
+ // If multiple errors, render as a list
318
+ const isMultiple = errorMessages && errorMessages.length > 1;
319
+
320
+ return (
321
+ <div
322
+ className={baseStyles}
323
+ data-hook={dataHook}
324
+ data-slot="field-error"
325
+ role="alert"
326
+ {...props}
327
+ >
328
+ {isMultiple ? (
329
+ <ul className="list-disc space-y-1 pl-4">
330
+ {errorMessages.map((error, index) => (
331
+ <li key={index}>{error.message}</li>
332
+ ))}
333
+ </ul>
334
+ ) : (
335
+ <p className="m-0">{children || errorMessages?.[0]?.message}</p>
336
+ )}
337
+ </div>
338
+ );
339
+ }
340
+
171
341
  export {
172
342
  Field,
173
343
  FieldLabel,
174
344
  FieldDescription,
345
+ FieldError,
346
+ FieldErrorIcon,
175
347
  FieldGroup,
176
348
  FieldLegend,
177
349
  fieldLegendVariants,
@@ -179,4 +351,4 @@ export {
179
351
  FieldContent,
180
352
  };
181
353
 
182
- export type { FieldProps };
354
+ export type { FieldProps, FieldErrorProps };
@@ -6,7 +6,7 @@ import { cva } from "class-variance-authority";
6
6
  import { cn } from "@brightlocal/ui-components/lib/utils";
7
7
 
8
8
  const labelVariants = cva(
9
- ["text-sm font-medium leading-none", "text-foreground"],
9
+ ["text-sm font-medium leading-normal", "text-foreground"],
10
10
  {
11
11
  variants: {
12
12
  optional: {
@@ -55,6 +55,11 @@ export interface LinkProps
55
55
  * If true, opens link in a new tab with target="_blank" and rel="noopener noreferrer"
56
56
  */
57
57
  external?: boolean;
58
+ /**
59
+ * If true, shows the external link icon when external is true
60
+ * @default true
61
+ */
62
+ showExternalIcon?: boolean;
58
63
  }
59
64
 
60
65
  const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
@@ -66,6 +71,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
66
71
  trackingLabel,
67
72
  asChild = false,
68
73
  external = false,
74
+ showExternalIcon = true,
69
75
  children,
70
76
  ...props
71
77
  },
@@ -88,7 +94,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
88
94
  {...props}
89
95
  >
90
96
  {children}
91
- {external && (
97
+ {external && showExternalIcon && (
92
98
  <ExternalLink
93
99
  aria-hidden="true"
94
100
  className="h-4 w-4"
@@ -6,7 +6,6 @@ import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "@brightlocal/icons";
6
6
  import { cva } from "class-variance-authority";
7
7
 
8
8
  import { cn } from "@brightlocal/ui-components/lib/utils";
9
- import { Label } from "@brightlocal/ui-components/label";
10
9
 
11
10
  // ============================================================================
12
11
  // Types
@@ -15,18 +14,8 @@ import { Label } from "@brightlocal/ui-components/label";
15
14
  interface SelectProps extends React.ComponentProps<
16
15
  typeof SelectPrimitive.Root
17
16
  > {
18
- /** Label text displayed above the select */
19
- label?: string;
20
- /** Helper text displayed below the select */
21
- description?: string;
22
- /** Shows required indicator (*) next to label */
23
- optional?: boolean;
24
- /** Placeholder text when no value is selected */
25
- placeholder?: string;
26
17
  /** Children to render inside the select */
27
18
  children?: React.ReactNode;
28
- /** Accessible label for screen readers when no visible label is provided */
29
- "aria-label"?: string;
30
19
  }
31
20
 
32
21
  // ============================================================================
@@ -34,83 +23,30 @@ interface SelectProps extends React.ComponentProps<
34
23
  // ============================================================================
35
24
 
36
25
  /**
37
- * Select component with optional label and description.
38
- * Wraps Radix UI Select with additional accessibility and styling features.
39
- * Error state should be managed via Form components (FormMessage).
26
+ * Select component for choosing from a list of options.
27
+ * Wraps Radix UI Select with styling and accessibility features.
28
+ *
29
+ * For labels and descriptions, compose with Field components:
30
+ * ```tsx
31
+ * <Field>
32
+ * <FieldLabel htmlFor="framework">Framework</FieldLabel>
33
+ * <Select>
34
+ * <SelectTrigger id="framework" dataHook="framework-select">
35
+ * <SelectValue placeholder="Select a framework" />
36
+ * </SelectTrigger>
37
+ * <SelectContent>
38
+ * <SelectItem value="react">React</SelectItem>
39
+ * </SelectContent>
40
+ * </Select>
41
+ * <FieldDescription>Choose your preferred framework</FieldDescription>
42
+ * </Field>
43
+ * ```
40
44
  */
41
- function Select({
42
- label,
43
- description,
44
- optional,
45
- placeholder,
46
- children,
47
- "aria-label": ariaLabel,
48
- ...props
49
- }: SelectProps) {
50
- // Generate unique IDs for accessibility
51
- const id = React.useId();
52
- const labelId = `${id}-label`;
53
- const descriptionId = `${id}-description`;
54
-
55
- const helperText = description;
56
- const hasWrapper = label || helperText;
57
-
58
- // Props to inject into SelectTrigger
59
- const triggerProps = {
60
- placeholder,
61
- "aria-required": optional ? undefined : true,
62
- "aria-label": ariaLabel,
63
- };
64
-
65
- // Add accessibility props when wrapped with label/description
66
- const wrappedTriggerProps = {
67
- ...triggerProps,
68
- id,
69
- };
70
-
71
- // Helper function to clone SelectTrigger with props
72
- const enhanceTrigger = (child: React.ReactNode, additionalProps: object) => {
73
- if (React.isValidElement(child) && child.type === SelectTrigger) {
74
- return React.cloneElement(
75
- child as React.ReactElement<SelectTriggerProps>,
76
- additionalProps
77
- );
78
- }
79
- return child;
80
- };
81
-
82
- // Simple select without wrapper
83
- if (!hasWrapper) {
84
- return (
85
- <SelectPrimitive.Root data-slot="select" {...props}>
86
- {React.Children.map(children, (child) =>
87
- enhanceTrigger(child, triggerProps)
88
- )}
89
- </SelectPrimitive.Root>
90
- );
91
- }
92
-
93
- // Select with label and/or description wrapper
45
+ function Select({ children, ...props }: SelectProps) {
94
46
  return (
95
- <div className="flex flex-col gap-2">
96
- {label && (
97
- <Label htmlFor={id} id={labelId} optional={optional}>
98
- {label}
99
- </Label>
100
- )}
101
-
102
- <SelectPrimitive.Root data-slot="select" {...props}>
103
- {React.Children.map(children, (child) =>
104
- enhanceTrigger(child, wrappedTriggerProps)
105
- )}
106
- </SelectPrimitive.Root>
107
-
108
- {helperText && (
109
- <p className="text-muted-foreground text-sm" id={descriptionId}>
110
- {helperText}
111
- </p>
112
- )}
113
- </div>
47
+ <SelectPrimitive.Root data-slot="select" {...props}>
48
+ {children}
49
+ </SelectPrimitive.Root>
114
50
  );
115
51
  }
116
52