UNPKG

@mitcsutt/kiln-forms

Version:

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

42 lines (41 loc) • 2.1 kB
import type { FieldOption, Primitive } from '../kit/contracts.js'; /** Loads options for a query (§7.4). Honour `signal` — superseded requests are aborted. */ export type OptionsLoader<V extends Primitive = Primitive> = (ctx: { query: string; values: unknown; signal: AbortSignal; }) => Promise<readonly FieldOption<V>[]>; export interface UseOptionsOptions { /** The current search text. Default `''`. */ query?: string; /** Reload (and cache separately) when these change — e.g. the values of `reloadOn` fields. */ deps?: readonly unknown[]; /** Passed to the loader as `values` (form values for dependent options). */ values?: unknown; /** Default 250. The first load is not debounced. */ debounceMs?: number; /** Below this many characters nothing loads (`status: 'idle'`). Default 0. */ minQueryLength?: number; } export type OptionsStatus = 'idle' | 'loading' | 'error' | 'ready'; export interface UseOptionsResult<V extends Primitive = Primitive> { options: readonly FieldOption<V>[]; status: OptionsStatus; error?: unknown; } /** * Options from a loader, debounced, cached and cancelled when superseded. The hook behind async * comboboxes. * * @remarks * `useOptions(source, options)` turns a loader into options for a list: it debounces the query, * aborts the previous request when a new one starts, and caches recent results (50 entries, keyed * by the query and its dependencies). Static options pass straight through. The bound combobox and * multi-select fields use it for `loadOptions`; use it directly for your own controls. * * @privateRemarks * Static options pass through; a loader is debounced, aborts the previous request and caches * results (LRU, 50 entries, key = query + JSON of deps). The loader is read through a ref, so an * inline loader (new identity every render) neither refetches nor loops; change `deps` to reload. */ export declare function useOptions<V extends Primitive = Primitive>(source: OptionsLoader<V> | readonly FieldOption<V>[], opts?: UseOptionsOptions): UseOptionsResult<V>;