@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
37 lines (36 loc) • 1.88 kB
TypeScript
import { type AnyKitForm } from '../runtime/formRuntime.js';
export interface ServerValuesOptions {
/** Keep the user's edits (paths that differ from the old baseline). Default `true`. */
keepDirty?: boolean;
/**
* Keep errors that were visible before the refresh, on values the refresh left alone, each
* announced (or quiet) as it was before. Default `true`.
*/
keepErrors?: boolean;
}
/**
* Per leaf path: the user's value where it differs from the old baseline (dirty), otherwise the
* new server value. Plain objects recurse; arrays and other values are leaves.
*/
export declare function mergeDirty(oldBaseline: unknown, current: unknown, next: unknown): unknown;
/**
* Applies new server data as the form's baseline (§6.5): new defaults = `data`; values keep the
* user's dirty edits; errors the user could see stay visible where the refresh kept the value
* they were about (one the refresh replaced is no longer that error's value).
*/
export declare function applyServerValues(target: AnyKitForm, data: unknown, opts?: ServerValuesOptions): void;
/**
* Keep an edit form in step with server data that refreshes, without losing what the reader has
* typed.
*
* @remarks
* `useServerValues(form, data)` is for edit forms over data that can change underneath them: a
* query that refetches, a record someone else edits. When `data` changes, it becomes the new
* baseline. Fields the reader hasn't touched take the new values; fields they've edited keep their
* edits.
*
* @privateRemarks
* Edit mode bound to refreshing server data. Whenever `data` changes (deep compare) it
* becomes the new baseline; untouched fields take the new values, edited ones keep the user's.
*/
export declare function useServerValues<A extends AnyKitForm>(form: A, data: A['state']['values'] | undefined, opts?: ServerValuesOptions): void;