UNPKG

@mitcsutt/kiln-forms

Version:

Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui

38 lines (37 loc) • 1.71 kB
import { type AnyKitForm, type AutosaveStatus } from '../runtime/formRuntime.js'; export interface AutosaveOptions { /** Default 800. */ debounceMs?: number; /** * Skip saving while the form has errors. Default `true`. Before each save it runs every edited * field's validators and the form-level ones (schema included) with cause `change`, even before a * field's first blur, so a stale `isValid` never lets an invalid value through. Errors it finds * stay hidden until the form's `errorVisibility` shows them. */ onlyWhenValid?: boolean; /** After a save, the saved values become the baseline (clean). Default `true`. */ rebaseline?: boolean; } export interface AutosaveState { status: AutosaveStatus; error?: unknown; /** Save now (skips the debounce). */ flush: () => void; } /** * Save as the reader types, debounced, aborting a save a newer one replaces, and only when the * form is valid. * * @remarks * `useAutosave(form, save, options)` calls `save` with the form's values after a quiet moment. A * newer save aborts the one in flight (through the `signal` it's given), a successful save makes * the saved values the new clean baseline, and the status (saving, saved, failed) is reported to * {@link FormStatus | `FormStatus`}. * * @privateRemarks * Debounced save of dirty values: aborts an in-flight save when a newer one starts, * rebaselines on success, and reports status to `FormStatus` through the form runtime. */ export declare function useAutosave<A extends AnyKitForm>(target: A, save: (value: A['state']['values'], ctx: { signal: AbortSignal; }) => Promise<unknown>, opts?: AutosaveOptions): AutosaveState;