useFluentForm
<Config extends FormConfig>(config: Config) => UseFluentForm
Description
Core react hook of this library.
Expects a FormConfig
as parameter:
Return type
values
ValuesType
Contains current values of form. Initial values are comming from FormConfig
touched
StateTouched
Contains information about the touched state of each field.
Initial value is {}
.
Usually fields are touched once onBlur
event was triggered for the field.
This can help to trigger validations depended on the specified validation trigger.
Possible values for each field:
undefined
: field was not touched yettrue
: field was touched
validity
StateValidity
Contains information about the validation state of each field. Initial value is {}
.
Possible values for each field:
undefined
: validation was not triggert yettrue
: field is validfalse
: field is invalid
errors
ErrorsType<ValuesType, ErrorType>
Contains errors for each field resulted from it's validation. Initial value is {}
.
Possible values for each field:
undefined
: field has no error or was not validated yet- For
yup.Schema
's theErrorType
will always bestring[]
- For
validate function
's theErrorType
could possibly be anything
context
object
Current context value. Initial value is {}
.
submitting
boolean
true
if form is currently submitting else false
.
fields
Fields<ValuesType>
Contains props for each component which resulted from evaluation of mapToComponentProps
member function of each field (see Field
).
In case above type of fields
would be:
setValues
(values: Partial<ValuesType>) => void
Sets values of form.
setInitialValues
(values: Partial<ValuesType>) => void
Sets initial values of form. This is important when resetting a form.
setContext
(context: object) => void
Updates value of validation context. To re-validate all fields on context change FormConfig.validateOnContextChange
can be used.
Works well in combination with useEffect
.
handleSubmit
(success?: Function, failure?: Function, options?: HandleSubmitOptions) => (event: any) => void
Returns a submit handler. When this handler is called validation for all fields will be trigger no matter which validation trigger was configured.
Following parameter can be passed:
success
: a callback when the validation was successfulfailure
: a callback when the validation has failedoptions
preventDefault
: will callpreventDefault
function in case submit handler passes an event. Default value istrue
stopPropagation
: will callstopPropagation
function in case submit handler passes an event. Default value istrue
validateField
<K extends keyof ValuesType>(field: K) => ErrorsType<ValuesType, ErrorType>[K]
Triggers validation for given field and returns error in case the field is invalid, otherwise undefined
.
It also updates the errors state based on the returned error.
validateAllFields
() => ErrorsType<ValuesType, ErrorType>
Triggers validation for all fields and returns an errors object.
It also updates the errors state based on the returned errors.
reset
() => void
Sets complete form state to inital state. Initial values can be modified using setInitialValues
.