@svadmin/ui 0.15.0 → 0.17.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/package.json +1 -1
- package/src/components/AutoForm.svelte +46 -137
- package/src/components/AutoTable.svelte +2 -0
- package/src/components/CommandPalette.svelte +1 -1
- package/src/components/Header.svelte +1 -1
- package/src/components/KeyboardShortcuts.svelte +1 -1
- package/src/components/StatsCard.svelte +1 -1
- package/src/components/StepsForm.svelte +59 -84
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { useForm, getResource, deriveValidator } from '@svadmin/core';
|
|
3
3
|
import type { FieldDefinition } from '@svadmin/core';
|
|
4
|
-
import { navigate } from '@svadmin/core/router';
|
|
5
|
-
import { canAccess } from '@svadmin/core/permissions';
|
|
6
4
|
import { t } from '@svadmin/core/i18n';
|
|
5
|
+
import { navigate } from '@svadmin/core/router';
|
|
7
6
|
import { Button } from './ui/button/index.js';
|
|
8
7
|
import TooltipButton from './TooltipButton.svelte';
|
|
9
8
|
import * as Card from './ui/card/index.js';
|
|
@@ -13,30 +12,20 @@
|
|
|
13
12
|
import * as Alert from './ui/alert/index.js';
|
|
14
13
|
import { Skeleton } from './ui/skeleton/index.js';
|
|
15
14
|
import ConfirmDialog from './ConfirmDialog.svelte';
|
|
16
|
-
|
|
17
15
|
import type { Snippet } from 'svelte';
|
|
18
16
|
|
|
19
17
|
interface Props {
|
|
20
18
|
resourceName: string;
|
|
21
19
|
id?: string | number;
|
|
22
20
|
mode?: 'create' | 'edit';
|
|
23
|
-
/** Custom field renderer — overrides default FieldRenderer */
|
|
24
21
|
fieldRenderer?: Snippet<[{ field: FieldDefinition; value: unknown; onchange: (v: unknown) => void }]>;
|
|
25
|
-
/** Custom form action buttons */
|
|
26
22
|
formActions?: Snippet<[{ isLoading: boolean; onSubmit: () => void }]>;
|
|
27
|
-
/** Custom header content (after title) */
|
|
28
23
|
headerContent?: Snippet;
|
|
29
24
|
}
|
|
30
25
|
|
|
31
|
-
let {
|
|
32
|
-
resourceName,
|
|
33
|
-
id = undefined,
|
|
34
|
-
mode = 'create',
|
|
35
|
-
fieldRenderer,
|
|
36
|
-
formActions,
|
|
37
|
-
headerContent,
|
|
38
|
-
}: Props = $props();
|
|
26
|
+
let { resourceName, id, mode = 'create', fieldRenderer, formActions, headerContent }: Props = $props();
|
|
39
27
|
|
|
28
|
+
// ─── Resource metadata ────────────────────────────────────────────
|
|
40
29
|
const resource = $derived(getResource(resourceName));
|
|
41
30
|
const primaryKey = $derived(resource.primaryKey ?? 'id');
|
|
42
31
|
|
|
@@ -61,19 +50,7 @@
|
|
|
61
50
|
return order.map(g => ({ name: g, fields: map.get(g)! }));
|
|
62
51
|
})());
|
|
63
52
|
|
|
64
|
-
//
|
|
65
|
-
const existingQuery = $derived.by(() => mode === 'edit' && id != null
|
|
66
|
-
? useOne({ get resource() { return resourceName; }, get id() { return id; } })
|
|
67
|
-
: null);
|
|
68
|
-
|
|
69
|
-
// Form state
|
|
70
|
-
let formData = $state<Record<string, unknown>>({});
|
|
71
|
-
let fieldErrors = $state<Record<string, string>>({});
|
|
72
|
-
let submitting = $state(false);
|
|
73
|
-
let error = $state<string | null>(null);
|
|
74
|
-
let initialized = $state(false);
|
|
75
|
-
let isDirty = $state(false);
|
|
76
|
-
|
|
53
|
+
// ─── Default values from field metadata ───────────────────────────
|
|
77
54
|
function getDefaultForType(field: FieldDefinition): unknown {
|
|
78
55
|
switch (field.type) {
|
|
79
56
|
case 'text': case 'textarea': case 'richtext': case 'image': return '';
|
|
@@ -86,115 +63,47 @@
|
|
|
86
63
|
}
|
|
87
64
|
}
|
|
88
65
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
for (const field of formFields) {
|
|
95
|
-
defaults[field.key] = field.defaultValue ?? getDefaultForType(field);
|
|
96
|
-
}
|
|
97
|
-
formData = defaults;
|
|
98
|
-
initialized = true;
|
|
99
|
-
} else if (existingQuery && existingQuery.query.data?.data) {
|
|
100
|
-
formData = { ...existingQuery.query.data.data as Record<string, unknown> };
|
|
101
|
-
initialized = true;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// Unsaved changes warning
|
|
106
|
-
$effect(() => {
|
|
107
|
-
if (!isDirty) return;
|
|
108
|
-
|
|
109
|
-
function handleBeforeUnload(e: BeforeUnloadEvent) {
|
|
110
|
-
e.preventDefault();
|
|
111
|
-
}
|
|
66
|
+
const defaults = $derived((() => {
|
|
67
|
+
const d: Record<string, unknown> = {};
|
|
68
|
+
for (const f of formFields) d[f.key] = f.defaultValue ?? getDefaultForType(f);
|
|
69
|
+
return d;
|
|
70
|
+
})());
|
|
112
71
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
72
|
+
// ─── useForm: single source of truth for values, errors, tainted ──
|
|
73
|
+
const form = useForm({
|
|
74
|
+
get resource() { return resourceName; },
|
|
75
|
+
get action() { return mode; },
|
|
76
|
+
get id() { return id; },
|
|
77
|
+
defaultValues: defaults,
|
|
78
|
+
redirect: 'list',
|
|
79
|
+
warnWhenUnsavedChanges: true,
|
|
80
|
+
validate: deriveValidator(formFields),
|
|
117
81
|
});
|
|
118
82
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
function validateFields(): boolean {
|
|
123
|
-
const errors: Record<string, string> = {};
|
|
124
|
-
for (const field of formFields) {
|
|
125
|
-
const value = formData[field.key];
|
|
126
|
-
// Required check
|
|
127
|
-
if (field.required) {
|
|
128
|
-
if (value === undefined || value === null || value === '') {
|
|
129
|
-
errors[field.key] = t('validation.required');
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
// Custom per-field validator
|
|
134
|
-
if (field.validate) {
|
|
135
|
-
const msg = field.validate(value);
|
|
136
|
-
if (msg) { errors[field.key] = msg; }
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
fieldErrors = errors;
|
|
140
|
-
return Object.keys(errors).length === 0;
|
|
141
|
-
}
|
|
83
|
+
// ─── Submission error (non-field, e.g. network error) ─────────────
|
|
84
|
+
let submitError = $state<string | null>(null);
|
|
142
85
|
|
|
143
86
|
async function handleSubmit() {
|
|
144
|
-
|
|
145
|
-
error = null;
|
|
146
|
-
|
|
147
|
-
if (!validateFields()) {
|
|
148
|
-
submitting = false;
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
87
|
+
submitError = null;
|
|
152
88
|
try {
|
|
153
|
-
|
|
154
|
-
for (const field of formFields) {
|
|
155
|
-
const value = formData[field.key];
|
|
156
|
-
if (value !== undefined) {
|
|
157
|
-
cleanData[field.key] = value;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (mode === 'create') {
|
|
162
|
-
await createMut.mutation.mutateAsync({ variables: cleanData });
|
|
163
|
-
} else if (id != null) {
|
|
164
|
-
await updateMut.mutation.mutateAsync({ id, variables: cleanData });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
isDirty = false;
|
|
168
|
-
navigate(`/${resourceName}`);
|
|
89
|
+
await form.submit();
|
|
169
90
|
} catch (e) {
|
|
170
|
-
|
|
171
|
-
} finally {
|
|
172
|
-
submitting = false;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function handleFieldChange(key: string, val: unknown) {
|
|
177
|
-
formData[key] = val;
|
|
178
|
-
isDirty = true;
|
|
179
|
-
// Clear field error when user starts typing
|
|
180
|
-
if (fieldErrors[key]) {
|
|
181
|
-
delete fieldErrors[key];
|
|
91
|
+
submitError = e instanceof Error ? e.message : t('common.operationFailed');
|
|
182
92
|
}
|
|
183
93
|
}
|
|
184
94
|
|
|
185
|
-
const isLoading = $derived(mode === 'edit' && existingQuery ? existingQuery.query.isLoading : false);
|
|
186
|
-
|
|
187
95
|
const pageTitle = $derived(
|
|
188
96
|
mode === 'create'
|
|
189
97
|
? `${t('common.create')}${resource.label}`
|
|
190
98
|
: `${t('common.edit')}${resource.label}`
|
|
191
99
|
);
|
|
192
|
-
|
|
100
|
+
|
|
101
|
+
// ─── Unsaved changes guard ────────────────────────────────────────
|
|
193
102
|
let confirmOpen = $state(false);
|
|
194
103
|
let pendingNavigation: (() => void) | null = null;
|
|
195
104
|
|
|
196
105
|
function guardNavigate(fn: () => void) {
|
|
197
|
-
if (
|
|
106
|
+
if (form.isTainted()) {
|
|
198
107
|
pendingNavigation = fn;
|
|
199
108
|
confirmOpen = true;
|
|
200
109
|
} else {
|
|
@@ -223,12 +132,12 @@
|
|
|
223
132
|
<ArrowLeft class="h-5 w-5" />
|
|
224
133
|
</TooltipButton>
|
|
225
134
|
<h1 class="text-xl font-semibold text-foreground">{pageTitle}</h1>
|
|
226
|
-
{#if
|
|
135
|
+
{#if form.isTainted()}
|
|
227
136
|
<Badge variant="outline" class="border-warning/30 bg-warning/10 text-warning-foreground">{t('common.unsaved')}</Badge>
|
|
228
137
|
{/if}
|
|
229
138
|
</div>
|
|
230
139
|
|
|
231
|
-
{#if
|
|
140
|
+
{#if form.loading}
|
|
232
141
|
<div class="max-w-3xl space-y-6">
|
|
233
142
|
<div class="rounded-lg border p-6 space-y-5">
|
|
234
143
|
{#each Array(4) as _}
|
|
@@ -241,10 +150,10 @@
|
|
|
241
150
|
</div>
|
|
242
151
|
{:else}
|
|
243
152
|
<form onsubmit={(e: Event) => { e.preventDefault(); handleSubmit(); }} class="max-w-3xl space-y-6">
|
|
244
|
-
{#if
|
|
153
|
+
{#if submitError}
|
|
245
154
|
<Alert.Root variant="destructive">
|
|
246
155
|
<AlertCircle class="h-4 w-4" />
|
|
247
|
-
<Alert.Description>{
|
|
156
|
+
<Alert.Description>{submitError}</Alert.Description>
|
|
248
157
|
</Alert.Root>
|
|
249
158
|
{/if}
|
|
250
159
|
|
|
@@ -258,18 +167,18 @@
|
|
|
258
167
|
{/if}
|
|
259
168
|
<Card.Content class="space-y-5 px-4 sm:px-6 pb-4 sm:pb-6 pt-0">
|
|
260
169
|
{#each group.fields as field (field.key)}
|
|
261
|
-
<div class:border-destructive={!!
|
|
170
|
+
<div class:border-destructive={!!form.errors[field.key]}>
|
|
262
171
|
{#if fieldRenderer}
|
|
263
|
-
{@render fieldRenderer({ field, value:
|
|
172
|
+
{@render fieldRenderer({ field, value: form.values[field.key], onchange: (val: unknown) => form.setFieldValue(field.key, val) })}
|
|
264
173
|
{:else}
|
|
265
174
|
<FieldRenderer
|
|
266
175
|
{field}
|
|
267
|
-
value={
|
|
268
|
-
onchange={(val: unknown) =>
|
|
176
|
+
value={form.values[field.key]}
|
|
177
|
+
onchange={(val: unknown) => form.setFieldValue(field.key, val)}
|
|
269
178
|
/>
|
|
270
179
|
{/if}
|
|
271
|
-
{#if
|
|
272
|
-
<p class="text-destructive text-[0.8125rem] mt-1">{
|
|
180
|
+
{#if form.errors[field.key]}
|
|
181
|
+
<p class="text-destructive text-[0.8125rem] mt-1">{form.errors[field.key]}</p>
|
|
273
182
|
{/if}
|
|
274
183
|
</div>
|
|
275
184
|
{/each}
|
|
@@ -280,18 +189,18 @@
|
|
|
280
189
|
<Card.Root>
|
|
281
190
|
<Card.Content class="space-y-5 px-4 sm:px-6 pb-4 sm:pb-6 pt-0">
|
|
282
191
|
{#each formFields as field (field.key)}
|
|
283
|
-
<div class:border-destructive={!!
|
|
192
|
+
<div class:border-destructive={!!form.errors[field.key]}>
|
|
284
193
|
{#if fieldRenderer}
|
|
285
|
-
{@render fieldRenderer({ field, value:
|
|
194
|
+
{@render fieldRenderer({ field, value: form.values[field.key], onchange: (val: unknown) => form.setFieldValue(field.key, val) })}
|
|
286
195
|
{:else}
|
|
287
196
|
<FieldRenderer
|
|
288
197
|
{field}
|
|
289
|
-
value={
|
|
290
|
-
onchange={(val: unknown) =>
|
|
198
|
+
value={form.values[field.key]}
|
|
199
|
+
onchange={(val: unknown) => form.setFieldValue(field.key, val)}
|
|
291
200
|
/>
|
|
292
201
|
{/if}
|
|
293
|
-
{#if
|
|
294
|
-
<p class="text-destructive text-[0.8125rem] mt-1">{
|
|
202
|
+
{#if form.errors[field.key]}
|
|
203
|
+
<p class="text-destructive text-[0.8125rem] mt-1">{form.errors[field.key]}</p>
|
|
295
204
|
{/if}
|
|
296
205
|
</div>
|
|
297
206
|
{/each}
|
|
@@ -301,10 +210,10 @@
|
|
|
301
210
|
|
|
302
211
|
<div class="flex items-center gap-3">
|
|
303
212
|
{#if formActions}
|
|
304
|
-
{@render formActions({ isLoading: submitting, onSubmit: handleSubmit })}
|
|
213
|
+
{@render formActions({ isLoading: form.submitting, onSubmit: handleSubmit })}
|
|
305
214
|
{:else}
|
|
306
|
-
<Button type="submit" disabled={submitting}>
|
|
307
|
-
{#if submitting}
|
|
215
|
+
<Button type="submit" disabled={form.submitting}>
|
|
216
|
+
{#if form.submitting}
|
|
308
217
|
<Loader2 class="h-4 w-4 animate-spin" data-icon="inline-start" />
|
|
309
218
|
{:else}
|
|
310
219
|
<Save class="h-4 w-4" data-icon="inline-start" />
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
placeholder={t('common.search')}
|
|
36
36
|
class="flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50"
|
|
37
37
|
/>
|
|
38
|
-
<kbd class="ml-2 text-[10px] font-medium text-muted-foreground bg-muted px-1.5 py-0.5 rounded border border-border">ESC</kbd>
|
|
38
|
+
<kbd class="ml-2 text-[10px] font-mono font-medium text-muted-foreground bg-muted px-1.5 py-0.5 rounded border border-border">ESC</kbd>
|
|
39
39
|
</div>
|
|
40
40
|
<Command.List class="max-h-[320px] overflow-y-auto overflow-x-hidden p-1">
|
|
41
41
|
<Command.Empty class="py-6 text-center text-sm text-muted-foreground">
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
<Button variant="outline" size="sm" onclick={onSearchClick} class="gap-2 text-muted-foreground h-8 px-3">
|
|
59
59
|
<Search class="h-3.5 w-3.5" />
|
|
60
60
|
<span class="hidden sm:inline text-xs">Search...</span>
|
|
61
|
-
<kbd class="hidden sm:inline-flex items-center gap-0.5 rounded border bg-muted px-1.5 py-0.5 text-[10px] font-medium text-muted-foreground">
|
|
61
|
+
<kbd class="hidden sm:inline-flex items-center gap-0.5 rounded border bg-muted px-1.5 py-0.5 text-[10px] font-medium font-mono text-muted-foreground">
|
|
62
62
|
<span class="text-xs">⌘</span>K
|
|
63
63
|
</kbd>
|
|
64
64
|
</Button>
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
<span class="text-sm text-foreground">{shortcut.label}</span>
|
|
49
49
|
<div class="flex items-center gap-1">
|
|
50
50
|
{#each shortcut.keys as key}
|
|
51
|
-
<kbd class="inline-flex h-5 min-w-5 items-center justify-center rounded border bg-muted px-1.5 text-[10px] font-medium text-muted-foreground">{key}</kbd>
|
|
51
|
+
<kbd class="inline-flex h-5 min-w-5 items-center justify-center rounded border bg-muted px-1.5 text-[10px] font-medium font-mono text-muted-foreground">{key}</kbd>
|
|
52
52
|
{/each}
|
|
53
53
|
</div>
|
|
54
54
|
</div>
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
<Skeleton class="mt-1 h-7 w-20" />
|
|
58
58
|
{:else}
|
|
59
59
|
<div class="flex items-baseline gap-2">
|
|
60
|
-
<p class="text-xl font-semibold text-foreground">{value}</p>
|
|
60
|
+
<p class="text-xl font-mono font-semibold text-foreground tracking-tight">{value}</p>
|
|
61
61
|
{#if trend}
|
|
62
62
|
<span
|
|
63
63
|
class="text-xs font-medium {trend.value >= 0 ? 'text-success' : 'text-destructive'}"
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { fly } from 'svelte/transition';
|
|
3
|
-
import { useStepsForm } from '@svadmin/core';
|
|
3
|
+
import { useStepsForm, deriveValidator } from '@svadmin/core';
|
|
4
4
|
import type { FieldDefinition } from '@svadmin/core';
|
|
5
|
-
import { getResource
|
|
5
|
+
import { getResource } from '@svadmin/core';
|
|
6
6
|
import { navigate } from '@svadmin/core/router';
|
|
7
7
|
import { t } from '@svadmin/core/i18n';
|
|
8
8
|
import { Button } from './ui/button/index.js';
|
|
9
9
|
import * as Card from './ui/card/index.js';
|
|
10
10
|
import { Badge } from './ui/badge/index.js';
|
|
11
11
|
import { Progress } from './ui/progress/index.js';
|
|
12
|
-
import { Skeleton } from './ui/skeleton/index.js';
|
|
13
12
|
import { Save, ArrowLeft, ArrowRight, Check, Loader2 } from 'lucide-svelte';
|
|
14
13
|
import FieldRenderer from './FieldRenderer.svelte';
|
|
15
14
|
|
|
@@ -30,37 +29,12 @@
|
|
|
30
29
|
const resource = $derived(getResource(resourceName));
|
|
31
30
|
const primaryKey = $derived(resource.primaryKey ?? 'id');
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
get resource() { return resourceName; },
|
|
35
|
-
get action() { return mode; },
|
|
36
|
-
get id() { return id; },
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const { currentStep, next, prev, gotoStep } = stepsForm;
|
|
40
|
-
|
|
41
|
-
const totalSteps = $derived(steps.length);
|
|
42
|
-
const progressValue = $derived(((currentStep + 1) / totalSteps) * 100);
|
|
43
|
-
const isLastStep = $derived(currentStep >= totalSteps - 1);
|
|
44
|
-
|
|
45
|
-
// Get fields for current step
|
|
46
|
-
const currentFields = $derived(
|
|
47
|
-
steps[currentStep]?.fields
|
|
48
|
-
.map(key => resource.fields.find(f => f.key === key))
|
|
49
|
-
.filter((f): f is FieldDefinition => f != null && f.key !== primaryKey) ?? []
|
|
50
|
-
);
|
|
51
|
-
|
|
32
|
+
// All fields used across all steps
|
|
52
33
|
const flatStepsFields = $derived(steps.flatMap(s => s.fields)
|
|
53
34
|
.map(key => resource.fields.find(f => f.key === key))
|
|
54
35
|
.filter((f): f is FieldDefinition => f != null));
|
|
55
36
|
|
|
56
|
-
|
|
57
|
-
? stepsForm.query as { data?: { data: Record<string, unknown> }, isLoading: boolean }
|
|
58
|
-
: null);
|
|
59
|
-
|
|
60
|
-
let formData = $state<Record<string, unknown>>({});
|
|
61
|
-
let fieldErrors = $state<Record<string, string>>({});
|
|
62
|
-
let initialized = $state(false);
|
|
63
|
-
|
|
37
|
+
// Default values from field metadata
|
|
64
38
|
function getDefaultForType(field: FieldDefinition): unknown {
|
|
65
39
|
switch (field.type) {
|
|
66
40
|
case 'text': case 'textarea': case 'richtext': case 'image': return '';
|
|
@@ -73,64 +47,65 @@
|
|
|
73
47
|
}
|
|
74
48
|
}
|
|
75
49
|
|
|
76
|
-
$
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
50
|
+
const defaults = $derived((() => {
|
|
51
|
+
const d: Record<string, unknown> = {};
|
|
52
|
+
for (const f of flatStepsFields) d[f.key] = f.defaultValue ?? getDefaultForType(f);
|
|
53
|
+
return d;
|
|
54
|
+
})());
|
|
55
|
+
|
|
56
|
+
// ─── useStepsForm: form state + step navigation ──────────────────
|
|
57
|
+
const form = useStepsForm({
|
|
58
|
+
get resource() { return resourceName; },
|
|
59
|
+
get action() { return mode; },
|
|
60
|
+
get id() { return id; },
|
|
61
|
+
defaultValues: defaults,
|
|
62
|
+
redirect: 'list',
|
|
63
|
+
warnWhenUnsavedChanges: true,
|
|
64
|
+
validate: deriveValidator(flatStepsFields),
|
|
89
65
|
});
|
|
90
66
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
67
|
+
const totalSteps = $derived(steps.length);
|
|
68
|
+
const progressValue = $derived(((form.currentStep + 1) / totalSteps) * 100);
|
|
69
|
+
const isLastStep = $derived(form.currentStep >= totalSteps - 1);
|
|
70
|
+
|
|
71
|
+
// Fields for the current step
|
|
72
|
+
const currentFields = $derived(
|
|
73
|
+
steps[form.currentStep]?.fields
|
|
74
|
+
.map(key => resource.fields.find(f => f.key === key))
|
|
75
|
+
.filter((f): f is FieldDefinition => f != null && f.key !== primaryKey) ?? []
|
|
76
|
+
);
|
|
95
77
|
|
|
78
|
+
// Validate only fields in the current step
|
|
96
79
|
function validateCurrentStep(): boolean {
|
|
97
|
-
const errors: Record<string, string> = {};
|
|
98
80
|
let valid = true;
|
|
81
|
+
form.clearErrors();
|
|
99
82
|
for (const field of currentFields) {
|
|
100
|
-
const value =
|
|
83
|
+
const value = form.values[field.key];
|
|
101
84
|
if (field.required && (value === undefined || value === null || value === '')) {
|
|
102
|
-
|
|
85
|
+
form.setFieldError(field.key, t('validation.required'));
|
|
103
86
|
valid = false;
|
|
104
87
|
} else if (field.validate) {
|
|
105
88
|
const msg = field.validate(value);
|
|
106
89
|
if (msg) {
|
|
107
|
-
|
|
90
|
+
form.setFieldError(field.key, msg);
|
|
108
91
|
valid = false;
|
|
109
92
|
}
|
|
110
93
|
}
|
|
111
94
|
}
|
|
112
|
-
fieldErrors = errors;
|
|
113
95
|
return valid;
|
|
114
96
|
}
|
|
115
97
|
|
|
116
|
-
function handleSubmit() {
|
|
98
|
+
async function handleSubmit() {
|
|
117
99
|
if (!validateCurrentStep()) return;
|
|
118
|
-
|
|
100
|
+
|
|
119
101
|
if (!isLastStep) {
|
|
120
|
-
next();
|
|
102
|
+
form.next();
|
|
121
103
|
return;
|
|
122
104
|
}
|
|
123
|
-
|
|
124
|
-
const cleanData: Record<string, unknown> = {};
|
|
125
|
-
for (const field of flatStepsFields) {
|
|
126
|
-
const value = formData[field.key];
|
|
127
|
-
if (value !== undefined) cleanData[field.key] = value;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
stepsForm.onFinish(cleanData, { redirect: 'list' });
|
|
131
|
-
}
|
|
132
105
|
|
|
133
|
-
|
|
106
|
+
// Last step — submit the full form
|
|
107
|
+
await form.submit({ redirect: 'list' });
|
|
108
|
+
}
|
|
134
109
|
</script>
|
|
135
110
|
|
|
136
111
|
<div class="space-y-6">
|
|
@@ -141,17 +116,17 @@
|
|
|
141
116
|
<Button
|
|
142
117
|
variant="ghost"
|
|
143
118
|
type="button"
|
|
144
|
-
class="flex items-center gap-2 transition-colors h-auto py-1 px-2 {i === currentStep ? 'text-primary font-semibold' : 'text-muted-foreground'}"
|
|
145
|
-
onclick={() => gotoStep(i)}
|
|
119
|
+
class="flex items-center gap-2 transition-colors h-auto py-1 px-2 {i === form.currentStep ? 'text-primary font-semibold' : 'text-muted-foreground'}"
|
|
120
|
+
onclick={() => form.gotoStep(i)}
|
|
146
121
|
>
|
|
147
122
|
<span
|
|
148
123
|
class="flex h-7 w-7 items-center justify-center rounded-full text-xs font-medium transition-colors"
|
|
149
|
-
class:bg-primary={i <= currentStep}
|
|
150
|
-
class:text-primary-foreground={i <= currentStep}
|
|
151
|
-
class:bg-muted={i > currentStep}
|
|
152
|
-
class:text-muted-foreground={i > currentStep}
|
|
124
|
+
class:bg-primary={i <= form.currentStep}
|
|
125
|
+
class:text-primary-foreground={i <= form.currentStep}
|
|
126
|
+
class:bg-muted={i > form.currentStep}
|
|
127
|
+
class:text-muted-foreground={i > form.currentStep}
|
|
153
128
|
>
|
|
154
|
-
{#if i < currentStep}
|
|
129
|
+
{#if i < form.currentStep}
|
|
155
130
|
<Check class="h-3.5 w-3.5" />
|
|
156
131
|
{:else}
|
|
157
132
|
{i + 1}
|
|
@@ -160,7 +135,7 @@
|
|
|
160
135
|
<span class="hidden sm:inline">{step.title}</span>
|
|
161
136
|
</Button>
|
|
162
137
|
{#if i < steps.length - 1}
|
|
163
|
-
<div class="flex-1 mx-2 h-px bg-border" class:bg-primary={i < currentStep}></div>
|
|
138
|
+
<div class="flex-1 mx-2 h-px bg-border" class:bg-primary={i < form.currentStep}></div>
|
|
164
139
|
{/if}
|
|
165
140
|
{/each}
|
|
166
141
|
</div>
|
|
@@ -170,22 +145,22 @@
|
|
|
170
145
|
<!-- Step content -->
|
|
171
146
|
<Card.Root>
|
|
172
147
|
<Card.CardHeader>
|
|
173
|
-
<Card.CardTitle>{steps[currentStep]?.title ?? ''}</Card.CardTitle>
|
|
148
|
+
<Card.CardTitle>{steps[form.currentStep]?.title ?? ''}</Card.CardTitle>
|
|
174
149
|
<Badge variant="outline" class="w-fit">
|
|
175
|
-
{t('common.step')} {currentStep + 1} / {totalSteps}
|
|
150
|
+
{t('common.step')} {form.currentStep + 1} / {totalSteps}
|
|
176
151
|
</Badge>
|
|
177
152
|
</Card.CardHeader>
|
|
178
153
|
<Card.CardContent>
|
|
179
|
-
{#key currentStep}
|
|
154
|
+
{#key form.currentStep}
|
|
180
155
|
<form class="space-y-5" onsubmit={(e) => { e.preventDefault(); handleSubmit(); }} in:fly={{ x: 30, duration: 250 }}>
|
|
181
156
|
{#each currentFields as field (field.key)}
|
|
182
157
|
<FieldRenderer
|
|
183
158
|
{field}
|
|
184
|
-
value={
|
|
185
|
-
onchange={(v) =>
|
|
159
|
+
value={form.values[field.key]}
|
|
160
|
+
onchange={(v) => form.setFieldValue(field.key, v)}
|
|
186
161
|
/>
|
|
187
|
-
{#if
|
|
188
|
-
<p class="text-destructive text-[0.8125rem] mt-1">{
|
|
162
|
+
{#if form.errors[field.key]}
|
|
163
|
+
<p class="text-destructive text-[0.8125rem] mt-1">{form.errors[field.key]}</p>
|
|
189
164
|
{/if}
|
|
190
165
|
{/each}
|
|
191
166
|
|
|
@@ -193,14 +168,14 @@
|
|
|
193
168
|
<Button
|
|
194
169
|
type="button"
|
|
195
170
|
variant="outline"
|
|
196
|
-
onclick={() => currentStep === 0 ? navigate(`/${resourceName}`) : prev()}
|
|
171
|
+
onclick={() => form.currentStep === 0 ? navigate(`/${resourceName}`) : form.prev()}
|
|
197
172
|
>
|
|
198
173
|
<ArrowLeft class="h-4 w-4" data-icon="inline-start" />
|
|
199
|
-
{currentStep === 0 ? t('common.cancel') : t('common.back')}
|
|
174
|
+
{form.currentStep === 0 ? t('common.cancel') : t('common.back')}
|
|
200
175
|
</Button>
|
|
201
176
|
|
|
202
|
-
<Button type="submit" disabled={
|
|
203
|
-
{#if
|
|
177
|
+
<Button type="submit" disabled={form.submitting}>
|
|
178
|
+
{#if form.submitting}
|
|
204
179
|
<Loader2 class="h-4 w-4 animate-spin" data-icon="inline-start" />
|
|
205
180
|
{:else if isLastStep}
|
|
206
181
|
<Save class="h-4 w-4" data-icon="inline-start" />
|