@alepha/ui 0.25.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 (136) hide show
  1. package/README.md +12 -0
  2. package/components.json +20 -0
  3. package/package.json +85 -0
  4. package/src/components/action-error-toaster/action-error-toaster.tsx +70 -0
  5. package/src/components/admin/admin-audits.tsx +247 -0
  6. package/src/components/admin/admin-files.tsx +346 -0
  7. package/src/components/admin/admin-jobs-executions-panel.tsx +235 -0
  8. package/src/components/admin/admin-jobs-status-badge.tsx +33 -0
  9. package/src/components/admin/admin-jobs.tsx +294 -0
  10. package/src/components/admin/admin-keys.tsx +162 -0
  11. package/src/components/admin/admin-notifications.tsx +130 -0
  12. package/src/components/admin/admin-page.tsx +24 -0
  13. package/src/components/admin/admin-parameters.tsx +864 -0
  14. package/src/components/admin/admin-payments.tsx +93 -0
  15. package/src/components/admin/admin-sessions.tsx +174 -0
  16. package/src/components/admin/admin-user-detail-audits-tab.tsx +82 -0
  17. package/src/components/admin/admin-user-detail-identity-aside.tsx +218 -0
  18. package/src/components/admin/admin-user-detail-overview-tab.tsx +87 -0
  19. package/src/components/admin/admin-user-detail-password-dialog.tsx +85 -0
  20. package/src/components/admin/admin-user-detail-password-schema.ts +11 -0
  21. package/src/components/admin/admin-user-detail-profile-schema.ts +19 -0
  22. package/src/components/admin/admin-user-detail-provider-labels.ts +15 -0
  23. package/src/components/admin/admin-user-detail-security-tab.tsx +146 -0
  24. package/src/components/admin/admin-user-detail-sessions-tab.tsx +93 -0
  25. package/src/components/admin/admin-user-detail.tsx +606 -0
  26. package/src/components/admin/admin-users-roles-picker.tsx +126 -0
  27. package/src/components/admin/admin-users-status-filter.tsx +51 -0
  28. package/src/components/admin/admin-users.tsx +518 -0
  29. package/src/components/admin/diff-lines.spec.ts +58 -0
  30. package/src/components/admin/diff-lines.ts +58 -0
  31. package/src/components/admin/parameter-diff-dialog.tsx +119 -0
  32. package/src/components/admin/parameter-history-item.tsx +252 -0
  33. package/src/components/admin/parameter-json-dialog.tsx +93 -0
  34. package/src/components/admin/parameter-save-dialog.tsx +126 -0
  35. package/src/components/admin/use-confirmed-action.tsx +93 -0
  36. package/src/components/alepha-table/__tests__/use-table-selection.browser.spec.tsx +92 -0
  37. package/src/components/alepha-table/alepha-table.tsx +1158 -0
  38. package/src/components/alepha-table/use-table-selection.ts +95 -0
  39. package/src/components/app-shell/app-shell.tsx +568 -0
  40. package/src/components/auth/__tests__/auth-router.spec.ts +52 -0
  41. package/src/components/auth/auth-login.tsx +382 -0
  42. package/src/components/auth/auth-register.tsx +786 -0
  43. package/src/components/auth/auth-reset-password.tsx +349 -0
  44. package/src/components/auth/auth-router.tsx +126 -0
  45. package/src/components/auth/auth-verify-email.tsx +179 -0
  46. package/src/components/auto-form/auto-form.tsx +867 -0
  47. package/src/components/brand-icon/brand-icon.tsx +104 -0
  48. package/src/components/button-dark/button-dark.tsx +78 -0
  49. package/src/components/button-language/button-language.tsx +98 -0
  50. package/src/components/button-theme/button-theme.tsx +148 -0
  51. package/src/components/button-user/button-user.tsx +214 -0
  52. package/src/components/captcha/turnstile-widget.tsx +157 -0
  53. package/src/components/control/control.tsx +735 -0
  54. package/src/components/control-array/control-array.tsx +700 -0
  55. package/src/components/control-base/__tests__/form-field-a11y.browser.spec.tsx +93 -0
  56. package/src/components/control-base/field-i18n.ts +60 -0
  57. package/src/components/control-base/form-field.tsx +275 -0
  58. package/src/components/control-base/grid.tsx +59 -0
  59. package/src/components/control-base/icon-hint.tsx +64 -0
  60. package/src/components/control-date/control-date.tsx +190 -0
  61. package/src/components/control-number/control-number.tsx +136 -0
  62. package/src/components/control-object/control-object.tsx +200 -0
  63. package/src/components/control-select/control-select.tsx +774 -0
  64. package/src/components/control-upload/control-upload.tsx +475 -0
  65. package/src/components/file-image/file-image.tsx +52 -0
  66. package/src/components/language-toggle/language-toggle.tsx +66 -0
  67. package/src/components/markdown-view/markdown-view.css +114 -0
  68. package/src/components/markdown-view/markdown-view.tsx +92 -0
  69. package/src/components/nav-shell/nav-page.tsx +56 -0
  70. package/src/components/nav-shell/nav-shell.tsx +72 -0
  71. package/src/components/nav-shell/nav-tree-util.ts +62 -0
  72. package/src/components/nav-shell/spotlight.tsx +163 -0
  73. package/src/components/nav-shell/use-nav-breadcrumbs.ts +52 -0
  74. package/src/components/nav-shell/use-nav-entries.ts +101 -0
  75. package/src/components/nav-shell/use-nav-tree.ts +61 -0
  76. package/src/components/ui/accordion.tsx +77 -0
  77. package/src/components/ui/alert-dialog.tsx +186 -0
  78. package/src/components/ui/alert.tsx +75 -0
  79. package/src/components/ui/aspect-ratio.tsx +22 -0
  80. package/src/components/ui/avatar.tsx +106 -0
  81. package/src/components/ui/badge.tsx +51 -0
  82. package/src/components/ui/breadcrumb.tsx +121 -0
  83. package/src/components/ui/button-group.tsx +86 -0
  84. package/src/components/ui/button.tsx +83 -0
  85. package/src/components/ui/calendar.tsx +235 -0
  86. package/src/components/ui/card.tsx +102 -0
  87. package/src/components/ui/carousel.tsx +241 -0
  88. package/src/components/ui/chart.tsx +370 -0
  89. package/src/components/ui/checkbox.tsx +27 -0
  90. package/src/components/ui/collapsible.tsx +19 -0
  91. package/src/components/ui/combobox.tsx +298 -0
  92. package/src/components/ui/command.tsx +196 -0
  93. package/src/components/ui/context-menu.tsx +271 -0
  94. package/src/components/ui/dialog.tsx +153 -0
  95. package/src/components/ui/direction.tsx +4 -0
  96. package/src/components/ui/drawer.tsx +130 -0
  97. package/src/components/ui/dropdown-menu.tsx +269 -0
  98. package/src/components/ui/empty.tsx +100 -0
  99. package/src/components/ui/hover-card.tsx +50 -0
  100. package/src/components/ui/input-group.tsx +157 -0
  101. package/src/components/ui/input-otp.tsx +83 -0
  102. package/src/components/ui/input.tsx +19 -0
  103. package/src/components/ui/item.tsx +200 -0
  104. package/src/components/ui/kbd.tsx +26 -0
  105. package/src/components/ui/label.tsx +19 -0
  106. package/src/components/ui/menubar.tsx +283 -0
  107. package/src/components/ui/navigation-menu.tsx +170 -0
  108. package/src/components/ui/pagination.tsx +133 -0
  109. package/src/components/ui/popover.tsx +87 -0
  110. package/src/components/ui/progress.tsx +82 -0
  111. package/src/components/ui/radio-group.tsx +35 -0
  112. package/src/components/ui/resizable.tsx +49 -0
  113. package/src/components/ui/scroll-area.tsx +51 -0
  114. package/src/components/ui/segmented.tsx +232 -0
  115. package/src/components/ui/select.tsx +201 -0
  116. package/src/components/ui/separator.tsx +22 -0
  117. package/src/components/ui/sheet.tsx +133 -0
  118. package/src/components/ui/sidebar.tsx +723 -0
  119. package/src/components/ui/skeleton.tsx +13 -0
  120. package/src/components/ui/slider.tsx +51 -0
  121. package/src/components/ui/sonner.tsx +46 -0
  122. package/src/components/ui/spinner.tsx +15 -0
  123. package/src/components/ui/switch.tsx +29 -0
  124. package/src/components/ui/table.tsx +115 -0
  125. package/src/components/ui/tabs.tsx +79 -0
  126. package/src/components/ui/textarea.tsx +17 -0
  127. package/src/components/ui/toggle-group.tsx +86 -0
  128. package/src/components/ui/toggle.tsx +44 -0
  129. package/src/components/ui/tooltip.tsx +63 -0
  130. package/src/components/use-dialog/use-dialog.tsx +244 -0
  131. package/src/components/use-toast/use-toast.tsx +89 -0
  132. package/src/hooks/use-mobile.ts +21 -0
  133. package/src/lib/__tests__/i18n-fr.spec.ts +72 -0
  134. package/src/lib/i18n-fr.ts +500 -0
  135. package/src/lib/utils.ts +6 -0
  136. package/src/styles.css +129 -0
@@ -0,0 +1,93 @@
1
+ import { render, screen } from "@testing-library/react";
2
+ import { describe, expect, it } from "vitest";
3
+ import {
4
+ FormField,
5
+ formFieldAriaProps,
6
+ useFormFieldA11y,
7
+ } from "../form-field.tsx";
8
+
9
+ const Probe = () => {
10
+ const a11y = useFormFieldA11y();
11
+ return (
12
+ <input
13
+ data-testid="probe"
14
+ aria-invalid={a11y.invalid}
15
+ aria-describedby={a11y.describedBy}
16
+ />
17
+ );
18
+ };
19
+
20
+ describe("FormField a11y wiring", () => {
21
+ it("links the error message to the control via aria-describedby", () => {
22
+ render(
23
+ <FormField id="email" label="Email" error="Email is required">
24
+ <Probe />
25
+ </FormField>,
26
+ );
27
+ const input = screen.getByTestId("probe");
28
+ expect(input.getAttribute("aria-invalid")).toBe("true");
29
+ expect(input.getAttribute("aria-describedby")).toBe("email-error");
30
+
31
+ const error = screen.getByRole("alert");
32
+ expect(error.id).toBe("email-error");
33
+ expect(error.textContent).toContain("Email is required");
34
+ });
35
+
36
+ it("links the description when there is no error", () => {
37
+ render(
38
+ <FormField id="name" label="Name" description="Your public name">
39
+ <Probe />
40
+ </FormField>,
41
+ );
42
+ const input = screen.getByTestId("probe");
43
+ expect(input.getAttribute("aria-invalid")).toBe(null);
44
+ expect(input.getAttribute("aria-describedby")).toBe("name-description");
45
+ expect(document.getElementById("name-description")?.textContent).toBe(
46
+ "Your public name",
47
+ );
48
+ });
49
+
50
+ it("sets no aria attributes without id, error, or description", () => {
51
+ render(
52
+ <FormField label="Plain">
53
+ <Probe />
54
+ </FormField>,
55
+ );
56
+ const input = screen.getByTestId("probe");
57
+ expect(input.getAttribute("aria-invalid")).toBe(null);
58
+ expect(input.getAttribute("aria-describedby")).toBe(null);
59
+ });
60
+
61
+ it("formFieldAriaProps points at the same ids FormField renders", () => {
62
+ render(
63
+ <FormField id="pw" label="Password" error="Too short">
64
+ <input
65
+ data-testid="pw"
66
+ {...formFieldAriaProps({ id: "pw", error: "Too short" })}
67
+ />
68
+ </FormField>,
69
+ );
70
+ const input = screen.getByTestId("pw");
71
+ expect(input.getAttribute("aria-invalid")).toBe("true");
72
+ expect(input.getAttribute("aria-describedby")).toBe(
73
+ screen.getByRole("alert").id,
74
+ );
75
+
76
+ expect(
77
+ formFieldAriaProps({ id: "x", description: "d" })["aria-describedby"],
78
+ ).toBe("x-description");
79
+ expect(formFieldAriaProps({})["aria-describedby"]).toBeUndefined();
80
+ });
81
+
82
+ it("wires the row layout the same way", () => {
83
+ render(
84
+ <FormField id="bio" label="Bio" layout="row" error="Too long">
85
+ <Probe />
86
+ </FormField>,
87
+ );
88
+ const input = screen.getByTestId("probe");
89
+ expect(input.getAttribute("aria-invalid")).toBe("true");
90
+ expect(input.getAttribute("aria-describedby")).toBe("bio-error");
91
+ expect(screen.getByRole("alert").id).toBe("bio-error");
92
+ });
93
+ });
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Dictionary-driven labels and help text for schema-generated fields.
3
+ *
4
+ * A generated form can only prettify a property name — `reducedFactor` becomes
5
+ * "Reduced Factor", which tells an operator nothing. Given a prefix, every
6
+ * field can instead read a human label from `<prefix>.<field>` and help text
7
+ * from `<prefix>.<field>.desc`, translated like the rest of the app.
8
+ *
9
+ * The prefix is propagated INTO nested objects and array items
10
+ * (`<prefix>.payg.dailyCapCents`), so a deeply structured parameter documents
11
+ * itself all the way down instead of only at the top level.
12
+ *
13
+ * Explicit props always win; a schema `title`/`description` (via `.meta()`)
14
+ * is the fallback when the dictionary has no entry.
15
+ */
16
+ export interface FieldI18nProps {
17
+ label?: string;
18
+ description?: string;
19
+ }
20
+
21
+ /**
22
+ * Fill `label`/`description` from the dictionary when the caller left them
23
+ * unset. A missing key makes `tr` echo the key back, so compare against it —
24
+ * an absent entry must leave the field to its schema/pretty-name fallback.
25
+ */
26
+ export const resolveFieldI18n = (
27
+ tr: (key: string, options?: { default?: string }) => string,
28
+ prefix: string | undefined,
29
+ name: string,
30
+ current: FieldI18nProps,
31
+ ): FieldI18nProps => {
32
+ if (!prefix) {
33
+ return current;
34
+ }
35
+ const next: FieldI18nProps = { ...current };
36
+
37
+ if (next.label === undefined) {
38
+ const key = `${prefix}.${name}`;
39
+ const label = tr(key, { default: "" });
40
+ if (label && label !== key) {
41
+ next.label = label;
42
+ }
43
+ }
44
+
45
+ if (next.description === undefined) {
46
+ const key = `${prefix}.${name}.desc`;
47
+ const description = tr(key, { default: "" });
48
+ if (description && description !== key) {
49
+ next.description = description;
50
+ }
51
+ }
52
+
53
+ return next;
54
+ };
55
+
56
+ /** Prefix for a child of `name` — `parameters.x.payg` + `dailyCapCents`. */
57
+ export const childI18nPrefix = (
58
+ prefix: string | undefined,
59
+ name: string,
60
+ ): string | undefined => (prefix ? `${prefix}.${name}` : undefined);
@@ -0,0 +1,275 @@
1
+ import * as React from "react";
2
+
3
+ void React;
4
+
5
+ import { Label } from "@alepha/ui/components/ui/label";
6
+ import { cn } from "@alepha/ui/lib/utils";
7
+ import { createContext, type ReactNode, useContext, useMemo } from "react";
8
+
9
+ export type FormFieldLayout = "stack" | "row";
10
+
11
+ /**
12
+ * Ambient layout for every nested `<FormField>`. Defaults to `"stack"`.
13
+ * `<AutoForm layout="row">` wraps its tree in this context so every Control
14
+ * variant renders as a settings-style row without prop drilling.
15
+ */
16
+ const FormFieldLayoutContext = createContext<FormFieldLayout>("stack");
17
+
18
+ export function FormFieldLayoutProvider(props: {
19
+ value: FormFieldLayout;
20
+ children: ReactNode;
21
+ }) {
22
+ return (
23
+ <FormFieldLayoutContext.Provider value={props.value}>
24
+ {props.children}
25
+ </FormFieldLayoutContext.Provider>
26
+ );
27
+ }
28
+
29
+ /**
30
+ * Ambient flag enabling the inline save (tick) affordance on text Controls.
31
+ * Set by `<AutoForm autoSave>`; standalone Controls never show the tick
32
+ * unless explicitly placed inside this provider.
33
+ */
34
+ const FormFieldAutoSaveContext = createContext<boolean>(false);
35
+
36
+ export function FormFieldAutoSaveProvider(props: {
37
+ value: boolean;
38
+ children: ReactNode;
39
+ }) {
40
+ return (
41
+ <FormFieldAutoSaveContext.Provider value={props.value}>
42
+ {props.children}
43
+ </FormFieldAutoSaveContext.Provider>
44
+ );
45
+ }
46
+
47
+ /**
48
+ * Read the ambient auto-save flag (see {@link FormFieldAutoSaveProvider}).
49
+ */
50
+ export function useFormFieldAutoSave(): boolean {
51
+ return useContext(FormFieldAutoSaveContext);
52
+ }
53
+
54
+ /**
55
+ * Id of the error element a `<FormField id={id} error>` renders.
56
+ */
57
+ export function formFieldErrorId(
58
+ id?: string,
59
+ error?: string,
60
+ ): string | undefined {
61
+ return id && error ? `${id}-error` : undefined;
62
+ }
63
+
64
+ /**
65
+ * Id of the description element a `<FormField id={id} description>` renders
66
+ * (the description is replaced by the error when both are set).
67
+ */
68
+ export function formFieldDescriptionId(
69
+ id?: string,
70
+ description?: string,
71
+ error?: string,
72
+ ): string | undefined {
73
+ return id && description && !error ? `${id}-description` : undefined;
74
+ }
75
+
76
+ /**
77
+ * The aria attributes a control spreads on its native element when it
78
+ * renders its own `<FormField>` (id/error/description in hand). Widgets
79
+ * nested under someone else's FormField use {@link useFormFieldA11y}
80
+ * instead.
81
+ */
82
+ export function formFieldAriaProps(props: {
83
+ id?: string;
84
+ error?: string;
85
+ description?: string;
86
+ }): { "aria-invalid"?: true; "aria-describedby"?: string } {
87
+ return {
88
+ "aria-invalid": props.error ? true : undefined,
89
+ "aria-describedby":
90
+ formFieldErrorId(props.id, props.error) ??
91
+ formFieldDescriptionId(props.id, props.description, props.error),
92
+ };
93
+ }
94
+
95
+ export interface FormFieldA11y {
96
+ /**
97
+ * `true` when the surrounding FormField carries an error — mirror it
98
+ * onto the widget's `aria-invalid`.
99
+ */
100
+ invalid?: true;
101
+ /**
102
+ * Id of the FormField's error (or description) element — mirror it
103
+ * onto the widget's `aria-describedby`.
104
+ */
105
+ describedBy?: string;
106
+ }
107
+
108
+ const FormFieldA11yContext = createContext<FormFieldA11y>({});
109
+
110
+ /**
111
+ * Read the surrounding FormField's accessibility wiring. Leaf widgets
112
+ * spread the result onto their native element so screen readers announce
113
+ * the invalid state and the error/description text:
114
+ *
115
+ * ```tsx
116
+ * const a11y = useFormFieldA11y();
117
+ * <input aria-invalid={a11y.invalid} aria-describedby={a11y.describedBy} />
118
+ * ```
119
+ */
120
+ export function useFormFieldA11y(): FormFieldA11y {
121
+ return useContext(FormFieldA11yContext);
122
+ }
123
+
124
+ export interface FormFieldProps {
125
+ /**
126
+ * `id` linked to the inner control's `htmlFor`.
127
+ */
128
+ id?: string;
129
+ /**
130
+ * Label text rendered above the control.
131
+ */
132
+ label?: string;
133
+ /**
134
+ * Helper text rendered below the control.
135
+ */
136
+ description?: string;
137
+ /**
138
+ * Error message. When set, marks descendant inputs invalid via `data-invalid`.
139
+ */
140
+ error?: string;
141
+ /**
142
+ * Show a required marker (`*`) next to the label.
143
+ */
144
+ required?: boolean;
145
+ /**
146
+ * Extra classes applied to the wrapper.
147
+ */
148
+ className?: string;
149
+ /**
150
+ * Layout variant.
151
+ * - `"stack"` (default): label on top, control below, description under.
152
+ * - `"row"`: settings-style — label/description on the left, control on
153
+ * the right. Stacks on narrow viewports.
154
+ *
155
+ * When omitted, falls back to the ambient `<FormFieldLayoutProvider>`.
156
+ */
157
+ layout?: FormFieldLayout;
158
+ /**
159
+ * The control to wrap.
160
+ */
161
+ children: ReactNode;
162
+ }
163
+
164
+ /**
165
+ * Label + description + error wrapper shared by every Control variant.
166
+ *
167
+ * When `error` is set, applies a red ring to any descendant `<input>`,
168
+ * `<textarea>`, or trigger button via the `data-invalid` attribute (so we
169
+ * don't have to thread `error` through every leaf widget).
170
+ */
171
+ export function FormField(props: FormFieldProps) {
172
+ const ambient = useContext(FormFieldLayoutContext);
173
+ const layout = props.layout ?? ambient;
174
+ const invalidClasses = props.error
175
+ ? "[&_input]:border-destructive [&_input]:focus-visible:ring-destructive/30 [&_textarea]:border-destructive [&_textarea]:focus-visible:ring-destructive/30 [&_[role=combobox]]:border-destructive"
176
+ : "";
177
+ const dataInvalid = props.error ? true : undefined;
178
+
179
+ const errorId = formFieldErrorId(props.id, props.error);
180
+ const descriptionId = formFieldDescriptionId(
181
+ props.id,
182
+ props.description,
183
+ props.error,
184
+ );
185
+ const a11y = useMemo<FormFieldA11y>(
186
+ () => ({
187
+ invalid: props.error ? true : undefined,
188
+ describedBy: errorId ?? descriptionId,
189
+ }),
190
+ [props.error, errorId, descriptionId],
191
+ );
192
+ const children = (
193
+ <FormFieldA11yContext.Provider value={a11y}>
194
+ {props.children}
195
+ </FormFieldA11yContext.Provider>
196
+ );
197
+
198
+ if (layout === "row") {
199
+ return (
200
+ <div
201
+ className={cn(
202
+ "flex flex-col gap-3 px-4 py-3 sm:grid sm:grid-cols-[minmax(0,1fr)_auto] sm:items-center sm:gap-6",
203
+ invalidClasses,
204
+ props.className,
205
+ )}
206
+ data-invalid={dataInvalid}
207
+ >
208
+ <div className="flex flex-col gap-0.5">
209
+ {props.label && (
210
+ <Label htmlFor={props.id} className="font-medium">
211
+ {props.label}
212
+ {props.required && (
213
+ <span className="text-destructive ml-0.5" aria-hidden>
214
+ *
215
+ </span>
216
+ )}
217
+ </Label>
218
+ )}
219
+ {props.description && !props.error && (
220
+ <p id={descriptionId} className="text-muted-foreground text-xs">
221
+ {props.description}
222
+ </p>
223
+ )}
224
+ {props.error && (
225
+ <p
226
+ id={errorId}
227
+ className="text-destructive text-xs flex items-center gap-1"
228
+ role="alert"
229
+ >
230
+ <span aria-hidden>⚠</span>
231
+ {props.error}
232
+ </p>
233
+ )}
234
+ </div>
235
+ <div className="flex min-w-0 justify-start sm:justify-end">
236
+ {children}
237
+ </div>
238
+ </div>
239
+ );
240
+ }
241
+
242
+ return (
243
+ <div
244
+ className={cn("flex flex-col gap-1.5", invalidClasses, props.className)}
245
+ data-invalid={dataInvalid}
246
+ >
247
+ {props.label && (
248
+ <Label htmlFor={props.id}>
249
+ {props.label}
250
+ {props.required && (
251
+ <span className="text-destructive ml-0.5" aria-hidden>
252
+ *
253
+ </span>
254
+ )}
255
+ </Label>
256
+ )}
257
+ {children}
258
+ {props.description && !props.error && (
259
+ <p id={descriptionId} className="text-muted-foreground text-xs">
260
+ {props.description}
261
+ </p>
262
+ )}
263
+ {props.error && (
264
+ <p
265
+ id={errorId}
266
+ className="text-destructive text-xs flex items-center gap-1"
267
+ role="alert"
268
+ >
269
+ <span aria-hidden>⚠</span>
270
+ {props.error}
271
+ </p>
272
+ )}
273
+ </div>
274
+ );
275
+ }
@@ -0,0 +1,59 @@
1
+ import * as React from "react";
2
+
3
+ void React;
4
+
5
+ import { z } from "alepha";
6
+ import type { BaseInputField } from "alepha/react/form";
7
+
8
+ /**
9
+ * Decides the column-span (out of 12) for a control inside a 12-col grid.
10
+ *
11
+ * Rules (mirroring the legacy `CreateFormGroup.getBestSizeForInput`):
12
+ * - explicit `$control.width` or per-field `width` override → use it
13
+ * - `$control.area` / `file` / `upload` preset → 100% (full row)
14
+ * - long text (`maxLength >= 256`) → 100%
15
+ * - nested object / array of objects → 100%
16
+ * - array of primitives → 66% (2/3 row)
17
+ * - default → 33% (1/3 row)
18
+ */
19
+ export const widthFor = (input: BaseInputField, override?: number): number => {
20
+ if (override) return override;
21
+ // Peel optional/nullable/default wrappers; `$control` rides on `.meta()`.
22
+ const schema = z.schema.unwrap(input.schema) as {
23
+ maxLength?: number;
24
+ element?: unknown;
25
+ meta?: () => {
26
+ $control?: {
27
+ width?: number;
28
+ area?: boolean;
29
+ file?: boolean;
30
+ upload?: unknown;
31
+ };
32
+ };
33
+ };
34
+ const control = schema?.meta?.()?.$control;
35
+ if (typeof control?.width === "number") return control.width;
36
+ if (control?.area || control?.file || control?.upload) return 100;
37
+ if ((schema?.maxLength ?? 0) >= 256) return 100;
38
+ if (z.schema.isObject(schema)) return 100;
39
+ if (z.schema.isArray(schema)) {
40
+ const element = z.schema.unwrap(schema.element);
41
+ if (z.schema.isObject(element)) return 100;
42
+ if (z.schema.isString(element) || z.schema.isNumber(element)) return 66;
43
+ return 100;
44
+ }
45
+ return 33;
46
+ };
47
+
48
+ /**
49
+ * Maps a percentage width to a Tailwind `col-span-N` class on a 12-col grid.
50
+ */
51
+ export const spanClass = (width: number): string => {
52
+ if (width >= 100) return "col-span-12";
53
+ if (width >= 75) return "col-span-9";
54
+ if (width >= 66) return "col-span-8";
55
+ if (width >= 50) return "col-span-6";
56
+ if (width >= 33) return "col-span-4";
57
+ if (width >= 25) return "col-span-3";
58
+ return "col-span-4";
59
+ };
@@ -0,0 +1,64 @@
1
+ import * as React from "react";
2
+
3
+ void React;
4
+
5
+ import type { IconHint } from "alepha/react/form";
6
+ import {
7
+ AtSign,
8
+ Calendar,
9
+ Clock,
10
+ Cog,
11
+ Download,
12
+ File,
13
+ Hash,
14
+ Key,
15
+ Link,
16
+ List,
17
+ Lock,
18
+ Phone,
19
+ RotateCcw,
20
+ ToggleLeft,
21
+ Type,
22
+ Upload,
23
+ User,
24
+ Wrench,
25
+ } from "lucide-react";
26
+ import type { ComponentType, SVGProps } from "react";
27
+
28
+ export type IconComponent = ComponentType<SVGProps<SVGSVGElement>>;
29
+
30
+ const map: Record<IconHint, IconComponent> = {
31
+ email: AtSign,
32
+ password: Lock,
33
+ phone: Phone,
34
+ url: Link,
35
+ number: Hash,
36
+ calendar: Calendar,
37
+ clock: Clock,
38
+ list: List,
39
+ text: Type,
40
+ user: User,
41
+ file: File,
42
+ switch: ToggleLeft,
43
+ };
44
+
45
+ const namedIcons: Record<string, IconComponent> = {
46
+ ...map,
47
+ key: Key,
48
+ cog: Cog,
49
+ download: Download,
50
+ upload: Upload,
51
+ wrench: Wrench,
52
+ "rotate-ccw": RotateCcw,
53
+ };
54
+
55
+ /**
56
+ * Maps a semantic {@link IconHint} or an icon name string to a lucide
57
+ * component. Returns `undefined` if not found.
58
+ */
59
+ export const iconFor = (
60
+ hint?: IconHint | string | null,
61
+ ): IconComponent | undefined => {
62
+ if (!hint) return undefined;
63
+ return namedIcons[hint as string];
64
+ };