react-hook-form 7.44.0-rc.0 → 7.44.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +6 -0
  2. package/dist/__tests__/controller.server.test.d.ts +1 -1
  3. package/dist/__tests__/controller.test.d.ts +1 -1
  4. package/dist/__tests__/form.test.d.ts +1 -1
  5. package/dist/__tests__/logic/validateField.test.d.ts +1 -1
  6. package/dist/__tests__/type.test.d.ts +1 -1
  7. package/dist/__tests__/useController.test.d.ts +1 -1
  8. package/dist/__tests__/useFieldArray/append.test.d.ts +1 -1
  9. package/dist/__tests__/useFieldArray/focus.test.d.ts +1 -1
  10. package/dist/__tests__/useFieldArray/insert.test.d.ts +1 -1
  11. package/dist/__tests__/useFieldArray/move.test.d.ts +1 -1
  12. package/dist/__tests__/useFieldArray/prepend.test.d.ts +1 -1
  13. package/dist/__tests__/useFieldArray/remove.test.d.ts +1 -1
  14. package/dist/__tests__/useFieldArray/replace.test.d.ts +1 -1
  15. package/dist/__tests__/useFieldArray/swap.test.d.ts +1 -1
  16. package/dist/__tests__/useFieldArray/update.test.d.ts +1 -1
  17. package/dist/__tests__/useFieldArray.test.d.ts +1 -1
  18. package/dist/__tests__/useForm/clearErrors.test.d.ts +1 -1
  19. package/dist/__tests__/useForm/formState.test.d.ts +1 -1
  20. package/dist/__tests__/useForm/getFieldState.test.d.ts +1 -1
  21. package/dist/__tests__/useForm/getValues.test.d.ts +1 -1
  22. package/dist/__tests__/useForm/handleSubmit.test.d.ts +1 -1
  23. package/dist/__tests__/useForm/register.test.d.ts +1 -1
  24. package/dist/__tests__/useForm/reset.test.d.ts +1 -1
  25. package/dist/__tests__/useForm/resetField.test.d.ts +1 -1
  26. package/dist/__tests__/useForm/resolver.test.d.ts +1 -1
  27. package/dist/__tests__/useForm/setError.test.d.ts +1 -1
  28. package/dist/__tests__/useForm/setValue.test.d.ts +1 -1
  29. package/dist/__tests__/useForm/trigger.test.d.ts +1 -1
  30. package/dist/__tests__/useForm/unregister.test.d.ts +1 -1
  31. package/dist/__tests__/useForm/watch.test.d.ts +1 -1
  32. package/dist/__tests__/useForm.server.test.d.ts +1 -1
  33. package/dist/__tests__/useForm.test.d.ts +1 -1
  34. package/dist/__tests__/useFormContext.server.test.d.ts +1 -1
  35. package/dist/__tests__/useFormContext.test.d.ts +1 -1
  36. package/dist/__tests__/useFormState.test.d.ts +1 -1
  37. package/dist/__tests__/useWatch.test.d.ts +1 -1
  38. package/dist/__typetest__/__fixtures__/index.d.ts +4 -4
  39. package/dist/__typetest__/__fixtures__/pathString.d.ts +3 -3
  40. package/dist/__typetest__/__fixtures__/traversable.d.ts +13 -13
  41. package/dist/__typetest__/__fixtures__/tuple.d.ts +14 -14
  42. package/dist/__typetest__/__fixtures__/type.d.ts +11 -11
  43. package/dist/__typetest__/errors.test-d.d.ts +1 -1
  44. package/dist/__typetest__/form.test-d.d.ts +1 -1
  45. package/dist/__typetest__/path/common.test-d.d.ts +1 -1
  46. package/dist/__typetest__/path/eager.test-d.d.ts +1 -1
  47. package/dist/__typetest__/util.test-d.d.ts +1 -1
  48. package/dist/constants.d.ts +16 -16
  49. package/dist/controller.d.ts +46 -46
  50. package/dist/controller.d.ts.map +1 -1
  51. package/dist/form.d.ts +59 -59
  52. package/dist/form.d.ts.map +1 -1
  53. package/dist/index.cjs.js +1 -1
  54. package/dist/index.cjs.js.map +1 -1
  55. package/dist/index.d.ts +11 -11
  56. package/dist/index.esm.mjs +2269 -2257
  57. package/dist/index.esm.mjs.map +1 -1
  58. package/dist/index.umd.js +1 -1
  59. package/dist/index.umd.js.map +1 -1
  60. package/dist/logic/appendErrors.d.ts +3 -3
  61. package/dist/logic/createFormControl.d.ts +26 -26
  62. package/dist/logic/createFormControl.d.ts.map +1 -1
  63. package/dist/logic/focusFieldBy.d.ts +3 -3
  64. package/dist/logic/generateId.d.ts +2 -2
  65. package/dist/logic/generateWatchOutput.d.ts +3 -3
  66. package/dist/logic/getCheckboxValue.d.ts +6 -6
  67. package/dist/logic/getDirtyFields.d.ts +2 -2
  68. package/dist/logic/getEventValue.d.ts +2 -2
  69. package/dist/logic/getFieldValue.d.ts +2 -2
  70. package/dist/logic/getFieldValueAs.d.ts +3 -3
  71. package/dist/logic/getFocusFieldName.d.ts +3 -3
  72. package/dist/logic/getNodeParentName.d.ts +2 -2
  73. package/dist/logic/getProxyFormState.d.ts +3 -3
  74. package/dist/logic/getRadioValue.d.ts +6 -6
  75. package/dist/logic/getResolverOptions.d.ts +13 -13
  76. package/dist/logic/getRuleValue.d.ts +3 -3
  77. package/dist/logic/getValidateError.d.ts +2 -2
  78. package/dist/logic/getValidationModes.d.ts +9 -9
  79. package/dist/logic/getValueAndMessage.d.ts +6 -6
  80. package/dist/logic/hasValidation.d.ts +3 -3
  81. package/dist/logic/index.d.ts +2 -2
  82. package/dist/logic/isNameInFieldArray.d.ts +3 -3
  83. package/dist/logic/isWatched.d.ts +3 -3
  84. package/dist/logic/schemaErrorLookup.d.ts +5 -5
  85. package/dist/logic/shouldRenderFormState.d.ts +5 -5
  86. package/dist/logic/shouldSubscribeByName.d.ts +2 -2
  87. package/dist/logic/skipValidation.d.ts +11 -11
  88. package/dist/logic/unsetEmptyArray.d.ts +2 -2
  89. package/dist/logic/updateFieldArrayRootError.d.ts +3 -3
  90. package/dist/logic/validateField.d.ts +3 -3
  91. package/dist/types/controller.d.ts +56 -56
  92. package/dist/types/errors.d.ts +35 -35
  93. package/dist/types/events.d.ts +1 -1
  94. package/dist/types/fieldArray.d.ts +194 -194
  95. package/dist/types/fields.d.ts +28 -28
  96. package/dist/types/form.d.ts +631 -630
  97. package/dist/types/form.d.ts.map +1 -1
  98. package/dist/types/index.d.ts +10 -10
  99. package/dist/types/path/common.d.ts +315 -315
  100. package/dist/types/path/eager.d.ts +120 -120
  101. package/dist/types/path/index.d.ts +3 -3
  102. package/dist/types/resolvers.d.ts +19 -19
  103. package/dist/types/utils.d.ts +70 -70
  104. package/dist/types/validator.d.ts +38 -38
  105. package/dist/types/validator.d.ts.map +1 -1
  106. package/dist/useController.d.ts +26 -26
  107. package/dist/useController.d.ts.map +1 -1
  108. package/dist/useFieldArray.d.ts +39 -39
  109. package/dist/useFieldArray.d.ts.map +1 -1
  110. package/dist/useForm.d.ts +31 -31
  111. package/dist/useForm.d.ts.map +1 -1
  112. package/dist/useFormContext.d.ts +64 -64
  113. package/dist/useFormContext.d.ts.map +1 -1
  114. package/dist/useFormState.d.ts +33 -33
  115. package/dist/useFormState.d.ts.map +1 -1
  116. package/dist/useSubscribe.d.ts +8 -8
  117. package/dist/useWatch.d.ts +99 -99
  118. package/dist/utils/append.d.ts +1 -1
  119. package/dist/utils/cloneObject.d.ts +1 -1
  120. package/dist/utils/cloneObject.d.ts.map +1 -1
  121. package/dist/utils/compact.d.ts +2 -2
  122. package/dist/utils/convertToArrayPayload.d.ts +2 -2
  123. package/dist/utils/createSubject.d.ts +13 -13
  124. package/dist/utils/deepEqual.d.ts +1 -1
  125. package/dist/utils/deepMerge.d.ts +1 -1
  126. package/dist/utils/fillBooleanArray.d.ts +2 -2
  127. package/dist/utils/fillEmptyArray.d.ts +2 -2
  128. package/dist/utils/get.d.ts +2 -2
  129. package/dist/utils/index.d.ts +3 -3
  130. package/dist/utils/insert.d.ts +2 -2
  131. package/dist/utils/isBoolean.d.ts +2 -2
  132. package/dist/utils/isCheckBoxInput.d.ts +3 -3
  133. package/dist/utils/isDateObject.d.ts +2 -2
  134. package/dist/utils/isEmptyObject.d.ts +3 -3
  135. package/dist/utils/isFileInput.d.ts +3 -3
  136. package/dist/utils/isFunction.d.ts +2 -2
  137. package/dist/utils/isHTMLElement.d.ts +2 -2
  138. package/dist/utils/isKey.d.ts +2 -2
  139. package/dist/utils/isMessage.d.ts +2 -2
  140. package/dist/utils/isMultipleSelect.d.ts +3 -3
  141. package/dist/utils/isNullOrUndefined.d.ts +2 -2
  142. package/dist/utils/isObject.d.ts +3 -3
  143. package/dist/utils/isPlainObject.d.ts +2 -2
  144. package/dist/utils/isPrimitive.d.ts +3 -3
  145. package/dist/utils/isRadioInput.d.ts +3 -3
  146. package/dist/utils/isRadioOrCheckbox.d.ts +3 -3
  147. package/dist/utils/isRegex.d.ts +2 -2
  148. package/dist/utils/isSelectInput.d.ts +3 -3
  149. package/dist/utils/isString.d.ts +2 -2
  150. package/dist/utils/isUndefined.d.ts +2 -2
  151. package/dist/utils/isWeb.d.ts +2 -2
  152. package/dist/utils/live.d.ts +3 -3
  153. package/dist/utils/move.d.ts +2 -2
  154. package/dist/utils/objectHasFunction.d.ts +2 -2
  155. package/dist/utils/omit.d.ts +2 -2
  156. package/dist/utils/prepend.d.ts +1 -1
  157. package/dist/utils/remove.d.ts +2 -2
  158. package/dist/utils/set.d.ts +2 -2
  159. package/dist/utils/sleep.d.ts +1 -1
  160. package/dist/utils/stringToPath.d.ts +2 -2
  161. package/dist/utils/swap.d.ts +2 -2
  162. package/dist/utils/unset.d.ts +1 -1
  163. package/dist/utils/update.d.ts +2 -2
  164. package/package.json +9 -9
@@ -1,631 +1,632 @@
1
- import React from 'react';
2
- import { Subject, Subscription } from '../utils/createSubject';
3
- import { ErrorOption, FieldError, FieldErrors } from './errors';
4
- import { EventType } from './events';
5
- import { FieldArray } from './fieldArray';
6
- import { FieldRefs, FieldValue, FieldValues, InternalFieldName } from './fields';
7
- import { FieldArrayPath, FieldPath, FieldPathValue, FieldPathValues } from './path';
8
- import { Resolver } from './resolvers';
9
- import { DeepMap, DeepPartial, Noop } from './utils';
10
- import { RegisterOptions } from './validator';
11
- declare const $NestedValue: unique symbol;
12
- /**
13
- * @deprecated to be removed in the next major version
14
- */
15
- export type NestedValue<TValue extends object = object> = {
16
- [$NestedValue]: never;
17
- } & TValue;
18
- /**
19
- * @deprecated to be removed in the next major version
20
- */
21
- export type UnpackNestedValue<T> = T extends NestedValue<infer U> ? U : T extends Date | FileList | File | Blob ? T : T extends object ? {
22
- [K in keyof T]: UnpackNestedValue<T[K]>;
23
- } : T;
24
- export type DefaultValues<TFieldValues> = DeepPartial<TFieldValues>;
25
- export type InternalNameSet = Set<InternalFieldName>;
26
- export type ValidationMode = {
27
- onBlur: 'onBlur';
28
- onChange: 'onChange';
29
- onSubmit: 'onSubmit';
30
- onTouched: 'onTouched';
31
- all: 'all';
32
- };
33
- export type Mode = keyof ValidationMode;
34
- export type CriteriaMode = 'firstError' | 'all';
35
- export type SubmitHandler<TFieldValues extends FieldValues> = (data: TFieldValues, event?: React.BaseSyntheticEvent) => unknown | Promise<unknown>;
36
- export type SubmitErrorHandler<TFieldValues extends FieldValues> = (errors: FieldErrors<TFieldValues>, event?: React.BaseSyntheticEvent) => unknown | Promise<unknown>;
37
- export type SetValueConfig = Partial<{
38
- shouldValidate: boolean;
39
- shouldDirty: boolean;
40
- shouldTouch: boolean;
41
- }>;
42
- export type TriggerConfig = Partial<{
43
- shouldFocus: boolean;
44
- }>;
45
- export type ChangeHandler = (event: {
46
- target: any;
47
- type?: any;
48
- }) => Promise<void | boolean>;
49
- export type DelayCallback = (wait: number) => void;
50
- type AsyncDefaultValues<TFieldValues> = (payload?: unknown) => Promise<TFieldValues>;
51
- export type UseFormProps<TFieldValues extends FieldValues = FieldValues, TContext = any> = Partial<{
52
- mode: Mode;
53
- reValidateMode: Exclude<Mode, 'onTouched' | 'all'>;
54
- defaultValues: DefaultValues<TFieldValues> | AsyncDefaultValues<TFieldValues>;
55
- values: TFieldValues;
56
- resetOptions: Parameters<UseFormReset<TFieldValues>>[1];
57
- resolver: Resolver<TFieldValues, TContext>;
58
- context: TContext;
59
- shouldFocusError: boolean;
60
- shouldUnregister: boolean;
61
- shouldUseNativeValidation: boolean;
62
- progressive: boolean;
63
- criteriaMode: CriteriaMode;
64
- delayError: number;
65
- }>;
66
- export type FieldNamesMarkedBoolean<TFieldValues extends FieldValues> = DeepMap<DeepPartial<TFieldValues>, boolean>;
67
- export type FormStateProxy<TFieldValues extends FieldValues = FieldValues> = {
68
- isDirty: boolean;
69
- isValidating: boolean;
70
- dirtyFields: FieldNamesMarkedBoolean<TFieldValues>;
71
- touchedFields: FieldNamesMarkedBoolean<TFieldValues>;
72
- errors: boolean;
73
- isValid: boolean;
74
- };
75
- export type ReadFormState = {
76
- [K in keyof FormStateProxy]: boolean | 'all';
77
- };
78
- export type FormState<TFieldValues extends FieldValues> = {
79
- isDirty: boolean;
80
- isLoading: boolean;
81
- isSubmitted: boolean;
82
- isSubmitSuccessful: boolean;
83
- isSubmitting: boolean;
84
- isValidating: boolean;
85
- isValid: boolean;
86
- submitCount: number;
87
- defaultValues?: undefined | Readonly<DeepPartial<TFieldValues>>;
88
- dirtyFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
89
- touchedFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
90
- errors: FieldErrors<TFieldValues>;
91
- };
92
- export type KeepStateOptions = Partial<{
93
- keepDirtyValues: boolean;
94
- keepErrors: boolean;
95
- keepDirty: boolean;
96
- keepValues: boolean;
97
- keepDefaultValues: boolean;
98
- keepIsSubmitted: boolean;
99
- keepTouched: boolean;
100
- keepIsValid: boolean;
101
- keepSubmitCount: boolean;
102
- }>;
103
- export type SetFieldValue<TFieldValues extends FieldValues> = FieldValue<TFieldValues>;
104
- export type RefCallBack = (instance: any) => void;
105
- export type UseFormRegisterReturn<TFieldName extends InternalFieldName = InternalFieldName> = {
106
- onChange: ChangeHandler;
107
- onBlur: ChangeHandler;
108
- ref: RefCallBack;
109
- name: TFieldName;
110
- min?: string | number;
111
- max?: string | number;
112
- maxLength?: number;
113
- minLength?: number;
114
- pattern?: string;
115
- required?: boolean;
116
- disabled?: boolean;
117
- };
118
- /**
119
- * Register field into hook form with or without the actual DOM ref. You can invoke register anywhere in the component including at `useEffect`.
120
- *
121
- * @remarks
122
- * [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)
123
- *
124
- * @param name - the path name to the form field value, name is required and unique
125
- * @param options - register options include validation, disabled, unregister, value as and dependent validation
126
- *
127
- * @returns onChange, onBlur, name, ref, and native contribute attribute if browser validation is enabled.
128
- *
129
- * @example
130
- * ```tsx
131
- * // Register HTML native input
132
- * <input {...register("input")} />
133
- * <select {...register("select")} />
134
- *
135
- * // Register options
136
- * <textarea {...register("textarea", { required: "This is required.", maxLength: 20 })} />
137
- * <input type="number" {...register("name2", { valueAsNumber: true })} />
138
- * <input {...register("name3", { deps: ["name2"] })} />
139
- *
140
- * // Register custom field at useEffect
141
- * useEffect(() => {
142
- * register("name4");
143
- * register("name5", { value: '"hiddenValue" });
144
- * }, [register])
145
- *
146
- * // Register without ref
147
- * const { onChange, onBlur, name } = register("name6")
148
- * <input onChange={onChange} onBlur={onBlur} name={name} />
149
- * ```
150
- */
151
- export type UseFormRegister<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, options?: RegisterOptions<TFieldValues, TFieldName>) => UseFormRegisterReturn<TFieldName>;
152
- export type SetFocusOptions = Partial<{
153
- shouldSelect: boolean;
154
- }>;
155
- /**
156
- * Set focus on a registered field. You can start to invoke this method after all fields are mounted to the DOM.
157
- *
158
- * @remarks
159
- * [API](https://react-hook-form.com/api/useform/setfocus) • [Demo](https://codesandbox.io/s/setfocus-rolus)
160
- *
161
- * @param name - the path name to the form field value.
162
- * @param options - input focus behavior options
163
- *
164
- * @example
165
- * ```tsx
166
- * useEffect(() => {
167
- * setFocus("name");
168
- * }, [setFocus])
169
- * // shouldSelect allows to select input's content on focus
170
- * <button onClick={() => setFocus("name", { shouldSelect: true })}>Focus</button>
171
- * ```
172
- */
173
- export type UseFormSetFocus<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, options?: SetFocusOptions) => void;
174
- export type UseFormGetValues<TFieldValues extends FieldValues> = {
175
- /**
176
- * Get the entire form values when no argument is supplied to this function.
177
- *
178
- * @remarks
179
- * [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
180
- *
181
- * @returns form values
182
- *
183
- * @example
184
- * ```tsx
185
- * <button onClick={() => getValues()}>getValues</button>
186
- *
187
- * <input {...register("name", {
188
- * validate: (value, formValues) => formValues.otherField === value;
189
- * })} />
190
- * ```
191
- */
192
- (): TFieldValues;
193
- /**
194
- * Get a single field value.
195
- *
196
- * @remarks
197
- * [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
198
- *
199
- * @param name - the path name to the form field value.
200
- *
201
- * @returns the single field value
202
- *
203
- * @example
204
- * ```tsx
205
- * <button onClick={() => getValues("name")}>getValues</button>
206
- *
207
- * <input {...register("name", {
208
- * validate: () => getValues('otherField') === "test";
209
- * })} />
210
- * ```
211
- */
212
- <TFieldName extends FieldPath<TFieldValues>>(name: TFieldName): FieldPathValue<TFieldValues, TFieldName>;
213
- /**
214
- * Get an array of field values.
215
- *
216
- * @remarks
217
- * [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
218
- *
219
- * @param names - an array of field names
220
- *
221
- * @returns An array of field values
222
- *
223
- * @example
224
- * ```tsx
225
- * <button onClick={() => getValues(["name", "name1"])}>getValues</button>
226
- *
227
- * <input {...register("name", {
228
- * validate: () => getValues(["fieldA", "fieldB"]).includes("test");
229
- * })} />
230
- * ```
231
- */
232
- <TFieldNames extends FieldPath<TFieldValues>[]>(names: readonly [...TFieldNames]): [...FieldPathValues<TFieldValues, TFieldNames>];
233
- };
234
- /**
235
- * 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.
236
- *
237
- * @remarks
238
- * [API](https://react-hook-form.com/api/useform/getfieldstate) • [Demo](https://codesandbox.io/s/getfieldstate-jvekk)
239
- *
240
- * @param name - the path name to the form field value.
241
- *
242
- * @returns invalid, isDirty, isTouched and error object
243
- *
244
- * @example
245
- * ```tsx
246
- * // those formState has to be subscribed
247
- * const { formState: { dirtyFields, errors, touchedFields } } = formState();
248
- * getFieldState('name')
249
- * // Get field state when form state is not subscribed yet
250
- * getFieldState('name', formState)
251
- *
252
- * // It's ok to combine with useFormState
253
- * const formState = useFormState();
254
- * getFieldState('name')
255
- * getFieldState('name', formState)
256
- * ```
257
- */
258
- export type UseFormGetFieldState<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues>>(name: TFieldName, formState?: FormState<TFieldValues>) => {
259
- invalid: boolean;
260
- isDirty: boolean;
261
- isTouched: boolean;
262
- error?: FieldError;
263
- };
264
- export type UseFormWatch<TFieldValues extends FieldValues> = {
265
- /**
266
- * Watch and subscribe to the entire form update/change based on onChange and re-render at the useForm.
267
- *
268
- * @remarks
269
- * [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)
270
- *
271
- * @returns return the entire form values
272
- *
273
- * @example
274
- * ```tsx
275
- * const formValues = watch();
276
- * ```
277
- */
278
- (): TFieldValues;
279
- /**
280
- * Watch and subscribe to an array of fields used outside of render.
281
- *
282
- * @remarks
283
- * [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)
284
- *
285
- * @param names - an array of field names
286
- * @param defaultValue - defaultValues for the entire form
287
- *
288
- * @returns return an array of field values
289
- *
290
- * @example
291
- * ```tsx
292
- * const [name, name1] = watch(["name", "name1"]);
293
- * ```
294
- */
295
- <TFieldNames extends readonly FieldPath<TFieldValues>[]>(names: readonly [...TFieldNames], defaultValue?: DeepPartial<TFieldValues>): FieldPathValues<TFieldValues, TFieldNames>;
296
- /**
297
- * Watch and subscribe to a single field used outside of render.
298
- *
299
- * @remarks
300
- * [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)
301
- *
302
- * @param name - the path name to the form field value.
303
- * @param defaultValue - defaultValues for the entire form
304
- *
305
- * @returns return the single field value
306
- *
307
- * @example
308
- * ```tsx
309
- * const name = watch("name");
310
- * ```
311
- */
312
- <TFieldName extends FieldPath<TFieldValues>>(name: TFieldName, defaultValue?: FieldPathValue<TFieldValues, TFieldName>): FieldPathValue<TFieldValues, TFieldName>;
313
- /**
314
- * Subscribe to field update/change without trigger re-render
315
- *
316
- * @remarks
317
- * [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)
318
- *
319
- * @param callback - call back function to subscribe all fields change and return unsubscribe function
320
- * @param defaultValues - defaultValues for the entire form
321
- *
322
- * @returns unsubscribe function
323
- *
324
- * @example
325
- * ```tsx
326
- * useEffect(() => {
327
- * const { unsubscribe } = watch((value) => {
328
- * console.log(value);
329
- * });
330
- * return () => unsubscribe();
331
- * }, [watch])
332
- * ```
333
- */
334
- (callback: WatchObserver<TFieldValues>, defaultValues?: DeepPartial<TFieldValues>): Subscription;
335
- };
336
- /**
337
- * Trigger field or form validation
338
- *
339
- * @remarks
340
- * [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)
341
- *
342
- * @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.
343
- * @param options - should focus on the error field
344
- *
345
- * @returns validation result
346
- *
347
- * @example
348
- * ```tsx
349
- * useEffect(() => {
350
- * trigger();
351
- * }, [trigger])
352
- *
353
- * <button onClick={async () => {
354
- * const result = await trigger(); // result will be a boolean value
355
- * }}>
356
- * trigger
357
- * </button>
358
- * ```
359
- */
360
- export type UseFormTrigger<TFieldValues extends FieldValues> = (name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[], options?: TriggerConfig) => Promise<boolean>;
361
- /**
362
- * Clear the entire form errors.
363
- *
364
- * @remarks
365
- * [API](https://react-hook-form.com/api/useform/clearerrors) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-clearerrors-w3ymx)
366
- *
367
- * @param name - the path name to the form field value.
368
- *
369
- * @example
370
- * Clear all errors
371
- * ```tsx
372
- * clearErrors(); // clear the entire form error
373
- * clearErrors(["name", "name1"]) // clear an array of fields' error
374
- * clearErrors("name2"); // clear a single field error
375
- * ```
376
- */
377
- export type UseFormClearErrors<TFieldValues extends FieldValues> = (name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[] | `root.${string}` | 'root') => void;
378
- /**
379
- * Set a single field value, or a group of fields value.
380
- *
381
- * @remarks
382
- * [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)
383
- *
384
- * @param name - the path name to the form field value.
385
- * @param value - field value
386
- * @param options - should validate or update form state
387
- *
388
- * @example
389
- * ```tsx
390
- * // Update a single field
391
- * setValue('name', 'value', {
392
- * shouldValidate: true, // trigger validation
393
- * shouldTouch: true, // update touched fields form state
394
- * shouldDirty: true, // update dirty and dirty fields form state
395
- * });
396
- *
397
- * // Update a group fields
398
- * setValue('root', {
399
- * a: 'test', // setValue('root.a', 'data')
400
- * b: 'test1', // setValue('root.b', 'data')
401
- * });
402
- *
403
- * // Update a nested object field
404
- * setValue('select', { label: 'test', value: 'Test' });
405
- * ```
406
- */
407
- export type UseFormSetValue<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, value: FieldPathValue<TFieldValues, TFieldName>, options?: SetValueConfig) => void;
408
- /**
409
- * Set an error for the field. When set an error which is not associated to a field then manual `clearErrors` invoke is required.
410
- *
411
- * @remarks
412
- * [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)
413
- *
414
- * @param name - the path name to the form field value.
415
- * @param error - an error object which contains type and optional message
416
- * @param options - whether or not to focus on the field
417
- *
418
- * @example
419
- * ```tsx
420
- * // when the error is not associated with any fields, `clearError` will need to invoke to clear the error
421
- * const onSubmit = () => setError("serverError", { type: "server", message: "Error occurred"})
422
- *
423
- * <button onClick={() => setError("name", { type: "min" })} />
424
- *
425
- * // focus on the input after setting the error
426
- * <button onClick={() => setError("name", { type: "max" }, { shouldFocus: true })} />
427
- * ```
428
- */
429
- export type UseFormSetError<TFieldValues extends FieldValues> = (name: FieldPath<TFieldValues> | `root.${string}` | 'root', error: ErrorOption, options?: {
430
- shouldFocus: boolean;
431
- }) => void;
432
- /**
433
- * Unregister a field reference and remove its value.
434
- *
435
- * @remarks
436
- * [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)
437
- *
438
- * @param name - the path name to the form field value.
439
- * @param options - keep form state options
440
- *
441
- * @example
442
- * ```tsx
443
- * register("name", { required: true })
444
- *
445
- * <button onClick={() => unregister("name")} />
446
- * // there are various keep options to retain formState
447
- * <button onClick={() => unregister("name", { keepErrors: true })} />
448
- * ```
449
- */
450
- export type UseFormUnregister<TFieldValues extends FieldValues> = (name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[], options?: Omit<KeepStateOptions, 'keepIsSubmitted' | 'keepSubmitCount' | 'keepValues' | 'keepDefaultValues' | 'keepErrors'> & {
451
- keepValue?: boolean;
452
- keepDefaultValue?: boolean;
453
- keepError?: boolean;
454
- }) => void;
455
- /**
456
- * Validate the entire form. Handle submit and error callback.
457
- *
458
- * @remarks
459
- * [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)
460
- *
461
- * @param onValid - callback function invoked after form pass validation
462
- * @param onInvalid - callback function invoked when form failed validation
463
- *
464
- * @returns callback - return callback function
465
- *
466
- * @example
467
- * ```tsx
468
- * const onSubmit = (data) => console.log(data);
469
- * const onError = (error) => console.log(error);
470
- *
471
- * <form onSubmit={handleSubmit(onSubmit, onError)} />
472
- * ```
473
- */
474
- export type UseFormHandleSubmit<TFieldValues extends FieldValues, TTransformedValues extends FieldValues | undefined = undefined> = (onValid: TTransformedValues extends FieldValues ? SubmitHandler<TTransformedValues> : SubmitHandler<TFieldValues>, onInvalid?: SubmitErrorHandler<TFieldValues>) => (e?: React.BaseSyntheticEvent) => Promise<void>;
475
- /**
476
- * Reset a field state and reference.
477
- *
478
- * @remarks
479
- * [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)
480
- *
481
- * @param name - the path name to the form field value.
482
- * @param options - keep form state options
483
- *
484
- * @example
485
- * ```tsx
486
- * <input {...register("firstName", { required: true })} />
487
- * <button type="button" onClick={() => resetField("firstName"))}>Reset</button>
488
- * ```
489
- */
490
- export type UseFormResetField<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, options?: Partial<{
491
- keepDirty: boolean;
492
- keepTouched: boolean;
493
- keepError: boolean;
494
- defaultValue: FieldPathValue<TFieldValues, TFieldName>;
495
- }>) => void;
496
- type ResetAction<TFieldValues> = (formValues: TFieldValues) => TFieldValues;
497
- /**
498
- * Reset at the entire form state.
499
- *
500
- * @remarks
501
- * [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)
502
- *
503
- * @param values - the entire form values to be reset
504
- * @param keepStateOptions - keep form state options
505
- *
506
- * @example
507
- * ```tsx
508
- * useEffect(() => {
509
- * // reset the entire form after component mount or form defaultValues is ready
510
- * reset({
511
- * fieldA: "test"
512
- * fieldB: "test"
513
- * });
514
- * }, [reset])
515
- *
516
- * // reset by combine with existing form values
517
- * reset({
518
- * ...getValues(),
519
- * fieldB: "test"
520
- *});
521
- *
522
- * // reset and keep form state
523
- * reset({
524
- * ...getValues(),
525
- *}, {
526
- * keepErrors: true,
527
- * keepDirty: true
528
- *});
529
- * ```
530
- */
531
- export type UseFormReset<TFieldValues extends FieldValues> = (values?: DefaultValues<TFieldValues> | TFieldValues | ResetAction<TFieldValues>, keepStateOptions?: KeepStateOptions) => void;
532
- export type WatchInternal<TFieldValues> = (fieldNames?: InternalFieldName | InternalFieldName[], defaultValue?: DeepPartial<TFieldValues>, isMounted?: boolean, isGlobal?: boolean) => FieldPathValue<FieldValues, InternalFieldName> | FieldPathValues<FieldValues, InternalFieldName[]>;
533
- export type GetIsDirty = <TName extends InternalFieldName, TData>(name?: TName, data?: TData) => boolean;
534
- export type FormStateSubjectRef<TFieldValues extends FieldValues> = Subject<Partial<FormState<TFieldValues>> & {
535
- name?: InternalFieldName;
536
- }>;
537
- export type Subjects<TFieldValues extends FieldValues = FieldValues> = {
538
- values: Subject<{
539
- name?: InternalFieldName;
540
- type?: EventType;
541
- values: FieldValues;
542
- }>;
543
- array: Subject<{
544
- name?: InternalFieldName;
545
- values?: FieldValues;
546
- }>;
547
- state: FormStateSubjectRef<TFieldValues>;
548
- };
549
- export type Names = {
550
- mount: InternalNameSet;
551
- unMount: InternalNameSet;
552
- array: InternalNameSet;
553
- watch: InternalNameSet;
554
- focus?: InternalFieldName;
555
- watchAll?: boolean;
556
- };
557
- 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<{
558
- argA: unknown;
559
- argB: unknown;
560
- }>, shouldSetValue?: boolean, shouldUpdateFieldsAndErrors?: boolean) => void;
561
- export type Control<TFieldValues extends FieldValues = FieldValues, TContext = any> = {
562
- _subjects: Subjects<TFieldValues>;
563
- _removeUnmounted: Noop;
564
- _names: Names;
565
- _state: {
566
- mount: boolean;
567
- action: boolean;
568
- watch: boolean;
569
- };
570
- _reset: UseFormReset<TFieldValues>;
571
- _options: UseFormProps<TFieldValues, TContext>;
572
- _getDirty: GetIsDirty;
573
- _formState: FormState<TFieldValues>;
574
- _updateValid: (shouldUpdateValid?: boolean) => void;
575
- _updateFormState: (formState: Partial<FormState<TFieldValues>>) => void;
576
- _fields: FieldRefs;
577
- _formValues: FieldValues;
578
- _proxyFormState: ReadFormState;
579
- _defaultValues: Partial<DefaultValues<TFieldValues>>;
580
- _getWatch: WatchInternal<TFieldValues>;
581
- _updateFieldArray: BatchFieldArrayUpdate;
582
- _getFieldArray: <TFieldArrayValues>(name: InternalFieldName) => Partial<TFieldArrayValues>[];
583
- _executeSchema: (names: InternalFieldName[]) => Promise<{
584
- errors: FieldErrors;
585
- }>;
586
- register: UseFormRegister<TFieldValues>;
587
- handleSubmit: UseFormHandleSubmit<TFieldValues>;
588
- unregister: UseFormUnregister<TFieldValues>;
589
- getFieldState: UseFormGetFieldState<TFieldValues>;
590
- setError: UseFormSetError<TFieldValues>;
591
- };
592
- export type WatchObserver<TFieldValues extends FieldValues> = (value: DeepPartial<TFieldValues>, info: {
593
- name?: FieldPath<TFieldValues>;
594
- type?: EventType;
595
- }) => void;
596
- export type UseFormReturn<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined> = {
597
- watch: UseFormWatch<TFieldValues>;
598
- getValues: UseFormGetValues<TFieldValues>;
599
- getFieldState: UseFormGetFieldState<TFieldValues>;
600
- setError: UseFormSetError<TFieldValues>;
601
- clearErrors: UseFormClearErrors<TFieldValues>;
602
- setValue: UseFormSetValue<TFieldValues>;
603
- trigger: UseFormTrigger<TFieldValues>;
604
- formState: FormState<TFieldValues>;
605
- resetField: UseFormResetField<TFieldValues>;
606
- reset: UseFormReset<TFieldValues>;
607
- handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues>;
608
- unregister: UseFormUnregister<TFieldValues>;
609
- control: Control<TFieldValues, TContext>;
610
- register: UseFormRegister<TFieldValues>;
611
- setFocus: UseFormSetFocus<TFieldValues>;
612
- };
613
- export type UseFormStateProps<TFieldValues extends FieldValues> = Partial<{
614
- control?: Control<TFieldValues>;
615
- disabled?: boolean;
616
- name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[];
617
- exact?: boolean;
618
- }>;
619
- export type UseFormStateReturn<TFieldValues extends FieldValues> = FormState<TFieldValues>;
620
- export type UseWatchProps<TFieldValues extends FieldValues = FieldValues> = {
621
- defaultValue?: unknown;
622
- disabled?: boolean;
623
- name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[];
624
- control?: Control<TFieldValues>;
625
- exact?: boolean;
626
- };
627
- export type FormProviderProps<TFieldValues extends FieldValues = FieldValues, TContext = any> = {
628
- children: React.ReactNode | React.ReactNode[];
629
- } & UseFormReturn<TFieldValues, TContext>;
630
- export {};
1
+ import React from 'react';
2
+ import { Subject, Subscription } from '../utils/createSubject';
3
+ import { ErrorOption, FieldError, FieldErrors } from './errors';
4
+ import { EventType } from './events';
5
+ import { FieldArray } from './fieldArray';
6
+ import { FieldRefs, FieldValue, FieldValues, InternalFieldName } from './fields';
7
+ import { FieldArrayPath, FieldPath, FieldPathValue, FieldPathValues } from './path';
8
+ import { Resolver } from './resolvers';
9
+ import { DeepMap, DeepPartial, Noop } from './utils';
10
+ import { RegisterOptions } from './validator';
11
+ declare const $NestedValue: unique symbol;
12
+ /**
13
+ * @deprecated to be removed in the next major version
14
+ */
15
+ export type NestedValue<TValue extends object = object> = {
16
+ [$NestedValue]: never;
17
+ } & TValue;
18
+ /**
19
+ * @deprecated to be removed in the next major version
20
+ */
21
+ export type UnpackNestedValue<T> = T extends NestedValue<infer U> ? U : T extends Date | FileList | File | Blob ? T : T extends object ? {
22
+ [K in keyof T]: UnpackNestedValue<T[K]>;
23
+ } : T;
24
+ export type DefaultValues<TFieldValues> = DeepPartial<TFieldValues>;
25
+ export type InternalNameSet = Set<InternalFieldName>;
26
+ export type ValidationMode = {
27
+ onBlur: 'onBlur';
28
+ onChange: 'onChange';
29
+ onSubmit: 'onSubmit';
30
+ onTouched: 'onTouched';
31
+ all: 'all';
32
+ };
33
+ export type Mode = keyof ValidationMode;
34
+ export type CriteriaMode = 'firstError' | 'all';
35
+ export type SubmitHandler<TFieldValues extends FieldValues> = (data: TFieldValues, event?: React.BaseSyntheticEvent) => unknown | Promise<unknown>;
36
+ export type SubmitErrorHandler<TFieldValues extends FieldValues> = (errors: FieldErrors<TFieldValues>, event?: React.BaseSyntheticEvent) => unknown | Promise<unknown>;
37
+ export type SetValueConfig = Partial<{
38
+ shouldValidate: boolean;
39
+ shouldDirty: boolean;
40
+ shouldTouch: boolean;
41
+ }>;
42
+ export type TriggerConfig = Partial<{
43
+ shouldFocus: boolean;
44
+ }>;
45
+ export type ChangeHandler = (event: {
46
+ target: any;
47
+ type?: any;
48
+ }) => Promise<void | boolean>;
49
+ export type DelayCallback = (wait: number) => void;
50
+ type AsyncDefaultValues<TFieldValues> = (payload?: unknown) => Promise<TFieldValues>;
51
+ export type UseFormProps<TFieldValues extends FieldValues = FieldValues, TContext = any> = Partial<{
52
+ mode: Mode;
53
+ reValidateMode: Exclude<Mode, 'onTouched' | 'all'>;
54
+ defaultValues: DefaultValues<TFieldValues> | AsyncDefaultValues<TFieldValues>;
55
+ values: TFieldValues;
56
+ resetOptions: Parameters<UseFormReset<TFieldValues>>[1];
57
+ resolver: Resolver<TFieldValues, TContext>;
58
+ context: TContext;
59
+ shouldFocusError: boolean;
60
+ shouldUnregister: boolean;
61
+ shouldUseNativeValidation: boolean;
62
+ progressive: boolean;
63
+ criteriaMode: CriteriaMode;
64
+ delayError: number;
65
+ }>;
66
+ export type FieldNamesMarkedBoolean<TFieldValues extends FieldValues> = DeepMap<DeepPartial<TFieldValues>, boolean>;
67
+ export type FormStateProxy<TFieldValues extends FieldValues = FieldValues> = {
68
+ isDirty: boolean;
69
+ isValidating: boolean;
70
+ dirtyFields: FieldNamesMarkedBoolean<TFieldValues>;
71
+ touchedFields: FieldNamesMarkedBoolean<TFieldValues>;
72
+ errors: boolean;
73
+ isValid: boolean;
74
+ };
75
+ export type ReadFormState = {
76
+ [K in keyof FormStateProxy]: boolean | 'all';
77
+ };
78
+ export type FormState<TFieldValues extends FieldValues> = {
79
+ isDirty: boolean;
80
+ isLoading: boolean;
81
+ isSubmitted: boolean;
82
+ isSubmitSuccessful: boolean;
83
+ isSubmitting: boolean;
84
+ isValidating: boolean;
85
+ isValid: boolean;
86
+ submitCount: number;
87
+ defaultValues?: undefined | Readonly<DeepPartial<TFieldValues>>;
88
+ dirtyFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
89
+ touchedFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
90
+ errors: FieldErrors<TFieldValues>;
91
+ };
92
+ export type KeepStateOptions = Partial<{
93
+ keepDirtyValues: boolean;
94
+ keepErrors: boolean;
95
+ keepDirty: boolean;
96
+ keepValues: boolean;
97
+ keepDefaultValues: boolean;
98
+ keepIsSubmitted: boolean;
99
+ keepTouched: boolean;
100
+ keepIsValid: boolean;
101
+ keepSubmitCount: boolean;
102
+ }>;
103
+ export type SetFieldValue<TFieldValues extends FieldValues> = FieldValue<TFieldValues>;
104
+ export type RefCallBack = (instance: any) => void;
105
+ export type UseFormRegisterReturn<TFieldName extends InternalFieldName = InternalFieldName> = {
106
+ onChange: ChangeHandler;
107
+ onBlur: ChangeHandler;
108
+ ref: RefCallBack;
109
+ name: TFieldName;
110
+ min?: string | number;
111
+ max?: string | number;
112
+ maxLength?: number;
113
+ minLength?: number;
114
+ pattern?: string;
115
+ required?: boolean;
116
+ disabled?: boolean;
117
+ };
118
+ /**
119
+ * Register field into hook form with or without the actual DOM ref. You can invoke register anywhere in the component including at `useEffect`.
120
+ *
121
+ * @remarks
122
+ * [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)
123
+ *
124
+ * @param name - the path name to the form field value, name is required and unique
125
+ * @param options - register options include validation, disabled, unregister, value as and dependent validation
126
+ *
127
+ * @returns onChange, onBlur, name, ref, and native contribute attribute if browser validation is enabled.
128
+ *
129
+ * @example
130
+ * ```tsx
131
+ * // Register HTML native input
132
+ * <input {...register("input")} />
133
+ * <select {...register("select")} />
134
+ *
135
+ * // Register options
136
+ * <textarea {...register("textarea", { required: "This is required.", maxLength: 20 })} />
137
+ * <input type="number" {...register("name2", { valueAsNumber: true })} />
138
+ * <input {...register("name3", { deps: ["name2"] })} />
139
+ *
140
+ * // Register custom field at useEffect
141
+ * useEffect(() => {
142
+ * register("name4");
143
+ * register("name5", { value: '"hiddenValue" });
144
+ * }, [register])
145
+ *
146
+ * // Register without ref
147
+ * const { onChange, onBlur, name } = register("name6")
148
+ * <input onChange={onChange} onBlur={onBlur} name={name} />
149
+ * ```
150
+ */
151
+ export type UseFormRegister<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, options?: RegisterOptions<TFieldValues, TFieldName>) => UseFormRegisterReturn<TFieldName>;
152
+ export type SetFocusOptions = Partial<{
153
+ shouldSelect: boolean;
154
+ }>;
155
+ /**
156
+ * Set focus on a registered field. You can start to invoke this method after all fields are mounted to the DOM.
157
+ *
158
+ * @remarks
159
+ * [API](https://react-hook-form.com/api/useform/setfocus) • [Demo](https://codesandbox.io/s/setfocus-rolus)
160
+ *
161
+ * @param name - the path name to the form field value.
162
+ * @param options - input focus behavior options
163
+ *
164
+ * @example
165
+ * ```tsx
166
+ * useEffect(() => {
167
+ * setFocus("name");
168
+ * }, [setFocus])
169
+ * // shouldSelect allows to select input's content on focus
170
+ * <button onClick={() => setFocus("name", { shouldSelect: true })}>Focus</button>
171
+ * ```
172
+ */
173
+ export type UseFormSetFocus<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, options?: SetFocusOptions) => void;
174
+ export type UseFormGetValues<TFieldValues extends FieldValues> = {
175
+ /**
176
+ * Get the entire form values when no argument is supplied to this function.
177
+ *
178
+ * @remarks
179
+ * [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
180
+ *
181
+ * @returns form values
182
+ *
183
+ * @example
184
+ * ```tsx
185
+ * <button onClick={() => getValues()}>getValues</button>
186
+ *
187
+ * <input {...register("name", {
188
+ * validate: (value, formValues) => formValues.otherField === value;
189
+ * })} />
190
+ * ```
191
+ */
192
+ (): TFieldValues;
193
+ /**
194
+ * Get a single field value.
195
+ *
196
+ * @remarks
197
+ * [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
198
+ *
199
+ * @param name - the path name to the form field value.
200
+ *
201
+ * @returns the single field value
202
+ *
203
+ * @example
204
+ * ```tsx
205
+ * <button onClick={() => getValues("name")}>getValues</button>
206
+ *
207
+ * <input {...register("name", {
208
+ * validate: () => getValues('otherField') === "test";
209
+ * })} />
210
+ * ```
211
+ */
212
+ <TFieldName extends FieldPath<TFieldValues>>(name: TFieldName): FieldPathValue<TFieldValues, TFieldName>;
213
+ /**
214
+ * Get an array of field values.
215
+ *
216
+ * @remarks
217
+ * [API](https://react-hook-form.com/api/useform/getvalues) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-getvalues-txsfg)
218
+ *
219
+ * @param names - an array of field names
220
+ *
221
+ * @returns An array of field values
222
+ *
223
+ * @example
224
+ * ```tsx
225
+ * <button onClick={() => getValues(["name", "name1"])}>getValues</button>
226
+ *
227
+ * <input {...register("name", {
228
+ * validate: () => getValues(["fieldA", "fieldB"]).includes("test");
229
+ * })} />
230
+ * ```
231
+ */
232
+ <TFieldNames extends FieldPath<TFieldValues>[]>(names: readonly [...TFieldNames]): [...FieldPathValues<TFieldValues, TFieldNames>];
233
+ };
234
+ /**
235
+ * 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.
236
+ *
237
+ * @remarks
238
+ * [API](https://react-hook-form.com/api/useform/getfieldstate) • [Demo](https://codesandbox.io/s/getfieldstate-jvekk)
239
+ *
240
+ * @param name - the path name to the form field value.
241
+ *
242
+ * @returns invalid, isDirty, isTouched and error object
243
+ *
244
+ * @example
245
+ * ```tsx
246
+ * // those formState has to be subscribed
247
+ * const { formState: { dirtyFields, errors, touchedFields } } = formState();
248
+ * getFieldState('name')
249
+ * // Get field state when form state is not subscribed yet
250
+ * getFieldState('name', formState)
251
+ *
252
+ * // It's ok to combine with useFormState
253
+ * const formState = useFormState();
254
+ * getFieldState('name')
255
+ * getFieldState('name', formState)
256
+ * ```
257
+ */
258
+ export type UseFormGetFieldState<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues>>(name: TFieldName, formState?: FormState<TFieldValues>) => {
259
+ invalid: boolean;
260
+ isDirty: boolean;
261
+ isTouched: boolean;
262
+ error?: FieldError;
263
+ };
264
+ export type UseFormWatch<TFieldValues extends FieldValues> = {
265
+ /**
266
+ * Watch and subscribe to the entire form update/change based on onChange and re-render at the useForm.
267
+ *
268
+ * @remarks
269
+ * [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)
270
+ *
271
+ * @returns return the entire form values
272
+ *
273
+ * @example
274
+ * ```tsx
275
+ * const formValues = watch();
276
+ * ```
277
+ */
278
+ (): TFieldValues;
279
+ /**
280
+ * Watch and subscribe to an array of fields used outside of render.
281
+ *
282
+ * @remarks
283
+ * [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)
284
+ *
285
+ * @param names - an array of field names
286
+ * @param defaultValue - defaultValues for the entire form
287
+ *
288
+ * @returns return an array of field values
289
+ *
290
+ * @example
291
+ * ```tsx
292
+ * const [name, name1] = watch(["name", "name1"]);
293
+ * ```
294
+ */
295
+ <TFieldNames extends readonly FieldPath<TFieldValues>[]>(names: readonly [...TFieldNames], defaultValue?: DeepPartial<TFieldValues>): FieldPathValues<TFieldValues, TFieldNames>;
296
+ /**
297
+ * Watch and subscribe to a single field used outside of render.
298
+ *
299
+ * @remarks
300
+ * [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)
301
+ *
302
+ * @param name - the path name to the form field value.
303
+ * @param defaultValue - defaultValues for the entire form
304
+ *
305
+ * @returns return the single field value
306
+ *
307
+ * @example
308
+ * ```tsx
309
+ * const name = watch("name");
310
+ * ```
311
+ */
312
+ <TFieldName extends FieldPath<TFieldValues>>(name: TFieldName, defaultValue?: FieldPathValue<TFieldValues, TFieldName>): FieldPathValue<TFieldValues, TFieldName>;
313
+ /**
314
+ * Subscribe to field update/change without trigger re-render
315
+ *
316
+ * @remarks
317
+ * [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)
318
+ *
319
+ * @param callback - call back function to subscribe all fields change and return unsubscribe function
320
+ * @param defaultValues - defaultValues for the entire form
321
+ *
322
+ * @returns unsubscribe function
323
+ *
324
+ * @example
325
+ * ```tsx
326
+ * useEffect(() => {
327
+ * const { unsubscribe } = watch((value) => {
328
+ * console.log(value);
329
+ * });
330
+ * return () => unsubscribe();
331
+ * }, [watch])
332
+ * ```
333
+ */
334
+ (callback: WatchObserver<TFieldValues>, defaultValues?: DeepPartial<TFieldValues>): Subscription;
335
+ };
336
+ /**
337
+ * Trigger field or form validation
338
+ *
339
+ * @remarks
340
+ * [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)
341
+ *
342
+ * @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.
343
+ * @param options - should focus on the error field
344
+ *
345
+ * @returns validation result
346
+ *
347
+ * @example
348
+ * ```tsx
349
+ * useEffect(() => {
350
+ * trigger();
351
+ * }, [trigger])
352
+ *
353
+ * <button onClick={async () => {
354
+ * const result = await trigger(); // result will be a boolean value
355
+ * }}>
356
+ * trigger
357
+ * </button>
358
+ * ```
359
+ */
360
+ export type UseFormTrigger<TFieldValues extends FieldValues> = (name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[], options?: TriggerConfig) => Promise<boolean>;
361
+ /**
362
+ * Clear the entire form errors.
363
+ *
364
+ * @remarks
365
+ * [API](https://react-hook-form.com/api/useform/clearerrors) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-clearerrors-w3ymx)
366
+ *
367
+ * @param name - the path name to the form field value.
368
+ *
369
+ * @example
370
+ * Clear all errors
371
+ * ```tsx
372
+ * clearErrors(); // clear the entire form error
373
+ * clearErrors(["name", "name1"]) // clear an array of fields' error
374
+ * clearErrors("name2"); // clear a single field error
375
+ * ```
376
+ */
377
+ export type UseFormClearErrors<TFieldValues extends FieldValues> = (name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[] | `root.${string}` | 'root') => void;
378
+ /**
379
+ * Set a single field value, or a group of fields value.
380
+ *
381
+ * @remarks
382
+ * [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)
383
+ *
384
+ * @param name - the path name to the form field value.
385
+ * @param value - field value
386
+ * @param options - should validate or update form state
387
+ *
388
+ * @example
389
+ * ```tsx
390
+ * // Update a single field
391
+ * setValue('name', 'value', {
392
+ * shouldValidate: true, // trigger validation
393
+ * shouldTouch: true, // update touched fields form state
394
+ * shouldDirty: true, // update dirty and dirty fields form state
395
+ * });
396
+ *
397
+ * // Update a group fields
398
+ * setValue('root', {
399
+ * a: 'test', // setValue('root.a', 'data')
400
+ * b: 'test1', // setValue('root.b', 'data')
401
+ * });
402
+ *
403
+ * // Update a nested object field
404
+ * setValue('select', { label: 'test', value: 'Test' });
405
+ * ```
406
+ */
407
+ export type UseFormSetValue<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, value: FieldPathValue<TFieldValues, TFieldName>, options?: SetValueConfig) => void;
408
+ /**
409
+ * Set an error for the field. When set an error which is not associated to a field then manual `clearErrors` invoke is required.
410
+ *
411
+ * @remarks
412
+ * [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)
413
+ *
414
+ * @param name - the path name to the form field value.
415
+ * @param error - an error object which contains type and optional message
416
+ * @param options - whether or not to focus on the field
417
+ *
418
+ * @example
419
+ * ```tsx
420
+ * // when the error is not associated with any fields, `clearError` will need to invoke to clear the error
421
+ * const onSubmit = () => setError("serverError", { type: "server", message: "Error occurred"})
422
+ *
423
+ * <button onClick={() => setError("name", { type: "min" })} />
424
+ *
425
+ * // focus on the input after setting the error
426
+ * <button onClick={() => setError("name", { type: "max" }, { shouldFocus: true })} />
427
+ * ```
428
+ */
429
+ export type UseFormSetError<TFieldValues extends FieldValues> = (name: FieldPath<TFieldValues> | `root.${string}` | 'root', error: ErrorOption, options?: {
430
+ shouldFocus: boolean;
431
+ }) => void;
432
+ /**
433
+ * Unregister a field reference and remove its value.
434
+ *
435
+ * @remarks
436
+ * [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)
437
+ *
438
+ * @param name - the path name to the form field value.
439
+ * @param options - keep form state options
440
+ *
441
+ * @example
442
+ * ```tsx
443
+ * register("name", { required: true })
444
+ *
445
+ * <button onClick={() => unregister("name")} />
446
+ * // there are various keep options to retain formState
447
+ * <button onClick={() => unregister("name", { keepErrors: true })} />
448
+ * ```
449
+ */
450
+ export type UseFormUnregister<TFieldValues extends FieldValues> = (name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[], options?: Omit<KeepStateOptions, 'keepIsSubmitted' | 'keepSubmitCount' | 'keepValues' | 'keepDefaultValues' | 'keepErrors'> & {
451
+ keepValue?: boolean;
452
+ keepDefaultValue?: boolean;
453
+ keepError?: boolean;
454
+ }) => void;
455
+ /**
456
+ * Validate the entire form. Handle submit and error callback.
457
+ *
458
+ * @remarks
459
+ * [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)
460
+ *
461
+ * @param onValid - callback function invoked after form pass validation
462
+ * @param onInvalid - callback function invoked when form failed validation
463
+ *
464
+ * @returns callback - return callback function
465
+ *
466
+ * @example
467
+ * ```tsx
468
+ * const onSubmit = (data) => console.log(data);
469
+ * const onError = (error) => console.log(error);
470
+ *
471
+ * <form onSubmit={handleSubmit(onSubmit, onError)} />
472
+ * ```
473
+ */
474
+ export type UseFormHandleSubmit<TFieldValues extends FieldValues, TTransformedValues extends FieldValues | undefined = undefined> = (onValid: TTransformedValues extends FieldValues ? SubmitHandler<TTransformedValues> : SubmitHandler<TFieldValues>, onInvalid?: SubmitErrorHandler<TFieldValues>) => (e?: React.BaseSyntheticEvent) => Promise<void>;
475
+ /**
476
+ * Reset a field state and reference.
477
+ *
478
+ * @remarks
479
+ * [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)
480
+ *
481
+ * @param name - the path name to the form field value.
482
+ * @param options - keep form state options
483
+ *
484
+ * @example
485
+ * ```tsx
486
+ * <input {...register("firstName", { required: true })} />
487
+ * <button type="button" onClick={() => resetField("firstName"))}>Reset</button>
488
+ * ```
489
+ */
490
+ export type UseFormResetField<TFieldValues extends FieldValues> = <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, options?: Partial<{
491
+ keepDirty: boolean;
492
+ keepTouched: boolean;
493
+ keepError: boolean;
494
+ defaultValue: FieldPathValue<TFieldValues, TFieldName>;
495
+ }>) => void;
496
+ type ResetAction<TFieldValues> = (formValues: TFieldValues) => TFieldValues;
497
+ /**
498
+ * Reset at the entire form state.
499
+ *
500
+ * @remarks
501
+ * [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)
502
+ *
503
+ * @param values - the entire form values to be reset
504
+ * @param keepStateOptions - keep form state options
505
+ *
506
+ * @example
507
+ * ```tsx
508
+ * useEffect(() => {
509
+ * // reset the entire form after component mount or form defaultValues is ready
510
+ * reset({
511
+ * fieldA: "test"
512
+ * fieldB: "test"
513
+ * });
514
+ * }, [reset])
515
+ *
516
+ * // reset by combine with existing form values
517
+ * reset({
518
+ * ...getValues(),
519
+ * fieldB: "test"
520
+ *});
521
+ *
522
+ * // reset and keep form state
523
+ * reset({
524
+ * ...getValues(),
525
+ *}, {
526
+ * keepErrors: true,
527
+ * keepDirty: true
528
+ *});
529
+ * ```
530
+ */
531
+ export type UseFormReset<TFieldValues extends FieldValues> = (values?: DefaultValues<TFieldValues> | TFieldValues | ResetAction<TFieldValues>, keepStateOptions?: KeepStateOptions) => void;
532
+ export type WatchInternal<TFieldValues> = (fieldNames?: InternalFieldName | InternalFieldName[], defaultValue?: DeepPartial<TFieldValues>, isMounted?: boolean, isGlobal?: boolean) => FieldPathValue<FieldValues, InternalFieldName> | FieldPathValues<FieldValues, InternalFieldName[]>;
533
+ export type GetIsDirty = <TName extends InternalFieldName, TData>(name?: TName, data?: TData) => boolean;
534
+ export type FormStateSubjectRef<TFieldValues extends FieldValues> = Subject<Partial<FormState<TFieldValues>> & {
535
+ name?: InternalFieldName;
536
+ }>;
537
+ export type Subjects<TFieldValues extends FieldValues = FieldValues> = {
538
+ values: Subject<{
539
+ name?: InternalFieldName;
540
+ type?: EventType;
541
+ values: FieldValues;
542
+ }>;
543
+ array: Subject<{
544
+ name?: InternalFieldName;
545
+ values?: FieldValues;
546
+ }>;
547
+ state: FormStateSubjectRef<TFieldValues>;
548
+ };
549
+ export type Names = {
550
+ mount: InternalNameSet;
551
+ unMount: InternalNameSet;
552
+ array: InternalNameSet;
553
+ watch: InternalNameSet;
554
+ focus?: InternalFieldName;
555
+ watchAll?: boolean;
556
+ };
557
+ 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<{
558
+ argA: unknown;
559
+ argB: unknown;
560
+ }>, shouldSetValue?: boolean, shouldUpdateFieldsAndErrors?: boolean) => void;
561
+ export type Control<TFieldValues extends FieldValues = FieldValues, TContext = any> = {
562
+ _subjects: Subjects<TFieldValues>;
563
+ _removeUnmounted: Noop;
564
+ _names: Names;
565
+ _state: {
566
+ mount: boolean;
567
+ action: boolean;
568
+ watch: boolean;
569
+ };
570
+ _reset: UseFormReset<TFieldValues>;
571
+ _options: UseFormProps<TFieldValues, TContext>;
572
+ _getDirty: GetIsDirty;
573
+ _resetDefaultValues: Noop;
574
+ _formState: FormState<TFieldValues>;
575
+ _updateValid: (shouldUpdateValid?: boolean) => void;
576
+ _updateFormState: (formState: Partial<FormState<TFieldValues>>) => void;
577
+ _fields: FieldRefs;
578
+ _formValues: FieldValues;
579
+ _proxyFormState: ReadFormState;
580
+ _defaultValues: Partial<DefaultValues<TFieldValues>>;
581
+ _getWatch: WatchInternal<TFieldValues>;
582
+ _updateFieldArray: BatchFieldArrayUpdate;
583
+ _getFieldArray: <TFieldArrayValues>(name: InternalFieldName) => Partial<TFieldArrayValues>[];
584
+ _executeSchema: (names: InternalFieldName[]) => Promise<{
585
+ errors: FieldErrors;
586
+ }>;
587
+ register: UseFormRegister<TFieldValues>;
588
+ handleSubmit: UseFormHandleSubmit<TFieldValues>;
589
+ unregister: UseFormUnregister<TFieldValues>;
590
+ getFieldState: UseFormGetFieldState<TFieldValues>;
591
+ setError: UseFormSetError<TFieldValues>;
592
+ };
593
+ export type WatchObserver<TFieldValues extends FieldValues> = (value: DeepPartial<TFieldValues>, info: {
594
+ name?: FieldPath<TFieldValues>;
595
+ type?: EventType;
596
+ }) => void;
597
+ export type UseFormReturn<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined> = {
598
+ watch: UseFormWatch<TFieldValues>;
599
+ getValues: UseFormGetValues<TFieldValues>;
600
+ getFieldState: UseFormGetFieldState<TFieldValues>;
601
+ setError: UseFormSetError<TFieldValues>;
602
+ clearErrors: UseFormClearErrors<TFieldValues>;
603
+ setValue: UseFormSetValue<TFieldValues>;
604
+ trigger: UseFormTrigger<TFieldValues>;
605
+ formState: FormState<TFieldValues>;
606
+ resetField: UseFormResetField<TFieldValues>;
607
+ reset: UseFormReset<TFieldValues>;
608
+ handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues>;
609
+ unregister: UseFormUnregister<TFieldValues>;
610
+ control: Control<TFieldValues, TContext>;
611
+ register: UseFormRegister<TFieldValues>;
612
+ setFocus: UseFormSetFocus<TFieldValues>;
613
+ };
614
+ export type UseFormStateProps<TFieldValues extends FieldValues> = Partial<{
615
+ control?: Control<TFieldValues>;
616
+ disabled?: boolean;
617
+ name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[];
618
+ exact?: boolean;
619
+ }>;
620
+ export type UseFormStateReturn<TFieldValues extends FieldValues> = FormState<TFieldValues>;
621
+ export type UseWatchProps<TFieldValues extends FieldValues = FieldValues> = {
622
+ defaultValue?: unknown;
623
+ disabled?: boolean;
624
+ name?: FieldPath<TFieldValues> | FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[];
625
+ control?: Control<TFieldValues>;
626
+ exact?: boolean;
627
+ };
628
+ export type FormProviderProps<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined> = {
629
+ children: React.ReactNode | React.ReactNode[];
630
+ } & UseFormReturn<TFieldValues, TContext, TTransformedValues>;
631
+ export {};
631
632
  //# sourceMappingURL=form.d.ts.map