@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
95 lines (94 loc) • 4.53 kB
TypeScript
import { type ReactNode } from 'react';
import { type DeepKeys, type DeepValue } from '@tanstack/react-form';
import { type TableColumnWidth } from '@mitcsutt/kiln-ui';
import type { AnyKitForm, ArrayPaths, BindValidators, BoundFields, FieldRegistry, ItemOf, RegistryOf, ValuesOf } from '../../../kit/types.js';
/** One item of a Repeater, passed to its render prop. */
export interface RepeaterItem<I, R = FieldRegistry> {
index: number;
count: number;
/** Stable React key (items are keyed by index). */
key: string;
/** TanStack path of the item, e.g. `guests[2]`. */
name: string;
/** Item-relative typed shorthand: `item.fields.TextField name="first"` binds `guests[2].first`. */
fields: BoundFields<I, R>;
remove(): void;
move(to: number): void;
canRemove: boolean;
canMoveUp: boolean;
canMoveDown: boolean;
}
type Values<A> = ValuesOf<A>;
export interface RepeaterProps<A extends AnyKitForm, N extends ArrayPaths<Values<A>>> {
form: A;
/** A path to an array of objects. */
name: N;
label: ReactNode;
description?: ReactNode;
/** The item Add appends (or a factory, for fresh ids). */
newItem: ItemOf<Values<A>, N> | (() => ItemOf<Values<A>, N>);
/** Default `'list'`. */
variant?: 'list' | 'table' | 'cards';
/** At `min`, Remove is hidden. Default 0. */
min?: number;
/** At `max`, Add is `aria-disabled` with `messages.maxItems(max)`. */
max?: number;
/**
* `true` adds Move up and Move down buttons to each item. `'drag'` adds a drag handle as well,
* which moves the item by pointer, touch or keyboard (Space or Enter to pick it up, the arrow
* keys, Space or Enter to drop). Drag needs the kit's `dragAndDrop` from
* `@mitcsutt/kiln-forms/dnd`; without it the buttons alone are shown. The buttons stay with a
* handle, so a move never needs a drag. Default `false`.
*/
reorderable?: boolean | 'drag';
/** Default `messages.item(index)` ("Item 1"). */
itemLabel?: (index: number) => string;
/** Default `messages.add`. */
addLabel?: string;
/** Shown when there are no items. */
empty?: ReactNode;
/** Table variant: one header per child field, in order. */
columns?: readonly {
header: ReactNode;
width?: TableColumnWidth;
}[];
/** Array-level validators (`minItems`, `unique`…); their errors render as the group's error. */
validators?: BindValidators<Values<A>, DeepValue<Values<A>, N & DeepKeys<Values<A>>>>;
children: (item: RepeaterItem<ItemOf<Values<A>, N>, RegistryOf<A>>) => ReactNode;
}
/**
* A list of repeated groups of fields, as a list, cards or a table, with add, remove and reorder.
*
* @remarks
* `Repeater` edits an array of objects: passengers, legs of a journey, line items. Each item gets
* typed shorthand fields relative to itself (`item.fields.TextField name="name"`), and the
* repeater handles adding, removing and moving items, focus after each change, and announcing it.
*
* `reorderable` adds Move up and Move down buttons. `reorderable="drag"` adds a drag handle too,
* from `@mitcsutt/kiln-forms/dnd`: give the kit its `dragAndDrop`
* (`kit.extend({ dragAndDrop })`) and import `@mitcsutt/kiln-dnd/styles.css`. Values,
* errors and focus move with the item, and each step of a drag is announced.
*
* @example In a schema
* ```json
* {
* "layout": "repeater",
* "name": "passengers",
* "label": "Passengers",
* "newItem": { "name": "", "age": null },
* "item": [{ "kind": "text", "name": "name", "label": "Name" }]
* }
* ```
*
* @privateRemarks
* A list of repeated items (§9.9): `list` (fieldsets), `cards` or `table`. The container is
* the form's array field (`mode="array"`, re-renders only on structural change); items are
* keyed by index. Add / Remove / Move move focus deterministically and are announced politely;
* `min` hides Remove, `max` makes Add `aria-disabled`; array-level errors render as the group's
* error. The render prop gets item-relative typed fields (`item.fields.TextField name="first"`).
* In view mode it creates no field and renders the items read-only. `reorderable="drag"` renders
* the kit's `dragAndDrop` parts (ADR 0043): a drop is one `moveFieldValues`, announced by
* kiln-dnd, not the repeater, and keyboard focus returns to the moved item's handle.
*/
export declare function Repeater<A extends AnyKitForm, const N extends ArrayPaths<Values<A>>>(props: RepeaterProps<A, N>): ReactNode;
export {};