import { status } from '@synnaxlabs/client'; import { compare, record, state } from '@synnaxlabs/x'; import { z } from 'zod'; import { ContextValue } from './Context'; import { DefaultGetOptions, ExtensionGetOptions, FieldState, GetOptions, OptionalGetOptions, RequiredGetOptions } from './state'; export type ContextOptions = { ctx?: ContextValue; }; export type UseFieldOptions = ContextOptions & { onChange?: (value: O, extra: ContextValue & { path: string; }) => void; }; /** Return value for {@link useField}. Spread it onto an input. */ export interface UseFieldReturn extends FieldState { onChange: (value: O) => void; setStatus: (status: status.Crude) => void; status: status.Crude; preview?: boolean; } interface UseField { (path: string, opts?: (RequiredGetOptions | DefaultGetOptions) & UseFieldOptions): UseFieldReturn; (path: string, opts?: OptionalGetOptions & UseFieldOptions): UseFieldReturn | null; (path: string, opts?: ExtensionGetOptions & UseFieldOptions): UseFieldReturn; } /** * Binds one field of the enclosing form, re-rendering the caller only when that field * changes. The return value satisfies {@link Input.Control}, so it spreads onto any * input. * * @example const { value, onChange } = Form.useField("name"); * @throws {Error} if the path holds no value and `optional` is not set. */ export declare const useField: UseField; export interface UseFieldValue { (path: string, opts?: (RequiredGetOptions | DefaultGetOptions) & ContextOptions): O; (path: string, opts?: OptionalGetOptions & ContextOptions): O | null; (path: string, opts?: ExtensionGetOptions & ContextOptions): O; } export interface UseFieldState { (path: string, opts?: (RequiredGetOptions | DefaultGetOptions) & ContextOptions): FieldState; (path: string, opts?: OptionalGetOptions & ContextOptions): FieldState | null; (path: string, opts?: ExtensionGetOptions & ContextOptions): FieldState; } /** Reads a field's value together with its status and required flag. */ export declare const useFieldState: UseFieldState; /** Reads just a field's value. Use it to render from a field the caller does not edit. */ export declare const useFieldValue: UseFieldValue; /** @returns whether the field at the path passed its last validation. */ export declare const useFieldValid: (path: string) => boolean; /** Edits for a form field holding an array of keyed entries. */ export interface FieldListUtils> { /** Appends entries, optionally re-sorting after. */ push: (value: E | E[], sort?: compare.Comparator) => void; /** Inserts entries at an index. */ add: (value: E | E[], start: number) => void; /** Drops the named entries. @returns the keys that remain. */ remove: (keys: K | K[]) => K[]; /** Drops everything but the named entries. @returns the keys that remain. */ keepOnly: (keys: K | K[]) => K[]; set: (values: state.SetArg) => void; value(): E[]; sort?: (compareFn: compare.Comparator) => void; } /** Builds {@link FieldListUtils} over a form context, outside of a React render. */ export declare const fieldListUtils: >(ctx: ContextValue, path: string) => FieldListUtils; /** @returns edits for an array field, without subscribing to its contents. */ export declare const useFieldListUtils: >(path: string, opts?: ContextOptions) => FieldListUtils; export interface UseFieldListReturn> extends FieldListUtils { data: K[]; } /** * Binds an array field, returning its keys alongside the edits. Feed `data` straight * into a {@link List.Frame}. */ export declare const useFieldList: , Z extends z.ZodType = z.ZodType>(path: string, opts?: ContextOptions & GetOptions) => UseFieldListReturn; export {}; //# sourceMappingURL=useField.d.ts.map