/
form.ts
824 lines (771 loc) · 24.3 KB
/
form.ts
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
import React from 'react';
import { Subject, Subscription } from '../utils/createSubject';
import { ErrorOption, FieldError, FieldErrors } from './errors';
import { EventType } from './events';
import { FieldArray } from './fieldArray';
import {
FieldRefs,
FieldValue,
FieldValues,
InternalFieldName,
} from './fields';
import {
FieldArrayPath,
FieldPath,
FieldPathValue,
FieldPathValues,
} from './path';
import { Resolver } from './resolvers';
import { DeepMap, DeepPartial, Noop } from './utils';
import { RegisterOptions } from './validator';
declare const $NestedValue: unique symbol;
/**
* @deprecated to be removed in the next major version
*/
export type NestedValue<TValue extends object = object> = {
[$NestedValue]: never;
} & TValue;
/**
* @deprecated to be removed in the next major version
*/
export type UnpackNestedValue<T> = T extends NestedValue<infer U>
? U
: T extends Date | FileList | File | Blob
? T
: T extends object
? { [K in keyof T]: UnpackNestedValue<T[K]> }
: T;
export type DefaultValues<TFieldValues> = DeepPartial<TFieldValues>;
export type InternalNameSet = Set<InternalFieldName>;
export type ValidationMode = {
onBlur: 'onBlur';
onChange: 'onChange';
onSubmit: 'onSubmit';
onTouched: 'onTouched';
all: 'all';
};
export type Mode = keyof ValidationMode;
export type CriteriaMode = 'firstError' | 'all';
export type SubmitHandler<TFieldValues extends FieldValues> = (
data: TFieldValues,
event?: React.BaseSyntheticEvent,
) => any | Promise<any>;
export type SubmitErrorHandler<TFieldValues extends FieldValues> = (
errors: FieldErrors<TFieldValues>,
event?: React.BaseSyntheticEvent,
) => any | Promise<any>;
export type SetValueConfig = Partial<{
shouldValidate: boolean;
shouldDirty: boolean;
shouldTouch: boolean;
}>;
export type TriggerConfig = Partial<{
shouldFocus: boolean;
}>;
export type ChangeHandler = (event: {
target: any;
type?: any;
}) => Promise<void | boolean>;
export type DelayCallback = (wait: number) => void;
export type UseFormProps<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
> = Partial<{
mode: Mode;
reValidateMode: Exclude<Mode, 'onTouched' | 'all'>;
defaultValues: DefaultValues<TFieldValues>;
resolver: Resolver<TFieldValues, TContext>;
context: TContext;
shouldFocusError: boolean;
shouldUnregister: boolean;
shouldUseNativeValidation: boolean;
criteriaMode: CriteriaMode;
delayError: number;
}>;
export type FieldNamesMarkedBoolean<TFieldValues extends FieldValues> = DeepMap<
DeepPartial<TFieldValues>,
boolean
>;
export type FormStateProxy<TFieldValues extends FieldValues = FieldValues> = {
isDirty: boolean;
isValidating: boolean;
dirtyFields: FieldNamesMarkedBoolean<TFieldValues>;
touchedFields: FieldNamesMarkedBoolean<TFieldValues>;
errors: boolean;
isValid: boolean;
};
export type ReadFormState = { [K in keyof FormStateProxy]: boolean | 'all' };
export type FormState<TFieldValues extends FieldValues> = {
isDirty: boolean;
isSubmitted: boolean;
isSubmitSuccessful: boolean;
isSubmitting: boolean;
isValidating: boolean;
isValid: boolean;
submitCount: number;
defaultValues?: Readonly<DeepPartial<TFieldValues>> | TFieldValues;
dirtyFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
touchedFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
errors: FieldErrors<TFieldValues>;
};
export type KeepStateOptions = Partial<{
keepDirtyValues: boolean;
keepErrors: boolean;
keepDirty: boolean;
keepValues: boolean;
keepDefaultValues: boolean;
keepIsSubmitted: boolean;
keepTouched: boolean;
keepIsValid: boolean;
keepSubmitCount: boolean;
}>;
export type SetFieldValue<TFieldValues extends FieldValues> =
FieldValue<TFieldValues>;
export type RefCallBack = (instance: any) => void;
export type UseFormRegisterReturn<
TFieldName extends InternalFieldName = InternalFieldName,
> = {
onChange: ChangeHandler;
onBlur: ChangeHandler;
ref: RefCallBack;
name: TFieldName;
min?: string | number;
max?: string | number;
maxLength?: number;
minLength?: number;
pattern?: string;
required?: boolean;
disabled?: boolean;
};
/**
* Register field into hook form with or without the actual DOM ref. You can invoke register anywhere in the component including at `useEffect`.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/register) • [Demo](https://codesandbox.io/s/react-hook-form-register-ts-ip2j3) • [Video](https://www.youtube.com/watch?v=JFIpCoajYkA)
*
* @param name - the path name to the form field value, name is required and unique
* @param options - register options include validation, disabled, unregister, value as and dependent validation
*
* @returns onChange, onBlur, name, ref, and native contribute attribute if browser validation is enabled.
*
* @example
* ```tsx
* // Register HTML native input
* <input {...register("input")} />
* <select {...register("select")} />
*
* // Register options
* <textarea {...register("textarea", { required: "This is required.", maxLength: 20 })} />
* <input type="number" {...register("name2", { valueAsNumber: true })} />
* <input {...register("name3", { deps: ["name2"] })} />
*
* // Register custom field at useEffect
* useEffect(() => {
* register("name4");
* register("name5", { value: '"hiddenValue" });
* }, [register])
*
* // Register without ref
* const { onChange, onBlur, name } = register("name6")
* <input onChange={onChange} onBlur={onBlur} name={name} />
* ```
*/
export type UseFormRegister<TFieldValues extends FieldValues> = <
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>(
name: TFieldName,
options?: RegisterOptions<TFieldValues, TFieldName>,
) => UseFormRegisterReturn<TFieldName>;
export type SetFocusOptions = Partial<{
shouldSelect: boolean;
}>;
/**
* Set focus on a registered field. You can start to invoke this method after all fields are mounted to the DOM.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/setfocus) • [Demo](https://codesandbox.io/s/setfocus-rolus)
*
* @param name - the path name to the form field value.
* @param options - input focus behavior options
*
* @example
* ```tsx
* useEffect(() => {
* setFocus("name");
* }, [setFocus])
* // shouldSelect allows to select input's content on focus
* <button onClick={() => setFocus("name", { shouldSelect: true })}>Focus</button>
* ```
*/
export type UseFormSetFocus<TFieldValues extends FieldValues> = <
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>(
name: TFieldName,
options?: SetFocusOptions,
) => void;
export type UseFormGetValues<TFieldValues extends FieldValues> = {
/**
* Get the entire form values when no argument is supplied to this function.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
*
* @returns form values
*
* @example
* ```tsx
* <button onClick={() => getValues()}>getValues</button>
*
* <input {...register("name", {
* validate: () => getValues().otherField === "test";
* })} />
* ```
*/
(): TFieldValues;
/**
* Get a single field value.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
*
* @param name - the path name to the form field value.
*
* @returns the single field value
*
* @example
* ```tsx
* <button onClick={() => getValues("name")}>getValues</button>
*
* <input {...register("name", {
* validate: () => getValues('otherField') === "test";
* })} />
* ```
*/
<TFieldName extends FieldPath<TFieldValues>>(
name: TFieldName,
): FieldPathValue<TFieldValues, TFieldName>;
/**
* Get an array of field values.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
*
* @param names - an array of field names
*
* @returns An array of field values
*
* @example
* ```tsx
* <button onClick={() => getValues(["name", "name1"])}>getValues</button>
*
* <input {...register("name", {
* validate: () => getValues(["fieldA", "fieldB"]).includes("test");
* })} />
* ```
*/
<TFieldNames extends FieldPath<TFieldValues>[]>(
names: readonly [...TFieldNames],
): [...FieldPathValues<TFieldValues, TFieldNames>];
};
/**
* This method will return individual field states. It will be useful when you are trying to retrieve the nested value field state in a typesafe approach.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/getfieldstate) • [Demo](https://codesandbox.io/s/getfieldstate-jvekk)
*
* @param name - the path name to the form field value.
*
* @returns invalid, isDirty, isTouched and error object
*
* @example
* ```tsx
* // those formState has to be subscribed
* const { formState: { dirtyFields, errors, touchedFields } } = formState();
* getFieldState('name')
* // Get field state when form state is not subscribed yet
* getFieldState('name', formState)
*
* // It's ok to combine with useFormState
* const formState = useFormState();
* getFieldState('name')
* getFieldState('name', formState)
* ```
*/
export type UseFormGetFieldState<TFieldValues extends FieldValues> = <
TFieldName extends FieldPath<TFieldValues>,
>(
name: TFieldName,
formState?: FormState<TFieldValues>,
) => {
/**
* @deprecated check `fieldState.error` instead
* ```jsx
* {fieldState.error && <p>{fieldState.error.message}</p>}
* ```
*/
invalid: boolean;
isDirty: boolean;
isTouched: boolean;
error?: FieldError;
};
export type UseFormWatch<TFieldValues extends FieldValues> = {
/**
* Watch and subscribe to the entire form update/change based on onChange and re-render at the useForm.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/watch) • [Demo](https://codesandbox.io/s/react-hook-form-watch-v7-ts-8et1d) • [Video](https://www.youtube.com/watch?v=3qLd69WMqKk)
*
* @returns return the entire form values
*
* @example
* ```tsx
* const formValues = watch();
* ```
*/
(): TFieldValues;
/**
* Watch and subscribe to an array of fields used outside of render.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/watch) • [Demo](https://codesandbox.io/s/react-hook-form-watch-v7-ts-8et1d) • [Video](https://www.youtube.com/watch?v=3qLd69WMqKk)
*
* @param names - an array of field names
* @param defaultValue - defaultValues for the entire form
*
* @returns return an array of field values
*
* @example
* ```tsx
* const [name, name1] = watch(["name", "name1"]);
* ```
*/
<TFieldNames extends readonly FieldPath<TFieldValues>[]>(
names: readonly [...TFieldNames],
defaultValue?: DeepPartial<TFieldValues>,
): FieldPathValues<TFieldValues, TFieldNames>;
/**
* Watch and subscribe to a single field used outside of render.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/watch) • [Demo](https://codesandbox.io/s/react-hook-form-watch-v7-ts-8et1d) • [Video](https://www.youtube.com/watch?v=3qLd69WMqKk)
*
* @param name - the path name to the form field value.
* @param defaultValue - defaultValues for the entire form
*
* @returns return the single field value
*
* @example
* ```tsx
* const name = watch("name");
* ```
*/
<TFieldName extends FieldPath<TFieldValues>>(
name: TFieldName,
defaultValue?: FieldPathValue<TFieldValues, TFieldName>,
): FieldPathValue<TFieldValues, TFieldName>;
/**
* Subscribe to field update/change without trigger re-render
*
* @remarks
* [API](https://react-hook-form.com/api/useform/watch) • [Demo](https://codesandbox.io/s/react-hook-form-watch-v7-ts-8et1d) • [Video](https://www.youtube.com/watch?v=3qLd69WMqKk)
*
* @param callback - call back function to subscribe all fields change and return unsubscribe function
* @param defaultValues - defaultValues for the entire form
*
* @returns unsubscribe function
*
* @example
* ```tsx
* useEffect(() => {
* const { unsubscribe } = watch((value) => {
* console.log(value);
* });
* return () => unsubscribe();
* }, [watch])
* ```
*/
(
callback: WatchObserver<TFieldValues>,
defaultValues?: DeepPartial<TFieldValues>,
): Subscription;
};
/**
* Trigger field or form validation
*
* @remarks
* [API](https://react-hook-form.com/api/useform/trigger) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-triggervalidation-forked-xs7hl) • [Video](https://www.youtube.com/watch?v=-bcyJCDjksE)
*
* @param name - provide empty argument will trigger the entire form validation, an array of field names will validate an arrange of fields, and a single field name will only trigger that field's validation.
* @param options - should focus on the error field
*
* @returns validation result
*
* @example
* ```tsx
* useEffect(() => {
* trigger();
* }, [trigger])
*
* <button onClick={async () => {
* const result = await trigger(); // result will be a boolean value
* }}>
* trigger
* </button>
* ```
*/
export type UseFormTrigger<TFieldValues extends FieldValues> = (
name?:
| FieldPath<TFieldValues>
| FieldPath<TFieldValues>[]
| readonly FieldPath<TFieldValues>[],
options?: TriggerConfig,
) => Promise<boolean>;
/**
* Clear the entire form errors.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/clearerrors) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-clearerrors-w3ymx)
*
* @param name - the path name to the form field value.
*
* @example
* Clear all errors
* ```tsx
* clearErrors(); // clear the entire form error
* clearErrors(["name", "name1"]) // clear an array of fields' error
* clearErrors("name2"); // clear a single field error
* ```
*/
export type UseFormClearErrors<TFieldValues extends FieldValues> = (
name?:
| FieldPath<TFieldValues>
| FieldPath<TFieldValues>[]
| readonly FieldPath<TFieldValues>[],
) => void;
/**
* Set a single field value, or a group of fields value.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/setvalue) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-setvalue-8z9hx) • [Video](https://www.youtube.com/watch?v=qpv51sCH3fI)
*
* @param name - the path name to the form field value.
* @param value - field value
* @param options - should validate or update form state
*
* @example
* ```tsx
* // Update a single field
* setValue('name', 'value', {
* shouldValidate: true, // trigger validation
* shouldTouch: true, // update touched fields form state
* shouldDirty: true, // update dirty and dirty fields form state
* });
*
* // Update a group fields
* setValue('root', {
* a: 'test', // setValue('root.a', 'data')
* b: 'test1', // setValue('root.b', 'data')
* });
*
* // Update a nested object field
* setValue('select', { label: 'test', value: 'Test' });
* ```
*/
export type UseFormSetValue<TFieldValues extends FieldValues> = <
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>(
name: TFieldName,
value: FieldPathValue<TFieldValues, TFieldName>,
options?: SetValueConfig,
) => void;
/**
* Set an error for the field. When set an error which is not associated to a field then manual `clearErrors` invoke is required.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/seterror) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-seterror-nfxxu) • [Video](https://www.youtube.com/watch?v=raMqvE0YyIY)
*
* @param name - the path name to the form field value.
* @param error - an error object which contains type and optional message
* @param options - whether or not to focus on the field
*
* @example
* ```tsx
* // when the error is not associated with any fields, `clearError` will need to invoke to clear the error
* const onSubmit = () => setError("serverError", { type: "server", message: "Error occurred"})
*
* <button onClick={() => setError("name", { type: "min" })} />
*
* // focus on the input after setting the error
* <button onClick={() => setError("name", { type: "max" }, { shouldFocus: true })} />
* ```
*/
export type UseFormSetError<TFieldValues extends FieldValues> = (
name: FieldPath<TFieldValues>,
error: ErrorOption,
options?: {
shouldFocus: boolean;
},
) => void;
/**
* Unregister a field reference and remove its value.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/unregister) • [Demo](https://codesandbox.io/s/react-hook-form-unregister-4k2ey) • [Video](https://www.youtube.com/watch?v=TM99g_NW5Gk&feature=emb_imp_woyt)
*
* @param name - the path name to the form field value.
* @param options - keep form state options
*
* @example
* ```tsx
* register("name", { required: true })
*
* <button onClick={() => unregister("name")} />
* // there are various keep options to retain formState
* <button onClick={() => unregister("name", { keepErrors: true })} />
* ```
*/
export type UseFormUnregister<TFieldValues extends FieldValues> = (
name?:
| FieldPath<TFieldValues>
| FieldPath<TFieldValues>[]
| readonly FieldPath<TFieldValues>[],
options?: Omit<
KeepStateOptions,
| 'keepIsSubmitted'
| 'keepSubmitCount'
| 'keepValues'
| 'keepDefaultValues'
| 'keepErrors'
> & { keepValue?: boolean; keepDefaultValue?: boolean; keepError?: boolean },
) => void;
/**
* Validate the entire form. Handle submit and error callback.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/handlesubmit) • [Demo](https://codesandbox.io/s/react-hook-form-handlesubmit-ts-v7-lcrtu) • [Video](https://www.youtube.com/watch?v=KzcPKB9SOEk)
*
* @param onValid - callback function invoked after form pass validation
* @param onInvalid - callback function invoked when form failed validation
*
* @returns callback - return callback function
*
* @example
* ```tsx
* const onSubmit = (data) => console.log(data);
* const onError = (error) => console.log(error);
*
* <form onSubmit={handleSubmit(onSubmit, onError)} />
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type UseFormHandleSubmit<TFieldValues extends FieldValues> = (
onValid: SubmitHandler<TFieldValues>,
onInvalid?: SubmitErrorHandler<TFieldValues>,
) => (e?: React.BaseSyntheticEvent) => Promise<void>;
/**
* Reset a field state and reference.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/resetfield) • [Demo](https://codesandbox.io/s/priceless-firefly-d0kuv) • [Video](https://www.youtube.com/watch?v=IdLFcNaEFEo)
*
* @param name - the path name to the form field value.
* @param options - keep form state options
*
* @example
* ```tsx
* <input {...register("firstName", { required: true })} />
* <button type="button" onClick={() => resetField("firstName"))}>Reset</button>
* ```
*/
export type UseFormResetField<TFieldValues extends FieldValues> = <
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>(
name: TFieldName,
options?: Partial<{
keepDirty: boolean;
keepTouched: boolean;
keepError: boolean;
defaultValue: any;
}>,
) => void;
type ResetAction<TFieldValues> = (formValues: TFieldValues) => TFieldValues;
/**
* Reset at the entire form state.
*
* @remarks
* [API](https://react-hook-form.com/api/useform/reset) • [Demo](https://codesandbox.io/s/react-hook-form-reset-v7-ts-pu901) • [Video](https://www.youtube.com/watch?v=qmCLBjyPwVk)
*
* @param values - the entire form values to be reset
* @param keepStateOptions - keep form state options
*
* @example
* ```tsx
* useEffect(() => {
* // reset the entire form after component mount or form defaultValues is ready
* reset({
* fieldA: "test"
* fieldB: "test"
* });
* }, [reset])
*
* // reset by combine with existing form values
* reset({
* ...getValues(),
* fieldB: "test"
*});
*
* // reset and keep form state
* reset({
* ...getValues(),
*}, {
* keepErrors: true,
* keepDirty: true
*});
* ```
*/
export type UseFormReset<TFieldValues extends FieldValues> = (
values?:
| DefaultValues<TFieldValues>
| TFieldValues
| ResetAction<TFieldValues>,
keepStateOptions?: KeepStateOptions,
) => void;
export type WatchInternal<TFieldValues> = (
fieldNames?: InternalFieldName | InternalFieldName[],
defaultValue?: DeepPartial<TFieldValues>,
isMounted?: boolean,
isGlobal?: boolean,
) =>
| FieldPathValue<FieldValues, InternalFieldName>
| FieldPathValues<FieldValues, InternalFieldName[]>;
export type GetIsDirty = <TName extends InternalFieldName, TData>(
name?: TName,
data?: TData,
) => boolean;
export type FormStateSubjectRef<TFieldValues extends FieldValues> = Subject<
Partial<FormState<TFieldValues>> & { name?: InternalFieldName }
>;
export type Subjects<TFieldValues extends FieldValues = FieldValues> = {
watch: Subject<{
name?: InternalFieldName;
type?: EventType;
values?: FieldValues;
}>;
array: Subject<{
name?: InternalFieldName;
values?: FieldValues;
}>;
state: FormStateSubjectRef<TFieldValues>;
};
export type Names = {
mount: InternalNameSet;
unMount: InternalNameSet;
array: InternalNameSet;
watch: InternalNameSet;
focus?: InternalFieldName;
watchAll?: boolean;
};
export type BatchFieldArrayUpdate = <
T extends Function,
TFieldValues extends FieldValues,
TFieldArrayName extends FieldArrayPath<TFieldValues> = FieldArrayPath<TFieldValues>,
>(
name: InternalFieldName,
updatedFieldArrayValues?: Partial<
FieldArray<TFieldValues, TFieldArrayName>
>[],
method?: T,
args?: Partial<{
argA: unknown;
argB: unknown;
}>,
shouldSetValue?: boolean,
shouldUpdateFieldsAndErrors?: boolean,
) => void;
export type Control<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
> = {
_subjects: Subjects<TFieldValues>;
_removeUnmounted: Noop;
_focusError: Noop;
_names: Names;
_stateFlags: {
mount: boolean;
action: boolean;
watch: boolean;
};
_options: UseFormProps<TFieldValues, TContext>;
_getDirty: GetIsDirty;
_formState: FormState<TFieldValues>;
_updateValid: Noop;
_fields: FieldRefs;
_formValues: FieldValues;
_proxyFormState: ReadFormState;
_defaultValues: Partial<DefaultValues<TFieldValues>>;
_getWatch: WatchInternal<TFieldValues>;
_updateFieldArray: BatchFieldArrayUpdate;
_getFieldArray: <TFieldArrayValues>(
name: InternalFieldName,
) => Partial<TFieldArrayValues>[];
_executeSchema: (
names: InternalFieldName[],
) => Promise<{ errors: FieldErrors }>;
register: UseFormRegister<TFieldValues>;
unregister: UseFormUnregister<TFieldValues>;
getFieldState: UseFormGetFieldState<TFieldValues>;
};
export type WatchObserver<TFieldValues extends FieldValues> = (
value: DeepPartial<TFieldValues>,
info: {
name?: FieldPath<TFieldValues>;
type?: EventType;
},
) => void;
export type UseFormReturn<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
> = {
watch: UseFormWatch<TFieldValues>;
getValues: UseFormGetValues<TFieldValues>;
getFieldState: UseFormGetFieldState<TFieldValues>;
setError: UseFormSetError<TFieldValues>;
clearErrors: UseFormClearErrors<TFieldValues>;
setValue: UseFormSetValue<TFieldValues>;
trigger: UseFormTrigger<TFieldValues>;
formState: FormState<TFieldValues>;
resetField: UseFormResetField<TFieldValues>;
reset: UseFormReset<TFieldValues>;
handleSubmit: UseFormHandleSubmit<TFieldValues>;
unregister: UseFormUnregister<TFieldValues>;
control: Control<TFieldValues, TContext>;
register: UseFormRegister<TFieldValues>;
setFocus: UseFormSetFocus<TFieldValues>;
};
export type UseFormStateProps<TFieldValues extends FieldValues> = Partial<{
control?: Control<TFieldValues>;
disabled?: boolean;
name?:
| FieldPath<TFieldValues>
| FieldPath<TFieldValues>[]
| readonly FieldPath<TFieldValues>[];
exact?: boolean;
}>;
export type UseFormStateReturn<TFieldValues extends FieldValues> =
FormState<TFieldValues>;
export type UseWatchProps<TFieldValues extends FieldValues = FieldValues> = {
defaultValue?: unknown;
disabled?: boolean;
name?:
| FieldPath<TFieldValues>
| FieldPath<TFieldValues>[]
| readonly FieldPath<TFieldValues>[];
control?: Control<TFieldValues>;
exact?: boolean;
};
export type FormProviderProps<
TFieldValues extends FieldValues = FieldValues,
TContext = any,
> = {
children: React.ReactNode | React.ReactNode[];
} & UseFormReturn<TFieldValues, TContext>;