-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathapi.tsx
More file actions
3024 lines (2917 loc) Β· 92.3 KB
/
api.tsx
File metadata and controls
3024 lines (2917 loc) Β· 92.3 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import CodeArea from "../components/CodeArea"
import useFieldArrayArgument from "../components/codeExamples/useFieldArrayArgument"
import generic from "./generic"
import Link from "next/link"
import typographyStyles from "../styles/typography.module.css"
import buttonStyles from "../styles/button.module.css"
import tableStyles from "../styles/table.module.css"
import TabGroup from "../components/TabGroup"
export default {
title: "API Documentation",
header: {
description: "focuses on providing the best DX by simplifying the API.",
},
useForm: {
title: "useForm",
intro: (
<>
By invoking <code>useForm</code>, you will receive the following methods{" "}
:
</>
),
description: (
<p>
<code>useForm</code> is a custom hook for managing forms with ease. It
takes one object as <b>optional</b> argument. The following example
demonstrates all of its properties along with their default values.
</p>
),
validateCriteriaMode: (
<ul style={{ marginLeft: 0, paddingLeft: 15 }}>
<li>
<p>
When set to <code>firstError</code> (default), only the first error
from each field will be gathered.
</p>
</li>
<li>
<p>
When set to <code>all</code>, all errors from each field will be
gathered.
</p>
</li>
</ul>
),
validateContext: (
<>
<p>
This context <code>object</code> is mutable and will be injected into{" "}
the <code>resolver</code>'s second argument or{" "}
<a
href="https://github.com/jquense/yup"
target="_blank"
rel="noopener noreferrer"
>
Yup
</a>{" "}
validation's context object.
</p>
</>
),
validateOnSubmit: (
<>
Validation is triggered on the <code>submit</code> event, and inputs
attach <code>onChange</code> event listeners to re-validate themselves.
</>
),
validateOnBlur: (
<>
Validation is triggered on the <code>blur</code> event.
</>
),
validateOnChange: (
<>
Validation is triggered on the <code>change</code>
event for each input, leading to multiple re-renders. Warning: this
often comes with a significant impact on performance.
</>
),
validationOnAll: (
<>
Validation is triggered on both <code>blur</code> and{" "}
<code>change</code> events.
</>
),
validationOnTouched: (
<>
<p>
Validation is initially triggered on the first <code>blur</code>{" "}
event. After that, it is triggered on every <code>change</code> event.
</p>
<p>
<b className={typographyStyles.note}>Note:</b> when using with{" "}
<code>Controller</code>, make sure to wire up <code>onBlur</code> with{" "}
the <code>render</code> prop.
</p>
</>
),
values: (
<>
<p>
The <code>values</code> props will react to changes and update the
form values, which is useful when your form needs to be updated by
external state or server data.
</p>
<CodeArea
rawData={`// set default value sync
function App({ values }) {
useForm({
values // will get updated when values props updates
})
}
function App() {
const values = useFetch('/api');
useForm({
defaultValues: {
firstName: '',
lastName: '',
},
values, // will get updated once values returns
})
}
`}
/>
</>
),
resetOptions: (
<>
<p>
This property is related to value update behaviors. When{" "}
<code>values</code> or <code>defaultValues</code> are updated, the{" "}
<code>reset</code> API is invoked internally. It's important to
specify the desired behavior after <code>values</code> or{" "}
<code>defaultValues</code> are asynchronously updated. The
configuration option itself is a reference to the{" "}
<Link href="/docs/useform/reset">reset</Link> method's options.
</p>
<CodeArea
tsUrl="https://codesandbox.io/s/useform-resetoptions-7bsuud"
rawData={`// by default asynchronously value or defaultValues update will reset the form values
useForm({ values })
useForm({ defaultValues: async () => await fetch() })
// options to config the behaviour
// eg: I want to keep user interacted/dirty value and not remove any user errors
useForm({
values,
resetOptions: {
keepDirtyValues: true, // user-interacted input will be retained
keepErrors: true, // input errors will be retained with value update
}
})
`}
/>
</>
),
defaultValues: (
<>
<p>
The <code>defaultValues</code> prop populates the entire form with
default values. It supports both synchronous and asynchronous
assignment of default values. While you can set an input's default
value using <code>defaultValue</code> or <code>defaultChecked</code>{" "}
<a
className={buttonStyles.links}
href="https://reactjs.org/docs/uncontrolled-components.html"
>
(as detailed in the official React documentation)
</a>
, it is <strong>recommended</strong> to use <code>defaultValues</code>{" "}
for the entire form.
</p>
<CodeArea
rawData={`// set default value sync
useForm({
defaultValues: {
firstName: '',
lastName: ''
}
})
// set default value async
useForm({
defaultValues: async () => fetch('/api-endpoint');
})`}
/>
<h3 className={typographyStyles.rulesTitle}>Rules</h3>
<ul>
<li>
<p>
You <strong>should avoid</strong> providing <code>undefined</code>{" "}
as a default value, as it conflicts with the default state of a
controlled component.
</p>
</li>
<li>
<p>
<code>defaultValues</code> are cached. To reset them, use the{" "}
<Link href="/docs/useform/reset">reset</Link> API.
</p>
</li>
<li>
<p>
<code>defaultValues</code> will be included in the submission
result by default.
</p>
</li>
<li>
<p>
It's recommended to avoid using custom objects containing
prototype methods, such as <code>Moment</code> or{" "}
<code>Luxon</code>, as <code>defaultValues</code>.
</p>
</li>
<li>
<p>There are other options for including form data:</p>
<CodeArea
rawData={`// include hidden input
<input {...register("hidden")} type="hidden" />
register("hidden", { value: "data" })
// include data onSubmit
const onSubmit = (data) => {
const output = {
...data,
others: "others"
}
}
`}
/>
</li>
</ul>
</>
),
reValidateMode: (
<p>
This option allows you to configure validation strategy when inputs with
errors get re-validated <strong>after</strong> a user submits the form (
<code>onSubmit</code> event and{" "}
<Link href="/docs/useform/handlesubmit">
<code>handleSubmit</code>
</Link>{" "}
function executed). By default, re-validation occurs during the input
change event.
</p>
),
validationFields: (
<p>
Providing an array of fields means only included fields will be
validated. This option is useful when you want to toggle which fields
are required to validate.
</p>
),
submitFocusError: (
<>
<p>
When set to <code>true</code> (default), and the user submits a form
that fails validation, focus is set on the first field with an error.
</p>
<p>
<b className={typographyStyles.note}>Note:</b> only registered fields
with a <code>ref</code> will work. Custom registered inputs do not
apply. For example: <code>{`register('test') // doesn't work`}</code>{" "}
</p>
<p>
<b className={typographyStyles.note}>Note:</b> the focus order is
based on the <code>register</code> order.
</p>
</>
),
shouldUnregister: (
<>
<p>
By default, an input value will be retained when input is removed.
However, you can set <code>shouldUnregister</code> to{" "}
<code>true</code> to <code>unregister</code> input during unmount.
</p>
<ul>
<li>
<p>
This is a global configuration that overrides child-level
configurations. To have individual behavior, set the configuration
at the component or hook level, not at <code>useForm</code>.
</p>
</li>
<li>
<p>
By default, <code>shouldUnregister: false</code> means unmounted
fields are <strong>not validated</strong> by built-in validation.
</p>
</li>
<li>
<p>
By setting <code>shouldUnregister</code> to true at{" "}
<code>useForm</code> level, <code>defaultValues</code> will{" "}
<b>not</b> be merged against submission result.
</p>
</li>
<li>
<p>
Setting <code>shouldUnregister: true</code> makes your form behave
more closely to native forms.
</p>
<ul>
<li>
<p>Form values are stored within the inputs themselves.</p>
</li>
<li>
<p>Unmounting an input removes its value.</p>
</li>
<li>
<p>
Hidden inputs should use the <code>hidden</code> attribute for
storing hidden data.
</p>
</li>
<li>
<p>Only registered inputs are included as submission data.</p>
</li>
<li>
<p>
Unmounted inputs must be notified at either{" "}
<code>useForm</code> or <code>useWatch</code>'s{" "}
<code>useEffect</code> for the hook form to verify that the
input is unmounted from the DOM.
</p>
<CodeArea
rawData={`const NotWork = () => {
const [show, setShow] = React.useState(false);
// β won't get notified, need to invoke unregister
return {show && <input {...register('test')} />}
}
const Work = ({ control }) => {
const { show } = useWatch({ control })
// β
get notified at useEffect
return {show && <input {...register('test1')} />}
}
const App = () => {
const [show, setShow] = React.useState(false);
const { control } = useForm({ shouldUnregister: true });
return (
<div>
// β
get notified at useForm's useEffect
{show && <input {...register('test2')} />}
<NotWork />
<Work control={control} />
</div>
)
}
`}
/>
</li>
</ul>
</li>
</ul>
</>
),
delayError: (
<p>
This configuration delays the display of error states to the end-user by
a specified number of milliseconds. If the user corrects the error
input, the error is removed instantly, and the delay is not applied.
</p>
),
},
unregister: {
title: "unregister",
description: (
<>
<p>
This method allows you to <code>unregister</code> a single input or an
array of inputs. It also provides a second optional argument to keep
state after unregistering an input.
</p>
<div className={tableStyles.tableWrapper}>
<h2 className={typographyStyles.subTitle}>Props</h2>
<p>
The example below shows what to expect when you invoke the{" "}
<code>unregister</code> method.
</p>
<CodeArea
rawData={`<input {...register('yourDetails.firstName')} />
<input {...register('yourDetails.lastName')} />
`}
/>
<table className={tableStyles.table}>
<tbody>
<tr>
<th>Type</th>
<th>Input Name</th>
<th>Value</th>
</tr>
<tr>
<td>
<code className={typographyStyles.typeText}>string</code>
</td>
<td>
<code>unregister("yourDetails")</code>
</td>
<td>
<code>{`{}`}</code>
</td>
</tr>
<tr>
<td>
<code className={typographyStyles.typeText}>string</code>
</td>
<td>
<code>unregister("yourDetails.firstName")</code>
</td>
<td>
<code>{`{ lastName: '' }`}</code>
</td>
</tr>
<tr>
<td>
<code className={typographyStyles.typeText}>string[]</code>
</td>
<td>
<code>unregister(["yourDetails.lastName"])</code>
</td>
<td>
<code>{`''`}</code>
</td>
</tr>
</tbody>
</table>
</div>
</>
),
},
register: {
title: "register",
description: (
<>
<p>
This method allows you to register an input or select element and
apply validation rules to React Hook Form. Validation rules are all
based on the HTML standard and also allow for custom validation
methods.
</p>
<p>
By invoking the register function and supplying an input's name, you
will receive the following methods:
</p>
<h2 className={typographyStyles.subTitle}>Props</h2>
<div className={tableStyles.tableWrapper}>
<table className={tableStyles.table}>
<thead>
<tr>
<th>{generic.name}</th>
<th>{generic.type}</th>
<th>{generic.description}</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>onChange</code>
</td>
<td>
<code className={typographyStyles.typeText}>
ChangeHandler
</code>
</td>
<td>
<p>
<code>onChange</code> prop to subscribe the input change
event.
</p>
</td>
</tr>
<tr>
<td>
<code>onBlur</code>
</td>
<td>
<code className={typographyStyles.typeText}>
ChangeHandler
</code>
</td>
<td>
<p>
<code>onBlur</code> prop to subscribe the input blur event.
</p>
</td>
</tr>
<tr>
<td>
<code>name</code>
</td>
<td>
<code className={typographyStyles.typeText}>string</code>
</td>
<td>
<p>Input's name being registered.</p>
</td>
</tr>
</tbody>
</table>
</div>
</>
),
example: "Submit Result",
selectHelp:
"By selecting the register option, the API table below will get updated.",
options: {
title: "Select Options",
registerWithValidation: "Register with validation",
registerWithValidationMessage:
"Register with validation and error message",
note: (
<>
<h2 className={typographyStyles.subTitle}>Tips</h2>
<h4 className={typographyStyles.questionTitle}>Custom Register</h4>
<p>
You can also <code>register</code> inputs with{" "}
<code>useEffect</code> and treat them as virtual inputs. For
controlled components, we provide a custom hook{" "}
<Link href="/docs/usecontroller">useController</Link> and{" "}
<Link href="/docs/usecontroller/controller">Controller</Link>{" "}
component to take care this process for you.
</p>
<p>
If you choose to manually register fields, you will need to update
the input value with{" "}
<Link href="/docs/useform/setvalue">setValue</Link>.
</p>
<CodeArea
rawData={`register('firstName', { required: true, min: 8 });
<TextInput onTextChange={(value) => setValue('lastChange', value))} />
`}
/>
<h4 className={typographyStyles.questionTitle}>
How to work with innerRef, inputRef?
</h4>
<p>
When the custom input component didn't expose ref correctly, you can
get it working via the following.
</p>
<CodeArea
rawData={`// not working, because ref is not assigned
<TextInput {...register('test')} />
const firstName = register('firstName', { required: true })
<TextInput
name={firstName.name}
onChange={firstName.onChange}
onBlur={firstName.onBlur}
inputRef={firstName.ref} // you can achieve the same for different ref name such as innerRef
/>
// correct way to forward input's ref
const Select = React.forwardRef(({ onChange, onBlur, name, label }, ref) => (
<select name={name} ref={ref} onChange={onChange} onBlur={onBlur}>
<option value="20">20</option>
<option value="30">30</option>
</select>
));
`}
/>
</>
),
},
validation: {
required: (
<>
<p>
A Boolean which, if true, indicates that the input must have a value
before the form can be submitted. You can assign a string to return
an error message in the <code>errors</code> object.
</p>
<p>
<b className={typographyStyles.note}>Note:</b> This config aligns
with web constrained API for required input validation, for{" "}
<code>object</code> or <code>array</code> type of input use{" "}
<code>validate</code> function instead.
</p>
</>
),
maxLength: "The maximum length of the value to accept for this input.",
minLength: "The minimum length of the value to accept for this input.",
max: "The maximum value to accept for this input.",
min: "The minimum value to accept for this input.",
pattern: (
<>
<p>The regex pattern for the input.</p>
<p>
<b className={typographyStyles.note}>Note:</b> A RegExp object with
the /g flag keeps track of the lastIndex where a match occurred.
</p>
</>
),
validate: (
<>
<p>
You can pass a callback function as the argument to validate, or you
can pass an object of callback functions to validate all of them.
This function will be executed on its own without depending on other
validation rules included in the <code>required</code> attribute.
</p>
<p>
<b className={typographyStyles.note}>Note:</b> for{" "}
<code>object</code> or <code>array</code> input data, it's
recommended to use the <code>validate</code> function for validation
as the other rules mostly apply to <code>string</code>,{" "}
<code>string[]</code>, <code>number</code> and <code>boolean</code>{" "}
data types.
</p>
</>
),
},
},
formState: {
title: "formState",
description: (
<>
<p>
This object contains information about the entire form state. It helps
you to keep on track with the user's interaction with your form
application.
</p>
</>
),
isSubmitSuccessful: (
<p>
Indicate the form was successfully submitted without any runtime error.
</p>
),
isDirty: (
<>
<p>
Set to <code>true</code> after the user modifies any of the inputs.
</p>
<ul>
<li>
<p>
<b>Important:</b> Make sure to provide all inputs' defaultValues
at the useForm, so hook form can have a single source of truth to
compare whether the form is dirty.
</p>
<CodeArea
rawData={`const {
formState: { isDirty, dirtyFields },
setValue,
} = useForm({ defaultValues: { test: "" } });
// isDirty: true
setValue('test', 'change')
// isDirty: false because there getValues() === defaultValues
setValue('test', '')
`}
/>
</li>
<li>
<p>
File typed input will need to be managed at the app level due to
the ability to cancel file selection and{" "}
<a
href="https://developer.mozilla.org/en-US/docs/Web/API/FileList"
target="_blank"
rel="noopener noreferrer"
>
FileList
</a>{" "}
object.
</p>
</li>
<li>
<p>Do not support custom object, Class or File object.</p>
</li>
</ul>
</>
),
isSubmitted: (
<>
Set to <code>true</code> after the form is submitted. Will remain{" "}
<code>true</code> until the <code>reset</code> method is invoked.
</>
),
dirtyFields: (
<>
<p>
An object with the user-modified fields. Make sure to provide all
inputs' defaultValues via useForm, so the library can compare against
the <code>defaultValues</code>.
</p>
<ul>
<li>
<p>
<b>Important:</b> Make sure to provide defaultValues at the
useForm, so hook form can have a single source of truth to compare
each field's dirtiness.
</p>
</li>
<li>
<p>
Dirty fields will <strong>not</strong> represent as{" "}
<code>isDirty</code> formState, because dirty fields are marked
field dirty at field level rather the entire form. If you want to
determine the entire form state use <code>isDirty</code> instead.
</p>
</li>
</ul>
</>
),
touched:
"An object containing all the inputs the user has interacted with.",
defaultValues: (
<p>
The value which has been set at{" "}
<Link href="/docs/useform" aria-label="read more about reset api">
useForm
</Link>
's defaultValues or updated defaultValues via{" "}
<Link href="/docs/useform/reset" aria-label="read more about reset api">
reset
</Link>{" "}
API.
</p>
),
isSubmitting: (
<>
<code>true</code> if the form is currently being submitted.{" "}
<code>false</code> otherwise.
</>
),
isLoading: (
<>
<p>
<code>true</code> if the form is currently loading async default
values.
</p>
<p>
<b className={typographyStyles.note}>Important:</b> this prop is only
applicable to async <code>defaultValues</code>
</p>
<CodeArea
rawData={`const {
formState: { isLoading }
} = useForm({
defaultValues: async () => await fetch('/api')
});
`}
/>
</>
),
submitCount: "Number of times the form was submitted.",
isValid: (
<>
Set to <code>true</code> if the form doesn't have any errors.
</>
),
isValidating: (
<>
Set to <code>true</code> during validation.
</>
),
validatingFields: <>Capture fields which are getting async validation.</>,
},
errors: {
title: "errors",
description: () => (
<>
<p>
Object containing form errors and error messages corresponding to each
field.
</p>
</>
),
types: (
<>
<p style={{ marginTop: 0 }}>
This is useful when you want to return all validation errors for a
single input. For instance, a password field that is required to have
a minimum length and contain a special character.
</p>
<p>
<b className={typographyStyles.note}>Note:</b> You need to set{" "}
<code>criteriaMode</code> to <code>'all'</code> for this option to
work.
</p>
</>
),
message: `If you registered your input with an error message, then it will be put in this field, otherwise it's an empty string by default.`,
ref: `Reference for your input element.`,
note: (
<>
<p>
<b className={typographyStyles.note}>Important:</b> Avoid using error
object key names to avoid data overwrite. <br />
eg:{" "}
<code>
register('user'); register('user.type');{" "}
<span role="img" aria-label="Big Red X">
β
</span>
{" // error's type will get overwritten."}
</code>
</p>
<p>
<b className={typographyStyles.note}>Note:</b> You can use the{" "}
<Link href="/docs/useformstate/errormessage">ErrorMessage</Link>{" "}
component to help display your error states
</p>
</>
),
},
watch: {
title: "watch",
description: (
<>
<p>
This method will watch specified inputs and return their values. It is
useful to render input value and for determining what to render by
condition.
</p>
</>
),
tableTitle: {
single: (
<>
Watch input value by name (similar to lodash{" "}
<a
target="_blank"
rel="noreferrer noopener"
href="https://lodash.com/docs/4.17.15#get"
>
get
</a>{" "}
function)
</>
),
multiple: "Watch multiple inputs",
all: "Watch all inputs",
callback: "Watch all inputs and invoke a callback",
},
},
handleSubmit: {
title: "handleSubmit",
description: (
<>
<p>
This function will receive the form data if form validation is
successful.
</p>
<h2 className={typographyStyles.subTitle}>Props</h2>
<div className={tableStyles.tableWrapper}>
<table className={tableStyles.table}>
<tbody>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>SubmitHandler</td>
<td>
<code
className={typographyStyles.typeText}
>{`(data: Object, e?: Event) => Promise<void>`}</code>
</td>
<td>A successful callback.</td>
</tr>
<tr>
<td>SubmitErrorHandler</td>
<td>
<code
className={typographyStyles.typeText}
>{`(errors: Object, e?: Event) => Promise<void>`}</code>
</td>
<td>An error callback.</td>
</tr>
</tbody>
</table>
</div>
<h2 id="rules" className={typographyStyles.rulesTitle}>
Rules
</h2>
<ul>
<li>
<p>You can easily submit form asynchronously with handleSubmit.</p>
<CodeArea
rawData={`// It can be invoked remotely as well
handleSubmit(onSubmit)();
// You can pass an async function for asynchronous validation.
handleSubmit(async (data) => await fetchAPI(data))`}
/>
</li>
<li>
<p>
<code>disabled</code> inputs will appear as <code>undefined</code>{" "}
values in form values. If you want to prevent users from updating
an input and wish to retain the form value, you can use{" "}
<code>readOnly</code> or disable the entire {`<fieldset />`}. Here
is an{" "}
<a
href="https://codesandbox.io/s/react-hook-form-disabled-inputs-oihxx"
target="_blank"
rel="noopener noreferrer"
>
example
</a>
.
</p>
</li>
<li>
<p>
<code>handleSubmit</code> function will not swallow errors that
occurred inside your onSubmit callback, so we recommend you to try
and catch inside async request and handle those errors gracefully
for your customers.
</p>
<CodeArea
rawData={`const onSubmit = async () => {
// async request which may result error
try {
// await fetch()
} catch (e) {
// handle your error
}
};
<form onSubmit={handleSubmit(onSubmit)} />
`}
/>
</li>
</ul>
</>
),
},
reset: {
title: "reset",
description: (
<>
<p>
Reset the entire form state, fields reference, and subscriptions.
There are optional arguments and will allow partial form state reset.
</p>
<h2 className={typographyStyles.subTitle}>Props</h2>
<p>
<code>Reset</code> has the ability to retain formState. Here are the
options you may use:{" "}
</p>
<div className={tableStyles.tableWrapper}>
<table className={tableStyles.table}>
<thead>
<tr>
<th>{generic.name}</th>
<th>{generic.type}</th>
<th>{generic.description}</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>values</code>
</td>
<td>
<code className={typographyStyles.typeText}>object</code>
</td>