useField
Hook to easily manage a single field.
#
Type#
UseFieldOptionsConfiguration to pass to useField()
#
formThe form instance this field should be bound to. If not specified, it will be used the form instance available in FormContext.
#
formatA function that takes the value from the form values and formats the value to give to the input (that will be exposed by UseFieldResult.input.value
). Common use cases include converting javascript Date values into a localized date string. Almost always used in conjunction with parse.
#
parseA function that takes the value from the input and converts the value into the value you want stored as this field's value in the form. Common usecases include converting strings into Numbers or parsing localized dates into actual javascript Date objects. Almost always used in conjuction with format.
#
parseOnBlurThe same as parse but it is called only when the field is blurred.
#
validateA function that receives the value of the field and the values of the form, and it should return a value of type string | string[] | undefined
.
string
in case of a single error messagestring[]
in case of multiple error messagesundefined
means no error
The function can be async.
In case it throws an error, the message of the error will be used as the error of the field.
#
validateDebounceValue that tells if validation debounce should be active and how:
false
: debounce will not be active (as per default)true
: debounce will be active, with default values forwait
(300) andleading
(false)- a
number
: debounce will be active withwait
as the number you passed, andleading
false - a
{ wait?: number; leading?: boolean }
object: debounce will be active with the values you passed
#
validateOnChangeWhether or not to validate the field every time the field's value changes. false
by default.
#
validateOnBlurWhether or not to validate the field when the field is blurred. false
by default.
#
validateOnChangeFieldsArray of field names that should trigger validation of this field whenever they change. If you pass a value that is not undefined
, validateOnChange is automatically considered true
unless you set it to false
.
#
UseFieldResultIt is returned by useField()
and represents the field instance that gives access to props and methods to easily manage the field.
#
inputIt's an object of type:
name
is the name of the field.value
is the current value of the field in the form values.onBlur
is a function that, when called, informs the form instance that this field has been touched.onChange
is a function that, when called, changes the value of the field extrapolating the new value from theReact.ChangeEvent
event it receives.
Usually, this input
object is spread into the input element props:
#
nameThe name of the field.
#
valueThe value of the field.
#
isTouchedtrue
if the field has been touched by the user.
#
errorThe first error of the field, if any.
#
errorsThe errors of the field, if any.
#
isValidtrue
if the field has no errors.
#
isDirtytrue
if the field has a different value than the initial value.
#
isValidatingtrue
if the field validation is running.
#
formThe Form instance this field is bound to.
#
setValueSets the value of the field.
#
setTouchedSets if the field should be considered touched.
#
setErrorSets the error of the field.
#
addErrorAdds an error to the field. It will be merged with the current error of the field.
#
validatePerforms field validation. It returns a Promise that is resolved with the errors of the field.