import { Dispatch, RefObject, SetStateAction } from 'react'; /** * Tells how the form should read and write values. */ export type FormMode = 'controlled' | 'uncontrolled'; /** * Contains the form values. */ export type Values = Record; /** * Allows any string while preserving autocomplete for known keys. */ export type LooseString = T | (string & {}); /** * Represents a primitive value. */ export type Primitive = string | number | boolean | bigint | symbol | null | undefined | Date | File | Blob; /** * Tells if a type is any. */ export type IsAny = 0 extends (1 & T) ? true : false; /** * Represents all known nested paths of an array. */ export type KnownArrayPath = T extends Primitive ? `${number}` : `${number}` | `${number}.${KnownFieldPath}`; /** * Represents all known nested paths of an object. */ export type KnownObjectPath = { [K in keyof T & string]: T[K] extends Primitive ? K : T[K] extends readonly (infer I)[] ? K | `${K}.${KnownArrayPath}` : T[K] extends object ? K | `${K}.${KnownFieldPath}` : K; }[keyof T & string]; /** * Represents all known nested paths of a value. */ export type KnownFieldPath = IsAny extends true ? string : T extends Primitive ? never : T extends readonly (infer I)[] ? KnownArrayPath : T extends object ? KnownObjectPath : never; /** * Represents a field path. */ export type FieldPath = LooseString>; /** * Contains field paths and values. */ export type PathsAndValues = Partial & Partial, unknown | null | undefined>>; /** * Contains form values as object or a flat object with paths and values. */ export type PathsOrValues = PathsAndValues | Partial; /** * Contains the form errors. */ export type Errors = Record; /** * Contains info about modified fields. */ export type ModifiedState = Record; /** * Contains info about touched fields. */ export type TouchedState = Record; export type FormState = { /** * Disables all fields and buttons. */ disabled: boolean; /** * The fields errors. */ errors: Errors; /** * Tells if there are any error. */ hasError: boolean; /** * Tells if the form was initialized. */ initialized: boolean; /** * Sets initial errors. */ initialErrors: Errors; /** * Sets initial modified fields. */ initialModified: ModifiedState; /** * Sets initial touched fields. */ initialTouched: TouchedState; /** * Contains initial form values. */ initialValues: V | undefined; /** * The loading error. */ loadError: Error | undefined; /** * Tells if the form is loading. */ loading: boolean; /** * Tells if the form was modified. */ modified: boolean; /** Contains modified fields state (controlled mode). */ modifiedFields: ModifiedState; /** * Tells if the form will trigger a validation or if it will validate some fields. */ needValidation: boolean | FieldPath[]; /** * The number of times the form was submitted (count is reset on success). */ submitCount: number; /** * The submit error. */ submitError: Error | undefined; /** * The submit result returned by onSubmit promise. */ submitResult: R | undefined; /** * Tells if the form was submitted successfully. */ submitted: boolean; /** * Tells if the form is submitting. */ submitting: boolean; /** * Tells if the form was touched. */ touched: boolean; /** * Contains the touched fields state (controlled mode). */ touchedFields: TouchedState; /** * The validation error. */ validateError: Error | undefined; /** * Tells if the form was validated. */ validated: boolean; /** * Tells if the form is validating. */ validating: boolean; /** * The form values. */ values: V; }; export type UseFormStateOptions = { debug?: boolean; /** * Sets the initial state of the form. */ initialState: Partial>; }; export type UseFormStateHook = { /** * Enable debug logging. */ debug?: boolean; /** * The errors ref (uncontrolled mode). */ errorsRef: RefObject>; /** * Tells if the form was initialized (uncontrolled mode). */ initializedRef: RefObject; /** * The initial values ref (uncontrolled mode). */ initialValuesRef: RefObject; /** * The modified fields ref (uncontrolled mode). */ modifiedRef: RefObject; /** * The current state of the form. */ state: FormState; /** * Sets the new state of the form. */ setState: Dispatch>>; /** * The touched fields ref (uncontrolled mode). */ touchedRef: RefObject; /** * The values ref (uncontrolled mode). */ valuesRef: RefObject; }; declare function useFormState(options: UseFormStateOptions): UseFormStateHook; export default useFormState; //# sourceMappingURL=useFormState.d.ts.map