-
Notifications
You must be signed in to change notification settings - Fork 0
/
formLerner.js
27 lines (25 loc) · 915 Bytes
/
formLerner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { Controller, useForm } from 'react-hook-form';
const { handleSubmit, control, reset, formState: { errors } } = useForm();
const onSubmit = React.useCallback((values) => {
console.log(values);
reset();
}, []);
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
render={({ field: { name, value, onChange } }) => (
<TextField
name={name}
value={value}
onChange={onChange}
// or shorter {...field}
label={fields.firstName.label}
error={Boolean(errors[fields.firstName.name])}
helperText={errors[fields.firstName.name] ? errors[fields.firstName.name].message : ''}
/>
)}
control={control}
name={fields.firstName.name}
defaultValue=""
rules={{ required: { value: true, message: 'Invalid input' } }}
/>
</form>