@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.
- package/README.md +12 -0
- package/components.json +20 -0
- package/package.json +85 -0
- package/src/components/action-error-toaster/action-error-toaster.tsx +70 -0
- package/src/components/admin/admin-audits.tsx +247 -0
- package/src/components/admin/admin-files.tsx +346 -0
- package/src/components/admin/admin-jobs-executions-panel.tsx +235 -0
- package/src/components/admin/admin-jobs-status-badge.tsx +33 -0
- package/src/components/admin/admin-jobs.tsx +294 -0
- package/src/components/admin/admin-keys.tsx +162 -0
- package/src/components/admin/admin-notifications.tsx +130 -0
- package/src/components/admin/admin-page.tsx +24 -0
- package/src/components/admin/admin-parameters.tsx +864 -0
- package/src/components/admin/admin-payments.tsx +93 -0
- package/src/components/admin/admin-sessions.tsx +174 -0
- package/src/components/admin/admin-user-detail-audits-tab.tsx +82 -0
- package/src/components/admin/admin-user-detail-identity-aside.tsx +218 -0
- package/src/components/admin/admin-user-detail-overview-tab.tsx +87 -0
- package/src/components/admin/admin-user-detail-password-dialog.tsx +85 -0
- package/src/components/admin/admin-user-detail-password-schema.ts +11 -0
- package/src/components/admin/admin-user-detail-profile-schema.ts +19 -0
- package/src/components/admin/admin-user-detail-provider-labels.ts +15 -0
- package/src/components/admin/admin-user-detail-security-tab.tsx +146 -0
- package/src/components/admin/admin-user-detail-sessions-tab.tsx +93 -0
- package/src/components/admin/admin-user-detail.tsx +606 -0
- package/src/components/admin/admin-users-roles-picker.tsx +126 -0
- package/src/components/admin/admin-users-status-filter.tsx +51 -0
- package/src/components/admin/admin-users.tsx +518 -0
- package/src/components/admin/diff-lines.spec.ts +58 -0
- package/src/components/admin/diff-lines.ts +58 -0
- package/src/components/admin/parameter-diff-dialog.tsx +119 -0
- package/src/components/admin/parameter-history-item.tsx +252 -0
- package/src/components/admin/parameter-json-dialog.tsx +93 -0
- package/src/components/admin/parameter-save-dialog.tsx +126 -0
- package/src/components/admin/use-confirmed-action.tsx +93 -0
- package/src/components/alepha-table/__tests__/use-table-selection.browser.spec.tsx +92 -0
- package/src/components/alepha-table/alepha-table.tsx +1158 -0
- package/src/components/alepha-table/use-table-selection.ts +95 -0
- package/src/components/app-shell/app-shell.tsx +568 -0
- package/src/components/auth/__tests__/auth-router.spec.ts +52 -0
- package/src/components/auth/auth-login.tsx +382 -0
- package/src/components/auth/auth-register.tsx +786 -0
- package/src/components/auth/auth-reset-password.tsx +349 -0
- package/src/components/auth/auth-router.tsx +126 -0
- package/src/components/auth/auth-verify-email.tsx +179 -0
- package/src/components/auto-form/auto-form.tsx +867 -0
- package/src/components/brand-icon/brand-icon.tsx +104 -0
- package/src/components/button-dark/button-dark.tsx +78 -0
- package/src/components/button-language/button-language.tsx +98 -0
- package/src/components/button-theme/button-theme.tsx +148 -0
- package/src/components/button-user/button-user.tsx +214 -0
- package/src/components/captcha/turnstile-widget.tsx +157 -0
- package/src/components/control/control.tsx +735 -0
- package/src/components/control-array/control-array.tsx +700 -0
- package/src/components/control-base/__tests__/form-field-a11y.browser.spec.tsx +93 -0
- package/src/components/control-base/field-i18n.ts +60 -0
- package/src/components/control-base/form-field.tsx +275 -0
- package/src/components/control-base/grid.tsx +59 -0
- package/src/components/control-base/icon-hint.tsx +64 -0
- package/src/components/control-date/control-date.tsx +190 -0
- package/src/components/control-number/control-number.tsx +136 -0
- package/src/components/control-object/control-object.tsx +200 -0
- package/src/components/control-select/control-select.tsx +774 -0
- package/src/components/control-upload/control-upload.tsx +475 -0
- package/src/components/file-image/file-image.tsx +52 -0
- package/src/components/language-toggle/language-toggle.tsx +66 -0
- package/src/components/markdown-view/markdown-view.css +114 -0
- package/src/components/markdown-view/markdown-view.tsx +92 -0
- package/src/components/nav-shell/nav-page.tsx +56 -0
- package/src/components/nav-shell/nav-shell.tsx +72 -0
- package/src/components/nav-shell/nav-tree-util.ts +62 -0
- package/src/components/nav-shell/spotlight.tsx +163 -0
- package/src/components/nav-shell/use-nav-breadcrumbs.ts +52 -0
- package/src/components/nav-shell/use-nav-entries.ts +101 -0
- package/src/components/nav-shell/use-nav-tree.ts +61 -0
- package/src/components/ui/accordion.tsx +77 -0
- package/src/components/ui/alert-dialog.tsx +186 -0
- package/src/components/ui/alert.tsx +75 -0
- package/src/components/ui/aspect-ratio.tsx +22 -0
- package/src/components/ui/avatar.tsx +106 -0
- package/src/components/ui/badge.tsx +51 -0
- package/src/components/ui/breadcrumb.tsx +121 -0
- package/src/components/ui/button-group.tsx +86 -0
- package/src/components/ui/button.tsx +83 -0
- package/src/components/ui/calendar.tsx +235 -0
- package/src/components/ui/card.tsx +102 -0
- package/src/components/ui/carousel.tsx +241 -0
- package/src/components/ui/chart.tsx +370 -0
- package/src/components/ui/checkbox.tsx +27 -0
- package/src/components/ui/collapsible.tsx +19 -0
- package/src/components/ui/combobox.tsx +298 -0
- package/src/components/ui/command.tsx +196 -0
- package/src/components/ui/context-menu.tsx +271 -0
- package/src/components/ui/dialog.tsx +153 -0
- package/src/components/ui/direction.tsx +4 -0
- package/src/components/ui/drawer.tsx +130 -0
- package/src/components/ui/dropdown-menu.tsx +269 -0
- package/src/components/ui/empty.tsx +100 -0
- package/src/components/ui/hover-card.tsx +50 -0
- package/src/components/ui/input-group.tsx +157 -0
- package/src/components/ui/input-otp.tsx +83 -0
- package/src/components/ui/input.tsx +19 -0
- package/src/components/ui/item.tsx +200 -0
- package/src/components/ui/kbd.tsx +26 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/menubar.tsx +283 -0
- package/src/components/ui/navigation-menu.tsx +170 -0
- package/src/components/ui/pagination.tsx +133 -0
- package/src/components/ui/popover.tsx +87 -0
- package/src/components/ui/progress.tsx +82 -0
- package/src/components/ui/radio-group.tsx +35 -0
- package/src/components/ui/resizable.tsx +49 -0
- package/src/components/ui/scroll-area.tsx +51 -0
- package/src/components/ui/segmented.tsx +232 -0
- package/src/components/ui/select.tsx +201 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/sheet.tsx +133 -0
- package/src/components/ui/sidebar.tsx +723 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/slider.tsx +51 -0
- package/src/components/ui/sonner.tsx +46 -0
- package/src/components/ui/spinner.tsx +15 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +115 -0
- package/src/components/ui/tabs.tsx +79 -0
- package/src/components/ui/textarea.tsx +17 -0
- package/src/components/ui/toggle-group.tsx +86 -0
- package/src/components/ui/toggle.tsx +44 -0
- package/src/components/ui/tooltip.tsx +63 -0
- package/src/components/use-dialog/use-dialog.tsx +244 -0
- package/src/components/use-toast/use-toast.tsx +89 -0
- package/src/hooks/use-mobile.ts +21 -0
- package/src/lib/__tests__/i18n-fr.spec.ts +72 -0
- package/src/lib/i18n-fr.ts +500 -0
- package/src/lib/utils.ts +6 -0
- package/src/styles.css +129 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
void React;
|
|
4
|
+
|
|
5
|
+
import { FormField } from "@alepha/ui/components/control-base/form-field";
|
|
6
|
+
import { Button } from "@alepha/ui/components/ui/button";
|
|
7
|
+
import { Calendar } from "@alepha/ui/components/ui/calendar";
|
|
8
|
+
import { Input } from "@alepha/ui/components/ui/input";
|
|
9
|
+
import {
|
|
10
|
+
Popover,
|
|
11
|
+
PopoverContent,
|
|
12
|
+
PopoverTrigger,
|
|
13
|
+
} from "@alepha/ui/components/ui/popover";
|
|
14
|
+
import { cn } from "@alepha/ui/lib/utils";
|
|
15
|
+
import {
|
|
16
|
+
type BaseInputField,
|
|
17
|
+
parseField,
|
|
18
|
+
useFieldValue,
|
|
19
|
+
useFormState,
|
|
20
|
+
} from "alepha/react/form";
|
|
21
|
+
import { Calendar as CalendarIcon, Clock } from "lucide-react";
|
|
22
|
+
import { useState } from "react";
|
|
23
|
+
|
|
24
|
+
export interface ControlDateProps {
|
|
25
|
+
/**
|
|
26
|
+
* Bound `InputField` from `useForm`. Stores an ISO string.
|
|
27
|
+
*/
|
|
28
|
+
input: BaseInputField;
|
|
29
|
+
/**
|
|
30
|
+
* Field label. Falls back to schema `title`.
|
|
31
|
+
*/
|
|
32
|
+
label?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Helper text shown below the picker.
|
|
35
|
+
*/
|
|
36
|
+
description?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Force date-only mode regardless of schema format.
|
|
39
|
+
*/
|
|
40
|
+
date?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Force date-time mode regardless of schema format.
|
|
43
|
+
*/
|
|
44
|
+
datetime?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Force time-only mode regardless of schema format.
|
|
47
|
+
*/
|
|
48
|
+
time?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Disable the picker.
|
|
51
|
+
*/
|
|
52
|
+
disabled?: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function ControlDate(props: ControlDateProps) {
|
|
56
|
+
const form = useFormState(props.input, ["error"]);
|
|
57
|
+
const [value, setValue] = useFieldValue(props.input);
|
|
58
|
+
|
|
59
|
+
if (!props.input?.props) return null;
|
|
60
|
+
|
|
61
|
+
const meta = parseField(props.input, {
|
|
62
|
+
label: props.label,
|
|
63
|
+
description: props.description,
|
|
64
|
+
error: form.error,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const isDateTime = props.datetime || meta.format === "date-time";
|
|
68
|
+
const isTime = props.time || meta.format === "time";
|
|
69
|
+
|
|
70
|
+
if (isTime) {
|
|
71
|
+
return (
|
|
72
|
+
<FormField
|
|
73
|
+
id={meta.id}
|
|
74
|
+
label={meta.label}
|
|
75
|
+
description={meta.description}
|
|
76
|
+
error={meta.error}
|
|
77
|
+
required={meta.required}
|
|
78
|
+
>
|
|
79
|
+
<div className="relative">
|
|
80
|
+
<Clock className="text-muted-foreground absolute top-1/2 left-3 size-4 -translate-y-1/2" />
|
|
81
|
+
<Input
|
|
82
|
+
id={meta.id}
|
|
83
|
+
name={props.input.props.name}
|
|
84
|
+
type="time"
|
|
85
|
+
className="pl-9"
|
|
86
|
+
disabled={props.disabled}
|
|
87
|
+
value={value ?? ""}
|
|
88
|
+
onChange={(e) => setValue(e.target.value)}
|
|
89
|
+
/>
|
|
90
|
+
</div>
|
|
91
|
+
</FormField>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<FormField
|
|
97
|
+
id={meta.id}
|
|
98
|
+
label={meta.label}
|
|
99
|
+
description={meta.description}
|
|
100
|
+
error={meta.error}
|
|
101
|
+
required={meta.required}
|
|
102
|
+
>
|
|
103
|
+
<DatePopover
|
|
104
|
+
id={meta.id}
|
|
105
|
+
value={value}
|
|
106
|
+
withTime={isDateTime}
|
|
107
|
+
disabled={props.disabled}
|
|
108
|
+
onChange={(v) => setValue(v)}
|
|
109
|
+
/>
|
|
110
|
+
</FormField>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface DatePopoverProps {
|
|
115
|
+
id?: string;
|
|
116
|
+
value?: string;
|
|
117
|
+
withTime: boolean;
|
|
118
|
+
disabled?: boolean;
|
|
119
|
+
onChange: (value: string | undefined) => void;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function DatePopover(props: DatePopoverProps) {
|
|
123
|
+
const [open, setOpen] = useState(false);
|
|
124
|
+
const date = props.value ? new Date(props.value) : undefined;
|
|
125
|
+
|
|
126
|
+
const formatted = date
|
|
127
|
+
? props.withTime
|
|
128
|
+
? date.toLocaleString()
|
|
129
|
+
: date.toLocaleDateString()
|
|
130
|
+
: "";
|
|
131
|
+
|
|
132
|
+
const handleDate = (d: Date | undefined) => {
|
|
133
|
+
if (!d) {
|
|
134
|
+
props.onChange(undefined);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (props.withTime) {
|
|
138
|
+
props.onChange(d.toISOString());
|
|
139
|
+
} else {
|
|
140
|
+
props.onChange(d.toISOString().slice(0, 10));
|
|
141
|
+
setOpen(false);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const handleTime = (timeStr: string) => {
|
|
146
|
+
if (!date) return;
|
|
147
|
+
const [h, m] = timeStr.split(":").map(Number);
|
|
148
|
+
const next = new Date(date);
|
|
149
|
+
next.setHours(h ?? 0, m ?? 0, 0, 0);
|
|
150
|
+
props.onChange(next.toISOString());
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const timeValue =
|
|
154
|
+
date && props.withTime
|
|
155
|
+
? `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`
|
|
156
|
+
: "";
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
160
|
+
<PopoverTrigger
|
|
161
|
+
render={
|
|
162
|
+
<Button
|
|
163
|
+
id={props.id}
|
|
164
|
+
variant="outline"
|
|
165
|
+
disabled={props.disabled}
|
|
166
|
+
className={cn(
|
|
167
|
+
"w-full justify-start text-left font-normal",
|
|
168
|
+
!date && "text-muted-foreground",
|
|
169
|
+
)}
|
|
170
|
+
/>
|
|
171
|
+
}
|
|
172
|
+
>
|
|
173
|
+
<CalendarIcon className="mr-2 size-4" />
|
|
174
|
+
{formatted || "Pick a date"}
|
|
175
|
+
</PopoverTrigger>
|
|
176
|
+
<PopoverContent className="w-auto p-0" align="start">
|
|
177
|
+
<Calendar mode="single" selected={date} onSelect={handleDate} />
|
|
178
|
+
{props.withTime && (
|
|
179
|
+
<div className="border-t p-3">
|
|
180
|
+
<Input
|
|
181
|
+
type="time"
|
|
182
|
+
value={timeValue}
|
|
183
|
+
onChange={(e) => handleTime(e.target.value)}
|
|
184
|
+
/>
|
|
185
|
+
</div>
|
|
186
|
+
)}
|
|
187
|
+
</PopoverContent>
|
|
188
|
+
</Popover>
|
|
189
|
+
);
|
|
190
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
void React;
|
|
4
|
+
|
|
5
|
+
import { FormField } from "@alepha/ui/components/control-base/form-field";
|
|
6
|
+
import { iconFor } from "@alepha/ui/components/control-base/icon-hint";
|
|
7
|
+
import { Input } from "@alepha/ui/components/ui/input";
|
|
8
|
+
import { Slider } from "@alepha/ui/components/ui/slider";
|
|
9
|
+
import {
|
|
10
|
+
type BaseInputField,
|
|
11
|
+
parseField,
|
|
12
|
+
useFieldValue,
|
|
13
|
+
useFormState,
|
|
14
|
+
} from "alepha/react/form";
|
|
15
|
+
|
|
16
|
+
export interface ControlNumberProps {
|
|
17
|
+
/**
|
|
18
|
+
* Bound numeric `InputField` from `useForm`.
|
|
19
|
+
*/
|
|
20
|
+
input: BaseInputField;
|
|
21
|
+
/**
|
|
22
|
+
* Field label. Falls back to schema `title`.
|
|
23
|
+
*/
|
|
24
|
+
label?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Helper text shown below the input.
|
|
27
|
+
*/
|
|
28
|
+
description?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Render as a slider instead of a number input.
|
|
31
|
+
*/
|
|
32
|
+
slider?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Minimum allowed value. Falls back to schema `minimum`.
|
|
35
|
+
*/
|
|
36
|
+
min?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Maximum allowed value. Falls back to schema `maximum`.
|
|
39
|
+
*/
|
|
40
|
+
max?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Step size. Falls back to schema `multipleOf`, then 1.
|
|
43
|
+
*/
|
|
44
|
+
step?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Disable the input.
|
|
47
|
+
*/
|
|
48
|
+
disabled?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function ControlNumber(props: ControlNumberProps) {
|
|
52
|
+
const form = useFormState(props.input, ["error"]);
|
|
53
|
+
const [value, setValue] = useFieldValue(props.input);
|
|
54
|
+
|
|
55
|
+
if (!props.input?.props) return null;
|
|
56
|
+
|
|
57
|
+
const meta = parseField(props.input, {
|
|
58
|
+
label: props.label,
|
|
59
|
+
description: props.description,
|
|
60
|
+
error: form.error,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const min = props.min ?? meta.constraints.minimum;
|
|
64
|
+
const max = props.max ?? meta.constraints.maximum;
|
|
65
|
+
|
|
66
|
+
if (props.slider) {
|
|
67
|
+
const lo = min ?? 0;
|
|
68
|
+
const hi = max ?? 100;
|
|
69
|
+
return (
|
|
70
|
+
<FormField
|
|
71
|
+
id={meta.id}
|
|
72
|
+
label={meta.label}
|
|
73
|
+
description={meta.description}
|
|
74
|
+
error={meta.error}
|
|
75
|
+
required={meta.required}
|
|
76
|
+
>
|
|
77
|
+
<div className="flex items-center gap-3 py-2">
|
|
78
|
+
<Slider
|
|
79
|
+
id={meta.id}
|
|
80
|
+
min={lo}
|
|
81
|
+
max={hi}
|
|
82
|
+
step={props.step ?? 1}
|
|
83
|
+
disabled={props.disabled}
|
|
84
|
+
value={[Number(value ?? lo)]}
|
|
85
|
+
onValueChange={(v) => setValue(Array.isArray(v) ? v[0] : v)}
|
|
86
|
+
className="flex-1"
|
|
87
|
+
/>
|
|
88
|
+
<span className="text-muted-foreground w-10 text-right text-sm tabular-nums">
|
|
89
|
+
{value ?? lo}
|
|
90
|
+
</span>
|
|
91
|
+
</div>
|
|
92
|
+
</FormField>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const Icon = iconFor(meta.iconHint);
|
|
97
|
+
return (
|
|
98
|
+
<FormField
|
|
99
|
+
id={meta.id}
|
|
100
|
+
label={meta.label}
|
|
101
|
+
description={meta.description}
|
|
102
|
+
error={meta.error}
|
|
103
|
+
required={meta.required}
|
|
104
|
+
>
|
|
105
|
+
<div className="relative">
|
|
106
|
+
{Icon && (
|
|
107
|
+
<Icon className="text-muted-foreground absolute top-1/2 left-3 size-4 -translate-y-1/2" />
|
|
108
|
+
)}
|
|
109
|
+
<Input
|
|
110
|
+
id={meta.id}
|
|
111
|
+
/*
|
|
112
|
+
* Text controls carry the field name; numeric ones did not, so a number
|
|
113
|
+
* rendered as `<input type="number">` with no `name` at all. Two things
|
|
114
|
+
* depended on it: `AutoForm`'s scroll-to-first-error looks the field up
|
|
115
|
+
* with `document.querySelector('[name="…"]')`, so it silently gave up on
|
|
116
|
+
* numeric fields, and the browser cannot label an unnamed field for
|
|
117
|
+
* autofill. Found while writing e2e tests against a price field that no
|
|
118
|
+
* selector could reach.
|
|
119
|
+
*/
|
|
120
|
+
name={props.input.props.name}
|
|
121
|
+
type="number"
|
|
122
|
+
min={min}
|
|
123
|
+
max={max}
|
|
124
|
+
step={props.step}
|
|
125
|
+
disabled={props.disabled}
|
|
126
|
+
value={value ?? ""}
|
|
127
|
+
onChange={(e) => {
|
|
128
|
+
const v = e.target.value;
|
|
129
|
+
setValue(v === "" ? undefined : Number(v));
|
|
130
|
+
}}
|
|
131
|
+
className={Icon ? "pl-9" : undefined}
|
|
132
|
+
/>
|
|
133
|
+
</div>
|
|
134
|
+
</FormField>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
void React;
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
Control,
|
|
7
|
+
type ControlProps,
|
|
8
|
+
} from "@alepha/ui/components/control/control";
|
|
9
|
+
import { spanClass, widthFor } from "@alepha/ui/components/control-base/grid";
|
|
10
|
+
import { Button } from "@alepha/ui/components/ui/button";
|
|
11
|
+
import { type ZObject, z } from "alepha";
|
|
12
|
+
import {
|
|
13
|
+
type BaseInputField,
|
|
14
|
+
type ObjectInputField,
|
|
15
|
+
parseField,
|
|
16
|
+
useFieldValue,
|
|
17
|
+
useFormState,
|
|
18
|
+
} from "alepha/react/form";
|
|
19
|
+
import { useI18n } from "alepha/react/i18n";
|
|
20
|
+
import { ChevronDown, ChevronRight, Plus, Trash2 } from "lucide-react";
|
|
21
|
+
import { useState } from "react";
|
|
22
|
+
import {
|
|
23
|
+
childI18nPrefix,
|
|
24
|
+
resolveFieldI18n,
|
|
25
|
+
} from "../control-base/field-i18n.ts";
|
|
26
|
+
|
|
27
|
+
export interface ControlObjectProps {
|
|
28
|
+
/**
|
|
29
|
+
* Bound object `InputField` from `useForm`.
|
|
30
|
+
*/
|
|
31
|
+
input: BaseInputField;
|
|
32
|
+
/**
|
|
33
|
+
* Field label. Falls back to schema `title`.
|
|
34
|
+
*/
|
|
35
|
+
label?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Helper text shown below the group.
|
|
38
|
+
*/
|
|
39
|
+
description?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Number of grid columns. @default 1
|
|
42
|
+
*/
|
|
43
|
+
columns?: 1 | 2 | 3 | 4;
|
|
44
|
+
/**
|
|
45
|
+
* "fieldset" wraps in a bordered container with legend. "plain" renders fields only.
|
|
46
|
+
*/
|
|
47
|
+
variant?: "fieldset" | "plain";
|
|
48
|
+
/**
|
|
49
|
+
* Per-field control props override, keyed by field name.
|
|
50
|
+
*/
|
|
51
|
+
controlProps?: Record<string, Partial<Omit<ControlProps, "input">>>;
|
|
52
|
+
/**
|
|
53
|
+
* Disable the group and all inner controls.
|
|
54
|
+
*/
|
|
55
|
+
disabled?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Default expanded state. @default true
|
|
58
|
+
*/
|
|
59
|
+
defaultExpanded?: boolean;
|
|
60
|
+
/** Dictionary prefix for this object's children (see `resolveFieldI18n`). */
|
|
61
|
+
i18nPrefix?: string;
|
|
62
|
+
/**
|
|
63
|
+
* Allow the user to clear the object (sets value to undefined).
|
|
64
|
+
*/
|
|
65
|
+
clearable?: boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function ControlObject(props: ControlObjectProps) {
|
|
69
|
+
const form = useFormState(props.input, ["error"]);
|
|
70
|
+
const [value, setValue] = useFieldValue(props.input);
|
|
71
|
+
const [expanded, setExpanded] = useState(props.defaultExpanded ?? true);
|
|
72
|
+
const { tr } = useI18n();
|
|
73
|
+
|
|
74
|
+
if (!props.input?.props) return null;
|
|
75
|
+
|
|
76
|
+
const meta = parseField(props.input, {
|
|
77
|
+
label: props.label,
|
|
78
|
+
description: props.description,
|
|
79
|
+
error: form.error,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// The field schema may be wrapped (optional/nullable/default); peel to the
|
|
83
|
+
// ZodObject before reading its shape.
|
|
84
|
+
const schema = z.schema.unwrap(props.input.schema) as ZObject;
|
|
85
|
+
if (!z.schema.isObject(schema)) return null;
|
|
86
|
+
|
|
87
|
+
// ── Init / clear ────────────────────────────────────────────────
|
|
88
|
+
const isInitialized = value != null && typeof value === "object";
|
|
89
|
+
|
|
90
|
+
const fieldNames = Object.keys(z.schema.shape(schema));
|
|
91
|
+
const nestedItems = (props.input as ObjectInputField<ZObject>).items as
|
|
92
|
+
| Record<string, BaseInputField>
|
|
93
|
+
| undefined;
|
|
94
|
+
|
|
95
|
+
const grid = (
|
|
96
|
+
<div className="grid gap-3 grid-cols-12">
|
|
97
|
+
{fieldNames.map((name) => {
|
|
98
|
+
const field = nestedItems?.[name];
|
|
99
|
+
if (!field) return null;
|
|
100
|
+
const fieldProps = {
|
|
101
|
+
...(props.controlProps?.[name] ?? {}),
|
|
102
|
+
...resolveFieldI18n(
|
|
103
|
+
tr as never,
|
|
104
|
+
props.i18nPrefix,
|
|
105
|
+
name,
|
|
106
|
+
props.controlProps?.[name] ?? {},
|
|
107
|
+
),
|
|
108
|
+
i18nPrefix: childI18nPrefix(props.i18nPrefix, name),
|
|
109
|
+
};
|
|
110
|
+
const width = widthFor(field, fieldProps.width as number | undefined);
|
|
111
|
+
return (
|
|
112
|
+
<div key={name} className={spanClass(width)}>
|
|
113
|
+
<Control input={field} disabled={props.disabled} {...fieldProps} />
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
})}
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
if (props.variant === "plain") return grid;
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<fieldset
|
|
124
|
+
className={`rounded-md border p-3 ${
|
|
125
|
+
meta.error ? "border-destructive" : "border-border"
|
|
126
|
+
}`}
|
|
127
|
+
>
|
|
128
|
+
<div className="flex items-start gap-3">
|
|
129
|
+
{!isInitialized && !props.disabled ? (
|
|
130
|
+
<Button
|
|
131
|
+
type="button"
|
|
132
|
+
variant="outline"
|
|
133
|
+
size="icon"
|
|
134
|
+
className="size-8 shrink-0"
|
|
135
|
+
aria-label={tr("controlObject.initialize", {
|
|
136
|
+
default: "Initialize",
|
|
137
|
+
})}
|
|
138
|
+
onClick={() => setValue({})}
|
|
139
|
+
>
|
|
140
|
+
<Plus className="size-4" />
|
|
141
|
+
</Button>
|
|
142
|
+
) : props.clearable && !props.disabled ? (
|
|
143
|
+
<Button
|
|
144
|
+
type="button"
|
|
145
|
+
variant="ghost"
|
|
146
|
+
size="icon"
|
|
147
|
+
className="size-8 shrink-0"
|
|
148
|
+
aria-label={tr("controlObject.clear", { default: "Clear" })}
|
|
149
|
+
onClick={() => setValue(undefined)}
|
|
150
|
+
>
|
|
151
|
+
<Trash2 className="size-4" />
|
|
152
|
+
</Button>
|
|
153
|
+
) : (
|
|
154
|
+
<div className="size-8 shrink-0" />
|
|
155
|
+
)}
|
|
156
|
+
<div className="flex flex-col min-w-0 flex-1">
|
|
157
|
+
{meta.label && (
|
|
158
|
+
<legend className="text-sm font-medium leading-tight">
|
|
159
|
+
{meta.label}
|
|
160
|
+
{meta.required && (
|
|
161
|
+
<span className="text-destructive ml-0.5">*</span>
|
|
162
|
+
)}
|
|
163
|
+
</legend>
|
|
164
|
+
)}
|
|
165
|
+
{meta.description && (
|
|
166
|
+
<p className="text-muted-foreground text-xs leading-tight">
|
|
167
|
+
{meta.description}
|
|
168
|
+
</p>
|
|
169
|
+
)}
|
|
170
|
+
{meta.error && (
|
|
171
|
+
<p className="text-destructive text-xs leading-tight">
|
|
172
|
+
{meta.error}
|
|
173
|
+
</p>
|
|
174
|
+
)}
|
|
175
|
+
</div>
|
|
176
|
+
{isInitialized && (
|
|
177
|
+
<Button
|
|
178
|
+
type="button"
|
|
179
|
+
variant="ghost"
|
|
180
|
+
size="icon"
|
|
181
|
+
className="size-8 shrink-0"
|
|
182
|
+
aria-label={
|
|
183
|
+
expanded
|
|
184
|
+
? tr("controlObject.collapse", { default: "Collapse" })
|
|
185
|
+
: tr("controlObject.expand", { default: "Expand" })
|
|
186
|
+
}
|
|
187
|
+
onClick={() => setExpanded((e) => !e)}
|
|
188
|
+
>
|
|
189
|
+
{expanded ? (
|
|
190
|
+
<ChevronDown className="size-4" />
|
|
191
|
+
) : (
|
|
192
|
+
<ChevronRight className="size-4" />
|
|
193
|
+
)}
|
|
194
|
+
</Button>
|
|
195
|
+
)}
|
|
196
|
+
</div>
|
|
197
|
+
{expanded && isInitialized && <div className="mt-3">{grid}</div>}
|
|
198
|
+
</fieldset>
|
|
199
|
+
);
|
|
200
|
+
}
|