import { ObjectConfig } from "./config"; import { ObjectState } from "./fields/objectField"; export type ObjectStateCache = Record, I]>; /** * The opts has for `useFormStates`. * * @typeparam T the form type, which is usually as close as possible to your *GraphQL input* * @typeparam I the *form input* type, which is usually the *GraphQL output* type, i.e. the type of the response from your GraphQL query */ type UseFormStatesOpts = { /** * The config to use for each form state. * * Should be stable/useMemo'd. */ config: ObjectConfig; /** * Fired for each individual `ObjectState` when it's had a value change. * * Does not need to be stable/useMemo'd. */ autoSave?: (state: ObjectState) => Promise; /** * A hook to add custom, cross-field validation rules that can be difficult to setup directly in the config DSL. * * This will be called once-per `ObjectState` instance, and so is effectively a `useEffect` hook with * a `[config, objectState]` dependency. * * Does not need to be stable/useMemo'd. */ addRules?: (state: ObjectState) => void; /** * Given an input to `getFormState`, returns the identity value that we'll cache that value's form state on. * * Does not need to be stable/useMemo'd. */ getId: (v: I) => string; /** * Maps an input to `getFormState` to the actual form shape `T`. * * Does not need to be stable/useMemo'd. */ map?: (input: Exclude) => T; /** * Sets all `ObjectState`s to readOnly. */ readOnly?: boolean; }; type UseFormStatesHook = { getFormState: (input: I, opts?: { readOnly?: boolean; }) => ObjectState; }; /** * A hook to manage many "mini-forms" on a single page, typically one form per row * in a table. * * This hook basically provides the page/table with a cache, so each table row naively ask "what's * the form state for this given row's data?" and get back a new-or-existing `ObjectState` instance * that, if already existing, still has any of the user's WIP changes. * * Each mini-form/row can have its own autoSave calls, independent of the other rows. * * @typeparam T the form type, which is usually as close as possible to your *GraphQL input* * @typeparam I the *form input* type, which is usually the *GraphQL output* type, i.e. the type of the response from your GraphQL query */ export declare function useFormStates(opts: UseFormStatesOpts): UseFormStatesHook; export {};