not available yet
React hook for building controlled and uncontrolled forms with validation. Highly inspired by Formik, React hook form and Angular ractive forms.
We need to pass an object where the key is the field name and the value is an array. The first element of the array is the default value and the second element is an array of validators
const loginForm = useForm(
{
email: ['', [required(), email()]],
password: ['', [required(), minLength(8)]],
},
{
onSubmit(values) {
// ...
},
}
);
return (
<form {...loginForm.form}>
<input {...login.field('email')} />
<input {...login.field('password')} />
<input type="submit" value={'Sign in'} />
</form>
);
In most cases we will use the field method, file, radio and checkbox are also available.
const loginForm = useForm({
// other fields
rememberMe: [false],
});
// ...
<input className="checkbox" {...login.checkbox('rememberMe')} />;
To read an error, we can use the get method.
login.get('email').errors?.email && <span>Please enter a valid email.</span>;
If you need to keep track of field changes, pass the watch option to field
{...login.field('email', { watch: true })}
To run the demo app
npm run dev
To build package
npm run build
To run tests
npm run test
To run tests with watch
npm run test:watch