@qlee/react-form
v0.1.8
Published
react form
Downloads
3
Readme
react-form
Create a form
Field Props
-defaultValues?: Record<string, any>
The form default value
Form Api
getFiled(fieldName: string): Field
Get 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: string
The field namedefaultValue?: T
The 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|undefined
Return field valuegetValidValue(): Promise<T|undefined>
Return a promise. resolve the field value when validating successfully reject a ValidationError when validating failedcleanError(): void
Clean the field errorgetError(): ValidationError<T> | undefined
Return the field errorsetValue(value: T): void
Set the field valuesubscribe(changeSubscriber: ChangeSubscriber<T>, errorSubscriber: ErrorSubscriber<T>): void
- changeSubscriber
(change: {name: string, prev?: T,curr: T}) => void
changeSubscribe 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}) => void
- changeSubscriber
unsubscribe(...subscriber: Array<ChangeSubscriber<T> | ErrorSubscriber<T>>): void
Not 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,
}