declare module "react-cool-form" { import { FocusEventHandler, RefCallback, SyntheticEvent } from "react"; // Type utils type ObjMap = Record; type DeepProps = { [K in keyof V]?: V[K] extends T ? T : DeepProps; }; // useForm export type FormValues = ObjMap; export interface EventOptions { removeField: RemoveField; getState: GetState; setValue: SetValue; setTouched: SetTouched; setDirty: SetDirty; setError: SetError; clearErrors: ClearErrors; runValidation: RunValidation; focus: Focus; reset: Reset; submit: Submit; } export type FormErrors = DeepProps; export type FormState = Readonly<{ values: V; touched: DeepProps; errors: FormErrors; isDirty: boolean; dirty: DeepProps; isValidating: boolean; isValid: boolean; isSubmitting: boolean; isSubmitted: boolean; submitCount: number; }>; export interface PreviousValuesFn { (previousValues: V): V; } export interface PreviousValueFn { (previousValue: any): any; } export interface PreviousErrorFn { (previousError?: any): any; } export interface FormValidator { (values: V): | FormErrors | false | void | Promise | false | void>; } export interface FieldValidator { (value: any, values: V): any | Promise; } export interface FieldParser { (value: V): R; } export interface FieldNamesFn { (names: N): N; } export interface ResetHandler { (values: V, options: EventOptions, event?: Event | SyntheticEvent): void; } export interface SubmitHandler { ( values: V, options: EventOptions, event?: Event | SyntheticEvent ): void | Promise; } export interface ErrorHandler { ( errors: FormErrors, options: EventOptions, event?: Event | SyntheticEvent ): void; } export interface OnStateChange { (formState: FormState): void; } export type RegisterForm = RefCallback; export interface RegisterFieldReturn { ( field: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null ): void; } export interface FieldOptions { validate?: FieldValidator; valueAsNumber?: boolean; valueAsDate?: boolean; parse?: FieldParser; } export interface RegisterField { (validate: FieldValidator): RegisterFieldReturn; (options: FieldOptions): RegisterFieldReturn; } export interface Focus { (name: string, delay?: number): void; } export interface RemoveField { ( name: string, exclude?: ("defaultValue" | "value" | "touched" | "dirty" | "error")[] ): void; } export interface UseOptions { defaultValues?: V; errorWithTouched?: boolean; } export interface Use { (path: string | string[] | ObjMap, options?: UseOptions): any; } export interface GetState { (path?: string | string[] | ObjMap): any; } export type SetValueOptions = { [k in "shouldValidate" | "shouldTouched" | "shouldDirty"]?: boolean; }; export interface SetValue { ( name: string, value: any | PreviousValueFn, options?: SetValueOptions ): void; } export type SetTouchedOptions = { shouldValidate?: boolean; }; export interface SetTouched { (name: string, isTouched?: boolean, options?: SetTouchedOptions): void; } export interface SetDirty { (name: string, isDirty?: boolean): void; } export interface SetError { (name: string, error: any | PreviousErrorFn): void; } export interface ClearErrors { (name?: string | string[]): void; } export type RunValidationOptions = { shouldFocus?: boolean; }; export interface RunValidation { ( name?: string | string[], options?: RunValidationOptions ): Promise; } export interface Reset { ( values?: V | PreviousValuesFn | null, exclude?: (keyof FormState)[] | null, event?: SyntheticEvent ): void; } export interface Submit { (event?: SyntheticEvent): Promise<{ values?: V; errors?: FormErrors; }>; } export type FormConfig = Partial<{ id: string; defaultValues: V; validate: FormValidator; validateOnChange: boolean; validateOnBlur: boolean; focusOnError: boolean | string[] | FieldNamesFn; removeOnUnmounted: boolean | string[] | FieldNamesFn; builtInValidationMode: "message" | "state" | false; excludeFields: string[]; onReset: ResetHandler; onSubmit: SubmitHandler; onError: ErrorHandler; onStateChange: OnStateChange; }>; export interface FormMethods { form: RegisterForm; field: RegisterField; focus: Focus; removeField: RemoveField; use: Use; getState: GetState; setValue: SetValue; setTouched: SetTouched; setDirty: SetDirty; setError: SetError; clearErrors: ClearErrors; runValidation: RunValidation; reset: Reset; submit: Submit; } export function useForm( config?: FormConfig ): FormMethods; // useFormMethods export function useFormMethods( formId?: string ): FormMethods; // useFormState export type Path = string | string[] | ObjMap; export type FormStateConfig = Partial<{ formId: string; defaultValues: V; errorWithTouched: boolean; }>; export interface FormStateCallback { (props: any): void; } export function useFormState( path: Path, config?: FormStateConfig, formId?: string ): any; export function useFormState( path: Path, callback?: FormStateCallback, formId?: string ): any; // useControlled export interface ControlledParser { (...event: E): R; } export interface ControlledFormatter { (value: V): R; } export type ControlledConfig = Partial<{ formId: string; defaultValue: any; validate: FieldValidator; parse: ControlledParser; format: ControlledFormatter; errorWithTouched: boolean; [k: string]: any; }>; export type ControlledReturn = [ { name: string; value: any; onChange: (...event: any[]) => void; onBlur: FocusEventHandler; [k: string]: any; }, { error: any; isTouched: boolean; isDirty: boolean } ]; export function useControlled( name: string, config?: ControlledConfig ): ControlledReturn; // useFieldArray export type HelperOptions = Partial<{ shouldTouched: boolean; shouldDirty: boolean; }>; export interface Push { (value: T, options?: HelperOptions): void; } export interface Insert { (index: number, value: T, options?: HelperOptions): void; } export interface Remove { (index: number): T | void; } export interface Swap { (indexA: number, indexB: number): void; } export interface Move { (from: number, to: number): void; } export type FieldArrayConfig< T = any, V extends FormValues = FormValues > = Partial<{ formId: string; defaultValue: T[]; validate: FieldValidator; }>; export type FieldArrayReturn = [ string[], { push: Push; insert: Insert; remove: Remove; swap: Swap; move: Move; } ]; export function useFieldArray( name: string, config?: FieldArrayConfig ): FieldArrayReturn; // Utility functions export function get(object: any, path: string, defaultValue?: unknown): any; export function set( object: any, path: string, value: unknown, immutable?: boolean ): any; export function unset(object: any, path: string, immutable?: boolean): any; }