getInputProps
A handler to get input props for any field in the form
Docs
Package
getInputProps handler
form.getInputProps returns an object with value, onChange, onFocus, onBlur, error
and all props specified in enhanceGetInputProps function. Return value should be spread to the input component.
You can pass the following options to form.getInputProps as second argument:
type: defaultinput. Must be set tocheckboxif the input requirescheckedprop instead ofvalue.withError: defaulttype === 'input'. Determines whether the returned object should contain anerrorproperty withform.errors[path]value.withFocus: defaulttrue. Determines whether the returned object should contain anonFocushandler. If disabled, the touched state will only change if value of the field has been changed.- Any additional props that can be accessed with
enhanceGetInputPropsfunction. These props are not passed to the input.
enhanceGetInputProps
enhanceGetInputProps is a function that can be used to add additional props to the object returned by form.getInputProps.
You can define it in useForm hook options. Its argument is an object with the following properties:
inputProps– object returned byform.getInputPropsby defaultfield– field path, first argument ofform.getInputProps, for examplename,user.email,users.0.nameoptions– second argument ofform.getInputProps, for example{ type: 'checkbox' }, can be used to pass additional options toenhanceGetInputPropsfunctionform– form instance
Example of using enhanceGetInputProps to disable input based on field path:
Example of using enhanceGetInputProps to add additional props to the input based on option passed to form.getInputProps:
Your personal information is stored securely. (Just kidding!)
Example of using enhanceGetInputProps to add disabled prop to all inputs if the form
has not been initialized yet:
Initialize form
When called form.initialize handler sets initialValues and values to the same value
and marks form as initialized. It can be used only once, next form.initialize calls
are ignored.
form.initialize is useful when you want to sync form values with backend API response:
Example with TanStack Query (react-query):
Integrate getInputProps with custom inputs
form.getInputProps returns an object with the following properties:
valuedefaultValueonChangeonFocusonBlurerror
To create a custom input that works with form.getInputProps, make sure that your component
accepts these props and passes them to the input component or uses them in any other way.
Example of creating a custom input component:
Then use it with form.getInputProps: