UNPKG

fielder

Version:

A field-first form library for React and React Native

40 lines (39 loc) 1.75 kB
import { FieldState, FormSchemaType, ObjectValidation, ValidationFn, FormValue } from './types'; export declare type UseFieldProps<T = any> = { /** Field name. */ readonly name: string; /** Field value. */ readonly value: T; /** Change event handler. */ readonly onChange: (e: { currentTarget: { value: any; }; } | T) => void; /** Blur event handler (sets blurred state). */ readonly onBlur: () => void; }; export declare type UseFieldMeta = { /** Field error. */ readonly error?: FieldState['error']; /** Field is currently valid. */ readonly isValid: FieldState['isValid']; /** Field is currently being validated (async). */ readonly isValidating: FieldState['isValidating']; /** Field has been blurred since mount. */ readonly hasBlurred: FieldState['hasBlurred']; /** Field has been changed since mount. */ readonly hasChanged: FieldState['hasChanged']; }; export declare type UseFieldArgs<S extends FormSchemaType = FormSchemaType, K extends keyof S = keyof S, V extends FormValue = S[K]> = { /** Unique identifier for field. */ readonly name: K; /** Starting value. */ readonly initialValue: V; /** Validation function (throws errors). */ readonly validate?: ObjectValidation<V, S> | ValidationFn<V, S>; /** Should destroy value when useField hook is unmounted. */ readonly destroyOnUnmount?: boolean; }; export declare type UseFieldResponse = readonly [UseFieldProps, UseFieldMeta]; export declare const useField: <T extends FormSchemaType = FormSchemaType>({ name, validate, initialValue, destroyOnUnmount, }: UseFieldArgs<T, keyof T, T[keyof T]>) => UseFieldResponse;