torch-glare 2.4.1 → 2.4.3

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 (85) hide show
  1. package/apps/lib/components/Button.tsx +1 -1
  2. package/apps/lib/components/Card.tsx +47 -20
  3. package/apps/lib/components/ColorPicker.tsx +441 -0
  4. package/apps/lib/components/ConclusionHeader.tsx +148 -0
  5. package/apps/lib/components/DatePicker.tsx +2 -0
  6. package/apps/lib/components/Drawer.tsx +66 -24
  7. package/apps/lib/components/FormBuilder/context.ts +68 -0
  8. package/apps/lib/components/FormBuilder/fields/ChoiceFields.tsx +60 -0
  9. package/apps/lib/components/FormBuilder/fields/ColorField.tsx +59 -0
  10. package/apps/lib/components/FormBuilder/fields/CustomField.tsx +11 -0
  11. package/apps/lib/components/FormBuilder/fields/DateField.tsx +32 -0
  12. package/apps/lib/components/FormBuilder/fields/FieldArray.tsx +74 -0
  13. package/apps/lib/components/FormBuilder/fields/FieldShell.tsx +148 -0
  14. package/apps/lib/components/FormBuilder/fields/FileField.tsx +39 -0
  15. package/apps/lib/components/FormBuilder/fields/OptionListFields.tsx +132 -0
  16. package/apps/lib/components/FormBuilder/fields/OtpField.tsx +31 -0
  17. package/apps/lib/components/FormBuilder/fields/PhoneField.tsx +83 -0
  18. package/apps/lib/components/FormBuilder/fields/RichTextEditorField.tsx +31 -0
  19. package/apps/lib/components/FormBuilder/fields/SelectField.tsx +91 -0
  20. package/apps/lib/components/FormBuilder/fields/SignatureField.tsx +157 -0
  21. package/apps/lib/components/FormBuilder/fields/SliderField.tsx +67 -0
  22. package/apps/lib/components/FormBuilder/fields/SwitchBoxField.tsx +41 -0
  23. package/apps/lib/components/FormBuilder/fields/TableField.tsx +308 -0
  24. package/apps/lib/components/FormBuilder/fields/TextField.tsx +213 -0
  25. package/apps/lib/components/FormBuilder/fields/TreeSelectField.tsx +43 -0
  26. package/apps/lib/components/FormBuilder/fields/countries.ts +303 -0
  27. package/apps/lib/components/FormBuilder/fields/index.ts +25 -0
  28. package/apps/lib/components/FormBuilder/form-builder.tsx +279 -0
  29. package/apps/lib/components/FormBuilder/header.tsx +101 -0
  30. package/apps/lib/components/FormBuilder/index.ts +33 -0
  31. package/apps/lib/components/FormBuilder/numberFormat.ts +16 -0
  32. package/apps/lib/components/FormBuilder/stepper.tsx +290 -0
  33. package/apps/lib/components/FormBuilder/submit.tsx +38 -0
  34. package/apps/lib/components/FormBuilder/types.ts +261 -0
  35. package/apps/lib/components/FormRenderer/FormDrawer.tsx +128 -0
  36. package/apps/lib/components/FormRenderer/form-renderer.tsx +111 -0
  37. package/apps/lib/components/FormRenderer/index.ts +4 -0
  38. package/apps/lib/components/FormRenderer/types.ts +77 -0
  39. package/apps/lib/components/FormSummary.tsx +282 -0
  40. package/apps/lib/components/ImageAttachment.tsx +36 -61
  41. package/apps/lib/components/Label.tsx +49 -42
  42. package/apps/lib/components/Popover.tsx +6 -2
  43. package/apps/lib/components/RadioCard.tsx +2 -0
  44. package/apps/lib/components/SearchableSelect.tsx +16 -5
  45. package/apps/lib/components/SectionBlock.tsx +16 -8
  46. package/apps/lib/components/Select.tsx +41 -120
  47. package/apps/lib/components/TextEditor/RichTextField.tsx +46 -0
  48. package/apps/lib/components/{TextEditor.tsx → TextEditor/TextEditor.tsx} +63 -9
  49. package/apps/lib/components/TextEditor/TextEditorToolbar.tsx +429 -0
  50. package/apps/lib/components/TextEditor/editor-tools/AlignmentTune.ts +70 -0
  51. package/apps/lib/components/TextEditor/editor-tools/ColorInlineTool.ts +50 -0
  52. package/apps/lib/components/TextEditor/editor-tools/StrikethroughInlineTool.ts +48 -0
  53. package/apps/lib/components/TextEditor/editor-tools/inlineFormat.ts +98 -0
  54. package/apps/lib/{types → components/TextEditor}/editorjs.d.ts +19 -0
  55. package/apps/lib/components/TextEditor/index.ts +7 -0
  56. package/apps/lib/components/Textarea.tsx +1 -1
  57. package/apps/lib/layouts/FieldSection.tsx +25 -22
  58. package/apps/lib/registry.json +51 -58
  59. package/apps/lib/tsconfig.tsbuildinfo +1 -0
  60. package/apps/lib/utils/color.ts +175 -0
  61. package/dist/src/shared/tailwindInit.d.ts.map +1 -1
  62. package/dist/src/shared/tailwindInit.js +3 -0
  63. package/dist/src/shared/tailwindInit.js.map +1 -1
  64. package/docs/components/card.md +4 -2
  65. package/docs/components/chart-block-tool.md +5 -4
  66. package/docs/components/color-picker.md +101 -0
  67. package/docs/components/conclusion-header.md +80 -0
  68. package/docs/components/drawer.md +153 -102
  69. package/docs/components/form-builder.md +260 -0
  70. package/docs/components/form-renderer.md +234 -0
  71. package/docs/components/form-summary.md +123 -0
  72. package/docs/components/image-attachment.md +10 -4
  73. package/docs/components/searchable-select.md +50 -46
  74. package/docs/components/table-dnd-wrapper.md +5 -3
  75. package/docs/components/text-editor.md +18 -0
  76. package/docs/how-to/form-and-list-recipes.md +26 -19
  77. package/docs/how-to/forms-with-form-builder.md +411 -0
  78. package/docs/how-to/guides.md +153 -179
  79. package/docs/reference/tailwind-plugins.md +11 -1
  80. package/docs/tutorials/building-first-form.md +150 -159
  81. package/docs/tutorials/getting-started.md +9 -0
  82. package/package.json +1 -1
  83. /package/apps/lib/components/{ChartBlockTool.ts → TextEditor/ChartBlockTool.ts} +0 -0
  84. /package/apps/lib/components/{TableDnDWrapper.ts → TextEditor/TableDnDWrapper.ts} +0 -0
  85. /package/apps/lib/{utils → components/TextEditor}/markdownParser.ts +0 -0
@@ -5,6 +5,7 @@ import { Drawer as DrawerPrimitive } from "vaul";
5
5
  import { cva, type VariantProps } from "class-variance-authority";
6
6
 
7
7
  import { cn } from "../utils/cn";
8
+ import { Button } from "./Button";
8
9
 
9
10
  const Drawer = ({
10
11
  shouldScaleBackground = true,
@@ -47,9 +48,58 @@ interface DrawerContentProps extends React.ComponentPropsWithoutRef<
47
48
  */
48
49
  framed?: boolean;
49
50
  wrapperClassName?: string;
51
+ /**
52
+ * @deprecated Use `className` — it now targets the tray directly. Kept as an
53
+ * alias (merged last, so it still wins) for the pre-`DrawerPanel` API.
54
+ */
50
55
  trayClassName?: string;
51
56
  }
52
57
 
58
+ interface DrawerPanelProps extends React.HTMLAttributes<HTMLDivElement> {
59
+ /** Panel border + inset shadow that pairs with the tray's frame. Default `true`. */
60
+ framed?: boolean;
61
+ /** Show the drag handle (bottom sheets). Default `false`. */
62
+ showHandle?: boolean;
63
+ }
64
+
65
+ /**
66
+ * The light `#F0F0F0` content surface inside a `DrawerContent`.
67
+ *
68
+ * It is an ordinary child, not something the tray paints — so a drawer can hold a panel
69
+ * and something else beside it (e.g. a `FormSummary`), each bringing its own background.
70
+ * Give the tray a `gap-*` to space them.
71
+ *
72
+ * ```tsx
73
+ * <DrawerContent className="gap-[6px]">
74
+ * <DrawerPanel>…form…</DrawerPanel>
75
+ * <FormSummary … />
76
+ * </DrawerContent>
77
+ * ```
78
+ */
79
+ const DrawerPanel = React.forwardRef<HTMLDivElement, DrawerPanelProps>(
80
+ ({ className, framed = true, showHandle = false, children, ...props }, ref) => (
81
+ <div
82
+ ref={ref}
83
+ // The content surface is always light (#F0F0F0) regardless of the page
84
+ // theme, so pin a light theme here. This makes theme-aware content tokens
85
+ // (DrawerTitle/Description and any consumer content) resolve to their
86
+ // dark-on-light values instead of following a dark page theme (which
87
+ // would render white-on-light = invisible).
88
+ data-theme="light"
89
+ className={cn(
90
+ "flex flex-1 gap-2 rounded-t-[16px] p-1 bg-[#F0F0F0] min-h-0",
91
+ framed && "border border-[#D4D4D4] shadow-[inset_0_-4px_16px_rgba(0,0,0,0.1)]",
92
+ className,
93
+ )}
94
+ {...props}
95
+ >
96
+ {showHandle && <div className="mx-auto h-2 w-[100px] rounded-full bg-[#D4D4D4]" />}
97
+ {children}
98
+ </div>
99
+ ),
100
+ );
101
+ DrawerPanel.displayName = "DrawerPanel";
102
+
53
103
  const DrawerContent = React.forwardRef<
54
104
  React.ElementRef<typeof DrawerPrimitive.Content>,
55
105
  DrawerContentProps
@@ -58,7 +108,6 @@ const DrawerContent = React.forwardRef<
58
108
  {
59
109
  className,
60
110
  children,
61
- showHandle = true,
62
111
  notch,
63
112
  notchSide = "left",
64
113
  framed: framedProp,
@@ -89,9 +138,12 @@ const DrawerContent = React.forwardRef<
89
138
  : notch}
90
139
  </div>
91
140
  )}
141
+ {/* The tray. It frames and positions, but paints nothing over its children —
142
+ each child brings its own background (see `DrawerPanel`). Supply a `gap-*`
143
+ via `className` when the tray holds more than one. */}
92
144
  <div
93
145
  className={cn(
94
- "flex flex-1 flex-col min-h-0",
146
+ "flex flex-1 min-h-0",
95
147
  framed
96
148
  ? "p-1.5 bg-black-400 shadow-[0_0_4px_rgba(0,0,0,0.2),0_0_30px_rgba(0,0,0,0.4)]"
97
149
  : "p-0",
@@ -102,27 +154,11 @@ const DrawerContent = React.forwardRef<
102
154
  : framed
103
155
  ? "rounded-t-[22px]"
104
156
  : "",
157
+ className,
105
158
  trayClassName,
106
159
  )}
107
160
  >
108
- <div
109
- // The content surface is always light (#F0F0F0) regardless of the
110
- // page theme, so pin a light theme here. This makes theme-aware
111
- // content tokens (DrawerTitle/Description and any consumer content)
112
- // resolve to their dark-on-light values instead of following a dark
113
- // page theme (which would render white-on-light = invisible).
114
- data-theme="light"
115
- className={cn(
116
- "flex flex-1 flex-col gap-2 rounded-t-[16px] p-1.5 bg-[#F0F0F0] min-h-0",
117
- framed && "border border-[#D4D4D4] shadow-[inset_0_-4px_16px_rgba(0,0,0,0.1)]",
118
- className,
119
- )}
120
- >
121
- {showHandle && !notch && (
122
- <div className="mx-auto h-2 w-[100px] rounded-full bg-[#D4D4D4]" />
123
- )}
124
- {children}
125
- </div>
161
+ {children}
126
162
  </div>
127
163
  </DrawerPrimitive.Content>
128
164
  </DrawerPortal>
@@ -233,18 +269,23 @@ const DrawerNotchClose = React.forwardRef<
233
269
  HTMLButtonElement,
234
270
  React.ButtonHTMLAttributes<HTMLButtonElement>
235
271
  >(({ className, children, ...props }, ref) => (
236
- <button
272
+ <Button
237
273
  ref={ref}
238
274
  type="button"
239
275
  aria-label="Close"
276
+ buttonType="icon"
277
+ size="S"
278
+ variant="PrimeContStyle"
240
279
  className={cn(
241
- "inline-flex h-[22px] w-[22px] items-center justify-center rounded-[13px] bg-white/15 text-content-presentation-global-primary transition-colors hover:bg-white/25",
280
+ // Glare Button (icon, S = 22×22) with the notch's own circular radius + translucent look
281
+ // (S is otherwise rounded-[4px]).
282
+ "!rounded-[13px] bg-white/15 text-white hover:bg-white/25 hover:text-white",
242
283
  className,
243
284
  )}
244
285
  {...props}
245
286
  >
246
- {children ?? <i className="ri-close-fill text-[14px]" />}
247
- </button>
287
+ {children ?? <i className="ri-close-fill !text-[14px]" />}
288
+ </Button>
248
289
  ));
249
290
  DrawerNotchClose.displayName = "DrawerNotchClose";
250
291
 
@@ -346,6 +387,7 @@ export {
346
387
  DrawerTrigger,
347
388
  DrawerClose,
348
389
  DrawerContent,
390
+ DrawerPanel,
349
391
  DrawerHeader,
350
392
  DrawerHeaderTitle,
351
393
  DrawerHeaderActions,
@@ -0,0 +1,68 @@
1
+ "use client";
2
+
3
+ import { createContext, useContext } from "react";
4
+
5
+ export type FieldDirection = "horizontal" | "vertical";
6
+
7
+ /** Loading flag — drives the Submit spinner and disables inputs. */
8
+ export const LoadingContext = createContext<boolean>(false);
9
+ export const useLoading = () => useContext(LoadingContext);
10
+
11
+ /**
12
+ * The `<form>`'s `id`. Provided by the FormBuilder root so a `FormBuilder.Submit` rendered
13
+ * **outside** the `<form>` element — the absolute header's action pill, or the drawer header —
14
+ * can still submit it via native `form={id}` association without the caller wiring an id by hand.
15
+ */
16
+ export const FormIdContext = createContext<string | undefined>(undefined);
17
+ export const useFormId = () => useContext(FormIdContext);
18
+
19
+ /**
20
+ * Field row direction — set via `FormBuilder`'s `fieldDirection` prop. `undefined` (the default)
21
+ * means "don't force one": `FieldSection` then uses its own responsive `flexible` layout, which
22
+ * stacks on small screens and goes two-column at `lg`. Only set this to pin a direction.
23
+ */
24
+ export const DirectionContext = createContext<FieldDirection | undefined>(undefined);
25
+ export const useDirection = () => useContext(DirectionContext);
26
+
27
+ /**
28
+ * Cell mode — set to `true` by `FormBuilder.Table` around each cell's field so `FieldShell`
29
+ * renders just the control (no label/row chrome) and surfaces validation errors as a tooltip
30
+ * on the control instead of a stacked hint. Off (`false`) everywhere else, so normal fields
31
+ * keep their full `FieldSection` layout.
32
+ */
33
+ export const CellContext = createContext<boolean>(false);
34
+ export const useCell = () => useContext(CellContext);
35
+
36
+ /**
37
+ * Step registry — a `FormBuilder.Step` provides this so the fields rendered
38
+ * inside it can register their `name`. `FormBuilder.Next` then validates just
39
+ * those names before advancing. `null` when not inside a stepper.
40
+ */
41
+ export interface StepRegistry {
42
+ register: (name: string) => void;
43
+ unregister: (name: string) => void;
44
+ }
45
+ export const StepContext = createContext<StepRegistry | null>(null);
46
+ export const useStepRegistry = () => useContext(StepContext);
47
+
48
+ /** Stepper state shared by the nav + Back/Next/Submit buttons. */
49
+ export interface StepperContextValue {
50
+ currentStep: number;
51
+ totalSteps: number;
52
+ titles: string[];
53
+ isFirstStep: boolean;
54
+ isLastStep: boolean;
55
+ goToNext: () => void | Promise<void>;
56
+ goToPrevious: () => void;
57
+ goToStep: (index: number) => void;
58
+ /** Field names registered per step, for per-step validation. */
59
+ stepFields: Record<number, Set<string>>;
60
+ /** Steps that have passed validation — stay checked even after navigating back. */
61
+ completedSteps: Set<number>;
62
+ }
63
+ export const StepperContext = createContext<StepperContextValue | null>(null);
64
+ export const useStepper = () => {
65
+ const ctx = useContext(StepperContext);
66
+ if (!ctx) throw new Error("FormBuilder.Step/Back/Next must be used within FormBuilder.Stepper");
67
+ return ctx;
68
+ };
@@ -0,0 +1,60 @@
1
+ "use client";
2
+
3
+ import { LabeledCheckBox } from "../../LabeledCheckBox";
4
+ import { RadioGroup } from "../../Radio";
5
+ import { RadioCard } from "../../RadioCard";
6
+ import { useLoading } from "../context";
7
+ import type { CheckboxFieldProps, RadioCardsFieldProps } from "../types";
8
+ import { FieldShell } from "./FieldShell";
9
+
10
+ /**
11
+ * `FormBuilder.Checkbox` — a single boolean checkbox. The field `label` sits in the
12
+ * normal label column; an optional `subLabel` renders inline beside the checkbox (via
13
+ * the clickable `LabeledCheckBox`).
14
+ */
15
+ export function CheckboxField({ subLabel, ...props }: CheckboxFieldProps) {
16
+ const loading = useLoading();
17
+ return (
18
+ <FieldShell {...props}>
19
+ {(field) => (
20
+ <div className="flex w-full items-center ps-2.5">
21
+ <LabeledCheckBox
22
+ id={props.name}
23
+ label={subLabel ?? ""}
24
+ checked={!!field.value}
25
+ size="S"
26
+ onCheckedChange={(checked) => field.onChange(checked === true)}
27
+ disabled={props.disabled || loading}
28
+ />
29
+ </div>
30
+ )}
31
+ </FieldShell>
32
+ );
33
+ }
34
+
35
+ /** `FormBuilder.RadioCards` — radio options rendered as cards (Glare `RadioCard`). */
36
+ export function RadioCardsField(props: RadioCardsFieldProps) {
37
+ const loading = useLoading();
38
+ return (
39
+ <FieldShell {...props}>
40
+ {(field) => (
41
+ <RadioGroup
42
+ value={field.value ?? ""}
43
+ onValueChange={field.onChange}
44
+ className="grid w-full grid-cols-2 gap-3"
45
+ >
46
+ {props.options.map((opt) => (
47
+ <RadioCard
48
+ key={opt.value}
49
+ id={`${props.name}-${opt.value}`}
50
+ value={opt.value}
51
+ headerLabel={opt.label}
52
+ description={opt.description}
53
+ disabled={opt.disabled || props.disabled || loading}
54
+ />
55
+ ))}
56
+ </RadioGroup>
57
+ )}
58
+ </FieldShell>
59
+ );
60
+ }
@@ -0,0 +1,59 @@
1
+ "use client";
2
+
3
+ import { InputField } from "../../InputField";
4
+ import { ColorPicker, CHECKERBOARD } from "../../ColorPicker";
5
+ import { useLoading } from "../context";
6
+ import type { ColorFieldProps } from "../types";
7
+ import { FieldShell } from "./FieldShell";
8
+
9
+ /**
10
+ * `FormBuilder.Color` — an `InputField` holding the hex text, with a trailing action button that
11
+ * carries the colour itself. That button is the `ColorPicker` trigger: clicking it opens the full
12
+ * palette. Value is a hex string.
13
+ */
14
+ export function ColorField(props: ColorFieldProps) {
15
+ const loading = useLoading();
16
+
17
+ return (
18
+ <FieldShell {...props}>
19
+ {(field) => {
20
+ const value = typeof field.value === "string" ? field.value : "";
21
+ const disabled = props.disabled || loading;
22
+ return (
23
+ // The InputField IS the trigger: ColorPicker detects it as an input (it takes a `value`)
24
+ // and feeds it the hex, so clicking the field opens the palette.
25
+ <ColorPicker
26
+ value={value || undefined}
27
+ onChange={field.onChange}
28
+ presets={props.presets}
29
+ alpha={props.alpha ?? true}
30
+ disabled={disabled}
31
+ >
32
+ <InputField
33
+ // Explicit — otherwise Radix's trigger `type="button"` would land on the input.
34
+ type="text"
35
+ value={value}
36
+ onChange={(e) => field.onChange(e.target.value)}
37
+ placeholder={props.placeholder ?? "#000000"}
38
+ disabled={disabled}
39
+ className="w-full"
40
+ // The trailing action button holds the colour itself.
41
+ childrenSide={
42
+ <span
43
+ aria-hidden
44
+ className="block h-[26px] w-[26px] shrink-0 overflow-hidden rounded-[6px] border border-border-presentation-action-primary"
45
+ style={CHECKERBOARD}
46
+ >
47
+ <span
48
+ className="block h-full w-full"
49
+ style={{ backgroundColor: value || "#000000" }}
50
+ />
51
+ </span>
52
+ }
53
+ />
54
+ </ColorPicker>
55
+ );
56
+ }}
57
+ </FieldShell>
58
+ );
59
+ }
@@ -0,0 +1,11 @@
1
+ "use client";
2
+
3
+ import type { CustomFieldProps } from "../types";
4
+ import { FieldShell } from "./FieldShell";
5
+
6
+ /** `FormBuilder.Custom` — render any control, keeping the FieldSection + validation wiring. */
7
+ export function CustomField(props: CustomFieldProps) {
8
+ return (
9
+ <FieldShell {...props}>{(field, fieldState) => props.render({ field, fieldState })}</FieldShell>
10
+ );
11
+ }
@@ -0,0 +1,32 @@
1
+ "use client";
2
+
3
+ import { DatePicker } from "../../DatePicker";
4
+ import { useCell } from "../context";
5
+ import type { DateFieldProps } from "../types";
6
+ import { FieldShell } from "./FieldShell";
7
+
8
+ /**
9
+ * `FormBuilder.Date` (single) — and, via `mode`/`timePicker`, the presets
10
+ * `.DateRange` (value `{from,to}`), `.DateMultiple` (value `Date[]`), and
11
+ * `.DateTime` (date + time). All back onto the one Glare `DatePicker`.
12
+ */
13
+ export function DateField(props: DateFieldProps) {
14
+ const cell = useCell();
15
+
16
+ return (
17
+ <FieldShell {...props}>
18
+ {(field) => (
19
+ <DatePicker
20
+ mode={props.mode ?? "single"}
21
+ timePicker={props.timePicker}
22
+ dateFormat={props.dateFormat}
23
+ value={field.value}
24
+ onTable={cell}
25
+ onChange={(e) =>
26
+ field.onChange((e as unknown as { target: { value: unknown } }).target.value)
27
+ }
28
+ />
29
+ )}
30
+ </FieldShell>
31
+ );
32
+ }
@@ -0,0 +1,74 @@
1
+ "use client";
2
+
3
+ import { useFormContext, useFieldArray, type FieldValues, type ArrayPath } from "react-hook-form";
4
+
5
+ import { Button } from "../../Button";
6
+ import { Card } from "../../Card";
7
+ import type { FieldArrayProps } from "../types";
8
+
9
+ /**
10
+ * `FormBuilder.FieldArray` — a repeating list of sub-fields (RHF `useFieldArray`).
11
+ * `children` renders one row's fields; name them `${rowName}.field`.
12
+ */
13
+ export function FieldArray(props: FieldArrayProps) {
14
+ const form = useFormContext<FieldValues>();
15
+ const { fields, append, remove } = useFieldArray({
16
+ control: form.control,
17
+ name: props.name as ArrayPath<FieldValues>,
18
+ });
19
+
20
+ if (props.hidden) return null;
21
+
22
+ const addButton = (
23
+ <Button
24
+ type="button"
25
+ size="S"
26
+ variant="BorderStyle"
27
+ onClick={() => append((props.defaultItem ?? {}) as never)}
28
+ >
29
+ <i className="ri-add-line" />
30
+ {props.addLabel ?? "Add"}
31
+ </Button>
32
+ );
33
+
34
+ return (
35
+ <section className="grid w-full max-w-[1200px] gap-3 px-[12px] py-[16px]">
36
+ <div className="flex items-center justify-between">
37
+ {props.label ? (
38
+ <span className="typography-body-medium-medium text-content-presentation-global-primary">
39
+ {props.label}
40
+ </span>
41
+ ) : (
42
+ <span />
43
+ )}
44
+ {addButton}
45
+ </div>
46
+ {props.description && (
47
+ <p className="typography-body-small-regular text-content-presentation-global-secondary">
48
+ {props.description}
49
+ </p>
50
+ )}
51
+
52
+ <div className="flex flex-col gap-3">
53
+ {fields.map((f, index) => (
54
+ <Card key={f.id} className="relative gap-3 pr-8">
55
+ <button
56
+ type="button"
57
+ aria-label="Remove"
58
+ onClick={() => remove(index)}
59
+ className="absolute right-2 top-2 inline-flex h-[22px] w-[22px] items-center justify-center rounded-full text-content-presentation-state-negative transition-colors hover:bg-background-presentation-action-hover"
60
+ >
61
+ <i className="ri-close-line text-[16px]" />
62
+ </button>
63
+ {props.children(`${props.name}.${index}`, index, () => remove(index))}
64
+ </Card>
65
+ ))}
66
+ {fields.length === 0 && (
67
+ <p className="typography-body-small-regular text-content-presentation-global-secondary">
68
+ No items yet.
69
+ </p>
70
+ )}
71
+ </div>
72
+ </section>
73
+ );
74
+ }
@@ -0,0 +1,148 @@
1
+ "use client";
2
+
3
+ import { ReactNode, useEffect } from "react";
4
+ import {
5
+ useFormContext,
6
+ useFormState,
7
+ type ControllerRenderProps,
8
+ type ControllerFieldState,
9
+ type FieldErrors,
10
+ type FieldValues,
11
+ type FieldPath,
12
+ } from "react-hook-form";
13
+
14
+ import { FieldSection } from "../../../layouts/FieldSection";
15
+ import { FormField, FormItem, FormControl } from "../../Form";
16
+ import { FieldHint } from "../../FieldHint";
17
+ import { Tooltip } from "../../Tooltip";
18
+ import { useDirection, useStepRegistry, useCell } from "../context";
19
+
20
+ export interface FieldShellProps {
21
+ name: string;
22
+ label?: ReactNode;
23
+ description?: ReactNode;
24
+ required?: boolean;
25
+ fullWidth?: boolean;
26
+ hidden?: boolean;
27
+ /** Force the field's layout direction, overriding the form's `useDirection()` context. */
28
+ direction?: "horizontal" | "vertical" | "flexible";
29
+ /** The input, wired to the react-hook-form field. */
30
+ children: (
31
+ field: ControllerRenderProps<FieldValues, string>,
32
+ fieldState: ControllerFieldState,
33
+ ) => ReactNode;
34
+ }
35
+
36
+ /**
37
+ * Shared wrapper for every `FormBuilder.*` field: the FieldSection row + the RHF
38
+ * `FormField`/`FormItem`/`FormControl`/`FormMessage` scaffolding. Also registers the
39
+ * field name into the enclosing `FormBuilder.Step` (if any) so the stepper can
40
+ * validate per step.
41
+ */
42
+ export function FieldShell({
43
+ name,
44
+ label,
45
+ description,
46
+ required,
47
+ fullWidth,
48
+ hidden,
49
+ direction: directionProp,
50
+ children,
51
+ }: FieldShellProps) {
52
+ const form = useFormContext();
53
+ const cell = useCell();
54
+ const ctxDirection = useDirection();
55
+ // A field may pin its own direction (e.g. RichText forces vertical), else the form's. When
56
+ // neither is set this stays `undefined` — FieldSection then falls back to its responsive
57
+ // `flexible` layout rather than a fixed two-column one.
58
+ const direction = directionProp ?? ctxDirection;
59
+ const step = useStepRegistry();
60
+
61
+ // Read this field's error at the shell level (not inside the FormField render) so
62
+ // the FieldHint can go in FieldSection's `childrenUnderLabel` — under the label.
63
+ // Subscribe to the whole `errors` object and resolve the field's path ourselves:
64
+ // `getFieldState(name)` misses controls that register late (DatePicker, RichText),
65
+ // whereas the errors object always carries every failing key from the resolver.
66
+ const { errors } = useFormState({ control: form.control });
67
+ const fieldError = resolveFieldError(errors, name);
68
+
69
+ useEffect(() => {
70
+ if (!step) return;
71
+ step.register(name);
72
+ return () => step.unregister(name);
73
+ }, [step, name]);
74
+
75
+ if (hidden) return null;
76
+
77
+ // Cell mode (inside FormBuilder.Table): render just the control — no FieldSection label/row.
78
+ // Errors surface as a tooltip on the control rather than a stacked FieldHint, so a row stays
79
+ // one line tall. Step registration above still applies.
80
+ if (cell) {
81
+ return (
82
+ <FormField
83
+ control={form.control}
84
+ name={name as FieldPath<FieldValues>}
85
+ render={({ field, fieldState }) => (
86
+ <FormItem className="w-full">
87
+ <FormControl>
88
+ <Tooltip
89
+ open={Boolean(fieldError)}
90
+ text={fieldError ?? ""}
91
+ toolTipSide="top"
92
+ variant="highlight"
93
+ >
94
+ <div className="w-full">{children(field, fieldState)}</div>
95
+ </Tooltip>
96
+ </FormControl>
97
+ </FormItem>
98
+ )}
99
+ />
100
+ );
101
+ }
102
+
103
+ return (
104
+ <FieldSection
105
+ label={label}
106
+ requiredLabel={required ? "(Required)" : undefined}
107
+ secondaryLabel={description}
108
+ direction={direction}
109
+ className={fullWidth ? "max-w-full" : undefined}
110
+ childrenUnderLabel={<FieldError message={fieldError} />}
111
+ >
112
+ <FormField
113
+ control={form.control}
114
+ name={name as FieldPath<FieldValues>}
115
+ render={({ field, fieldState }) => (
116
+ <FormItem className="w-full">
117
+ <FormControl>
118
+ <div className="w-full">{children(field, fieldState)}</div>
119
+ </FormControl>
120
+ </FormItem>
121
+ )}
122
+ />
123
+ </FieldSection>
124
+ );
125
+ }
126
+
127
+ /** Validation error, shown as a `FieldHint` alert (no tooltip); null when there's none. */
128
+ function FieldError({ message }: { message?: string }) {
129
+ if (!message) return null;
130
+ return <FieldHint state="error" label={message} />;
131
+ }
132
+
133
+ /**
134
+ * Resolve a field's error message from the RHF `errors` tree by its dotted `name`
135
+ * (handles nested field-array paths like `contacts.0.email`). Reading the errors
136
+ * object directly is more reliable than `getFieldState` for controls that register
137
+ * late (DatePicker, RichText), whose hints were otherwise missing.
138
+ */
139
+ function resolveFieldError(errors: FieldErrors, name: string): string | undefined {
140
+ const node = name
141
+ .split(".")
142
+ .reduce<unknown>(
143
+ (acc, key) => (acc == null ? undefined : (acc as Record<string, unknown>)[key]),
144
+ errors,
145
+ );
146
+ const message = (node as { message?: unknown } | undefined)?.message;
147
+ return typeof message === "string" ? message : undefined;
148
+ }
@@ -0,0 +1,39 @@
1
+ "use client";
2
+
3
+ import { ImageAttachment } from "../../ImageAttachment";
4
+ import { useLoading } from "../context";
5
+ import type { FileFieldProps } from "../types";
6
+ import { FieldShell } from "./FieldShell";
7
+
8
+ /** `FormBuilder.File` / `.Image` — stores the selected `File` (consumer uploads). */
9
+ export function FileField(props: FileFieldProps & { image?: boolean }) {
10
+ const loading = useLoading();
11
+ return (
12
+ <FieldShell {...props}>
13
+ {(field) => {
14
+ const current: unknown = field.value;
15
+ const fileName = Array.isArray(current)
16
+ ? `${current.length} file(s)`
17
+ : current instanceof File
18
+ ? current.name
19
+ : "";
20
+ return (
21
+ <ImageAttachment
22
+ mainLabel={props.placeholder ?? (props.image ? "Upload image" : "Upload file")}
23
+ secondaryLabel={
24
+ fileName || (typeof props.description === "string" ? props.description : "")
25
+ }
26
+ accept={props.accept ?? (props.image ? "image/*" : undefined)}
27
+ multiple={props.multiple}
28
+ disabled={props.disabled || loading}
29
+ onChange={(e) => {
30
+ const files = (e.target as HTMLInputElement).files;
31
+ if (!files || files.length === 0) return;
32
+ field.onChange(props.multiple ? Array.from(files) : files[0]);
33
+ }}
34
+ />
35
+ );
36
+ }}
37
+ </FieldShell>
38
+ );
39
+ }