File size: 1,304 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React from 'react';
import { useForm } from 'react-hook-form';

const sleep = <T,>(data: T, ms: number) =>
  new Promise<T>((res) => setTimeout(() => res(data), ms));

function DefaultValues() {
  const { register } = useForm({
    defaultValues: async () =>
      sleep(
        {
          test: 'test',
          checkbox: ['1', '2'],
          test1: {
            firstName: 'firstName',
            lastName: ['lastName0', 'lastName1'],
            deep: {
              nest: 'nest',
            },
          },
        },
        10,
      ),
  });
  const [show, setShow] = React.useState(true);

  return (
    <>
      {show ? (
        <form>
          <input {...register('test')} />
          <input {...register('test1.firstName')} />
          <input {...register('test1.deep.nest')} />
          <input {...register('test1.deep.nest')} />
          <input {...register('test1.lastName.0')} />
          <input {...register('test1.lastName.1')} />
          <input type="checkbox" value={'1'} {...register('checkbox')} />
          <input type="checkbox" value={'2'} {...register('checkbox')} />
        </form>
      ) : null}
      <button type={'button'} id={'toggle'} onClick={() => setShow(!show)}>
        toggle
      </button>
    </>
  );
}

export default DefaultValues;