UNPKG

@olapat/react-useform

Version:
47 lines (46 loc) 1.95 kB
import * as React from 'react'; export type Rules<ValuesType> = { [unit in keyof Partial<ValuesType>]: { 'required'?: string | boolean; 'isAllowed'?: { func: (value: any, values: ValuesType) => boolean; msg: React.ReactNode | string; }; }; }; export type ErrorForm<ValuesType> = { [unit in keyof ValuesType | string]: string | React.ReactNode | undefined | null; } | { [K: string]: string | React.ReactNode | undefined | null; }; export type ReactChangeEvent = React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>; export type Argument1OnChange = Function | ReactChangeEvent | string | {}; export type Validate<ValuesType> = boolean | [boolean, {}, ValuesType]; export type UseFormType<ValuesType> = { initialValues: ValuesType; values: ValuesType; submitting: boolean; setSubmitting: Function; rules: Rules<ValuesType>; errors: ErrorForm<ValuesType>; setValues: (prev: ValuesType) => any; validate: (next: Function | null, end: Function, getErrorArray?: boolean, onSubmitError?: (errors: {}) => void) => Validate<ValuesType>; setRules: (prev: Rules<ValuesType> | ((prev: Rules<ValuesType>) => Rules<ValuesType>)) => void; setErrors: (prev: ErrorForm<ValuesType>) => any; handlerReset: (values: {}) => void; handlerChange: (name: Argument1OnChange | Partial<ValuesType> | keyof ValuesType, value?: any, type?: 'string' | 'number') => void; blackList?: string | string[]; whiteList?: string | string[]; submitted: boolean; }; export interface Props<ValuesType> { initialValues: ValuesType; rules: Rules<ValuesType>; blackList?: string | string[]; whiteList?: string | string[]; onValuesUpdate?: (values: ValuesType) => void; } declare const useForm: <ValuesType extends { [key: string]: any; } = {}>(props: Props<ValuesType>) => UseFormType<ValuesType>; export default useForm;