UNPKG

@mitcsutt/kiln-forms

Version:

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

86 lines (85 loc) • 3.49 kB
import { type RefCallback } from 'react'; import { type AnyFieldApi } from '@tanstack/react-form'; import type { FieldLayout } from '@mitcsutt/kiln-ui'; export { accepts } from '../kit/accepts.js'; export interface CommonFieldProps<V> { /** Non-blocking advice shown in the warning channel (never blocks submit). */ warn?: (value: V) => string | null | undefined; /** Visible but excluded: skips validation, clears its errors, submits its default value. */ excluded?: boolean; disabled?: boolean; readOnly?: boolean; required?: boolean; } export interface FieldBindingOptions<V> extends CommonFieldProps<V> { /** Dev-only guard for canonical `field.X` use, where TS cannot check the value type. */ accepts: (value: unknown) => boolean; /** Value this field writes when cleared ('' | null | [] | false). Used by rules like `required`. */ empty: V; /** The field's own layout; wins over a surrounding `FieldPresentation`. */ layout?: FieldLayout; /** The field's own `labelHidden`; wins over a surrounding `FieldPresentation`. */ labelHidden?: boolean; /** Extra ids to describe the control, merged with an external error id. */ 'aria-describedby'?: string; /** * The field's own check of its value, for a value the control can hold but the field can't * accept (a JSON field's text that doesn't parse). Return a message to reject it. It runs when * the field's validators do and blocks submit like them, ahead of the field's `onDynamic` * validator, and it doesn't run while the field is inactive. */ check?: (value: V) => string | undefined; } /** Exactly the `@mitcsutt/kiln-ui` FieldLabelProps additions (§8.1) + wiring. Undefined keys are omitted. */ export interface BoundFieldProps { id: string; name: string; 'data-field': string; error?: string | true; errorLive: boolean; errorHidden?: boolean; warning?: string; required?: boolean; disabled?: boolean; readOnly?: boolean; validating?: boolean; layout?: FieldLayout; labelHidden?: boolean; 'aria-describedby'?: string; } export interface FieldBinding<V> { /** Raw TanStack FieldApi — the escape hatch (value typed via `value`/`setValue`). */ api: AnyFieldApi; /** TanStack path, also the native `name` (bracket syntax, FormData-compatible). */ name: string; /** `useId()`-based control id (never derived from name). */ id: string; value: V; /** `field.handleChange`; a no-op while readOnly/disabled. */ setValue: (next: V) => void; /** `field.handleBlur`. */ onBlur: () => void; /** Registers the focus target (the control element). */ ref: RefCallback<HTMLElement>; mode: 'edit' | 'view'; state: { showError: boolean; /** First visible error, formatted. */ error?: string; warning?: string; isValidating: boolean; /** `!isDefaultValue`. */ isDirty: boolean; /** disabled | readOnly | excluded. */ inactive: boolean; }; /** Spread onto the @mitcsutt/kiln-ui *Field component. */ fieldProps: BoundFieldProps; } /** * THE binding hook: ids, error visibility + normalisation, warnings, disabled/readOnly/excluded * semantics, focus registration and view mode, for the field in context. * * @privateRemarks Design reference §4. */ export declare function useFieldBinding<V>(options: FieldBindingOptions<V>): FieldBinding<V>;