File size: 4,234 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { useEffect } from 'react';
import { useForm } from 'react-hook-form';

let renderCounter = 0;

const SetValue: React.FC = () => {
  const {
    register,
    setValue,
    handleSubmit,
    formState: { errors },
  } = useForm<{
    firstName: string;
    lastName: string;
    age: string;
    trigger: string;
    checkbox: boolean;
    checkboxArray: string[];
    radio: string;
    select: string;
    multiple: string[];
    array: string[];
    object: {
      firstName: string;
      lastName: string;
      middleName: string;
    };
    nestedValue: string[];
  }>();

  useEffect(() => {
    register('lastName', { required: true });
    setValue('firstName', 'wrong', { shouldDirty: true });
    setValue('age', '2', { shouldDirty: true });
    setValue('trigger', '', { shouldDirty: true, shouldValidate: true });
    setValue('checkbox', true, { shouldDirty: true });
    setValue('checkboxArray', ['2', '3'], {
      shouldDirty: true,
      shouldValidate: true,
    });
    setValue('radio', 'radio', { shouldDirty: true });
    setValue('select', 'a', { shouldDirty: true });
    setValue('multiple', ['a', 'b'], { shouldDirty: true });
    setValue('array', ['array.0', 'array.1', 'array.2'], {
      shouldDirty: true,
    });
    setValue(
      'object',
      {
        firstName: 'firstName',
        lastName: 'lastName',
        middleName: 'middleName',
      },
      { shouldDirty: true },
    );
    setValue('nestedValue', [], {
      shouldValidate: true,
      shouldDirty: true,
    });
  }, [register, setValue]);

  renderCounter++;

  return (
    <form onSubmit={handleSubmit(() => {})}>
      <input {...register('firstName')} placeholder="firstName" />
      <input {...register('array.0')} placeholder="array[0]" />
      <input {...register('array.1')} placeholder="array[1]" />
      <input {...register('array.2')} placeholder="array[2]" />
      <input {...register('object.firstName')} placeholder="object.firstName" />
      <input {...register('object.lastName')} placeholder="object.lastName" />
      <input
        {...register('object.middleName')}
        placeholder="object.middleName"
      />
      <input type="number" {...register('age')} placeholder="age" />
      <input value="radio" type="radio" {...register('radio')} />
      <input type="checkbox" {...register('checkbox')} />
      <input type="checkbox" value="1" {...register('checkboxArray')} />
      <input type="checkbox" value="2" {...register('checkboxArray')} />
      <input type="checkbox" value="3" {...register('checkboxArray')} />
      <select {...register('select')}>
        <option>Select</option>
        <option value="a">a</option>
        <option value="b">b</option>
      </select>
      <select multiple {...register('multiple')}>
        <option>Select</option>
        <option value="a">a</option>
        <option value="b">b</option>
      </select>

      <input
        name="lastName"
        placeholder="lastName"
        onChange={() => {
          setValue('lastName', 'test');
        }}
      />
      {errors.lastName && <p id="lastName">Last name error</p>}

      <input
        {...register('trigger', { required: true })}
        placeholder="trigger"
      />
      {errors.trigger && <p id="trigger">Trigger error</p>}

      <input
        {...register('nestedValue', { required: 'required' })}
        placeholder="nestedValue"
      />
      {errors.nestedValue && (
        <p id="nestedValue">{errors.nestedValue.message}</p>
      )}

      <button
        type="button"
        id="setMultipleValues"
        onClick={() => {
          setValue(
            'object',
            {
              firstName: 'firstName1',
              lastName: 'lastName1',
              middleName: 'middleName1',
            },
            { shouldDirty: true },
          );
          setValue('array', ['array[0]1', 'array[1]1', 'array[2]1'], {
            shouldDirty: true,
          });
          setValue('nestedValue', ['a', 'b'], { shouldDirty: true });
        }}
      >
        Set Multiple Values
      </button>

      <button id="submit">Submit</button>
      <div id="renderCount">{renderCounter}</div>
    </form>
  );
};

export default SetValue;