UNPKG

@mitcsutt/kiln-forms

Version:

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

45 lines (44 loc) • 1.94 kB
import { type AnyFormApi, type StandardSchemaV1 } from '@tanstack/react-form'; import { type AfterSubmit, type FormRuntime } from './formRuntime.js'; /** * Thrown inside TanStack's `onSubmit` after the pipeline has already handled a failure, so * TanStack records `isSubmitSuccessful: false`. `guardSubmit` swallows it — callers of * `handleSubmit` never see a rejection. */ export declare class SubmitHandled extends Error { constructor(); } /** * Replaces the value of every inactive path marked `prune` with its default — the payload never * carries hidden or excluded values (§5.4). The default is the field-level `defaultValue`, else — * inside a mounted repeater's row — the row's `newItem` value for that field, else the form's * defaults. When that default is `undefined` the key is removed: a pruned path is never written as * `undefined`. */ export declare function prune<T>(values: T, runtime: FormRuntime, defaults: unknown): T; export interface SubmitConfig { schema?: StandardSchemaV1; afterSubmit: AfterSubmit; onSubmit?: (ctx: { value: unknown; output: unknown; formApi: AnyFormApi; meta: unknown; }) => unknown; onSubmitError?: (ctx: { error: unknown; formApi: AnyFormApi; }) => void; } /** Builds TanStack's `onSubmit`: prune → parse → onSubmit → afterSubmit, or map the failure (§5.5). */ export declare function createSubmitHandler(getConfig: () => SubmitConfig): ({ value, formApi, meta, }: { value: unknown; formApi: AnyFormApi; meta: unknown; }) => Promise<void>; /** * Makes `handleSubmit` never reject with our sentinel, ignores it while a submit is in flight, * and unlocks `afterSubmit: 'lock'` on `reset()`. Applied to both TanStack's core FormApi and * the React extended form (they hold separate copies of these methods). */ export declare function guardSubmit(target: AnyFormApi): void;