UNPKG

react-formal

Version:

Classy HTML form management for React

56 lines (55 loc) 2.23 kB
import React from 'react'; import { formGetter, formSetter } from './BindingContext'; import { AnyObjectSchema, InferType } from 'yup'; import { BeforeSubmitData, Errors, Touched, ValidateData } from './types'; export interface FormProps<TSchema extends AnyObjectSchema, TValue = Record<string, any>> { as?: React.ElementType | null | false; className?: string; children?: React.ReactNode; schema?: TSchema; value?: TValue; defaultValue?: Partial<TValue>; errors?: Errors; defaultErrors?: Errors; touched?: Touched; defaultTouched?: Touched; noValidate?: boolean; onChange?: (input: TValue, changedPaths: string[]) => void; onError?: (errors: Errors) => void; onTouch?: (touched: Touched, changedPaths: string[]) => void; onValidate?: (data: ValidateData) => void; onBeforeSubmit?: (data: BeforeSubmitData) => void; onSubmit?: (validatedValue: InferType<TSchema>) => void; onInvalidSubmit?: (errors: Errors) => void; onSubmitFinished?: (error?: Error) => void; onReset?: () => void; submitForm?: (input: TValue) => Promise<any> | any; getter?: (path: string, value: TValue) => any; setter?: (path: string, value: TValue, fieldValue: any) => TValue; context?: Record<string, unknown>; delay?: number; stripUnknown?: boolean; /** * Controls how errors are dealt with for field level validation. When * set, the first validation error a field throws is returned instead of waiting * for all validations to finish */ abortEarly?: boolean; strict?: boolean; /** Adds some additional runtime console warnings */ debug?: boolean; } export interface FormHandle { /** manually submit a Form, returns a boolean indicating if the submission was successful */ submit: () => Promise<boolean>; validate(fields: string[]): void; } export declare interface Form { <T extends AnyObjectSchema, TValue = Record<string, any>>(props: FormProps<T, TValue> & React.RefAttributes<FormHandle>): React.ReactElement | null; displayName?: string; propTypes?: any; } /** @alias Form */ declare const _Form: Form; export default _Form; export { formGetter as getter, formSetter as setter };