@qlee/react-form v0.1.8
react-form
Create a form
Field Props
-defaultValues?: Record<string, any>
The form default value
Form Api
getFiled(fieldName: string): FieldGet the field by field nameSubmit(): Promise<Record<string, any>>Return a promise resolve form data when validating successfully reject a map of ValidationError when validating failed
Create a field
Field Props
name: stringThe field namedefaultValue?: TThe field default valuevalidators?: Validator<T>[]The field validator array
Field Api
validate(): Promise<void>Return a promise. resolve void when validating successfully reject a ValidationError when validating failedgetValue(): T|undefinedReturn field valuegetValidValue(): Promise<T|undefined>Return a promise. resolve the field value when validating successfully reject a ValidationError when validating failedcleanError(): voidClean the field errorgetError(): ValidationError<T> | undefinedReturn the field errorsetValue(value: T): voidSet the field valuesubscribe(changeSubscriber: ChangeSubscriber<T>, errorSubscriber: ErrorSubscriber<T>): void- changeSubscriber(change: {name: string, prev?: T,curr: T}) => voidchangeSubscribe is a function triggered when the value changed - errorSubscriber is a function triggered when the field error changed (the error is missed when clear error)(error?: {fieldName: string, value?: T, message: string}) => voidunsubscribe(...subscriber: Array<ChangeSubscriber<T> | ErrorSubscriber<T>>): voidNot trigger the subscribers when there is a change
Validation
Validator Constructor
interface MessageGenerator {
(fieldName: string, fieldValue: any): string
}
interface Validator<T> {
validate: (value: T, form: Form) => boolean | Promise<boolean>
message: MessageGenerator | string,
}