import React, { ComponentProps, ElementType } from 'react'; import { DebouncedFunction } from './useDebouncePromise'; import { Errors, FieldPath, FormMode, FormState, ModifiedState, Values } from './useFormState'; import { UseFormKeysHook } from './useFormKeys'; import { UseFormWatchHook } from './useFormWatch'; import { UseFormErrorsHook } from './useFormErrors'; import { UseFormStatusHook } from './useFormStatus'; import { SetValuesOptions, UseFormValuesHook, UseFormValuesOptions } from './useFormValues'; import { UseFormLoaderHook, UseFormLoaderOptions } from './useFormLoader'; import { UseFormListHook } from './useFormList'; import { UseFormValidationHook } from './useFormValidation'; import { AfterSubmitOption } from './useFormSubmission'; export type FieldElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement; export type GetButtonProps = { disabled?: boolean; onClick?: (...args: any) => void; type?: HTMLButtonElement['type']; [key: string]: any; }; export type GetButtonPropsReturnType = { disabled?: boolean; onClick?: (...args: any) => void; type?: HTMLButtonElement['type']; }; export type InitializeFieldFunction = (path: FieldPath, formState: FormState) => ComponentProps | undefined; /** * A function that converts a value to a string. */ export type FormatFunction = (value: unknown) => string; /** * A function that converts a string to a value. */ export type ParseFunction = (value: string, target?: HTMLElement) => T; export type UseFormHook = FormState & { /** * Performs an update of the form state (uncontrolled mode). */ forceUpdate(): void; /** * Returns form button props. * @param props */ getButtonProps(props?: GetButtonProps): GetButtonPropsReturnType; /** * Returns field props by name. * @param path * @param props * @param opts */ getFieldProps(path: FieldPath, props?: ComponentProps, opts?: { format?: FormatFunction | null | false; formatItems?: boolean; parse?: ParseFunction; replaceNull?: boolean; setValueOptions?: SetValuesOptions; }): any; /** * Returns form props. * @param props */ getFormProps(props?: ComponentProps<'form'>): ComponentProps<'form'>; /** * Handles field blur event. * @param path */ handleFieldBlur(path: FieldPath): (event: React.FocusEvent | unknown) => void; /** * Handles field change automatically by detecting if a value or an event is returned. * @param path * @param options */ handleFieldChange(path: FieldPath, options?: { parse?: ParseFunction; setValueOptions?: SetValuesOptions; }): (valueOrEvent: React.ChangeEvent | unknown) => void; /** * Handles form reset event. * @param event */ handleReset(event: React.FormEvent): void; /** * Handles field change event (value based like useState()). * @param path * @param options */ handleSetValue(path: FieldPath, options?: { parse?: ParseFunction; setValueOptions?: SetValuesOptions; }): (value: unknown | undefined) => void; /** * Handles form submit event. * @param event */ handleSubmit: React.SubmitEventHandler; /** * The generated form id. */ id: string; /** * Returns the key of a field. */ key: UseFormKeysHook['getKey']; /** * Loads the form initial values. */ load: UseFormLoaderHook['load']; /** * The form mode (controlled/uncontrolled). */ mode: FormMode; /** * Calls the onSubmit function with form values. */ submit: DebouncedFunction; /** * Enables validation on field change. */ validateOnChange: boolean; /** * Enables validation on form initialization. */ validateOnInit: boolean; /** * Enables validation on form submitting. */ validateOnSubmit: boolean; /** * Enables validation on field touch. */ validateOnTouch: boolean; } & Pick, 'clearErrors' | 'getError' | 'getErrors' | 'getInitialError' | 'getInitialErrors' | 'resetErrors' | 'setError' | 'setErrors'> & Pick, 'appendListItem' | 'insertListItem' | 'moveListItem' | 'prependListItem' | 'removeListItem' | 'replaceListItem' | 'swapListItem'> & Pick, 'clearModifiedFields' | 'clearTouchedFields' | 'getModifiedFields' | 'getTouchedFields' | 'isModified' | 'isTouched' | 'resetModifiedFields' | 'resetTouchedFields' | 'setModifiedFields' | 'setModifiedField' | 'setTouchedFields' | 'setTouchedField'> & Pick, 'clearValues' | 'getInitialValue' | 'getInitialValues' | 'getValue' | 'getValues' | 'initialize' | 'removeValues' | 'resetValues' | 'setInitialValues' | 'setValue' | 'setValues'> & Pick, 'setValidateError' | 'setValidated' | 'setValidating' | 'validate' | 'validateField' | 'validateFields'> & Pick, 'watch' | 'watchers'>; export type UseFormOptions = { /** * Tells what to do with values after submit. */ afterSubmit?: AfterSubmitOption; /** * Enables debugging. */ debug?: boolean; /** * Should the form be disabled during submit. */ disableOnSubmit?: boolean; /** * Should the form be disabled during validation. */ disableOnValidate?: boolean; /** * Disable submit button if form is not modified. */ disableSubmitIfNotModified?: boolean; /** * Disable submit button if form is not valid. */ disableSubmitIfNotValid?: boolean; /** * Disables the form (fields and buttons). */ disabled?: boolean; /** * Enable HTML validation when submit event is triggered. */ enableHTMLValidation?: boolean; /** * Update the form when it is modified or touched (happens only at the form level). */ forceUpdateOnStatusChange?: boolean; /** * Sets the initial errors. */ initialErrors?: Errors; /** * Sets the initial modified fields. */ initialModified?: FormState['initialModified']; /** * Sets the initial touched fields. */ initialTouched?: FormState['initialTouched']; /** * Sets the initial values. */ initialValues?: V; /** * Sets field props dynamically. * @param path * @param formState */ initializeField?: InitializeFieldFunction; /** * The loading function. */ load?: UseFormLoaderOptions['onLoad']; /** * The form mode (controlled/uncontrolled). */ mode?: FormMode; /** * Replaces empty string by null on change and on submit. */ nullify?: boolean; /** * Called when the form submission has failed. * @param error */ onError?(error: Error): void; /** * Called when form is submitted. * @param values */ onSubmit?: (values: V) => Promise; /** * Called when form has been successfully submitted. * @param result * @param values */ onSuccess?(result: R, values: V): void; /** * Called when form values have changed. * @param values * @param previousValues */ onValuesChange?(values: V, previousValues: V): void; /** * Prevents native action when form is submitted. */ preventDefaultOnSubmit?: boolean; /** * Resets form with initial values whenever they change. */ reinitialize?: boolean; /** * The delay before submitting the form. */ submitDelay?: number; /** * Allows transforming values when form is modified. * @param mutation * @param values */ transform?: UseFormValuesOptions['transform']; /** * Enables trimming on blur. */ trimOnBlur?: boolean; /** * Enables trimming on submit. */ trimOnSubmit?: boolean; /** * Called for form validation. * @param values * @param modifiedFields */ validate?(values: V, modifiedFields: ModifiedState): Promise | undefined>; /** * The delay before starting validation. */ validateDelay?: number; /** * Called for single field validation. * @param path * @param value * @param values */ validateField?(path: FieldPath, value: unknown, values: V): Promise; /** * Enables validation on field change. */ validateOnChange?: boolean; /** * Enables validation on form initialization. */ validateOnInit?: boolean; /** * Enables validation on form submit. */ validateOnSubmit?: boolean; /** * Enables validation on field touch. */ validateOnTouch?: boolean; }; /** * Manage form state and actions. */ declare function useForm(options: UseFormOptions): UseFormHook; export default useForm; //# sourceMappingURL=useForm.d.ts.map