UNPKG

sailboat-design

Version:
53 lines (52 loc) 1.73 kB
/// <reference types="react" /> /// <reference types="lodash" /> import { RuleItem, ValidateError } from 'async-validator'; export declare type CustomRuleFunc = ({ getFieldValue }: { getFieldValue: (key: string) => any; }) => RuleItem; export declare type CustomRule = RuleItem | CustomRuleFunc; export interface FieldDetail { name: string; value: string; rules: CustomRule[]; isValid: boolean; errors: ValidateError[]; } export interface FieldsState { [key: string]: FieldDetail; } export interface FormState { isValid: boolean; isSubmitting: boolean; errors: Record<string, ValidateError[]>; } export interface ValidateErrorType extends Error { errors: ValidateError[]; fields: Record<string, ValidateError[]>; } export declare const ADD_FIELD = "addField"; export declare const UPDATE_VALUE = "updateValue"; export declare const UPDATE_VALIDATE_RESULT = "updateValidateResult"; export interface FieldAction { type: 'addField' | 'updateValue' | 'updateValidateResult'; name: string; value: any; } declare function useStore(initialValues?: Record<string, any>): { form: FormState; fields: FieldsState; dispatch: import("react").Dispatch<FieldAction>; validateField: (name: string) => Promise<void>; validateAllField: () => Promise<{ isValid: boolean; errors: Record<string, ValidateError[]>; value: { [x: string]: string; }; }>; getFieldValue: (key: string) => string; getFieldsValue: () => import("lodash").Dictionary<string>; setFieldValue: (name: string, value: any) => void; resetFields: () => void; }; export default useStore;