@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
52 lines (51 loc) • 2.77 kB
TypeScript
import type { SwatchColor } from '@mitcsutt/kiln-ui';
import type { FieldOption, Primitive } from '../kit/contracts.js';
/** Item value used for an `emptyOption` row; maps back to `null`. Never a real option value. */
export declare const EMPTY_OPTION_VALUE = "__repo-forms-empty__";
/** An option as `@mitcsutt/kiln-ui` controls take it: string values only. */
export interface UiOption {
value: string;
label: string;
description?: string;
group?: string;
keywords?: readonly string[];
disabled?: boolean;
}
export interface OptionMapping<V extends Primitive> {
/** Options with string values (+ the `emptyOption` row first, when given). */
uiOptions: UiOption[];
/**
* Field value → ui value. `null`/`undefined`/unknown → `''` (Radix shows the placeholder). A
* `retain`ed value maps through even when it isn't among the current options.
*/
toUi: (value: V | null | undefined) => string;
/** Ui value → field value with its original primitive type; `''`, the empty row or unknown → `null`. */
fromUi: (value: string) => V | null;
/** The label of a field value, for view mode. */
labelOf: (value: V | null | undefined) => string | undefined;
/** The option's `swatch`, by field value or ui value (they share a key). */
swatchOf: (value: V | string | null | undefined) => SwatchColor | undefined;
}
/**
* Maps typed option values (`1`, `true`, `'gbp'`) to the strings Radix-based ui controls use, and
* back, losslessly (§7.3). Keys are `String(value)`; one shared helper for every option field.
*/
export interface OptionMappingOptions<V extends Primitive> {
/** A leading "none" row whose ui value maps back to `null`. */
emptyOption?: string;
/**
* Values that must survive a round trip even when the current `options` don't contain them —
* the field's selected values while an async search shows other results. They
* map to `String(value)` and back to the original typed value; they add no ui option rows
* (the ui control keeps their labels itself).
*/
retain?: readonly (V | null | undefined)[];
/** Options seen earlier (older loader results): consulted for values not in `options`. */
known?: ReadonlyMap<string, FieldOption<V>>;
}
export declare function createOptionMapping<V extends Primitive>(options: readonly FieldOption<V>[] | undefined, opts?: OptionMappingOptions<V>): OptionMapping<V>;
/**
* Hook form of `createOptionMapping`, memoised on the options array and `emptyOption` (and
* `retain`/`known` by identity — pass stable references).
*/
export declare function useOptionMapping<V extends Primitive>(options: readonly FieldOption<V>[] | undefined, opts?: OptionMappingOptions<V>): OptionMapping<V>;