@svgrid/grid 2.1.21 → 2.1.23
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 +9 -2
- package/dist/DockNodeView.svelte +306 -0
- package/dist/DockNodeView.svelte.d.ts +7 -0
- package/dist/DockPopoutHost.svelte +17 -0
- package/dist/DockPopoutHost.svelte.d.ts +14 -0
- package/dist/GridMenus.svelte +83 -3
- package/dist/SvAutoComplete.svelte +5 -3
- package/dist/SvCarousel.svelte +20 -42
- package/dist/SvColorInput.svelte +4 -3
- package/dist/SvColorInput.svelte.d.ts +1 -1
- package/dist/SvComboBox.svelte +7 -3
- package/dist/SvCommand.svelte +28 -85
- package/dist/SvCountryInput.svelte +1 -1
- package/dist/SvDateRangeInput.svelte +1 -1
- package/dist/SvDateTimePicker.svelte +9 -3
- package/dist/SvDateTimePicker.svelte.d.ts +3 -0
- package/dist/SvDockLayout.svelte +244 -0
- package/dist/SvDockLayout.svelte.d.ts +77 -0
- package/dist/SvDockManager.svelte +922 -0
- package/dist/SvDockManager.svelte.d.ts +106 -0
- package/dist/SvDrawer.svelte +69 -47
- package/dist/SvDrawer.svelte.d.ts +4 -0
- package/dist/SvDropDownList.svelte +85 -13
- package/dist/SvDropDownList.svelte.d.ts +3 -0
- package/dist/SvField.svelte +21 -4
- package/dist/SvField.svelte.d.ts +1 -0
- package/dist/SvForm.svelte +201 -91
- package/dist/SvForm.svelte.d.ts +15 -22
- package/dist/SvGrid.controller.svelte.d.ts +32 -1
- package/dist/SvGrid.controller.svelte.js +156 -27
- package/dist/SvGrid.css +193 -5
- package/dist/SvGrid.svelte +357 -12
- package/dist/SvGrid.types.d.ts +452 -1
- package/dist/SvGridDropdown.svelte +8 -1
- package/dist/SvGridSelect.svelte +1 -0
- package/dist/SvListBox.svelte +36 -14
- package/dist/SvMaskedInput.svelte +4 -3
- package/dist/SvMaskedInput.svelte.d.ts +1 -1
- package/dist/SvMenu.svelte +1 -1
- package/dist/SvMenuList.svelte +23 -71
- package/dist/SvMenuList.svelte.d.ts +2 -13
- package/dist/SvModal.svelte +12 -29
- package/dist/SvMultiSelect.svelte +1 -0
- package/dist/SvNumberInput.svelte +4 -3
- package/dist/SvNumberInput.svelte.d.ts +1 -1
- package/dist/SvPagination.svelte +18 -18
- package/dist/SvPasswordInput.svelte +3 -2
- package/dist/SvPasswordInput.svelte.d.ts +1 -1
- package/dist/SvPhoneInput.svelte +3 -2
- package/dist/SvPhoneInput.svelte.d.ts +1 -1
- package/dist/SvPopover.svelte +1 -1
- package/dist/SvStepper.svelte +6 -9
- package/dist/SvTagsInput.svelte +3 -2
- package/dist/SvTagsInput.svelte.d.ts +1 -1
- package/dist/SvTooltip.svelte +12 -23
- package/dist/SvTour.svelte +32 -4
- package/dist/SvTreeSelect.svelte +1 -0
- package/dist/build-api.js +76 -54
- package/dist/cdn/svgrid.js +10907 -6444
- package/dist/cdn/svgrid.svelte-external.js +9622 -5475
- package/dist/columns.js +6 -0
- package/dist/conditional-formatting.d.ts +67 -3
- package/dist/conditional-formatting.js +124 -17
- package/dist/createCarousel.svelte.d.ts +72 -0
- package/dist/createCarousel.svelte.js +80 -0
- package/dist/createCommand.svelte.d.ts +62 -0
- package/dist/createCommand.svelte.js +109 -0
- package/dist/createForm.svelte.d.ts +62 -0
- package/dist/createForm.svelte.js +374 -0
- package/dist/createMenu.svelte.d.ts +88 -0
- package/dist/createMenu.svelte.js +153 -0
- package/dist/createOverlay.svelte.d.ts +38 -0
- package/dist/createOverlay.svelte.js +68 -0
- package/dist/createPagination.d.ts +74 -0
- package/dist/createPagination.js +71 -0
- package/dist/createPopoverSelect.svelte.js +6 -12
- package/dist/createStepper.d.ts +44 -0
- package/dist/createStepper.js +26 -0
- package/dist/createTooltip.svelte.d.ts +32 -0
- package/dist/createTooltip.svelte.js +61 -0
- package/dist/datetime/timezone.d.ts +36 -0
- package/dist/datetime/timezone.js +110 -0
- package/dist/dock-context.d.ts +52 -0
- package/dist/dock-context.js +1 -0
- package/dist/dock-manager-model.d.ts +136 -0
- package/dist/dock-manager-model.js +514 -0
- package/dist/dock-model.d.ts +105 -0
- package/dist/dock-model.js +288 -0
- package/dist/dock-popout.d.ts +30 -0
- package/dist/dock-popout.js +81 -0
- package/dist/editor-contract.d.ts +2 -0
- package/dist/filter-operators.d.ts +6 -0
- package/dist/filter-operators.js +35 -2
- package/dist/filtering/excel-filters.d.ts +18 -1
- package/dist/filtering/excel-filters.js +56 -0
- package/dist/form-field.d.ts +73 -0
- package/dist/form-field.js +15 -0
- package/dist/index.d.ts +25 -5
- package/dist/index.js +40 -4
- package/dist/list-nav.d.ts +18 -0
- package/dist/list-nav.js +30 -0
- package/dist/list-option.d.ts +2 -0
- package/dist/menu-item.d.ts +20 -0
- package/dist/menu-item.js +1 -0
- package/dist/menus.d.ts +5 -0
- package/dist/menus.js +68 -16
- package/dist/panel-resize.d.ts +25 -0
- package/dist/panel-resize.js +40 -0
- package/dist/popover.d.ts +29 -3
- package/dist/popover.js +22 -6
- package/dist/recurrence.d.ts +33 -7
- package/dist/recurrence.js +151 -13
- package/dist/row-drag.d.ts +8 -0
- package/dist/row-drag.js +11 -0
- package/dist/scheduler-ical.d.ts +30 -0
- package/dist/scheduler-ical.js +180 -0
- package/dist/scheduler-model.d.ts +269 -0
- package/dist/scheduler-model.js +602 -0
- package/dist/scheduler-view.svelte.d.ts +22 -0
- package/dist/scheduler-view.svelte.js +13 -0
- package/dist/spreadsheet.js +20 -12
- package/dist/ui-app.types.d.ts +3 -0
- package/package.json +5 -1
- package/src/DockNodeView.svelte +306 -0
- package/src/DockPopoutHost.svelte +17 -0
- package/src/GridMenus.svelte +83 -3
- package/src/SvAutoComplete.svelte +5 -3
- package/src/SvCarousel.svelte +20 -42
- package/src/SvCarousel.test.ts +68 -0
- package/src/SvColorInput.svelte +4 -3
- package/src/SvComboBox.svelte +7 -3
- package/src/SvCommand.svelte +28 -85
- package/src/SvCommand.test.ts +78 -0
- package/src/SvCountryInput.svelte +1 -1
- package/src/SvDateRangeInput.svelte +1 -1
- package/src/SvDateTimePicker.svelte +9 -3
- package/src/SvDockLayout.svelte +244 -0
- package/src/SvDockManager.svelte +922 -0
- package/src/SvDrawer.svelte +69 -47
- package/src/SvDropDownList.svelte +85 -13
- package/src/SvField.svelte +21 -4
- package/src/SvForm.svelte +201 -91
- package/src/SvForm.test.ts +299 -0
- package/src/SvGrid.controller.svelte.ts +152 -28
- package/src/SvGrid.css +193 -5
- package/src/SvGrid.svelte +357 -12
- package/src/SvGrid.types.ts +463 -1
- package/src/SvGridDropdown.svelte +8 -1
- package/src/SvGridSelect.svelte +1 -0
- package/src/SvListBox.svelte +36 -14
- package/src/SvMaskedInput.svelte +4 -3
- package/src/SvMenu.svelte +1 -1
- package/src/SvMenu.test.ts +24 -0
- package/src/SvMenuList.svelte +23 -71
- package/src/SvModal.svelte +12 -29
- package/src/SvModal.test.ts +82 -0
- package/src/SvMultiSelect.svelte +1 -0
- package/src/SvNumberInput.svelte +4 -3
- package/src/SvPagination.svelte +18 -18
- package/src/SvPasswordInput.svelte +3 -2
- package/src/SvPhoneInput.svelte +3 -2
- package/src/SvPopover.svelte +1 -1
- package/src/SvStepper.svelte +6 -9
- package/src/SvTagsInput.svelte +3 -2
- package/src/SvTooltip.svelte +12 -23
- package/src/SvTooltip.test.ts +73 -0
- package/src/SvTour.svelte +32 -4
- package/src/SvTreeSelect.svelte +1 -0
- package/src/build-api.ts +74 -51
- package/src/columns.ts +5 -0
- package/src/conditional-formatting.test.ts +104 -0
- package/src/conditional-formatting.ts +176 -19
- package/src/createCarousel.svelte.ts +104 -0
- package/src/createCommand.svelte.ts +125 -0
- package/src/createForm.svelte.ts +333 -0
- package/src/createMenu.svelte.ts +202 -0
- package/src/createOverlay.svelte.ts +101 -0
- package/src/createPagination.test.ts +65 -0
- package/src/createPagination.ts +84 -0
- package/src/createPopoverSelect.svelte.ts +5 -9
- package/src/createStepper.test.ts +62 -0
- package/src/createStepper.ts +61 -0
- package/src/createTooltip.svelte.ts +86 -0
- package/src/datetime/timezone.test.ts +69 -0
- package/src/datetime/timezone.ts +134 -0
- package/src/dock-context.ts +47 -0
- package/src/dock-manager-model.test.ts +275 -0
- package/src/dock-manager-model.ts +593 -0
- package/src/dock-model.test.ts +163 -0
- package/src/dock-model.ts +368 -0
- package/src/dock-popout.ts +94 -0
- package/src/editor-contract.ts +2 -0
- package/src/filter-operators.test.ts +59 -0
- package/src/filter-operators.ts +35 -2
- package/src/filtering/excel-filters.test.ts +86 -1
- package/src/filtering/excel-filters.ts +65 -0
- package/src/form-field.ts +87 -0
- package/src/index.ts +158 -4
- package/src/list-nav.test.ts +49 -0
- package/src/list-nav.ts +29 -0
- package/src/list-option.ts +2 -0
- package/src/menu-item.ts +21 -0
- package/src/menus.test.ts +48 -0
- package/src/menus.ts +73 -16
- package/src/panel-resize.ts +52 -0
- package/src/popover.bounds.test.ts +93 -0
- package/src/popover.ts +51 -7
- package/src/recurrence.test.ts +88 -1
- package/src/recurrence.ts +166 -15
- package/src/row-drag.ts +10 -0
- package/src/scheduler-ical.test.ts +68 -0
- package/src/scheduler-ical.ts +180 -0
- package/src/scheduler-model.test.ts +546 -0
- package/src/scheduler-model.ts +853 -0
- package/src/scheduler-view.svelte.ts +33 -0
- package/src/spreadsheet.ts +20 -12
- package/src/svgrid.effect-driven-filter.svelte.test.ts +116 -0
- package/src/ui-app.types.ts +3 -0
- package/src/virtual.test.ts +6 -4
package/src/SvForm.svelte
CHANGED
|
@@ -1,31 +1,16 @@
|
|
|
1
1
|
<script lang="ts" module>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
| 'select' | 'checkbox' | 'switch' | 'date' | 'color' | 'rating'
|
|
7
|
-
|
|
8
|
-
export type FormField = {
|
|
9
|
-
name: string
|
|
10
|
-
label: string
|
|
11
|
-
type?: FormFieldType
|
|
12
|
-
required?: boolean
|
|
13
|
-
placeholder?: string
|
|
14
|
-
options?: Array<{ value: string | number; label: string }>
|
|
15
|
-
/** Declarative validation rules (see `rules` - email/pattern/min/compare...). */
|
|
16
|
-
rules?: ReadonlyArray<Validator>
|
|
17
|
-
/** Return an error message, or null/undefined when valid. */
|
|
18
|
-
validate?: (value: any, values: Record<string, any>) => string | null | undefined
|
|
19
|
-
/** Span full width in the grid. */
|
|
20
|
-
full?: boolean
|
|
21
|
-
}
|
|
2
|
+
// FormField/FormFieldType live in a plain `.ts` (breaks a Vite dev import
|
|
3
|
+
// cycle); re-exported so `import { type FormField } from './SvForm.svelte'`
|
|
4
|
+
// sites keep working.
|
|
5
|
+
export type { FormField, FormFieldType, FormSection, FormEntry } from './form-field'
|
|
22
6
|
</script>
|
|
23
7
|
|
|
24
8
|
<script lang="ts">
|
|
25
9
|
/**
|
|
26
10
|
* SvForm - a schema-driven form that renders the SvGrid UI-kit controls with
|
|
27
|
-
* labels, required + custom validation, and a submit handler.
|
|
28
|
-
*
|
|
11
|
+
* labels, required + custom validation, and a submit handler. The schema may be
|
|
12
|
+
* a flat `FormField[]` or mix in titled `FormSection`s; with `stepper` the
|
|
13
|
+
* sections become a validated wizard. Emits `onSubmit(values)` only when valid.
|
|
29
14
|
*/
|
|
30
15
|
import SvNumberInput from './SvNumberInput.svelte'
|
|
31
16
|
import SvPasswordInput from './SvPasswordInput.svelte'
|
|
@@ -36,105 +21,211 @@
|
|
|
36
21
|
import SvColorInput from './SvColorInput.svelte'
|
|
37
22
|
import SvRating from './SvRating.svelte'
|
|
38
23
|
import SvButton from './SvButton.svelte'
|
|
39
|
-
import
|
|
24
|
+
import SvStepper from './SvStepper.svelte'
|
|
25
|
+
import { isFormSection, type FormField, type FormEntry, type FormSection } from './form-field'
|
|
26
|
+
import { createForm } from './createForm.svelte'
|
|
40
27
|
|
|
41
28
|
type Props = {
|
|
42
|
-
fields: ReadonlyArray<
|
|
29
|
+
fields: ReadonlyArray<FormEntry>
|
|
43
30
|
initial?: Record<string, any>
|
|
44
|
-
|
|
31
|
+
/** Called with the visible-field values on a valid submit. May be async -
|
|
32
|
+
* the Submit button shows a loading state until it settles. */
|
|
33
|
+
onSubmit?: (values: Record<string, any>) => void | Promise<void>
|
|
45
34
|
onChange?: (values: Record<string, any>) => void
|
|
35
|
+
/** When set, a secondary Cancel button renders next to Submit. */
|
|
36
|
+
onCancel?: () => void
|
|
46
37
|
submitLabel?: string
|
|
47
|
-
|
|
38
|
+
cancelLabel?: string
|
|
39
|
+
/** Show a Reset button that restores the initial values. */
|
|
40
|
+
showReset?: boolean
|
|
41
|
+
resetLabel?: string
|
|
42
|
+
/** Render titled sections as a validated multi-step wizard (requires sections). */
|
|
43
|
+
stepper?: boolean
|
|
44
|
+
/** Columns in the responsive field grid (a section can override its own). */
|
|
48
45
|
columns?: number
|
|
49
46
|
disabled?: boolean
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
let {
|
|
49
|
+
let {
|
|
50
|
+
fields, initial = {}, onSubmit, onChange, onCancel,
|
|
51
|
+
submitLabel = 'Submit', cancelLabel = 'Cancel', showReset = false, resetLabel = 'Reset',
|
|
52
|
+
stepper = false, columns = 1, disabled = false,
|
|
53
|
+
}: Props = $props()
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
let seededForm = false
|
|
58
|
-
$effect(() => { if (!seededForm) { seededForm = true; values = { ...initial } } })
|
|
55
|
+
const form = createForm({ fields: () => fields, initial, onSubmit, onChange })
|
|
56
|
+
const submit = (e: Event) => { e.preventDefault(); form.submit() }
|
|
57
|
+
const dateVal = (v: any): Date | null => (v instanceof Date ? v : v ? new Date(v) : null)
|
|
59
58
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (touched[name]) validateField(name)
|
|
64
|
-
}
|
|
59
|
+
const sections = $derived(fields.filter(isFormSection) as FormSection[])
|
|
60
|
+
const wizard = $derived(stepper && sections.length > 0)
|
|
61
|
+
const stepItems = $derived(sections.map((s) => ({ label: s.section, description: s.description })))
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
if (!err && field.rules) err = runRules(v, field.rules, values)
|
|
73
|
-
if (!err && field.validate) err = field.validate(v, values)
|
|
74
|
-
const next = { ...errors }
|
|
75
|
-
if (err) next[name] = err
|
|
76
|
-
else delete next[name]
|
|
77
|
-
errors = next
|
|
78
|
-
return !err
|
|
63
|
+
let step = $state(0)
|
|
64
|
+
async function next() {
|
|
65
|
+
const s = sections[step]
|
|
66
|
+
if (!s) return
|
|
67
|
+
const ok = await form.validateFields(s.fields.map((f) => f.name))
|
|
68
|
+
if (ok && step < sections.length - 1) step += 1
|
|
79
69
|
}
|
|
70
|
+
const back = () => { if (step > 0) step -= 1 }
|
|
80
71
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
72
|
+
// Shared shape passed to the `control` snippet so top-level fields and array
|
|
73
|
+
// item fields render the same set of controls without duplication.
|
|
74
|
+
type ControlOpts = {
|
|
75
|
+
id: string
|
|
76
|
+
type: string
|
|
77
|
+
label: string
|
|
78
|
+
value: any
|
|
79
|
+
onChange: (v: any) => void
|
|
80
|
+
onBlur?: () => void
|
|
81
|
+
disabled: boolean
|
|
82
|
+
placeholder?: string
|
|
83
|
+
options?: ReadonlyArray<{ value: string | number; label: string; color?: string }>
|
|
88
84
|
}
|
|
89
|
-
|
|
90
|
-
const dateVal = (v: any): Date | null => (v instanceof Date ? v : v ? new Date(v) : null)
|
|
91
85
|
</script>
|
|
92
86
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
{
|
|
96
|
-
|
|
97
|
-
|
|
87
|
+
{#snippet control(o: ControlOpts)}
|
|
88
|
+
{#if o.type === 'textarea'}
|
|
89
|
+
<textarea id={o.id} class="sv-form__control sv-form__textarea" rows="3" disabled={o.disabled} placeholder={o.placeholder} value={o.value ?? ''} oninput={(e) => o.onChange(e.currentTarget.value)} onblur={o.onBlur}></textarea>
|
|
90
|
+
{:else if o.type === 'number'}
|
|
91
|
+
<SvNumberInput value={o.value ?? null} disabled={o.disabled} onChange={o.onChange} />
|
|
92
|
+
{:else if o.type === 'password'}
|
|
93
|
+
<SvPasswordInput value={o.value ?? ''} disabled={o.disabled} onChange={o.onChange} />
|
|
94
|
+
{:else if o.type === 'select'}
|
|
95
|
+
<SvDropDownList options={o.options ?? []} value={o.value ?? null} disabled={o.disabled} placeholder={o.placeholder} onChange={o.onChange} />
|
|
96
|
+
{:else if o.type === 'checkbox'}
|
|
97
|
+
<SvCheckBox checked={!!o.value} disabled={o.disabled} onChange={o.onChange}>{o.label}</SvCheckBox>
|
|
98
|
+
{:else if o.type === 'switch'}
|
|
99
|
+
<div class="sv-form__inline"><SvSwitchButton checked={!!o.value} disabled={o.disabled} onChange={o.onChange} ariaLabel={o.label} /><span>{o.label}</span></div>
|
|
100
|
+
{:else if o.type === 'date'}
|
|
101
|
+
<SvDateTimePicker value={dateVal(o.value)} dropDownDisplayMode="calendar" formatString="yyyy-MM-dd" disabled={o.disabled} onChange={o.onChange} />
|
|
102
|
+
{:else if o.type === 'color'}
|
|
103
|
+
<SvColorInput value={o.value ?? '#3b82f6'} disabled={o.disabled} onChange={o.onChange} />
|
|
104
|
+
{:else if o.type === 'rating'}
|
|
105
|
+
<SvRating value={o.value ?? 0} disabled={o.disabled} onChange={o.onChange} />
|
|
106
|
+
{:else}
|
|
107
|
+
<input id={o.id} class="sv-form__control" type={o.type} disabled={o.disabled} placeholder={o.placeholder} value={o.value ?? ''} oninput={(e) => o.onChange(e.currentTarget.value)} onblur={o.onBlur} />
|
|
108
|
+
{/if}
|
|
109
|
+
{/snippet}
|
|
110
|
+
|
|
111
|
+
{#snippet renderItemField(arrayName: string, i: number, itf: FormField)}
|
|
112
|
+
{@const type = itf.type ?? 'text'}
|
|
113
|
+
{@const id = `f-${arrayName}-${i}-${itf.name}`}
|
|
114
|
+
{@const err = form.itemError(arrayName, i, itf.name)}
|
|
115
|
+
<div class="sv-form__field" class:has-error={err}>
|
|
116
|
+
{#if type !== 'checkbox' && type !== 'switch'}
|
|
117
|
+
<label class="sv-form__label" for={id}>{itf.label}{#if itf.required}<span class="sv-form__req" aria-hidden="true"> *</span>{/if}</label>
|
|
118
|
+
{/if}
|
|
119
|
+
{@render control({ id, type, label: itf.label, value: form.itemValue(arrayName, i, itf.name), onChange: (v) => form.setItemValue(arrayName, i, itf.name, v), onBlur: () => form.handleItemBlur(arrayName, i, itf.name), disabled, placeholder: itf.placeholder, options: itf.options })}
|
|
120
|
+
{#if err}<span class="sv-form__error" role="alert">{err}</span>{/if}
|
|
121
|
+
</div>
|
|
122
|
+
{/snippet}
|
|
123
|
+
|
|
124
|
+
{#snippet renderArray(field: FormField)}
|
|
125
|
+
<div class="sv-form__field is-full sv-form__array" class:has-error={form.error(field.name)}>
|
|
126
|
+
<label class="sv-form__label">{field.label}{#if field.required}<span class="sv-form__req" aria-hidden="true"> *</span>{/if}</label>
|
|
127
|
+
{#each form.arrayItems(field.name) as _item, i (i)}
|
|
128
|
+
<div class="sv-form__arow">
|
|
129
|
+
<div class="sv-form__grid" style:--cols={field.itemFields?.length ?? 1}>
|
|
130
|
+
{#each field.itemFields ?? [] as itf (itf.name)}{@render renderItemField(field.name, i, itf)}{/each}
|
|
131
|
+
</div>
|
|
132
|
+
<button type="button" class="sv-form__aremove" disabled={disabled} onclick={() => form.removeItem(field.name, i)} aria-label="Remove item">×</button>
|
|
133
|
+
</div>
|
|
134
|
+
{/each}
|
|
135
|
+
<div><SvButton type="button" variant="outline" size="sm" disabled={disabled} onclick={() => form.addItem(field.name)}>{field.addLabel ?? '+ Add'}</SvButton></div>
|
|
136
|
+
{#if form.error(field.name)}<span class="sv-form__error" role="alert">{form.error(field.name)}</span>{/if}
|
|
137
|
+
</div>
|
|
138
|
+
{/snippet}
|
|
139
|
+
|
|
140
|
+
{#snippet renderField(field: FormField, cols: number)}
|
|
141
|
+
{#if form.isVisible(field.name)}
|
|
142
|
+
{@const type = field.type ?? 'text'}
|
|
143
|
+
{#if type === 'array'}
|
|
144
|
+
{@render renderArray(field)}
|
|
145
|
+
{:else}
|
|
146
|
+
{@const fieldDisabled = disabled || form.isDisabled(field.name)}
|
|
147
|
+
<div class="sv-form__field" class:is-full={field.full || cols === 1} class:has-error={form.error(field.name)}>
|
|
98
148
|
{#if type !== 'checkbox' && type !== 'switch'}
|
|
99
149
|
<label class="sv-form__label" for={`f-${field.name}`}>
|
|
100
150
|
{field.label}{#if field.required}<span class="sv-form__req" aria-hidden="true"> *</span>{/if}
|
|
101
151
|
</label>
|
|
102
152
|
{/if}
|
|
103
|
-
|
|
104
|
-
{#if
|
|
105
|
-
<
|
|
106
|
-
{:else if
|
|
107
|
-
<
|
|
108
|
-
{:else if type === 'password'}
|
|
109
|
-
<SvPasswordInput value={values[field.name] ?? ''} {disabled} onChange={(v) => setValue(field.name, v)} />
|
|
110
|
-
{:else if type === 'select'}
|
|
111
|
-
<SvDropDownList options={field.options ?? []} value={values[field.name] ?? null} {disabled} placeholder={field.placeholder} onChange={(v) => setValue(field.name, v)} />
|
|
112
|
-
{:else if type === 'checkbox'}
|
|
113
|
-
<SvCheckBox checked={!!values[field.name]} {disabled} onChange={(v) => setValue(field.name, v)}>{field.label}</SvCheckBox>
|
|
114
|
-
{:else if type === 'switch'}
|
|
115
|
-
<div class="sv-form__inline"><SvSwitchButton checked={!!values[field.name]} {disabled} onChange={(v) => setValue(field.name, v)} ariaLabel={field.label} /><span>{field.label}</span></div>
|
|
116
|
-
{:else if type === 'date'}
|
|
117
|
-
<SvDateTimePicker value={dateVal(values[field.name])} dropDownDisplayMode="calendar" formatString="yyyy-MM-dd" {disabled} onChange={(v) => setValue(field.name, v)} />
|
|
118
|
-
{:else if type === 'color'}
|
|
119
|
-
<SvColorInput value={values[field.name] ?? '#3b82f6'} {disabled} onChange={(v) => setValue(field.name, v)} />
|
|
120
|
-
{:else if type === 'rating'}
|
|
121
|
-
<SvRating value={values[field.name] ?? 0} {disabled} onChange={(v) => setValue(field.name, v)} />
|
|
122
|
-
{:else}
|
|
123
|
-
<input id={`f-${field.name}`} class="sv-form__control" type={type} {disabled} placeholder={field.placeholder} value={values[field.name] ?? ''} oninput={(e) => setValue(field.name, e.currentTarget.value)} onblur={() => { touched[field.name] = true; validateField(field.name) }} />
|
|
153
|
+
{@render control({ id: `f-${field.name}`, type, label: field.label, value: form.value(field.name), onChange: (v) => form.setValue(field.name, v), onBlur: () => form.handleBlur(field.name), disabled: fieldDisabled, placeholder: field.placeholder, options: field.options })}
|
|
154
|
+
{#if form.error(field.name)}
|
|
155
|
+
<span class="sv-form__error" role="alert">{form.error(field.name)}</span>
|
|
156
|
+
{:else if form.isValidating(field.name)}
|
|
157
|
+
<span class="sv-form__checking" role="status"><span class="sv-form__spinner" aria-hidden="true"></span>Checking…</span>
|
|
124
158
|
{/if}
|
|
125
|
-
|
|
126
|
-
{#if errors[field.name]}<span class="sv-form__error" role="alert">{errors[field.name]}</span>{/if}
|
|
127
159
|
</div>
|
|
128
|
-
{/
|
|
160
|
+
{/if}
|
|
161
|
+
{/if}
|
|
162
|
+
{/snippet}
|
|
163
|
+
|
|
164
|
+
{#snippet grid(flds: ReadonlyArray<FormField>, cols: number)}
|
|
165
|
+
<div class="sv-form__grid" style:--cols={cols}>
|
|
166
|
+
{#each flds as field (field.name)}{@render renderField(field, cols)}{/each}
|
|
129
167
|
</div>
|
|
168
|
+
{/snippet}
|
|
169
|
+
|
|
170
|
+
{#snippet actions()}
|
|
130
171
|
<div class="sv-form__actions">
|
|
131
|
-
|
|
172
|
+
{#if showReset}
|
|
173
|
+
<SvButton type="button" variant="ghost" disabled={disabled || form.submitting} onclick={() => form.reset()}>{resetLabel}</SvButton>
|
|
174
|
+
{/if}
|
|
175
|
+
{#if onCancel}
|
|
176
|
+
<SvButton type="button" variant="secondary" disabled={disabled || form.submitting} onclick={onCancel}>{cancelLabel}</SvButton>
|
|
177
|
+
{/if}
|
|
178
|
+
<SvButton type="submit" loading={form.submitting} disabled={disabled || form.submitting}>{submitLabel}</SvButton>
|
|
132
179
|
</div>
|
|
180
|
+
{/snippet}
|
|
181
|
+
|
|
182
|
+
<form class="sv-form" onsubmit={submit} novalidate>
|
|
183
|
+
{#if wizard}
|
|
184
|
+
{@const s = sections[Math.min(step, sections.length - 1)]!}
|
|
185
|
+
<SvStepper steps={stepItems} current={step} onChange={(n) => (step = n)} linear />
|
|
186
|
+
<fieldset class="sv-form__section">
|
|
187
|
+
{#if s.description}<p class="sv-form__section-desc">{s.description}</p>{/if}
|
|
188
|
+
{@render grid(s.fields, s.columns ?? columns)}
|
|
189
|
+
</fieldset>
|
|
190
|
+
<div class="sv-form__actions">
|
|
191
|
+
{#if step > 0}
|
|
192
|
+
<SvButton type="button" variant="secondary" disabled={form.submitting} onclick={back}>Back</SvButton>
|
|
193
|
+
{/if}
|
|
194
|
+
<span class="sv-form__spacer"></span>
|
|
195
|
+
{#if step < sections.length - 1}
|
|
196
|
+
<SvButton type="button" disabled={form.submitting} onclick={next}>Next</SvButton>
|
|
197
|
+
{:else}
|
|
198
|
+
{#if showReset}
|
|
199
|
+
<SvButton type="button" variant="ghost" disabled={disabled || form.submitting} onclick={() => { form.reset(); step = 0 }}>{resetLabel}</SvButton>
|
|
200
|
+
{/if}
|
|
201
|
+
<SvButton type="submit" loading={form.submitting} disabled={disabled || form.submitting}>{submitLabel}</SvButton>
|
|
202
|
+
{/if}
|
|
203
|
+
</div>
|
|
204
|
+
{:else if sections.length > 0}
|
|
205
|
+
{#each fields as entry (isFormSection(entry) ? entry.section : entry.name)}
|
|
206
|
+
{#if isFormSection(entry)}
|
|
207
|
+
<fieldset class="sv-form__section">
|
|
208
|
+
<legend class="sv-form__section-title">{entry.section}</legend>
|
|
209
|
+
{#if entry.description}<p class="sv-form__section-desc">{entry.description}</p>{/if}
|
|
210
|
+
{@render grid(entry.fields, entry.columns ?? columns)}
|
|
211
|
+
</fieldset>
|
|
212
|
+
{:else}
|
|
213
|
+
{@render grid([entry], columns)}
|
|
214
|
+
{/if}
|
|
215
|
+
{/each}
|
|
216
|
+
{@render actions()}
|
|
217
|
+
{:else}
|
|
218
|
+
{@render grid(fields as ReadonlyArray<FormField>, columns)}
|
|
219
|
+
{@render actions()}
|
|
220
|
+
{/if}
|
|
133
221
|
</form>
|
|
134
222
|
|
|
135
223
|
<style>
|
|
136
|
-
.sv-form { --_accent: var(--sg-accent, #2563eb); width: 100%; }
|
|
224
|
+
.sv-form { --_accent: var(--sg-accent, #2563eb); width: 100%; display: flex; flex-direction: column; gap: 16px; }
|
|
137
225
|
.sv-form__grid { display: grid; grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr)); gap: 14px 18px; }
|
|
226
|
+
.sv-form__section { border: 1px solid var(--sg-border, #e2e8f0); border-radius: 10px; padding: 16px; margin: 0; min-inline-size: 0; }
|
|
227
|
+
.sv-form__section-title { font-size: 13px; font-weight: 700; color: var(--sg-fg, #0f172a); padding: 0 4px; }
|
|
228
|
+
.sv-form__section-desc { margin: 0 0 12px; font-size: 12px; color: var(--sg-muted, #64748b); }
|
|
138
229
|
.sv-form__field { display: flex; flex-direction: column; gap: 5px; }
|
|
139
230
|
.sv-form__field.is-full { grid-column: 1 / -1; }
|
|
140
231
|
.sv-form__label { font-size: 12.5px; font-weight: 600; color: var(--sg-fg, #0f172a); }
|
|
@@ -149,5 +240,24 @@
|
|
|
149
240
|
.sv-form__field.has-error .sv-form__control { border-color: var(--sg-danger, #dc2626); }
|
|
150
241
|
.sv-form__inline { display: flex; align-items: center; gap: 8px; font-size: 13px; }
|
|
151
242
|
.sv-form__error { font-size: 11.5px; color: var(--sg-danger, #dc2626); }
|
|
152
|
-
.sv-
|
|
243
|
+
.sv-form__checking { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--sg-muted, #64748b); }
|
|
244
|
+
.sv-form__spinner {
|
|
245
|
+
width: 11px; height: 11px; border-radius: 50%;
|
|
246
|
+
border: 2px solid color-mix(in srgb, var(--_accent) 30%, transparent);
|
|
247
|
+
border-top-color: var(--_accent); animation: sv-form-spin 0.6s linear infinite;
|
|
248
|
+
}
|
|
249
|
+
@keyframes sv-form-spin { to { transform: rotate(360deg); } }
|
|
250
|
+
@media (prefers-reduced-motion: reduce) { .sv-form__spinner { animation: none; } }
|
|
251
|
+
.sv-form__actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
|
|
252
|
+
.sv-form__spacer { flex: 1; }
|
|
253
|
+
.sv-form__array { gap: 8px; }
|
|
254
|
+
.sv-form__arow { display: flex; align-items: flex-start; gap: 8px; }
|
|
255
|
+
.sv-form__arow > .sv-form__grid { flex: 1; padding: 10px; border: 1px solid var(--sg-border, #e2e8f0); border-radius: 8px; }
|
|
256
|
+
.sv-form__aremove {
|
|
257
|
+
flex: none; width: 28px; height: 28px; margin-top: 10px; border-radius: 6px; cursor: pointer;
|
|
258
|
+
border: 1px solid var(--sg-border, #cbd5e1); background: var(--sg-input-bg, #fff);
|
|
259
|
+
color: var(--sg-muted, #64748b); font-size: 16px; line-height: 1;
|
|
260
|
+
}
|
|
261
|
+
.sv-form__aremove:hover:not(:disabled) { color: var(--sg-danger, #dc2626); border-color: var(--sg-danger, #dc2626); }
|
|
262
|
+
.sv-form__aremove:disabled { opacity: 0.5; cursor: default; }
|
|
153
263
|
</style>
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Component tests for SvForm, exercising the shared createForm core: required +
|
|
3
|
+
* custom validation block submit and surface errors, a valid form submits its
|
|
4
|
+
* values, and onChange fires on edits.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
7
|
+
import { mount, unmount, flushSync } from 'svelte'
|
|
8
|
+
import SvForm from './SvForm.svelte'
|
|
9
|
+
|
|
10
|
+
function mountForm(props: Record<string, unknown>) {
|
|
11
|
+
const target = document.createElement('div')
|
|
12
|
+
document.body.appendChild(target)
|
|
13
|
+
const app = mount(SvForm, { target, props: props as any })
|
|
14
|
+
flushSync()
|
|
15
|
+
return { target, destroy: () => { unmount(app); target.remove() } }
|
|
16
|
+
}
|
|
17
|
+
const submitForm = (target: HTMLElement) => {
|
|
18
|
+
target.querySelector('form.sv-form')!.dispatchEvent(new Event('submit', { cancelable: true, bubbles: true }))
|
|
19
|
+
flushSync()
|
|
20
|
+
}
|
|
21
|
+
const setInput = (target: HTMLElement, id: string, value: string) => {
|
|
22
|
+
const el = target.querySelector<HTMLInputElement>(`#${id}`)!
|
|
23
|
+
el.value = value
|
|
24
|
+
el.dispatchEvent(new Event('input', { bubbles: true }))
|
|
25
|
+
flushSync()
|
|
26
|
+
}
|
|
27
|
+
const tick = async () => { await new Promise((r) => setTimeout(r)); flushSync() }
|
|
28
|
+
const byText = (target: HTMLElement, text: string) =>
|
|
29
|
+
[...target.querySelectorAll('button')].find((b) => b.textContent?.trim() === text)!
|
|
30
|
+
|
|
31
|
+
const fields = [
|
|
32
|
+
{ name: 'name', label: 'Name', required: true },
|
|
33
|
+
{ name: 'email', label: 'Email', type: 'email' as const,
|
|
34
|
+
validate: (v: any) => (v && !String(v).includes('@') ? 'Invalid email' : null) },
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
describe('SvForm', () => {
|
|
38
|
+
it('blocks submit and shows an error when a required field is empty', () => {
|
|
39
|
+
const onSubmit = vi.fn()
|
|
40
|
+
const { target, destroy } = mountForm({ fields, onSubmit })
|
|
41
|
+
try {
|
|
42
|
+
submitForm(target)
|
|
43
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
44
|
+
const err = target.querySelector('.sv-form__error[role="alert"]')
|
|
45
|
+
expect(err?.textContent).toContain('Name is required')
|
|
46
|
+
} finally { destroy() }
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('submits the collected values when valid', () => {
|
|
50
|
+
const onSubmit = vi.fn()
|
|
51
|
+
const { target, destroy } = mountForm({ fields, onSubmit })
|
|
52
|
+
try {
|
|
53
|
+
setInput(target, 'f-name', 'Ada')
|
|
54
|
+
submitForm(target)
|
|
55
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
56
|
+
expect(onSubmit.mock.calls[0]![0]).toMatchObject({ name: 'Ada' })
|
|
57
|
+
} finally { destroy() }
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('runs a custom validator', () => {
|
|
61
|
+
const onSubmit = vi.fn()
|
|
62
|
+
const { target, destroy } = mountForm({ fields, onSubmit })
|
|
63
|
+
try {
|
|
64
|
+
setInput(target, 'f-name', 'Ada')
|
|
65
|
+
setInput(target, 'f-email', 'nope')
|
|
66
|
+
submitForm(target)
|
|
67
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
68
|
+
expect([...target.querySelectorAll('.sv-form__error')].some((e) => e.textContent?.includes('Invalid email'))).toBe(true)
|
|
69
|
+
} finally { destroy() }
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('fires onChange as fields are edited', () => {
|
|
73
|
+
const onChange = vi.fn()
|
|
74
|
+
const { target, destroy } = mountForm({ fields, onChange })
|
|
75
|
+
try {
|
|
76
|
+
setInput(target, 'f-name', 'A')
|
|
77
|
+
expect(onChange).toHaveBeenCalled()
|
|
78
|
+
expect(onChange.mock.calls.at(-1)![0]).toMatchObject({ name: 'A' })
|
|
79
|
+
} finally { destroy() }
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe('SvForm - conditional fields', () => {
|
|
84
|
+
const flds = [
|
|
85
|
+
{ name: 'kind', label: 'Kind' },
|
|
86
|
+
{ name: 'other', label: 'Other', required: true, visible: (v: any) => v.kind === 'x' },
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
it('hides a field until its condition is met, and excludes hidden fields from validation + submit', () => {
|
|
90
|
+
const onSubmit = vi.fn()
|
|
91
|
+
const { target, destroy } = mountForm({ fields: flds, initial: { kind: '' }, onSubmit })
|
|
92
|
+
try {
|
|
93
|
+
// 'other' is hidden -> not rendered
|
|
94
|
+
expect(target.querySelector('#f-other')).toBeNull()
|
|
95
|
+
// ...and its `required` does not block submit; it's absent from the payload
|
|
96
|
+
setInput(target, 'f-kind', 'y')
|
|
97
|
+
submitForm(target)
|
|
98
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
99
|
+
expect(onSubmit.mock.calls[0]![0]).not.toHaveProperty('other')
|
|
100
|
+
// Meeting the condition reveals the field
|
|
101
|
+
setInput(target, 'f-kind', 'x')
|
|
102
|
+
expect(target.querySelector('#f-other')).not.toBeNull()
|
|
103
|
+
// Now visible + required + empty -> blocks submit
|
|
104
|
+
onSubmit.mockClear()
|
|
105
|
+
submitForm(target)
|
|
106
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
107
|
+
} finally { destroy() }
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('derives a field disabled state from other values', () => {
|
|
111
|
+
const dflds = [
|
|
112
|
+
{ name: 'lock', label: 'Lock', type: 'switch' as const },
|
|
113
|
+
{ name: 'note', label: 'Note', disabled: (v: any) => !!v.lock },
|
|
114
|
+
]
|
|
115
|
+
const { target, destroy } = mountForm({ fields: dflds, initial: { lock: true } })
|
|
116
|
+
try {
|
|
117
|
+
expect(target.querySelector<HTMLInputElement>('#f-note')!.disabled).toBe(true)
|
|
118
|
+
} finally { destroy() }
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
describe('SvForm - reset + async submit', () => {
|
|
123
|
+
it('reset restores the initial values', () => {
|
|
124
|
+
const { target, destroy } = mountForm({ fields: [{ name: 'name', label: 'Name' }], initial: { name: 'Ada' }, showReset: true })
|
|
125
|
+
try {
|
|
126
|
+
setInput(target, 'f-name', 'Grace')
|
|
127
|
+
expect(target.querySelector<HTMLInputElement>('#f-name')!.value).toBe('Grace')
|
|
128
|
+
const reset = [...target.querySelectorAll('button')].find((b) => b.textContent?.trim() === 'Reset')!
|
|
129
|
+
reset.click(); flushSync()
|
|
130
|
+
expect(target.querySelector<HTMLInputElement>('#f-name')!.value).toBe('Ada')
|
|
131
|
+
} finally { destroy() }
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('disables the submit button while an async onSubmit is in flight', async () => {
|
|
135
|
+
let release: () => void = () => {}
|
|
136
|
+
const gate = new Promise<void>((r) => { release = r })
|
|
137
|
+
const onSubmit = vi.fn(() => gate)
|
|
138
|
+
const { target, destroy } = mountForm({ fields: [{ name: 'name', label: 'Name' }], initial: { name: 'Ada' }, onSubmit })
|
|
139
|
+
try {
|
|
140
|
+
submitForm(target)
|
|
141
|
+
const btn = target.querySelector<HTMLButtonElement>('button[type="submit"]')!
|
|
142
|
+
expect(btn.disabled).toBe(true) // submitting
|
|
143
|
+
release()
|
|
144
|
+
await gate; await Promise.resolve(); await Promise.resolve(); flushSync()
|
|
145
|
+
expect(btn.disabled).toBe(false)
|
|
146
|
+
} finally { destroy() }
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
describe('SvForm - async validation', () => {
|
|
151
|
+
const flush = async () => { await new Promise((r) => setTimeout(r)); flushSync() }
|
|
152
|
+
|
|
153
|
+
it('async validator blocks submit on error and passes once it clears', async () => {
|
|
154
|
+
const onSubmit = vi.fn()
|
|
155
|
+
const check = vi.fn(async (v: string) => (v === 'taken' ? 'Already taken' : null))
|
|
156
|
+
const flds = [{ name: 'user', label: 'User', asyncValidate: check, asyncDebounce: 0 }]
|
|
157
|
+
const { target, destroy } = mountForm({ fields: flds, initial: { user: 'taken' }, onSubmit })
|
|
158
|
+
try {
|
|
159
|
+
submitForm(target)
|
|
160
|
+
await flush()
|
|
161
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
162
|
+
expect([...target.querySelectorAll('.sv-form__error')].some((e) => e.textContent?.includes('Already taken'))).toBe(true)
|
|
163
|
+
setInput(target, 'f-user', 'free')
|
|
164
|
+
submitForm(target)
|
|
165
|
+
await flush()
|
|
166
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
167
|
+
} finally { destroy() }
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('shows a checking indicator while an async validator runs, then clears', async () => {
|
|
171
|
+
let resolve: (m: string | null) => void = () => {}
|
|
172
|
+
const check = () => new Promise<string | null>((r) => { resolve = r })
|
|
173
|
+
const flds = [{ name: 'user', label: 'User', asyncValidate: check, asyncDebounce: 0 }]
|
|
174
|
+
const { target, destroy } = mountForm({ fields: flds, initial: { user: 'x' } })
|
|
175
|
+
try {
|
|
176
|
+
target.querySelector('#f-user')!.dispatchEvent(new Event('blur', { bubbles: true }))
|
|
177
|
+
flushSync()
|
|
178
|
+
await flush() // debounce timer fires -> runAsync -> validating
|
|
179
|
+
expect(target.querySelector('.sv-form__checking')).not.toBeNull()
|
|
180
|
+
resolve(null)
|
|
181
|
+
await flush()
|
|
182
|
+
expect(target.querySelector('.sv-form__checking')).toBeNull()
|
|
183
|
+
} finally { destroy() }
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it('drops a stale async response (only the latest wins)', async () => {
|
|
187
|
+
const resolvers: Array<(m: string | null) => void> = []
|
|
188
|
+
const check = () => new Promise<string | null>((r) => resolvers.push(r))
|
|
189
|
+
const flds = [{ name: 'user', label: 'User', asyncValidate: check, asyncDebounce: 0 }]
|
|
190
|
+
const { target, destroy } = mountForm({ fields: flds, initial: { user: '' } })
|
|
191
|
+
try {
|
|
192
|
+
target.querySelector('#f-user')!.dispatchEvent(new Event('blur', { bubbles: true }))
|
|
193
|
+
flushSync()
|
|
194
|
+
setInput(target, 'f-user', 'a')
|
|
195
|
+
await flush() // run #1 (resolvers[0])
|
|
196
|
+
setInput(target, 'f-user', 'ab')
|
|
197
|
+
await flush() // run #2 (resolvers[1])
|
|
198
|
+
resolvers[1]?.(null) // latest resolves clean
|
|
199
|
+
resolvers[0]?.('stale err') // earlier resolves with an error - must be ignored
|
|
200
|
+
await flush()
|
|
201
|
+
expect(target.querySelectorAll('.sv-form__error').length).toBe(0)
|
|
202
|
+
} finally { destroy() }
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
describe('SvForm - sections', () => {
|
|
207
|
+
const schema = [
|
|
208
|
+
{ section: 'Account', fields: [{ name: 'user', label: 'User', required: true }] },
|
|
209
|
+
{ section: 'Profile', description: 'About you', fields: [{ name: 'bio', label: 'Bio', type: 'textarea' as const }] },
|
|
210
|
+
]
|
|
211
|
+
|
|
212
|
+
it('renders sections as fieldsets with legends, showing all fields', () => {
|
|
213
|
+
const { target, destroy } = mountForm({ fields: schema })
|
|
214
|
+
try {
|
|
215
|
+
expect(target.querySelectorAll('fieldset.sv-form__section').length).toBe(2)
|
|
216
|
+
expect([...target.querySelectorAll('legend')].map((l) => l.textContent?.trim())).toEqual(['Account', 'Profile'])
|
|
217
|
+
expect(target.querySelector('#f-user')).not.toBeNull()
|
|
218
|
+
expect(target.querySelector('#f-bio')).not.toBeNull()
|
|
219
|
+
} finally { destroy() }
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
describe('SvForm - stepper wizard', () => {
|
|
224
|
+
const schema = [
|
|
225
|
+
{ section: 'Step 1', fields: [{ name: 'user', label: 'User', required: true }] },
|
|
226
|
+
{ section: 'Step 2', fields: [{ name: 'bio', label: 'Bio' }] },
|
|
227
|
+
]
|
|
228
|
+
|
|
229
|
+
it('shows one step, gates Next on validation, reveals Submit on the last step', async () => {
|
|
230
|
+
const onSubmit = vi.fn()
|
|
231
|
+
const { target, destroy } = mountForm({ fields: schema, stepper: true, onSubmit })
|
|
232
|
+
try {
|
|
233
|
+
// Only step 1 is rendered
|
|
234
|
+
expect(target.querySelector('#f-user')).not.toBeNull()
|
|
235
|
+
expect(target.querySelector('#f-bio')).toBeNull()
|
|
236
|
+
// Next is blocked while the required field is empty
|
|
237
|
+
byText(target, 'Next').click(); await tick()
|
|
238
|
+
expect(target.querySelector('#f-bio')).toBeNull()
|
|
239
|
+
expect(target.querySelector('.sv-form__error')?.textContent).toContain('User is required')
|
|
240
|
+
// Fill and advance
|
|
241
|
+
setInput(target, 'f-user', 'ada')
|
|
242
|
+
byText(target, 'Next').click(); await tick()
|
|
243
|
+
expect(target.querySelector('#f-bio')).not.toBeNull()
|
|
244
|
+
expect(target.querySelector('#f-user')).toBeNull()
|
|
245
|
+
// Last step: Submit is present and fires
|
|
246
|
+
expect(target.querySelector('button[type="submit"]')).not.toBeNull()
|
|
247
|
+
submitForm(target); await tick()
|
|
248
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
249
|
+
expect(onSubmit.mock.calls[0]![0]).toMatchObject({ user: 'ada' })
|
|
250
|
+
} finally { destroy() }
|
|
251
|
+
})
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
describe('SvForm - field arrays', () => {
|
|
255
|
+
const schema = [
|
|
256
|
+
{ name: 'contacts', label: 'Contacts', type: 'array' as const, required: true, itemFields: [
|
|
257
|
+
{ name: 'name', label: 'Name', required: true },
|
|
258
|
+
{ name: 'email', label: 'Email', type: 'email' as const },
|
|
259
|
+
] },
|
|
260
|
+
]
|
|
261
|
+
|
|
262
|
+
it('adds and removes item rows', () => {
|
|
263
|
+
const { target, destroy } = mountForm({ fields: schema, initial: { contacts: [] } })
|
|
264
|
+
try {
|
|
265
|
+
expect(target.querySelectorAll('.sv-form__arow').length).toBe(0)
|
|
266
|
+
byText(target, '+ Add').click(); flushSync()
|
|
267
|
+
expect(target.querySelectorAll('.sv-form__arow').length).toBe(1)
|
|
268
|
+
expect(target.querySelector('#f-contacts-0-name')).not.toBeNull()
|
|
269
|
+
byText(target, '+ Add').click(); flushSync()
|
|
270
|
+
expect(target.querySelectorAll('.sv-form__arow').length).toBe(2)
|
|
271
|
+
;(target.querySelectorAll<HTMLButtonElement>('.sv-form__aremove')[0]!).click(); flushSync()
|
|
272
|
+
expect(target.querySelectorAll('.sv-form__arow').length).toBe(1)
|
|
273
|
+
} finally { destroy() }
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('validates item fields and includes the array in the submitted payload', async () => {
|
|
277
|
+
const onSubmit = vi.fn()
|
|
278
|
+
const { target, destroy } = mountForm({ fields: schema, initial: { contacts: [{ name: '', email: '' }] }, onSubmit })
|
|
279
|
+
try {
|
|
280
|
+
submitForm(target); await tick()
|
|
281
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
282
|
+
expect([...target.querySelectorAll('.sv-form__error')].some((e) => e.textContent?.includes('Name is required'))).toBe(true)
|
|
283
|
+
setInput(target, 'f-contacts-0-name', 'Ada')
|
|
284
|
+
submitForm(target); await tick()
|
|
285
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
286
|
+
expect(onSubmit.mock.calls[0]![0].contacts).toEqual([{ name: 'Ada', email: '' }])
|
|
287
|
+
} finally { destroy() }
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('array `required` blocks submit when empty', async () => {
|
|
291
|
+
const onSubmit = vi.fn()
|
|
292
|
+
const { target, destroy } = mountForm({ fields: schema, initial: { contacts: [] }, onSubmit })
|
|
293
|
+
try {
|
|
294
|
+
submitForm(target); await tick()
|
|
295
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
296
|
+
expect([...target.querySelectorAll('.sv-form__error')].some((e) => e.textContent?.includes('Contacts is required'))).toBe(true)
|
|
297
|
+
} finally { destroy() }
|
|
298
|
+
})
|
|
299
|
+
})
|