/** * The form runtime: field registration, validation, dependency rules, steps and * submission, as one observable store. * * Everything a framework binding has to supply is a way to re-render on * `subscribe` and a `FormData` on submit. * * @module */ import { type ReadableStore } from '../store.js'; export type FormStatus = 'idle' | 'submitting' | 'success' | 'error'; export type SubmissionState = { status: FormStatus; /** Whatever the failing submit threw, if anything. */ error: unknown; /** * The failure's message, ready to render. * * Only set when a `submitHandler` threw an `Error`. A failed built-in POST * leaves this undefined, so a template rendering it cannot put `Failed to * fetch` or a bare status code in front of a visitor. */ errorMessage: string | undefined; formSuccess: boolean; formError: boolean; isSubmitting: boolean; }; export type SubmissionStore = ReadableStore & { /** * Sets the status. An error passed alongside `'error'` becomes available as * `error` and `errorMessage`; any other status clears it. */ setStatus(status: FormStatus, error?: unknown): void; }; /** * Holds a form's submission status. * * Separate from the controller because the status is usually read outside the * form — an alert above it, a button in a footer. */ export declare function createSubmissionStore(): SubmissionStore; /** * Sends a validated form. * * Resolving means success: the caller resets the fields, returns to the first * step and scrolls, exactly as it does after its own POST. Throwing means * failure, and an `Error`'s message reaches the submission store's `errorMessage`. * * Runs in the browser. Anything needing a credential belongs in a server action * or a route handler called from here. * * @param formData The form's current values. * @param context.action The container's Submit URL, for a handler that wants it. */ export type FormSubmitHandler = (formData: FormData, context: { action: string; }) => Promise; /** The DOM work the controller does. Overridable so the controller is testable headless. */ export type FormEffects = { /** Scrolls the element with this id into view. */ scrollToElementId(elementId: string): void; /** Brings a field on screen and puts the caret in it. */ revealField(element: HTMLElement): void; }; /** The step rules navigation consults. The binding owns the field values they read. */ export type StepRules = { getJumpTarget(stepIds: string[]): string | null; isStepVisible(stepIds: string[]): boolean; }; /** The parts a binding may change without tearing the controller down. */ export type FormControllerSettings = { /** Where the built-in POST goes. Ignored when `submitHandler` is given. */ action?: string; /** Replaces the built-in POST. Everything around it is unchanged. */ submitHandler?: FormSubmitHandler; /** One entry per step, holding the ids rules may name that step by. */ stepIds?: string[][]; /** Consulted by `nextStep`. Without it, steps advance in order. */ stepRules?: StepRules; scrollToOnSuccess?: string | false; scrollToOnError?: string | false; }; export type FormControllerOptions = FormControllerSettings & { submission: SubmissionStore; effects?: Partial; }; export type FormState = { currentStepIndex: number; attemptedSubmit: boolean; hasAnyErrors: boolean; /** * Increments when the form is reset. Fields watch it and return to their * initial value: the inputs are controlled, so a DOM `form.reset()` clears the * markup but leaves framework state holding the old values. */ resetToken: number; /** * The field to bring on screen, or `null`. Set when submitting finds an invalid * field; the binding calls {@linkcode FormController.revealPendingField} once the * step holding it has been rendered. */ fieldToReveal: string | null; }; export type FormController = ReadableStore & { registerField(name: string, ref: HTMLElement | null, validate: () => boolean, stepIndex?: number): void; unregisterField(name: string): void; setFieldError(name: string, hasError: boolean): void; getFieldRef(name: string): HTMLElement | null; /** The step a field was registered on, or `undefined` if it is not in a step. */ getFieldStepIndex(name: string): number | undefined; /** * Runs the registered fields' validators. * * @param options.stepIndex Validate only the fields on this step. Omit to * validate the whole form, including steps that are not on screen. * @returns The names of the fields that failed, in page order. Empty means * everything validated passed. */ validateAllFields(options?: { stepIndex?: number; }): string[]; /** * Merges into the current settings; keys left out keep their value, keys set * to `undefined` clear it. A binding re-renders with new props far more often * than a form is mounted, and an inline `submitHandler` is a new function each * time, so these are read at use rather than captured at creation. */ update(settings: Partial): void; setAttemptedSubmit(value: boolean): void; nextStep(): void; prevStep(): void; /** Performs the deferred scroll-and-focus, if one is pending. */ revealPendingField(): void; /** Bumps {@linkcode FormState.resetToken} so fields return to their initial value. */ resetFields(): void; /** Returns to the first step, clears the fields and forgets the last attempt. */ reset(): void; /** * Validates, then sends. * * @param formData The values to send. Blanks shadowing an answer given on * another step are dropped first. * @param form The form element, reset on success so uncontrolled inputs clear. */ submit(formData: FormData, form?: { reset(): void; }): Promise; }; export declare function createFormController(options: FormControllerOptions): FormController;