UNPKG

@mitcsutt/kiln-forms

Version:

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

69 lines (68 loc) • 3.4 kB
import { type ReactNode } from 'react'; import { type DeepKeys } from '@tanstack/react-form'; import type { AnyKitForm, ValuesOf } from '../../../kit/types.js'; import type { ScopeNamesProps } from '../internal/types.js'; import type { Condition } from '../../../schema/core/types.js'; /** What happens to hidden fields' values (§5.4). */ export type WhenHidden = 'prune' | 'keep' | 'reset'; export interface WhenProps<A extends AnyKitForm> extends ScopeNamesProps { form: A; /** Component mode: visible while this returns `true`. Subscribes with a boolean selector. */ is?: (values: ValuesOf<A>) => boolean; /** Or a JSON condition (the schema-mode `Condition`). Ignored when `is` is given. */ condition?: Condition<ValuesOf<A>>; /** @internal Schema mode: the render `context` that `context` conditions read. */ context?: Record<string, unknown>; /** * `'prune'` (default): submitted as the field's default; the user's input survives hide/show. * `'keep'`: submitted as the current value. `'reset'`: reset to the default when hidden. */ whenHidden?: WhenHidden; /** Paths it governs that may never have mounted (edit mode with a hidden server value). */ names?: readonly DeepKeys<ValuesOf<A>>[]; /** Rendered instead while hidden. */ fallback?: ReactNode; /** * Animate the change: the children grow open when they appear and shrink closed before they * unmount (the `fallback` the other way round), with kiln-ui's `Collapse`. It wraps them in a * block element, so use it where they stack, not straight inside a grid. Ignored in view mode * and in inline layouts (a sentence, a table row). Default `false`. */ animate?: boolean; children: ReactNode; } /** * Shows fields only when a condition holds. Hidden fields stop validating and are reset in what's * submitted. * * @remarks * `When` renders its children only while a condition holds. Hidden fields unmount, so their * validators stop, their errors clear, and by default their values go back to their defaults in * what `onSubmit` receives. What the reader typed survives hiding and showing while they're * editing; only the payload is pruned. * * @example In a schema * Any node takes `when`: * * ```json * { * "kind": "textarea", * "name": "address", * "label": "Postal address", * "when": { "field": "delivery", "op": "eq", "value": "post" } * } * ``` * * See [Conditions](/docs/forms/schema/conditions) for every operator. `"whenAnimate": true` is * the schema's `animate`. * * @privateRemarks * Conditional rendering (§6.6). Children unmount while `is(values)` (or `condition`) is false (their validators * stop) and every path they governed — collected from the fields mounted inside, remembered after * they hide, plus `names` — becomes inactive with the `whenHidden` policy: errors cleared, and * the submitted value pruned to the default (`prune`), kept (`keep`) or reset now (`reset`). * Re-renders only when visibility flips. With `animate`, `Collapse` keeps the children mounted * through the close: their paths are already inactive by then, so they neither validate nor * submit while they shrink. */ export declare function When<A extends AnyKitForm>({ form, is, condition, context, whenHidden, names, scopeNames, fallback, animate, children, }: WhenProps<A>): ReactNode;