ui/form_state.svelte.ts

Reactive form state for controlling when field errors appear and focusing invalid fields.

Tracks per-field touched state (set on blur via delegated focusout) and form-level attempted state (set on submit attempt). Errors show after a field is blurred or after a submit attempt, avoiding premature validation while the user is still typing.

The FormState.form attachment also handles the Enter key: in an input followed by another input it moves focus to that input (past any button between), and in the last input it submits the form (requestSubmit) — so Enter in a login form's password field logs in, even when the form's button is type="button", as PendingButton is. Hidden inputs don't count, and an Enter that commits IME composition is left alone.

While a submit is in flight, make the fields readonly rather than disabled (and the button aria-disabled, guarding the handler against re-entry) — disabling the focused element drops focus to <body>. Enter still advances through and submits from readonly inputs.

The type parameter names the form's fields, so focus, show, touch, and is_touched reject a misspelled field at compile time.

All trackable inputs must have a name attribute — an error is thrown in dev if an input without name loses focus.

<script lang="ts"> const form_state = new FormState<'username'>(); let username = $state.raw(''); const username_valid = $derived(is(Username, username)); const can_submit = $derived(username.trim() && username_valid); const handle_submit = async () => { form_state.attempt(); if (!can_submit) { if (!username.trim() || !username_valid) form_state.focus('username'); return; } // submit... }; </script> <form {@attach form_state.form()} onsubmit={(e) => { e.preventDefault(); void handle_submit(); }}> <input name="username" bind:value={username} /> {#if form_state.show('username') && username && !username_valid} <p>error message</p> {/if} <PendingButton onclick={handle_submit}>submit</PendingButton> </form>
view source

Declarations
#

FormState
#

ui/form_state.svelte.ts view source

import {FormState} from '@fuzdev/fuz_app/ui/form_state.svelte.js';

generics

FormState<TField extends string = string>
TField
constraint string
default string

form

Creates a form attachment that handles the Enter key (advance focus to the next input, or submit from the last one) and tracks field touched state via delegated focusout.

Fields are identified by their name attribute.

type (): Attachment<HTMLFormElement>

returns Attachment<HTMLFormElement>

throws

  • Error - in DEV when called while already attached, or when an input loses focus without a `name` attribute

is_touched

Whether a field has been blurred at least once.

type (field: TField): boolean

field

type TField
returns boolean

show

Whether to show validation errors for a field. Returns true if the field has been blurred or a submit attempt was made.

type (field: TField): boolean

field

type TField
returns boolean

touch

Programmatically marks a field as touched without requiring a blur event.

type (field: TField): void

field

type TField
returns void

mutates

  • this

focus

Focuses the named input within the form.

type (field: TField): void

field

type TField
returns void

attempt

Marks the form as having been submitted, causing all field errors to show.

type (): void

returns void

mutates

  • this

reset

Resets all touched and attempted state.

type (): void

returns void

mutates

  • this

attempted

Whether a submit attempt has been made.

type boolean

getter

Imported by
#