How @mantine/form works
use-form is used to store form values,
errors, touched, dirty and validation state. In order to support all @mantine/form
features (including form.getInputProps()
) custom inputs must accept the following props:
value
– input value in controlled modedefaultValue
– input value in uncontrolled modeonChange
– input change handler used both for controlled and uncontrolled modeserror
– validation error messageonBlur
– used to set field as touched
use-uncontrolled hook
In most cases, the easiest way to add support for both controlled and uncontrolled modes is to use
use-uncontrolled hook. It allows to use value
and
defaultValue
props together and automatically handles controlled/uncontrolled mode switching.
Example of a custom input that supports both controlled and uncontrolled modes:
Full example
In the following example CustomInput
component supports all @mantine/form
features:
value
,defaultValue
andonChange
are used to control input valueerror
is used to display validation error messageonBlur
(part of...others
props) is used to set field as touched