/** * createForm - the HEADLESS core behind : values / errors / touched * state, required + rule + custom validation (validate-on-blur-then-live), and a * submit that validates every field first. No markup, no controls - you render * the fields however you like and read/write through the core. * * ```svelte * *
{ e.preventDefault(); form.submit() }}> * {#each fields as f} * form.setValue(f.name, e.currentTarget.value)} * onblur={() => form.handleBlur(f.name)} /> * {#if form.error(f.name)}{form.error(f.name)}{/if} * {/each} *
* ``` */ import { type FormEntry } from './form-field'; /** Localizable strings the form generates itself (override via `messages`). */ export type FormMessages = { required: (label: string) => string; minItems: (label: string, n: number) => string; maxItems: (label: string, n: number) => string; /** Shown by SvForm while an async validator runs. */ checking: string; }; export type FormConfig = { /** The schema - a mix of flat fields and titled sections. */ fields: () => ReadonlyArray; /** Seed values on creation. */ initial?: Record; /** Called with the (visible-field) values on a valid submit. May be async; * `submitting` stays true until the returned promise settles. */ onSubmit?: (values: Record) => void | Promise; onChange?: (values: Record) => void; /** Override the built-in generated strings (required / min-max items / checking). */ messages?: Partial; }; export declare function createForm(config: FormConfig): { readonly values: Record; readonly submitting: boolean; readonly isDirty: boolean; value: (name: string) => any; error: (name: string) => string | undefined; isTouched: (name: string) => boolean; hasError: (name: string) => boolean; isValidating: (name: string) => boolean; isFieldDirty: (name: string) => boolean; isVisible: (name: string) => boolean; isDisabled: (name: string) => boolean; entries: () => readonly FormEntry[]; arrayItems: (name: string) => Record[]; itemValue: (name: string, i: number, field: string) => any; itemError: (name: string, i: number, field: string) => string | undefined; addItem: (name: string, item?: Record) => void; removeItem: (name: string, i: number) => void; moveItem: (name: string, from: number, to: number) => void; setItemValue: (name: string, i: number, field: string, v: any) => void; handleItemBlur: (name: string, i: number, field: string) => void; setValue: (name: string, v: any) => void; handleBlur: (name: string) => void; validateField: (name: string) => boolean; validateFields: (names: string[]) => Promise; submit: () => Promise; reset: (next?: Record) => void; setErrors: (map: Record) => void; errorList: () => { name: string; label: string; message: string; }[]; firstErrorField: () => string | undefined; /** The resolved message strings (defaults merged with `config.messages`). */ messages: FormMessages; }; export type Form = ReturnType;