| 2 | import { useForm } from 'react-hook-form'; |
| 3 | |
| 4 | function DefaultValues() { |
| 5 | const { register } = useForm<{ |
| 6 | test: string; |
| 7 | test1: { |
| 8 | firstName: string; |
| 9 | lastName: string[]; |
| 10 | deep: { |
| 11 | nest: string; |
| 12 | }; |
| 13 | }; |
| 14 | checkbox: string[]; |
| 15 | }>({ |
| 16 | defaultValues: { |
| 17 | test: 'test', |
| 18 | checkbox: ['1', '2'], |
| 19 | test1: { |
| 20 | firstName: 'firstName', |
| 21 | lastName: ['lastName0', 'lastName1'], |
| 22 | deep: { |
| 23 | nest: 'nest', |
| 24 | }, |
| 25 | }, |
| 26 | }, |
| 27 | }); |
| 28 | const [show, setShow] = React.useState(true); |
| 29 | |
| 30 | return ( |
| 31 | <> |
| 32 | {show ? ( |
| 33 | <form> |
| 34 | <input {...register('test')} /> |
| 35 | <input {...register('test1.firstName')} /> |
| 36 | <input {...register('test1.deep.nest')} /> |
| 37 | <input {...register('test1.deep.nest')} /> |
| 38 | <input {...register('test1.lastName.0')} /> |
| 39 | <input {...register('test1.lastName.1')} /> |
| 40 | <input type="checkbox" value={'1'} {...register('checkbox')} /> |
| 41 | <input type="checkbox" value={'2'} {...register('checkbox')} /> |
| 42 | </form> |
| 43 | ) : null} |
| 44 | <button type={'button'} id={'toggle'} onClick={() => setShow(!show)}> |
| 45 | toggle |
| 46 | </button> |
| 47 | </> |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | export default DefaultValues; |