Skip to content

Instantly share code, notes, and snippets.

@IniZio
Last active October 19, 2022 10:55
Show Gist options
  • Save IniZio/19799397d26e35fe27ee0da164195acf to your computer and use it in GitHub Desktop.
Save IniZio/19799397d26e35fe27ee0da164195acf to your computer and use it in GitHub Desktop.
function Demo() {
const loginForm = useForm({
email: ['', [Validators.required]],
username: ['', [Validators.required, AsyncUsernameUniquenessValidator]],
password: ['', [PasswordStringValidator]],
contacts: [[], [Validators.minLength(1)],
);
const onLogin = useCallback((values) => {
console.log("=== form values", values);
})
return (
<form onSubmit={loginForm.handleSubmit(onLogin)}>
<Field field={loginForm.controls.email}>
{({ handlers, touched, errors }) => (
<div className="form-group">
<input {...handlers('text')} />
<span>
{touched && errors.required && 'Email is required'}
</span>
</div>
)}
</Field>
<Field field={loginForm.controls.username}>
{({ handlers, pending, touched, errors }) => (
<div className="form-group">
<input {...handlers('text')} />
{pending && <Loading />}
<span>
{touched && errors.required && 'Email is required'}
</span>
</div>
)}
</Field>
<button
disabled={
!loginForm.touched
|| loginForm.pending
|| loginForm.invalid
|| loginForm.submitting
}
type="submit"
>Register</button>
</form>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment