react-hook-form 7.43.5 → 7.43.7
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.
- package/README.md +3 -0
- package/dist/__tests__/controller.server.test.d.ts +1 -1
- package/dist/__tests__/controller.test.d.ts +1 -1
- package/dist/__tests__/form.test.d.ts +2 -2
- package/dist/__tests__/logic/validateField.test.d.ts +1 -1
- package/dist/__tests__/type.test.d.ts +1 -1
- package/dist/__tests__/useController.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/append.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/focus.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/insert.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/move.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/prepend.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/remove.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/replace.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/swap.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray/update.test.d.ts +1 -1
- package/dist/__tests__/useFieldArray.test.d.ts +1 -1
- package/dist/__tests__/useForm/clearErrors.test.d.ts +1 -1
- package/dist/__tests__/useForm/formState.test.d.ts +1 -1
- package/dist/__tests__/useForm/getFieldState.test.d.ts +1 -1
- package/dist/__tests__/useForm/getValues.test.d.ts +1 -1
- package/dist/__tests__/useForm/handleSubmit.test.d.ts +1 -1
- package/dist/__tests__/useForm/register.test.d.ts +1 -1
- package/dist/__tests__/useForm/reset.test.d.ts +1 -1
- package/dist/__tests__/useForm/resetField.test.d.ts +1 -1
- package/dist/__tests__/useForm/resolver.test.d.ts +1 -1
- package/dist/__tests__/useForm/setError.test.d.ts +1 -1
- package/dist/__tests__/useForm/setValue.test.d.ts +1 -1
- package/dist/__tests__/useForm/trigger.test.d.ts +1 -1
- package/dist/__tests__/useForm/unregister.test.d.ts +1 -1
- package/dist/__tests__/useForm/watch.test.d.ts +1 -1
- package/dist/__tests__/useForm.server.test.d.ts +1 -1
- package/dist/__tests__/useForm.test.d.ts +1 -1
- package/dist/__tests__/useFormContext.server.test.d.ts +1 -1
- package/dist/__tests__/useFormContext.test.d.ts +1 -1
- package/dist/__tests__/useFormState.test.d.ts +1 -1
- package/dist/__tests__/useWatch.test.d.ts +1 -1
- package/dist/__typetest__/__fixtures__/index.d.ts +4 -4
- package/dist/__typetest__/__fixtures__/pathString.d.ts +3 -3
- package/dist/__typetest__/__fixtures__/traversable.d.ts +13 -13
- package/dist/__typetest__/__fixtures__/tuple.d.ts +14 -14
- package/dist/__typetest__/__fixtures__/type.d.ts +11 -11
- package/dist/__typetest__/errors.test-d.d.ts +1 -1
- package/dist/__typetest__/form.test-d.d.ts +1 -1
- package/dist/__typetest__/path/common.test-d.d.ts +1 -1
- package/dist/__typetest__/path/eager.test-d.d.ts +1 -1
- package/dist/__typetest__/util.test-d.d.ts +1 -1
- package/dist/constants.d.ts +16 -16
- package/dist/controller.d.ts +46 -46
- package/dist/controller.d.ts.map +1 -1
- package/dist/form.d.ts +60 -79
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +10 -10
- package/dist/index.esm.mjs +2174 -2164
- package/dist/index.esm.mjs.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/logic/appendErrors.d.ts +3 -3
- package/dist/logic/createFormControl.d.ts +25 -25
- package/dist/logic/createFormControl.d.ts.map +1 -1
- package/dist/logic/focusFieldBy.d.ts +3 -3
- package/dist/logic/generateId.d.ts +2 -2
- package/dist/logic/generateWatchOutput.d.ts +3 -3
- package/dist/logic/getCheckboxValue.d.ts +6 -6
- package/dist/logic/getDirtyFields.d.ts +2 -2
- package/dist/logic/getEventValue.d.ts +2 -2
- package/dist/logic/getFieldValue.d.ts +2 -2
- package/dist/logic/getFieldValueAs.d.ts +3 -3
- package/dist/logic/getFocusFieldName.d.ts +3 -3
- package/dist/logic/getNodeParentName.d.ts +2 -2
- package/dist/logic/getProxyFormState.d.ts +3 -3
- package/dist/logic/getRadioValue.d.ts +6 -6
- package/dist/logic/getResolverOptions.d.ts +13 -13
- package/dist/logic/getRuleValue.d.ts +3 -3
- package/dist/logic/getValidateError.d.ts +2 -2
- package/dist/logic/getValidationModes.d.ts +9 -9
- package/dist/logic/getValueAndMessage.d.ts +6 -6
- package/dist/logic/hasValidation.d.ts +3 -3
- package/dist/logic/index.d.ts +2 -2
- package/dist/logic/isNameInFieldArray.d.ts +3 -3
- package/dist/logic/isWatched.d.ts +3 -3
- package/dist/logic/schemaErrorLookup.d.ts +5 -5
- package/dist/logic/shouldRenderFormState.d.ts +5 -5
- package/dist/logic/shouldSubscribeByName.d.ts +2 -2
- package/dist/logic/skipValidation.d.ts +11 -11
- package/dist/logic/unsetEmptyArray.d.ts +2 -2
- package/dist/logic/updateFieldArrayRootError.d.ts +3 -3
- package/dist/logic/validateField.d.ts +3 -3
- package/dist/types/controller.d.ts +56 -56
- package/dist/types/errors.d.ts +35 -35
- package/dist/types/events.d.ts +1 -1
- package/dist/types/fieldArray.d.ts +194 -194
- package/dist/types/fields.d.ts +28 -28
- package/dist/types/form.d.ts +627 -627
- package/dist/types/index.d.ts +10 -10
- package/dist/types/path/common.d.ts +315 -315
- package/dist/types/path/eager.d.ts +120 -120
- package/dist/types/path/index.d.ts +3 -3
- package/dist/types/resolvers.d.ts +19 -19
- package/dist/types/utils.d.ts +70 -70
- package/dist/types/validator.d.ts +38 -38
- package/dist/types/validator.d.ts.map +1 -1
- package/dist/useController.d.ts +26 -26
- package/dist/useController.d.ts.map +1 -1
- package/dist/useFieldArray.d.ts +39 -39
- package/dist/useForm.d.ts +31 -31
- package/dist/useFormContext.d.ts +64 -64
- package/dist/useFormContext.d.ts.map +1 -1
- package/dist/useFormState.d.ts +33 -33
- package/dist/useSubscribe.d.ts +8 -8
- package/dist/useWatch.d.ts +99 -99
- package/dist/utils/append.d.ts +1 -1
- package/dist/utils/cloneObject.d.ts +1 -1
- package/dist/utils/compact.d.ts +2 -2
- package/dist/utils/convertToArrayPayload.d.ts +2 -2
- package/dist/utils/createSubject.d.ts +13 -13
- package/dist/utils/deepEqual.d.ts +1 -1
- package/dist/utils/deepMerge.d.ts +1 -1
- package/dist/utils/fillBooleanArray.d.ts +2 -2
- package/dist/utils/fillEmptyArray.d.ts +2 -2
- package/dist/utils/get.d.ts +2 -2
- package/dist/utils/index.d.ts +3 -3
- package/dist/utils/insert.d.ts +2 -2
- package/dist/utils/isBoolean.d.ts +2 -2
- package/dist/utils/isCheckBoxInput.d.ts +3 -3
- package/dist/utils/isDateObject.d.ts +2 -2
- package/dist/utils/isEmptyObject.d.ts +3 -3
- package/dist/utils/isFileInput.d.ts +3 -3
- package/dist/utils/isFunction.d.ts +2 -2
- package/dist/utils/isHTMLElement.d.ts +2 -2
- package/dist/utils/isKey.d.ts +2 -2
- package/dist/utils/isMessage.d.ts +2 -2
- package/dist/utils/isMultipleSelect.d.ts +3 -3
- package/dist/utils/isNullOrUndefined.d.ts +2 -2
- package/dist/utils/isObject.d.ts +3 -3
- package/dist/utils/isPlainObject.d.ts +2 -2
- package/dist/utils/isPrimitive.d.ts +3 -3
- package/dist/utils/isRadioInput.d.ts +3 -3
- package/dist/utils/isRadioOrCheckbox.d.ts +3 -3
- package/dist/utils/isRegex.d.ts +2 -2
- package/dist/utils/isSelectInput.d.ts +3 -3
- package/dist/utils/isString.d.ts +2 -2
- package/dist/utils/isUndefined.d.ts +2 -2
- package/dist/utils/isWeb.d.ts +2 -2
- package/dist/utils/live.d.ts +3 -3
- package/dist/utils/move.d.ts +2 -2
- package/dist/utils/objectHasFunction.d.ts +2 -2
- package/dist/utils/omit.d.ts +2 -2
- package/dist/utils/prepend.d.ts +1 -1
- package/dist/utils/remove.d.ts +2 -2
- package/dist/utils/set.d.ts +2 -2
- package/dist/utils/sleep.d.ts +1 -1
- package/dist/utils/stringToPath.d.ts +2 -2
- package/dist/utils/swap.d.ts +2 -2
- package/dist/utils/unset.d.ts +1 -1
- package/dist/utils/update.d.ts +2 -2
- package/package.json +8 -8
package/dist/index.esm.mjs
CHANGED
@@ -6,590 +6,594 @@ var isDateObject = (value) => value instanceof Date;
|
|
6
6
|
|
7
7
|
var isNullOrUndefined = (value) => value == null;
|
8
8
|
|
9
|
-
const isObjectType = (value) => typeof value === 'object';
|
10
|
-
var isObject = (value) => !isNullOrUndefined(value) &&
|
11
|
-
!Array.isArray(value) &&
|
12
|
-
isObjectType(value) &&
|
9
|
+
const isObjectType = (value) => typeof value === 'object';
|
10
|
+
var isObject = (value) => !isNullOrUndefined(value) &&
|
11
|
+
!Array.isArray(value) &&
|
12
|
+
isObjectType(value) &&
|
13
13
|
!isDateObject(value);
|
14
14
|
|
15
|
-
var getEventValue = (event) => isObject(event) && event.target
|
16
|
-
? isCheckBoxInput(event.target)
|
17
|
-
? event.target.checked
|
18
|
-
: event.target.value
|
15
|
+
var getEventValue = (event) => isObject(event) && event.target
|
16
|
+
? isCheckBoxInput(event.target)
|
17
|
+
? event.target.checked
|
18
|
+
: event.target.value
|
19
19
|
: event;
|
20
20
|
|
21
21
|
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
22
22
|
|
23
23
|
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
24
24
|
|
25
|
-
var isPlainObject = (tempObject) => {
|
26
|
-
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
27
|
-
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
25
|
+
var isPlainObject = (tempObject) => {
|
26
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
27
|
+
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
28
28
|
};
|
29
29
|
|
30
|
-
var isWeb = typeof window !== 'undefined' &&
|
31
|
-
typeof window.HTMLElement !== 'undefined' &&
|
30
|
+
var isWeb = typeof window !== 'undefined' &&
|
31
|
+
typeof window.HTMLElement !== 'undefined' &&
|
32
32
|
typeof document !== 'undefined';
|
33
33
|
|
34
|
-
function cloneObject(data) {
|
35
|
-
let copy;
|
36
|
-
const isArray = Array.isArray(data);
|
37
|
-
if (data instanceof Date) {
|
38
|
-
copy = new Date(data);
|
39
|
-
}
|
40
|
-
else if (data instanceof Set) {
|
41
|
-
copy = new Set(data);
|
42
|
-
}
|
43
|
-
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
|
44
|
-
(isArray || isObject(data))) {
|
45
|
-
copy = isArray ? [] : {};
|
46
|
-
if (!Array.isArray(data) && !isPlainObject(data)) {
|
47
|
-
copy = data;
|
48
|
-
}
|
49
|
-
else {
|
50
|
-
for (const key in data) {
|
51
|
-
copy[key] = cloneObject(data[key]);
|
52
|
-
}
|
53
|
-
}
|
54
|
-
}
|
55
|
-
else {
|
56
|
-
return data;
|
57
|
-
}
|
58
|
-
return copy;
|
34
|
+
function cloneObject(data) {
|
35
|
+
let copy;
|
36
|
+
const isArray = Array.isArray(data);
|
37
|
+
if (data instanceof Date) {
|
38
|
+
copy = new Date(data);
|
39
|
+
}
|
40
|
+
else if (data instanceof Set) {
|
41
|
+
copy = new Set(data);
|
42
|
+
}
|
43
|
+
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
|
44
|
+
(isArray || isObject(data))) {
|
45
|
+
copy = isArray ? [] : {};
|
46
|
+
if (!Array.isArray(data) && !isPlainObject(data)) {
|
47
|
+
copy = data;
|
48
|
+
}
|
49
|
+
else {
|
50
|
+
for (const key in data) {
|
51
|
+
copy[key] = cloneObject(data[key]);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
else {
|
56
|
+
return data;
|
57
|
+
}
|
58
|
+
return copy;
|
59
59
|
}
|
60
60
|
|
61
61
|
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
62
62
|
|
63
63
|
var isUndefined = (val) => val === undefined;
|
64
64
|
|
65
|
-
var get = (obj, path, defaultValue) => {
|
66
|
-
if (!path || !isObject(obj)) {
|
67
|
-
return defaultValue;
|
68
|
-
}
|
69
|
-
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], obj);
|
70
|
-
return isUndefined(result) || result === obj
|
71
|
-
? isUndefined(obj[path])
|
72
|
-
? defaultValue
|
73
|
-
: obj[path]
|
74
|
-
: result;
|
65
|
+
var get = (obj, path, defaultValue) => {
|
66
|
+
if (!path || !isObject(obj)) {
|
67
|
+
return defaultValue;
|
68
|
+
}
|
69
|
+
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], obj);
|
70
|
+
return isUndefined(result) || result === obj
|
71
|
+
? isUndefined(obj[path])
|
72
|
+
? defaultValue
|
73
|
+
: obj[path]
|
74
|
+
: result;
|
75
75
|
};
|
76
76
|
|
77
|
-
const EVENTS = {
|
78
|
-
BLUR: 'blur',
|
79
|
-
FOCUS_OUT: 'focusout',
|
80
|
-
CHANGE: 'change',
|
81
|
-
};
|
82
|
-
const VALIDATION_MODE = {
|
83
|
-
onBlur: 'onBlur',
|
84
|
-
onChange: 'onChange',
|
85
|
-
onSubmit: 'onSubmit',
|
86
|
-
onTouched: 'onTouched',
|
87
|
-
all: 'all',
|
88
|
-
};
|
89
|
-
const INPUT_VALIDATION_RULES = {
|
90
|
-
max: 'max',
|
91
|
-
min: 'min',
|
92
|
-
maxLength: 'maxLength',
|
93
|
-
minLength: 'minLength',
|
94
|
-
pattern: 'pattern',
|
95
|
-
required: 'required',
|
96
|
-
validate: 'validate',
|
77
|
+
const EVENTS = {
|
78
|
+
BLUR: 'blur',
|
79
|
+
FOCUS_OUT: 'focusout',
|
80
|
+
CHANGE: 'change',
|
81
|
+
};
|
82
|
+
const VALIDATION_MODE = {
|
83
|
+
onBlur: 'onBlur',
|
84
|
+
onChange: 'onChange',
|
85
|
+
onSubmit: 'onSubmit',
|
86
|
+
onTouched: 'onTouched',
|
87
|
+
all: 'all',
|
88
|
+
};
|
89
|
+
const INPUT_VALIDATION_RULES = {
|
90
|
+
max: 'max',
|
91
|
+
min: 'min',
|
92
|
+
maxLength: 'maxLength',
|
93
|
+
minLength: 'minLength',
|
94
|
+
pattern: 'pattern',
|
95
|
+
required: 'required',
|
96
|
+
validate: 'validate',
|
97
97
|
};
|
98
98
|
|
99
|
-
const HookFormContext = React.createContext(null);
|
100
|
-
/**
|
101
|
-
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
|
102
|
-
*
|
103
|
-
* @remarks
|
104
|
-
* [API](https://react-hook-form.com/api/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
105
|
-
*
|
106
|
-
* @returns return all useForm methods
|
107
|
-
*
|
108
|
-
* @example
|
109
|
-
* ```tsx
|
110
|
-
* function App() {
|
111
|
-
* const methods = useForm();
|
112
|
-
* const onSubmit = data => console.log(data);
|
113
|
-
*
|
114
|
-
* return (
|
115
|
-
* <FormProvider {...methods} >
|
116
|
-
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
117
|
-
* <NestedInput />
|
118
|
-
* <input type="submit" />
|
119
|
-
* </form>
|
120
|
-
* </FormProvider>
|
121
|
-
* );
|
122
|
-
* }
|
123
|
-
*
|
124
|
-
* function NestedInput() {
|
125
|
-
* const { register } = useFormContext(); // retrieve all hook methods
|
126
|
-
* return <input {...register("test")} />;
|
127
|
-
* }
|
128
|
-
* ```
|
129
|
-
*/
|
130
|
-
const useFormContext = () => React.useContext(HookFormContext);
|
131
|
-
/**
|
132
|
-
* A provider component that propagates the `useForm` methods to all children components via [React Context](https://reactjs.org/docs/context.html) API. To be used with {@link useFormContext}.
|
133
|
-
*
|
134
|
-
* @remarks
|
135
|
-
* [API](https://react-hook-form.com/api/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
136
|
-
*
|
137
|
-
* @param props - all useFrom methods
|
138
|
-
*
|
139
|
-
* @example
|
140
|
-
* ```tsx
|
141
|
-
* function App() {
|
142
|
-
* const methods = useForm();
|
143
|
-
* const onSubmit = data => console.log(data);
|
144
|
-
*
|
145
|
-
* return (
|
146
|
-
* <FormProvider {...methods} >
|
147
|
-
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
148
|
-
* <NestedInput />
|
149
|
-
* <input type="submit" />
|
150
|
-
* </form>
|
151
|
-
* </FormProvider>
|
152
|
-
* );
|
153
|
-
* }
|
154
|
-
*
|
155
|
-
* function NestedInput() {
|
156
|
-
* const { register } = useFormContext(); // retrieve all hook methods
|
157
|
-
* return <input {...register("test")} />;
|
158
|
-
* }
|
159
|
-
* ```
|
160
|
-
*/
|
161
|
-
const FormProvider = (props) => {
|
162
|
-
const { children, ...data } = props;
|
163
|
-
return (React.createElement(HookFormContext.Provider, { value: data }, children));
|
99
|
+
const HookFormContext = React.createContext(null);
|
100
|
+
/**
|
101
|
+
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
|
102
|
+
*
|
103
|
+
* @remarks
|
104
|
+
* [API](https://react-hook-form.com/api/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
105
|
+
*
|
106
|
+
* @returns return all useForm methods
|
107
|
+
*
|
108
|
+
* @example
|
109
|
+
* ```tsx
|
110
|
+
* function App() {
|
111
|
+
* const methods = useForm();
|
112
|
+
* const onSubmit = data => console.log(data);
|
113
|
+
*
|
114
|
+
* return (
|
115
|
+
* <FormProvider {...methods} >
|
116
|
+
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
117
|
+
* <NestedInput />
|
118
|
+
* <input type="submit" />
|
119
|
+
* </form>
|
120
|
+
* </FormProvider>
|
121
|
+
* );
|
122
|
+
* }
|
123
|
+
*
|
124
|
+
* function NestedInput() {
|
125
|
+
* const { register } = useFormContext(); // retrieve all hook methods
|
126
|
+
* return <input {...register("test")} />;
|
127
|
+
* }
|
128
|
+
* ```
|
129
|
+
*/
|
130
|
+
const useFormContext = () => React.useContext(HookFormContext);
|
131
|
+
/**
|
132
|
+
* A provider component that propagates the `useForm` methods to all children components via [React Context](https://reactjs.org/docs/context.html) API. To be used with {@link useFormContext}.
|
133
|
+
*
|
134
|
+
* @remarks
|
135
|
+
* [API](https://react-hook-form.com/api/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
136
|
+
*
|
137
|
+
* @param props - all useFrom methods
|
138
|
+
*
|
139
|
+
* @example
|
140
|
+
* ```tsx
|
141
|
+
* function App() {
|
142
|
+
* const methods = useForm();
|
143
|
+
* const onSubmit = data => console.log(data);
|
144
|
+
*
|
145
|
+
* return (
|
146
|
+
* <FormProvider {...methods} >
|
147
|
+
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
148
|
+
* <NestedInput />
|
149
|
+
* <input type="submit" />
|
150
|
+
* </form>
|
151
|
+
* </FormProvider>
|
152
|
+
* );
|
153
|
+
* }
|
154
|
+
*
|
155
|
+
* function NestedInput() {
|
156
|
+
* const { register } = useFormContext(); // retrieve all hook methods
|
157
|
+
* return <input {...register("test")} />;
|
158
|
+
* }
|
159
|
+
* ```
|
160
|
+
*/
|
161
|
+
const FormProvider = (props) => {
|
162
|
+
const { children, ...data } = props;
|
163
|
+
return (React.createElement(HookFormContext.Provider, { value: data }, children));
|
164
164
|
};
|
165
165
|
|
166
|
-
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
167
|
-
const result = {
|
168
|
-
defaultValues: control._defaultValues,
|
169
|
-
};
|
170
|
-
for (const key in formState) {
|
171
|
-
Object.defineProperty(result, key, {
|
172
|
-
get: () => {
|
173
|
-
const _key = key;
|
174
|
-
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
175
|
-
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
176
|
-
}
|
177
|
-
localProxyFormState && (localProxyFormState[_key] = true);
|
178
|
-
return formState[_key];
|
179
|
-
},
|
180
|
-
});
|
181
|
-
}
|
182
|
-
return result;
|
166
|
+
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
167
|
+
const result = {
|
168
|
+
defaultValues: control._defaultValues,
|
169
|
+
};
|
170
|
+
for (const key in formState) {
|
171
|
+
Object.defineProperty(result, key, {
|
172
|
+
get: () => {
|
173
|
+
const _key = key;
|
174
|
+
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
175
|
+
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
176
|
+
}
|
177
|
+
localProxyFormState && (localProxyFormState[_key] = true);
|
178
|
+
return formState[_key];
|
179
|
+
},
|
180
|
+
});
|
181
|
+
}
|
182
|
+
return result;
|
183
183
|
};
|
184
184
|
|
185
185
|
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
186
186
|
|
187
|
-
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
188
|
-
updateFormState(formStateData);
|
189
|
-
const { name, ...formState } = formStateData;
|
190
|
-
return (isEmptyObject(formState) ||
|
191
|
-
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
192
|
-
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
193
|
-
(!isRoot || VALIDATION_MODE.all)));
|
187
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
188
|
+
updateFormState(formStateData);
|
189
|
+
const { name, ...formState } = formStateData;
|
190
|
+
return (isEmptyObject(formState) ||
|
191
|
+
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
192
|
+
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
193
|
+
(!isRoot || VALIDATION_MODE.all)));
|
194
194
|
};
|
195
195
|
|
196
196
|
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
197
197
|
|
198
|
-
var shouldSubscribeByName = (name, signalName, exact) => exact && signalName
|
199
|
-
? name === signalName
|
200
|
-
: !name ||
|
201
|
-
!signalName ||
|
202
|
-
name === signalName ||
|
203
|
-
convertToArrayPayload(name).some((currentName) => currentName &&
|
204
|
-
(currentName.startsWith(signalName) ||
|
198
|
+
var shouldSubscribeByName = (name, signalName, exact) => exact && signalName
|
199
|
+
? name === signalName
|
200
|
+
: !name ||
|
201
|
+
!signalName ||
|
202
|
+
name === signalName ||
|
203
|
+
convertToArrayPayload(name).some((currentName) => currentName &&
|
204
|
+
(currentName.startsWith(signalName) ||
|
205
205
|
signalName.startsWith(currentName)));
|
206
206
|
|
207
|
-
function useSubscribe(props) {
|
208
|
-
const _props = React.useRef(props);
|
209
|
-
_props.current = props;
|
210
|
-
React.useEffect(() => {
|
211
|
-
const subscription = !props.disabled &&
|
212
|
-
_props.current.subject &&
|
213
|
-
_props.current.subject.subscribe({
|
214
|
-
next: _props.current.next,
|
215
|
-
});
|
216
|
-
return () => {
|
217
|
-
subscription && subscription.unsubscribe();
|
218
|
-
};
|
219
|
-
}, [props.disabled]);
|
207
|
+
function useSubscribe(props) {
|
208
|
+
const _props = React.useRef(props);
|
209
|
+
_props.current = props;
|
210
|
+
React.useEffect(() => {
|
211
|
+
const subscription = !props.disabled &&
|
212
|
+
_props.current.subject &&
|
213
|
+
_props.current.subject.subscribe({
|
214
|
+
next: _props.current.next,
|
215
|
+
});
|
216
|
+
return () => {
|
217
|
+
subscription && subscription.unsubscribe();
|
218
|
+
};
|
219
|
+
}, [props.disabled]);
|
220
220
|
}
|
221
221
|
|
222
|
-
/**
|
223
|
-
* This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
|
224
|
-
*
|
225
|
-
* @remarks
|
226
|
-
* [API](https://react-hook-form.com/api/useformstate) • [Demo](https://codesandbox.io/s/useformstate-75xly)
|
227
|
-
*
|
228
|
-
* @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
|
229
|
-
*
|
230
|
-
* @example
|
231
|
-
* ```tsx
|
232
|
-
* function App() {
|
233
|
-
* const { register, handleSubmit, control } = useForm({
|
234
|
-
* defaultValues: {
|
235
|
-
* firstName: "firstName"
|
236
|
-
* }});
|
237
|
-
* const { dirtyFields } = useFormState({
|
238
|
-
* control
|
239
|
-
* });
|
240
|
-
* const onSubmit = (data) => console.log(data);
|
241
|
-
*
|
242
|
-
* return (
|
243
|
-
* <form onSubmit={handleSubmit(onSubmit)}>
|
244
|
-
* <input {...register("firstName")} placeholder="First Name" />
|
245
|
-
* {dirtyFields.firstName && <p>Field is dirty.</p>}
|
246
|
-
* <input type="submit" />
|
247
|
-
* </form>
|
248
|
-
* );
|
249
|
-
* }
|
250
|
-
* ```
|
251
|
-
*/
|
252
|
-
function useFormState(props) {
|
253
|
-
const methods = useFormContext();
|
254
|
-
const { control = methods.control, disabled, name, exact } = props || {};
|
255
|
-
const [formState, updateFormState] = React.useState(control._formState);
|
256
|
-
const _mounted = React.useRef(true);
|
257
|
-
const _localProxyFormState = React.useRef({
|
258
|
-
isDirty: false,
|
259
|
-
isLoading: false,
|
260
|
-
dirtyFields: false,
|
261
|
-
touchedFields: false,
|
262
|
-
isValidating: false,
|
263
|
-
isValid: false,
|
264
|
-
errors: false,
|
265
|
-
});
|
266
|
-
const _name = React.useRef(name);
|
267
|
-
_name.current = name;
|
268
|
-
useSubscribe({
|
269
|
-
disabled,
|
270
|
-
next: (value) => _mounted.current &&
|
271
|
-
shouldSubscribeByName(_name.current, value.name, exact) &&
|
272
|
-
shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) &&
|
273
|
-
updateFormState({
|
274
|
-
...control._formState,
|
275
|
-
...value,
|
276
|
-
}),
|
277
|
-
subject: control._subjects.state,
|
278
|
-
});
|
279
|
-
React.useEffect(() => {
|
280
|
-
_mounted.current = true;
|
281
|
-
const isDirty = control._proxyFormState.isDirty && control._getDirty();
|
282
|
-
if (isDirty !== control._formState.isDirty) {
|
283
|
-
control._subjects.state.next({
|
284
|
-
isDirty,
|
285
|
-
});
|
286
|
-
}
|
287
|
-
_localProxyFormState.current.isValid && control._updateValid(true);
|
288
|
-
return () => {
|
289
|
-
_mounted.current = false;
|
290
|
-
};
|
291
|
-
}, [control]);
|
292
|
-
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
222
|
+
/**
|
223
|
+
* This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
|
224
|
+
*
|
225
|
+
* @remarks
|
226
|
+
* [API](https://react-hook-form.com/api/useformstate) • [Demo](https://codesandbox.io/s/useformstate-75xly)
|
227
|
+
*
|
228
|
+
* @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
|
229
|
+
*
|
230
|
+
* @example
|
231
|
+
* ```tsx
|
232
|
+
* function App() {
|
233
|
+
* const { register, handleSubmit, control } = useForm({
|
234
|
+
* defaultValues: {
|
235
|
+
* firstName: "firstName"
|
236
|
+
* }});
|
237
|
+
* const { dirtyFields } = useFormState({
|
238
|
+
* control
|
239
|
+
* });
|
240
|
+
* const onSubmit = (data) => console.log(data);
|
241
|
+
*
|
242
|
+
* return (
|
243
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
244
|
+
* <input {...register("firstName")} placeholder="First Name" />
|
245
|
+
* {dirtyFields.firstName && <p>Field is dirty.</p>}
|
246
|
+
* <input type="submit" />
|
247
|
+
* </form>
|
248
|
+
* );
|
249
|
+
* }
|
250
|
+
* ```
|
251
|
+
*/
|
252
|
+
function useFormState(props) {
|
253
|
+
const methods = useFormContext();
|
254
|
+
const { control = methods.control, disabled, name, exact } = props || {};
|
255
|
+
const [formState, updateFormState] = React.useState(control._formState);
|
256
|
+
const _mounted = React.useRef(true);
|
257
|
+
const _localProxyFormState = React.useRef({
|
258
|
+
isDirty: false,
|
259
|
+
isLoading: false,
|
260
|
+
dirtyFields: false,
|
261
|
+
touchedFields: false,
|
262
|
+
isValidating: false,
|
263
|
+
isValid: false,
|
264
|
+
errors: false,
|
265
|
+
});
|
266
|
+
const _name = React.useRef(name);
|
267
|
+
_name.current = name;
|
268
|
+
useSubscribe({
|
269
|
+
disabled,
|
270
|
+
next: (value) => _mounted.current &&
|
271
|
+
shouldSubscribeByName(_name.current, value.name, exact) &&
|
272
|
+
shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) &&
|
273
|
+
updateFormState({
|
274
|
+
...control._formState,
|
275
|
+
...value,
|
276
|
+
}),
|
277
|
+
subject: control._subjects.state,
|
278
|
+
});
|
279
|
+
React.useEffect(() => {
|
280
|
+
_mounted.current = true;
|
281
|
+
const isDirty = control._proxyFormState.isDirty && control._getDirty();
|
282
|
+
if (isDirty !== control._formState.isDirty) {
|
283
|
+
control._subjects.state.next({
|
284
|
+
isDirty,
|
285
|
+
});
|
286
|
+
}
|
287
|
+
_localProxyFormState.current.isValid && control._updateValid(true);
|
288
|
+
return () => {
|
289
|
+
_mounted.current = false;
|
290
|
+
};
|
291
|
+
}, [control]);
|
292
|
+
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
293
293
|
}
|
294
294
|
|
295
295
|
var isString = (value) => typeof value === 'string';
|
296
296
|
|
297
|
-
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
298
|
-
if (isString(names)) {
|
299
|
-
isGlobal && _names.watch.add(names);
|
300
|
-
return get(formValues, names, defaultValue);
|
301
|
-
}
|
302
|
-
if (Array.isArray(names)) {
|
303
|
-
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
304
|
-
}
|
305
|
-
isGlobal && (_names.watchAll = true);
|
306
|
-
return formValues;
|
297
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
298
|
+
if (isString(names)) {
|
299
|
+
isGlobal && _names.watch.add(names);
|
300
|
+
return get(formValues, names, defaultValue);
|
301
|
+
}
|
302
|
+
if (Array.isArray(names)) {
|
303
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
304
|
+
}
|
305
|
+
isGlobal && (_names.watchAll = true);
|
306
|
+
return formValues;
|
307
307
|
};
|
308
308
|
|
309
|
-
/**
|
310
|
-
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
311
|
-
*
|
312
|
-
* @remarks
|
313
|
-
*
|
314
|
-
* [API](https://react-hook-form.com/api/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
|
315
|
-
*
|
316
|
-
* @example
|
317
|
-
* ```tsx
|
318
|
-
* const { watch } = useForm();
|
319
|
-
* const values = useWatch({
|
320
|
-
* name: "fieldName"
|
321
|
-
* control,
|
322
|
-
* })
|
323
|
-
* ```
|
324
|
-
*/
|
325
|
-
function useWatch(props) {
|
326
|
-
const methods = useFormContext();
|
327
|
-
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
328
|
-
const _name = React.useRef(name);
|
329
|
-
_name.current = name;
|
330
|
-
useSubscribe({
|
331
|
-
disabled,
|
332
|
-
subject: control._subjects.values,
|
333
|
-
next: (formState) => {
|
334
|
-
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
335
|
-
updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
|
336
|
-
}
|
337
|
-
},
|
338
|
-
});
|
339
|
-
const [value, updateValue] = React.useState(control._getWatch(name, defaultValue));
|
340
|
-
React.useEffect(() => control._removeUnmounted());
|
341
|
-
return value;
|
309
|
+
/**
|
310
|
+
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
311
|
+
*
|
312
|
+
* @remarks
|
313
|
+
*
|
314
|
+
* [API](https://react-hook-form.com/api/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
|
315
|
+
*
|
316
|
+
* @example
|
317
|
+
* ```tsx
|
318
|
+
* const { watch } = useForm();
|
319
|
+
* const values = useWatch({
|
320
|
+
* name: "fieldName"
|
321
|
+
* control,
|
322
|
+
* })
|
323
|
+
* ```
|
324
|
+
*/
|
325
|
+
function useWatch(props) {
|
326
|
+
const methods = useFormContext();
|
327
|
+
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
328
|
+
const _name = React.useRef(name);
|
329
|
+
_name.current = name;
|
330
|
+
useSubscribe({
|
331
|
+
disabled,
|
332
|
+
subject: control._subjects.values,
|
333
|
+
next: (formState) => {
|
334
|
+
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
335
|
+
updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
|
336
|
+
}
|
337
|
+
},
|
338
|
+
});
|
339
|
+
const [value, updateValue] = React.useState(control._getWatch(name, defaultValue));
|
340
|
+
React.useEffect(() => control._removeUnmounted());
|
341
|
+
return value;
|
342
342
|
}
|
343
343
|
|
344
344
|
var isKey = (value) => /^\w*$/.test(value);
|
345
345
|
|
346
346
|
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
347
347
|
|
348
|
-
function set(object, path, value) {
|
349
|
-
let index = -1;
|
350
|
-
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
351
|
-
const length = tempPath.length;
|
352
|
-
const lastIndex = length - 1;
|
353
|
-
while (++index < length) {
|
354
|
-
const key = tempPath[index];
|
355
|
-
let newValue = value;
|
356
|
-
if (index !== lastIndex) {
|
357
|
-
const objValue = object[key];
|
358
|
-
newValue =
|
359
|
-
isObject(objValue) || Array.isArray(objValue)
|
360
|
-
? objValue
|
361
|
-
: !isNaN(+tempPath[index + 1])
|
362
|
-
? []
|
363
|
-
: {};
|
364
|
-
}
|
365
|
-
object[key] = newValue;
|
366
|
-
object = object[key];
|
367
|
-
}
|
368
|
-
return object;
|
348
|
+
function set(object, path, value) {
|
349
|
+
let index = -1;
|
350
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
351
|
+
const length = tempPath.length;
|
352
|
+
const lastIndex = length - 1;
|
353
|
+
while (++index < length) {
|
354
|
+
const key = tempPath[index];
|
355
|
+
let newValue = value;
|
356
|
+
if (index !== lastIndex) {
|
357
|
+
const objValue = object[key];
|
358
|
+
newValue =
|
359
|
+
isObject(objValue) || Array.isArray(objValue)
|
360
|
+
? objValue
|
361
|
+
: !isNaN(+tempPath[index + 1])
|
362
|
+
? []
|
363
|
+
: {};
|
364
|
+
}
|
365
|
+
object[key] = newValue;
|
366
|
+
object = object[key];
|
367
|
+
}
|
368
|
+
return object;
|
369
369
|
}
|
370
370
|
|
371
|
-
/**
|
372
|
-
* Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
|
373
|
-
*
|
374
|
-
* @remarks
|
375
|
-
* [API](https://react-hook-form.com/api/usecontroller) • [Demo](https://codesandbox.io/s/usecontroller-0o8px)
|
376
|
-
*
|
377
|
-
* @param props - the path name to the form field value, and validation rules.
|
378
|
-
*
|
379
|
-
* @returns field properties, field and form state. {@link UseControllerReturn}
|
380
|
-
*
|
381
|
-
* @example
|
382
|
-
* ```tsx
|
383
|
-
* function Input(props) {
|
384
|
-
* const { field, fieldState, formState } = useController(props);
|
385
|
-
* return (
|
386
|
-
* <div>
|
387
|
-
* <input {...field} placeholder={props.name} />
|
388
|
-
* <p>{fieldState.isTouched && "Touched"}</p>
|
389
|
-
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
390
|
-
* </div>
|
391
|
-
* );
|
392
|
-
* }
|
393
|
-
* ```
|
394
|
-
*/
|
395
|
-
function useController(props) {
|
396
|
-
const methods = useFormContext();
|
397
|
-
const { name, control = methods.control, shouldUnregister } = props;
|
398
|
-
const isArrayField = isNameInFieldArray(control._names.array, name);
|
399
|
-
const value = useWatch({
|
400
|
-
control,
|
401
|
-
name,
|
402
|
-
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
403
|
-
exact: true,
|
404
|
-
});
|
405
|
-
const formState = useFormState({
|
406
|
-
control,
|
407
|
-
name,
|
408
|
-
});
|
409
|
-
const _registerProps = React.useRef(control.register(name, {
|
410
|
-
...props.rules,
|
411
|
-
value,
|
412
|
-
}));
|
413
|
-
React.useEffect(() => {
|
414
|
-
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
415
|
-
const updateMounted = (name, value) => {
|
416
|
-
const field = get(control._fields, name);
|
417
|
-
if (field) {
|
418
|
-
field._f.mount = value;
|
419
|
-
}
|
420
|
-
};
|
421
|
-
updateMounted(name, true);
|
422
|
-
if (_shouldUnregisterField) {
|
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
|
-
enumerable: true,
|
471
|
-
get: () => !!get(formState.
|
472
|
-
},
|
473
|
-
|
474
|
-
enumerable: true,
|
475
|
-
get: () => !!get(formState.
|
476
|
-
},
|
477
|
-
|
478
|
-
enumerable: true,
|
479
|
-
get: () => get(formState.
|
480
|
-
},
|
481
|
-
|
482
|
-
|
371
|
+
/**
|
372
|
+
* Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
|
373
|
+
*
|
374
|
+
* @remarks
|
375
|
+
* [API](https://react-hook-form.com/api/usecontroller) • [Demo](https://codesandbox.io/s/usecontroller-0o8px)
|
376
|
+
*
|
377
|
+
* @param props - the path name to the form field value, and validation rules.
|
378
|
+
*
|
379
|
+
* @returns field properties, field and form state. {@link UseControllerReturn}
|
380
|
+
*
|
381
|
+
* @example
|
382
|
+
* ```tsx
|
383
|
+
* function Input(props) {
|
384
|
+
* const { field, fieldState, formState } = useController(props);
|
385
|
+
* return (
|
386
|
+
* <div>
|
387
|
+
* <input {...field} placeholder={props.name} />
|
388
|
+
* <p>{fieldState.isTouched && "Touched"}</p>
|
389
|
+
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
390
|
+
* </div>
|
391
|
+
* );
|
392
|
+
* }
|
393
|
+
* ```
|
394
|
+
*/
|
395
|
+
function useController(props) {
|
396
|
+
const methods = useFormContext();
|
397
|
+
const { name, control = methods.control, shouldUnregister } = props;
|
398
|
+
const isArrayField = isNameInFieldArray(control._names.array, name);
|
399
|
+
const value = useWatch({
|
400
|
+
control,
|
401
|
+
name,
|
402
|
+
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
403
|
+
exact: true,
|
404
|
+
});
|
405
|
+
const formState = useFormState({
|
406
|
+
control,
|
407
|
+
name,
|
408
|
+
});
|
409
|
+
const _registerProps = React.useRef(control.register(name, {
|
410
|
+
...props.rules,
|
411
|
+
value,
|
412
|
+
}));
|
413
|
+
React.useEffect(() => {
|
414
|
+
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
415
|
+
const updateMounted = (name, value) => {
|
416
|
+
const field = get(control._fields, name);
|
417
|
+
if (field) {
|
418
|
+
field._f.mount = value;
|
419
|
+
}
|
420
|
+
};
|
421
|
+
updateMounted(name, true);
|
422
|
+
if (_shouldUnregisterField) {
|
423
|
+
const value = cloneObject(get(control._options.defaultValues, name));
|
424
|
+
set(control._defaultValues, name, value);
|
425
|
+
if (isUndefined(get(control._formValues, name))) {
|
426
|
+
set(control._formValues, name, value);
|
427
|
+
}
|
428
|
+
}
|
429
|
+
return () => {
|
430
|
+
(isArrayField
|
431
|
+
? _shouldUnregisterField && !control._state.action
|
432
|
+
: _shouldUnregisterField)
|
433
|
+
? control.unregister(name)
|
434
|
+
: updateMounted(name, false);
|
435
|
+
};
|
436
|
+
}, [name, control, isArrayField, shouldUnregister]);
|
437
|
+
return {
|
438
|
+
field: {
|
439
|
+
name,
|
440
|
+
value,
|
441
|
+
onChange: React.useCallback((event) => _registerProps.current.onChange({
|
442
|
+
target: {
|
443
|
+
value: getEventValue(event),
|
444
|
+
name: name,
|
445
|
+
},
|
446
|
+
type: EVENTS.CHANGE,
|
447
|
+
}), [name]),
|
448
|
+
onBlur: React.useCallback(() => _registerProps.current.onBlur({
|
449
|
+
target: {
|
450
|
+
value: get(control._formValues, name),
|
451
|
+
name: name,
|
452
|
+
},
|
453
|
+
type: EVENTS.BLUR,
|
454
|
+
}), [name, control]),
|
455
|
+
ref: (elm) => {
|
456
|
+
const field = get(control._fields, name);
|
457
|
+
if (field && elm) {
|
458
|
+
field._f.ref = {
|
459
|
+
focus: () => elm.focus(),
|
460
|
+
select: () => elm.select(),
|
461
|
+
setCustomValidity: (message) => elm.setCustomValidity(message),
|
462
|
+
reportValidity: () => elm.reportValidity(),
|
463
|
+
};
|
464
|
+
}
|
465
|
+
},
|
466
|
+
},
|
467
|
+
formState,
|
468
|
+
fieldState: Object.defineProperties({}, {
|
469
|
+
invalid: {
|
470
|
+
enumerable: true,
|
471
|
+
get: () => !!get(formState.errors, name),
|
472
|
+
},
|
473
|
+
isDirty: {
|
474
|
+
enumerable: true,
|
475
|
+
get: () => !!get(formState.dirtyFields, name),
|
476
|
+
},
|
477
|
+
isTouched: {
|
478
|
+
enumerable: true,
|
479
|
+
get: () => !!get(formState.touchedFields, name),
|
480
|
+
},
|
481
|
+
error: {
|
482
|
+
enumerable: true,
|
483
|
+
get: () => get(formState.errors, name),
|
484
|
+
},
|
485
|
+
}),
|
486
|
+
};
|
483
487
|
}
|
484
488
|
|
485
|
-
/**
|
486
|
-
* Component based on `useController` hook to work with controlled component.
|
487
|
-
*
|
488
|
-
* @remarks
|
489
|
-
* [API](https://react-hook-form.com/api/usecontroller/controller) • [Demo](https://codesandbox.io/s/react-hook-form-v6-controller-ts-jwyzw) • [Video](https://www.youtube.com/watch?v=N2UNk_UCVyA)
|
490
|
-
*
|
491
|
-
* @param props - the path name to the form field value, and validation rules.
|
492
|
-
*
|
493
|
-
* @returns provide field handler functions, field and form state.
|
494
|
-
*
|
495
|
-
* @example
|
496
|
-
* ```tsx
|
497
|
-
* function App() {
|
498
|
-
* const { control } = useForm<FormValues>({
|
499
|
-
* defaultValues: {
|
500
|
-
* test: ""
|
501
|
-
* }
|
502
|
-
* });
|
503
|
-
*
|
504
|
-
* return (
|
505
|
-
* <form>
|
506
|
-
* <Controller
|
507
|
-
* control={control}
|
508
|
-
* name="test"
|
509
|
-
* render={({ field: { onChange, onBlur, value, ref }, formState, fieldState }) => (
|
510
|
-
* <>
|
511
|
-
* <input
|
512
|
-
* onChange={onChange} // send value to hook form
|
513
|
-
* onBlur={onBlur} // notify when input is touched
|
514
|
-
* value={value} // return updated value
|
515
|
-
* ref={ref} // set ref for focus management
|
516
|
-
* />
|
517
|
-
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
518
|
-
* <p>{fieldState.isTouched ? "touched" : ""}</p>
|
519
|
-
* </>
|
520
|
-
* )}
|
521
|
-
* />
|
522
|
-
* </form>
|
523
|
-
* );
|
524
|
-
* }
|
525
|
-
* ```
|
526
|
-
*/
|
489
|
+
/**
|
490
|
+
* Component based on `useController` hook to work with controlled component.
|
491
|
+
*
|
492
|
+
* @remarks
|
493
|
+
* [API](https://react-hook-form.com/api/usecontroller/controller) • [Demo](https://codesandbox.io/s/react-hook-form-v6-controller-ts-jwyzw) • [Video](https://www.youtube.com/watch?v=N2UNk_UCVyA)
|
494
|
+
*
|
495
|
+
* @param props - the path name to the form field value, and validation rules.
|
496
|
+
*
|
497
|
+
* @returns provide field handler functions, field and form state.
|
498
|
+
*
|
499
|
+
* @example
|
500
|
+
* ```tsx
|
501
|
+
* function App() {
|
502
|
+
* const { control } = useForm<FormValues>({
|
503
|
+
* defaultValues: {
|
504
|
+
* test: ""
|
505
|
+
* }
|
506
|
+
* });
|
507
|
+
*
|
508
|
+
* return (
|
509
|
+
* <form>
|
510
|
+
* <Controller
|
511
|
+
* control={control}
|
512
|
+
* name="test"
|
513
|
+
* render={({ field: { onChange, onBlur, value, ref }, formState, fieldState }) => (
|
514
|
+
* <>
|
515
|
+
* <input
|
516
|
+
* onChange={onChange} // send value to hook form
|
517
|
+
* onBlur={onBlur} // notify when input is touched
|
518
|
+
* value={value} // return updated value
|
519
|
+
* ref={ref} // set ref for focus management
|
520
|
+
* />
|
521
|
+
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
522
|
+
* <p>{fieldState.isTouched ? "touched" : ""}</p>
|
523
|
+
* </>
|
524
|
+
* )}
|
525
|
+
* />
|
526
|
+
* </form>
|
527
|
+
* );
|
528
|
+
* }
|
529
|
+
* ```
|
530
|
+
*/
|
527
531
|
const Controller = (props) => props.render(useController(props));
|
528
532
|
|
529
|
-
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
530
|
-
? {
|
531
|
-
...errors[name],
|
532
|
-
types: {
|
533
|
-
...(errors[name] && errors[name].types ? errors[name].types : {}),
|
534
|
-
[type]: message || true,
|
535
|
-
},
|
536
|
-
}
|
533
|
+
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
534
|
+
? {
|
535
|
+
...errors[name],
|
536
|
+
types: {
|
537
|
+
...(errors[name] && errors[name].types ? errors[name].types : {}),
|
538
|
+
[type]: message || true,
|
539
|
+
},
|
540
|
+
}
|
537
541
|
: {};
|
538
542
|
|
539
|
-
const focusFieldBy = (fields, callback, fieldsNames) => {
|
540
|
-
for (const key of fieldsNames || Object.keys(fields)) {
|
541
|
-
const field = get(fields, key);
|
542
|
-
if (field) {
|
543
|
-
const { _f, ...currentField } = field;
|
544
|
-
if (_f && callback(_f.name)) {
|
545
|
-
if (_f.ref.focus) {
|
546
|
-
_f.ref.focus();
|
547
|
-
break;
|
548
|
-
}
|
549
|
-
else if (_f.refs && _f.refs[0].focus) {
|
550
|
-
_f.refs[0].focus();
|
551
|
-
break;
|
552
|
-
}
|
553
|
-
}
|
554
|
-
else if (isObject(currentField)) {
|
555
|
-
focusFieldBy(currentField, callback);
|
556
|
-
}
|
557
|
-
}
|
558
|
-
}
|
543
|
+
const focusFieldBy = (fields, callback, fieldsNames) => {
|
544
|
+
for (const key of fieldsNames || Object.keys(fields)) {
|
545
|
+
const field = get(fields, key);
|
546
|
+
if (field) {
|
547
|
+
const { _f, ...currentField } = field;
|
548
|
+
if (_f && callback(_f.name)) {
|
549
|
+
if (_f.ref.focus) {
|
550
|
+
_f.ref.focus();
|
551
|
+
break;
|
552
|
+
}
|
553
|
+
else if (_f.refs && _f.refs[0].focus) {
|
554
|
+
_f.refs[0].focus();
|
555
|
+
break;
|
556
|
+
}
|
557
|
+
}
|
558
|
+
else if (isObject(currentField)) {
|
559
|
+
focusFieldBy(currentField, callback);
|
560
|
+
}
|
561
|
+
}
|
562
|
+
}
|
559
563
|
};
|
560
564
|
|
561
|
-
var generateId = () => {
|
562
|
-
const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
|
563
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
564
|
-
const r = (Math.random() * 16 + d) % 16 | 0;
|
565
|
-
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
|
566
|
-
});
|
565
|
+
var generateId = () => {
|
566
|
+
const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
|
567
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
568
|
+
const r = (Math.random() * 16 + d) % 16 | 0;
|
569
|
+
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
|
570
|
+
});
|
567
571
|
};
|
568
572
|
|
569
|
-
var getFocusFieldName = (name, index, options = {}) => options.shouldFocus || isUndefined(options.shouldFocus)
|
570
|
-
? options.focusName ||
|
571
|
-
`${name}.${isUndefined(options.focusIndex) ? index : options.focusIndex}.`
|
573
|
+
var getFocusFieldName = (name, index, options = {}) => options.shouldFocus || isUndefined(options.shouldFocus)
|
574
|
+
? options.focusName ||
|
575
|
+
`${name}.${isUndefined(options.focusIndex) ? index : options.focusIndex}.`
|
572
576
|
: '';
|
573
577
|
|
574
|
-
var getValidationModes = (mode) => ({
|
575
|
-
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
576
|
-
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
577
|
-
isOnChange: mode === VALIDATION_MODE.onChange,
|
578
|
-
isOnAll: mode === VALIDATION_MODE.all,
|
579
|
-
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
578
|
+
var getValidationModes = (mode) => ({
|
579
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
580
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
581
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
582
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
583
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
580
584
|
});
|
581
585
|
|
582
|
-
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
583
|
-
(_names.watchAll ||
|
584
|
-
_names.watch.has(name) ||
|
585
|
-
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
586
|
+
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
587
|
+
(_names.watchAll ||
|
588
|
+
_names.watch.has(name) ||
|
589
|
+
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
586
590
|
/^\.\w+/.test(name.slice(watchName.length))));
|
587
591
|
|
588
|
-
var updateFieldArrayRootError = (errors, error, name) => {
|
589
|
-
const fieldArrayErrors = compact(get(errors, name));
|
590
|
-
set(fieldArrayErrors, 'root', error[name]);
|
591
|
-
set(errors, name, fieldArrayErrors);
|
592
|
-
return errors;
|
592
|
+
var updateFieldArrayRootError = (errors, error, name) => {
|
593
|
+
const fieldArrayErrors = compact(get(errors, name));
|
594
|
+
set(fieldArrayErrors, 'root', error[name]);
|
595
|
+
set(errors, name, fieldArrayErrors);
|
596
|
+
return errors;
|
593
597
|
};
|
594
598
|
|
595
599
|
var isBoolean = (value) => typeof value === 'boolean';
|
@@ -598,13 +602,13 @@ var isFileInput = (element) => element.type === 'file';
|
|
598
602
|
|
599
603
|
var isFunction = (value) => typeof value === 'function';
|
600
604
|
|
601
|
-
var isHTMLElement = (value) => {
|
602
|
-
if (!isWeb) {
|
603
|
-
return false;
|
604
|
-
}
|
605
|
-
const owner = value ? value.ownerDocument : 0;
|
606
|
-
return (value instanceof
|
607
|
-
(owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
|
605
|
+
var isHTMLElement = (value) => {
|
606
|
+
if (!isWeb) {
|
607
|
+
return false;
|
608
|
+
}
|
609
|
+
const owner = value ? value.ownerDocument : 0;
|
610
|
+
return (value instanceof
|
611
|
+
(owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
|
608
612
|
};
|
609
613
|
|
610
614
|
var isMessage = (value) => isString(value);
|
@@ -613,602 +617,602 @@ var isRadioInput = (element) => element.type === 'radio';
|
|
613
617
|
|
614
618
|
var isRegex = (value) => value instanceof RegExp;
|
615
619
|
|
616
|
-
const defaultResult = {
|
617
|
-
value: false,
|
618
|
-
isValid: false,
|
619
|
-
};
|
620
|
-
const validResult = { value: true, isValid: true };
|
621
|
-
var getCheckboxValue = (options) => {
|
622
|
-
if (Array.isArray(options)) {
|
623
|
-
if (options.length > 1) {
|
624
|
-
const values = options
|
625
|
-
.filter((option) => option && option.checked && !option.disabled)
|
626
|
-
.map((option) => option.value);
|
627
|
-
return { value: values, isValid: !!values.length };
|
628
|
-
}
|
629
|
-
return options[0].checked && !options[0].disabled
|
630
|
-
? // @ts-expect-error expected to work in the browser
|
631
|
-
options[0].attributes && !isUndefined(options[0].attributes.value)
|
632
|
-
? isUndefined(options[0].value) || options[0].value === ''
|
633
|
-
? validResult
|
634
|
-
: { value: options[0].value, isValid: true }
|
635
|
-
: validResult
|
636
|
-
: defaultResult;
|
637
|
-
}
|
638
|
-
return defaultResult;
|
620
|
+
const defaultResult = {
|
621
|
+
value: false,
|
622
|
+
isValid: false,
|
623
|
+
};
|
624
|
+
const validResult = { value: true, isValid: true };
|
625
|
+
var getCheckboxValue = (options) => {
|
626
|
+
if (Array.isArray(options)) {
|
627
|
+
if (options.length > 1) {
|
628
|
+
const values = options
|
629
|
+
.filter((option) => option && option.checked && !option.disabled)
|
630
|
+
.map((option) => option.value);
|
631
|
+
return { value: values, isValid: !!values.length };
|
632
|
+
}
|
633
|
+
return options[0].checked && !options[0].disabled
|
634
|
+
? // @ts-expect-error expected to work in the browser
|
635
|
+
options[0].attributes && !isUndefined(options[0].attributes.value)
|
636
|
+
? isUndefined(options[0].value) || options[0].value === ''
|
637
|
+
? validResult
|
638
|
+
: { value: options[0].value, isValid: true }
|
639
|
+
: validResult
|
640
|
+
: defaultResult;
|
641
|
+
}
|
642
|
+
return defaultResult;
|
639
643
|
};
|
640
644
|
|
641
|
-
const defaultReturn = {
|
642
|
-
isValid: false,
|
643
|
-
value: null,
|
644
|
-
};
|
645
|
-
var getRadioValue = (options) => Array.isArray(options)
|
646
|
-
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
647
|
-
? {
|
648
|
-
isValid: true,
|
649
|
-
value: option.value,
|
650
|
-
}
|
651
|
-
: previous, defaultReturn)
|
645
|
+
const defaultReturn = {
|
646
|
+
isValid: false,
|
647
|
+
value: null,
|
648
|
+
};
|
649
|
+
var getRadioValue = (options) => Array.isArray(options)
|
650
|
+
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
651
|
+
? {
|
652
|
+
isValid: true,
|
653
|
+
value: option.value,
|
654
|
+
}
|
655
|
+
: previous, defaultReturn)
|
652
656
|
: defaultReturn;
|
653
657
|
|
654
|
-
function getValidateError(result, ref, type = 'validate') {
|
655
|
-
if (isMessage(result) ||
|
656
|
-
(Array.isArray(result) && result.every(isMessage)) ||
|
657
|
-
(isBoolean(result) && !result)) {
|
658
|
-
return {
|
659
|
-
type,
|
660
|
-
message: isMessage(result) ? result : '',
|
661
|
-
ref,
|
662
|
-
};
|
663
|
-
}
|
658
|
+
function getValidateError(result, ref, type = 'validate') {
|
659
|
+
if (isMessage(result) ||
|
660
|
+
(Array.isArray(result) && result.every(isMessage)) ||
|
661
|
+
(isBoolean(result) && !result)) {
|
662
|
+
return {
|
663
|
+
type,
|
664
|
+
message: isMessage(result) ? result : '',
|
665
|
+
ref,
|
666
|
+
};
|
667
|
+
}
|
664
668
|
}
|
665
669
|
|
666
|
-
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData)
|
667
|
-
? validationData
|
668
|
-
: {
|
669
|
-
value: validationData,
|
670
|
-
message: '',
|
670
|
+
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData)
|
671
|
+
? validationData
|
672
|
+
: {
|
673
|
+
value: validationData,
|
674
|
+
message: '',
|
671
675
|
};
|
672
676
|
|
673
|
-
var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
674
|
-
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
|
675
|
-
const inputValue = get(formValues, name);
|
676
|
-
if (!mount || disabled) {
|
677
|
-
return {};
|
678
|
-
}
|
679
|
-
const inputRef = refs ? refs[0] : ref;
|
680
|
-
const setCustomValidity = (message) => {
|
681
|
-
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
682
|
-
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
683
|
-
inputRef.reportValidity();
|
684
|
-
}
|
685
|
-
};
|
686
|
-
const error = {};
|
687
|
-
const isRadio = isRadioInput(ref);
|
688
|
-
const isCheckBox = isCheckBoxInput(ref);
|
689
|
-
const isRadioOrCheckbox = isRadio || isCheckBox;
|
690
|
-
const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
|
691
|
-
isUndefined(ref.value) &&
|
692
|
-
isUndefined(inputValue)) ||
|
693
|
-
(isHTMLElement(ref) && ref.value === '') ||
|
694
|
-
inputValue === '' ||
|
695
|
-
(Array.isArray(inputValue) && !inputValue.length);
|
696
|
-
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
697
|
-
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
698
|
-
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
699
|
-
error[name] = {
|
700
|
-
type: exceedMax ? maxType : minType,
|
701
|
-
message,
|
702
|
-
ref,
|
703
|
-
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
704
|
-
};
|
705
|
-
};
|
706
|
-
if (isFieldArray
|
707
|
-
? !Array.isArray(inputValue) || !inputValue.length
|
708
|
-
: required &&
|
709
|
-
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
710
|
-
(isBoolean(inputValue) && !inputValue) ||
|
711
|
-
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
712
|
-
(isRadio && !getRadioValue(refs).isValid))) {
|
713
|
-
const { value, message } = isMessage(required)
|
714
|
-
? { value: !!required, message: required }
|
715
|
-
: getValueAndMessage(required);
|
716
|
-
if (value) {
|
717
|
-
error[name] = {
|
718
|
-
type: INPUT_VALIDATION_RULES.required,
|
719
|
-
message,
|
720
|
-
ref: inputRef,
|
721
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
722
|
-
};
|
723
|
-
if (!validateAllFieldCriteria) {
|
724
|
-
setCustomValidity(message);
|
725
|
-
return error;
|
726
|
-
}
|
727
|
-
}
|
728
|
-
}
|
729
|
-
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
730
|
-
let exceedMax;
|
731
|
-
let exceedMin;
|
732
|
-
const maxOutput = getValueAndMessage(max);
|
733
|
-
const minOutput = getValueAndMessage(min);
|
734
|
-
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
735
|
-
const valueNumber = ref.valueAsNumber ||
|
736
|
-
(inputValue ? +inputValue : inputValue);
|
737
|
-
if (!isNullOrUndefined(maxOutput.value)) {
|
738
|
-
exceedMax = valueNumber > maxOutput.value;
|
739
|
-
}
|
740
|
-
if (!isNullOrUndefined(minOutput.value)) {
|
741
|
-
exceedMin = valueNumber < minOutput.value;
|
742
|
-
}
|
743
|
-
}
|
744
|
-
else {
|
745
|
-
const valueDate = ref.valueAsDate || new Date(inputValue);
|
746
|
-
const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
|
747
|
-
const isTime = ref.type == 'time';
|
748
|
-
const isWeek = ref.type == 'week';
|
749
|
-
if (isString(maxOutput.value) && inputValue) {
|
750
|
-
exceedMax = isTime
|
751
|
-
? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
|
752
|
-
: isWeek
|
753
|
-
? inputValue > maxOutput.value
|
754
|
-
: valueDate > new Date(maxOutput.value);
|
755
|
-
}
|
756
|
-
if (isString(minOutput.value) && inputValue) {
|
757
|
-
exceedMin = isTime
|
758
|
-
? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
|
759
|
-
: isWeek
|
760
|
-
? inputValue < minOutput.value
|
761
|
-
: valueDate < new Date(minOutput.value);
|
762
|
-
}
|
763
|
-
}
|
764
|
-
if (exceedMax || exceedMin) {
|
765
|
-
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
766
|
-
if (!validateAllFieldCriteria) {
|
767
|
-
setCustomValidity(error[name].message);
|
768
|
-
return error;
|
769
|
-
}
|
770
|
-
}
|
771
|
-
}
|
772
|
-
if ((maxLength || minLength) &&
|
773
|
-
!isEmpty &&
|
774
|
-
(isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
|
775
|
-
const maxLengthOutput = getValueAndMessage(maxLength);
|
776
|
-
const minLengthOutput = getValueAndMessage(minLength);
|
777
|
-
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
778
|
-
inputValue.length > maxLengthOutput.value;
|
779
|
-
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
780
|
-
inputValue.length < minLengthOutput.value;
|
781
|
-
if (exceedMax || exceedMin) {
|
782
|
-
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
783
|
-
if (!validateAllFieldCriteria) {
|
784
|
-
setCustomValidity(error[name].message);
|
785
|
-
return error;
|
786
|
-
}
|
787
|
-
}
|
788
|
-
}
|
789
|
-
if (pattern && !isEmpty && isString(inputValue)) {
|
790
|
-
const { value: patternValue, message } = getValueAndMessage(pattern);
|
791
|
-
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
792
|
-
error[name] = {
|
793
|
-
type: INPUT_VALIDATION_RULES.pattern,
|
794
|
-
message,
|
795
|
-
ref,
|
796
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
797
|
-
};
|
798
|
-
if (!validateAllFieldCriteria) {
|
799
|
-
setCustomValidity(message);
|
800
|
-
return error;
|
801
|
-
}
|
802
|
-
}
|
803
|
-
}
|
804
|
-
if (validate) {
|
805
|
-
if (isFunction(validate)) {
|
806
|
-
const result = await validate(inputValue, formValues);
|
807
|
-
const validateError = getValidateError(result, inputRef);
|
808
|
-
if (validateError) {
|
809
|
-
error[name] = {
|
810
|
-
...validateError,
|
811
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
812
|
-
};
|
813
|
-
if (!validateAllFieldCriteria) {
|
814
|
-
setCustomValidity(validateError.message);
|
815
|
-
return error;
|
816
|
-
}
|
817
|
-
}
|
818
|
-
}
|
819
|
-
else if (isObject(validate)) {
|
820
|
-
let validationResult = {};
|
821
|
-
for (const key in validate) {
|
822
|
-
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
823
|
-
break;
|
824
|
-
}
|
825
|
-
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
826
|
-
if (validateError) {
|
827
|
-
validationResult = {
|
828
|
-
...validateError,
|
829
|
-
...appendErrorsCurry(key, validateError.message),
|
830
|
-
};
|
831
|
-
setCustomValidity(validateError.message);
|
832
|
-
if (validateAllFieldCriteria) {
|
833
|
-
error[name] = validationResult;
|
834
|
-
}
|
835
|
-
}
|
836
|
-
}
|
837
|
-
if (!isEmptyObject(validationResult)) {
|
838
|
-
error[name] = {
|
839
|
-
ref: inputRef,
|
840
|
-
...validationResult,
|
841
|
-
};
|
842
|
-
if (!validateAllFieldCriteria) {
|
843
|
-
return error;
|
844
|
-
}
|
845
|
-
}
|
846
|
-
}
|
847
|
-
}
|
848
|
-
setCustomValidity(true);
|
849
|
-
return error;
|
677
|
+
var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
678
|
+
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
|
679
|
+
const inputValue = get(formValues, name);
|
680
|
+
if (!mount || disabled) {
|
681
|
+
return {};
|
682
|
+
}
|
683
|
+
const inputRef = refs ? refs[0] : ref;
|
684
|
+
const setCustomValidity = (message) => {
|
685
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
686
|
+
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
687
|
+
inputRef.reportValidity();
|
688
|
+
}
|
689
|
+
};
|
690
|
+
const error = {};
|
691
|
+
const isRadio = isRadioInput(ref);
|
692
|
+
const isCheckBox = isCheckBoxInput(ref);
|
693
|
+
const isRadioOrCheckbox = isRadio || isCheckBox;
|
694
|
+
const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
|
695
|
+
isUndefined(ref.value) &&
|
696
|
+
isUndefined(inputValue)) ||
|
697
|
+
(isHTMLElement(ref) && ref.value === '') ||
|
698
|
+
inputValue === '' ||
|
699
|
+
(Array.isArray(inputValue) && !inputValue.length);
|
700
|
+
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
701
|
+
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
702
|
+
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
703
|
+
error[name] = {
|
704
|
+
type: exceedMax ? maxType : minType,
|
705
|
+
message,
|
706
|
+
ref,
|
707
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
708
|
+
};
|
709
|
+
};
|
710
|
+
if (isFieldArray
|
711
|
+
? !Array.isArray(inputValue) || !inputValue.length
|
712
|
+
: required &&
|
713
|
+
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
714
|
+
(isBoolean(inputValue) && !inputValue) ||
|
715
|
+
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
716
|
+
(isRadio && !getRadioValue(refs).isValid))) {
|
717
|
+
const { value, message } = isMessage(required)
|
718
|
+
? { value: !!required, message: required }
|
719
|
+
: getValueAndMessage(required);
|
720
|
+
if (value) {
|
721
|
+
error[name] = {
|
722
|
+
type: INPUT_VALIDATION_RULES.required,
|
723
|
+
message,
|
724
|
+
ref: inputRef,
|
725
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
726
|
+
};
|
727
|
+
if (!validateAllFieldCriteria) {
|
728
|
+
setCustomValidity(message);
|
729
|
+
return error;
|
730
|
+
}
|
731
|
+
}
|
732
|
+
}
|
733
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
734
|
+
let exceedMax;
|
735
|
+
let exceedMin;
|
736
|
+
const maxOutput = getValueAndMessage(max);
|
737
|
+
const minOutput = getValueAndMessage(min);
|
738
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
739
|
+
const valueNumber = ref.valueAsNumber ||
|
740
|
+
(inputValue ? +inputValue : inputValue);
|
741
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
742
|
+
exceedMax = valueNumber > maxOutput.value;
|
743
|
+
}
|
744
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
745
|
+
exceedMin = valueNumber < minOutput.value;
|
746
|
+
}
|
747
|
+
}
|
748
|
+
else {
|
749
|
+
const valueDate = ref.valueAsDate || new Date(inputValue);
|
750
|
+
const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
|
751
|
+
const isTime = ref.type == 'time';
|
752
|
+
const isWeek = ref.type == 'week';
|
753
|
+
if (isString(maxOutput.value) && inputValue) {
|
754
|
+
exceedMax = isTime
|
755
|
+
? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
|
756
|
+
: isWeek
|
757
|
+
? inputValue > maxOutput.value
|
758
|
+
: valueDate > new Date(maxOutput.value);
|
759
|
+
}
|
760
|
+
if (isString(minOutput.value) && inputValue) {
|
761
|
+
exceedMin = isTime
|
762
|
+
? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
|
763
|
+
: isWeek
|
764
|
+
? inputValue < minOutput.value
|
765
|
+
: valueDate < new Date(minOutput.value);
|
766
|
+
}
|
767
|
+
}
|
768
|
+
if (exceedMax || exceedMin) {
|
769
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
770
|
+
if (!validateAllFieldCriteria) {
|
771
|
+
setCustomValidity(error[name].message);
|
772
|
+
return error;
|
773
|
+
}
|
774
|
+
}
|
775
|
+
}
|
776
|
+
if ((maxLength || minLength) &&
|
777
|
+
!isEmpty &&
|
778
|
+
(isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
|
779
|
+
const maxLengthOutput = getValueAndMessage(maxLength);
|
780
|
+
const minLengthOutput = getValueAndMessage(minLength);
|
781
|
+
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
782
|
+
inputValue.length > +maxLengthOutput.value;
|
783
|
+
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
784
|
+
inputValue.length < +minLengthOutput.value;
|
785
|
+
if (exceedMax || exceedMin) {
|
786
|
+
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
787
|
+
if (!validateAllFieldCriteria) {
|
788
|
+
setCustomValidity(error[name].message);
|
789
|
+
return error;
|
790
|
+
}
|
791
|
+
}
|
792
|
+
}
|
793
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
794
|
+
const { value: patternValue, message } = getValueAndMessage(pattern);
|
795
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
796
|
+
error[name] = {
|
797
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
798
|
+
message,
|
799
|
+
ref,
|
800
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
801
|
+
};
|
802
|
+
if (!validateAllFieldCriteria) {
|
803
|
+
setCustomValidity(message);
|
804
|
+
return error;
|
805
|
+
}
|
806
|
+
}
|
807
|
+
}
|
808
|
+
if (validate) {
|
809
|
+
if (isFunction(validate)) {
|
810
|
+
const result = await validate(inputValue, formValues);
|
811
|
+
const validateError = getValidateError(result, inputRef);
|
812
|
+
if (validateError) {
|
813
|
+
error[name] = {
|
814
|
+
...validateError,
|
815
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
816
|
+
};
|
817
|
+
if (!validateAllFieldCriteria) {
|
818
|
+
setCustomValidity(validateError.message);
|
819
|
+
return error;
|
820
|
+
}
|
821
|
+
}
|
822
|
+
}
|
823
|
+
else if (isObject(validate)) {
|
824
|
+
let validationResult = {};
|
825
|
+
for (const key in validate) {
|
826
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
827
|
+
break;
|
828
|
+
}
|
829
|
+
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
830
|
+
if (validateError) {
|
831
|
+
validationResult = {
|
832
|
+
...validateError,
|
833
|
+
...appendErrorsCurry(key, validateError.message),
|
834
|
+
};
|
835
|
+
setCustomValidity(validateError.message);
|
836
|
+
if (validateAllFieldCriteria) {
|
837
|
+
error[name] = validationResult;
|
838
|
+
}
|
839
|
+
}
|
840
|
+
}
|
841
|
+
if (!isEmptyObject(validationResult)) {
|
842
|
+
error[name] = {
|
843
|
+
ref: inputRef,
|
844
|
+
...validationResult,
|
845
|
+
};
|
846
|
+
if (!validateAllFieldCriteria) {
|
847
|
+
return error;
|
848
|
+
}
|
849
|
+
}
|
850
|
+
}
|
851
|
+
}
|
852
|
+
setCustomValidity(true);
|
853
|
+
return error;
|
850
854
|
};
|
851
855
|
|
852
|
-
function append(data, value) {
|
853
|
-
return [...data, ...convertToArrayPayload(value)];
|
856
|
+
function append(data, value) {
|
857
|
+
return [...data, ...convertToArrayPayload(value)];
|
854
858
|
}
|
855
859
|
|
856
860
|
var fillEmptyArray = (value) => Array.isArray(value) ? value.map(() => undefined) : undefined;
|
857
861
|
|
858
|
-
function insert(data, index, value) {
|
859
|
-
return [
|
860
|
-
...data.slice(0, index),
|
861
|
-
...convertToArrayPayload(value),
|
862
|
-
...data.slice(index),
|
863
|
-
];
|
862
|
+
function insert(data, index, value) {
|
863
|
+
return [
|
864
|
+
...data.slice(0, index),
|
865
|
+
...convertToArrayPayload(value),
|
866
|
+
...data.slice(index),
|
867
|
+
];
|
864
868
|
}
|
865
869
|
|
866
|
-
var moveArrayAt = (data, from, to) => {
|
867
|
-
if (!Array.isArray(data)) {
|
868
|
-
return [];
|
869
|
-
}
|
870
|
-
if (isUndefined(data[to])) {
|
871
|
-
data[to] = undefined;
|
872
|
-
}
|
873
|
-
data.splice(to, 0, data.splice(from, 1)[0]);
|
874
|
-
return data;
|
870
|
+
var moveArrayAt = (data, from, to) => {
|
871
|
+
if (!Array.isArray(data)) {
|
872
|
+
return [];
|
873
|
+
}
|
874
|
+
if (isUndefined(data[to])) {
|
875
|
+
data[to] = undefined;
|
876
|
+
}
|
877
|
+
data.splice(to, 0, data.splice(from, 1)[0]);
|
878
|
+
return data;
|
875
879
|
};
|
876
880
|
|
877
|
-
function prepend(data, value) {
|
878
|
-
return [...convertToArrayPayload(value), ...convertToArrayPayload(data)];
|
881
|
+
function prepend(data, value) {
|
882
|
+
return [...convertToArrayPayload(value), ...convertToArrayPayload(data)];
|
879
883
|
}
|
880
884
|
|
881
|
-
function removeAtIndexes(data, indexes) {
|
882
|
-
let i = 0;
|
883
|
-
const temp = [...data];
|
884
|
-
for (const index of indexes) {
|
885
|
-
temp.splice(index - i, 1);
|
886
|
-
i++;
|
887
|
-
}
|
888
|
-
return compact(temp).length ? temp : [];
|
889
|
-
}
|
890
|
-
var removeArrayAt = (data, index) => isUndefined(index)
|
891
|
-
? []
|
885
|
+
function removeAtIndexes(data, indexes) {
|
886
|
+
let i = 0;
|
887
|
+
const temp = [...data];
|
888
|
+
for (const index of indexes) {
|
889
|
+
temp.splice(index - i, 1);
|
890
|
+
i++;
|
891
|
+
}
|
892
|
+
return compact(temp).length ? temp : [];
|
893
|
+
}
|
894
|
+
var removeArrayAt = (data, index) => isUndefined(index)
|
895
|
+
? []
|
892
896
|
: removeAtIndexes(data, convertToArrayPayload(index).sort((a, b) => a - b));
|
893
897
|
|
894
|
-
var swapArrayAt = (data, indexA, indexB) => {
|
895
|
-
data[indexA] = [data[indexB], (data[indexB] = data[indexA])][0];
|
898
|
+
var swapArrayAt = (data, indexA, indexB) => {
|
899
|
+
data[indexA] = [data[indexB], (data[indexB] = data[indexA])][0];
|
896
900
|
};
|
897
901
|
|
898
|
-
function baseGet(object, updatePath) {
|
899
|
-
const length = updatePath.slice(0, -1).length;
|
900
|
-
let index = 0;
|
901
|
-
while (index < length) {
|
902
|
-
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
903
|
-
}
|
904
|
-
return object;
|
905
|
-
}
|
906
|
-
function isEmptyArray(obj) {
|
907
|
-
for (const key in obj) {
|
908
|
-
if (!isUndefined(obj[key])) {
|
909
|
-
return false;
|
910
|
-
}
|
911
|
-
}
|
912
|
-
return true;
|
913
|
-
}
|
914
|
-
function unset(object, path) {
|
915
|
-
const paths = Array.isArray(path)
|
916
|
-
? path
|
917
|
-
: isKey(path)
|
918
|
-
? [path]
|
919
|
-
: stringToPath(path);
|
920
|
-
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
921
|
-
const index = paths.length - 1;
|
922
|
-
const key = paths[index];
|
923
|
-
if (childObject) {
|
924
|
-
delete childObject[key];
|
925
|
-
}
|
926
|
-
if (index !== 0 &&
|
927
|
-
((isObject(childObject) && isEmptyObject(childObject)) ||
|
928
|
-
(Array.isArray(childObject) && isEmptyArray(childObject)))) {
|
929
|
-
unset(object, paths.slice(0, -1));
|
930
|
-
}
|
931
|
-
return object;
|
902
|
+
function baseGet(object, updatePath) {
|
903
|
+
const length = updatePath.slice(0, -1).length;
|
904
|
+
let index = 0;
|
905
|
+
while (index < length) {
|
906
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
907
|
+
}
|
908
|
+
return object;
|
909
|
+
}
|
910
|
+
function isEmptyArray(obj) {
|
911
|
+
for (const key in obj) {
|
912
|
+
if (!isUndefined(obj[key])) {
|
913
|
+
return false;
|
914
|
+
}
|
915
|
+
}
|
916
|
+
return true;
|
917
|
+
}
|
918
|
+
function unset(object, path) {
|
919
|
+
const paths = Array.isArray(path)
|
920
|
+
? path
|
921
|
+
: isKey(path)
|
922
|
+
? [path]
|
923
|
+
: stringToPath(path);
|
924
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
925
|
+
const index = paths.length - 1;
|
926
|
+
const key = paths[index];
|
927
|
+
if (childObject) {
|
928
|
+
delete childObject[key];
|
929
|
+
}
|
930
|
+
if (index !== 0 &&
|
931
|
+
((isObject(childObject) && isEmptyObject(childObject)) ||
|
932
|
+
(Array.isArray(childObject) && isEmptyArray(childObject)))) {
|
933
|
+
unset(object, paths.slice(0, -1));
|
934
|
+
}
|
935
|
+
return object;
|
932
936
|
}
|
933
937
|
|
934
|
-
var updateAt = (fieldValues, index, value) => {
|
935
|
-
fieldValues[index] = value;
|
936
|
-
return fieldValues;
|
938
|
+
var updateAt = (fieldValues, index, value) => {
|
939
|
+
fieldValues[index] = value;
|
940
|
+
return fieldValues;
|
937
941
|
};
|
938
942
|
|
939
|
-
/**
|
940
|
-
* A custom hook that exposes convenient methods to perform operations with a list of dynamic inputs that need to be appended, updated, removed etc. • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn) • [Video](https://youtu.be/4MrbfGSFY2A)
|
941
|
-
*
|
942
|
-
* @remarks
|
943
|
-
* [API](https://react-hook-form.com/api/usefieldarray) • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn)
|
944
|
-
*
|
945
|
-
* @param props - useFieldArray props
|
946
|
-
*
|
947
|
-
* @returns methods - functions to manipulate with the Field Arrays (dynamic inputs) {@link UseFieldArrayReturn}
|
948
|
-
*
|
949
|
-
* @example
|
950
|
-
* ```tsx
|
951
|
-
* function App() {
|
952
|
-
* const { register, control, handleSubmit, reset, trigger, setError } = useForm({
|
953
|
-
* defaultValues: {
|
954
|
-
* test: []
|
955
|
-
* }
|
956
|
-
* });
|
957
|
-
* const { fields, append } = useFieldArray({
|
958
|
-
* control,
|
959
|
-
* name: "test"
|
960
|
-
* });
|
961
|
-
*
|
962
|
-
* return (
|
963
|
-
* <form onSubmit={handleSubmit(data => console.log(data))}>
|
964
|
-
* {fields.map((item, index) => (
|
965
|
-
* <input key={item.id} {...register(`test.${index}.firstName`)} />
|
966
|
-
* ))}
|
967
|
-
* <button type="button" onClick={() => append({ firstName: "bill" })}>
|
968
|
-
* append
|
969
|
-
* </button>
|
970
|
-
* <input type="submit" />
|
971
|
-
* </form>
|
972
|
-
* );
|
973
|
-
* }
|
974
|
-
* ```
|
975
|
-
*/
|
976
|
-
function useFieldArray(props) {
|
977
|
-
const methods = useFormContext();
|
978
|
-
const { control = methods.control, name, keyName = 'id', shouldUnregister, } = props;
|
979
|
-
const [fields, setFields] = React.useState(control._getFieldArray(name));
|
980
|
-
const ids = React.useRef(control._getFieldArray(name).map(generateId));
|
981
|
-
const _fieldIds = React.useRef(fields);
|
982
|
-
const _name = React.useRef(name);
|
983
|
-
const _actioned = React.useRef(false);
|
984
|
-
_name.current = name;
|
985
|
-
_fieldIds.current = fields;
|
986
|
-
control._names.array.add(name);
|
987
|
-
props.rules &&
|
988
|
-
control.register(name, props.rules);
|
989
|
-
useSubscribe({
|
990
|
-
next: ({ values, name: fieldArrayName, }) => {
|
991
|
-
if (fieldArrayName === _name.current || !fieldArrayName) {
|
992
|
-
const fieldValues = get(values, _name.current);
|
993
|
-
if (Array.isArray(fieldValues)) {
|
994
|
-
setFields(fieldValues);
|
995
|
-
ids.current = fieldValues.map(generateId);
|
996
|
-
}
|
997
|
-
}
|
998
|
-
},
|
999
|
-
subject: control._subjects.array,
|
1000
|
-
});
|
1001
|
-
const updateValues = React.useCallback((updatedFieldArrayValues) => {
|
1002
|
-
_actioned.current = true;
|
1003
|
-
control._updateFieldArray(name, updatedFieldArrayValues);
|
1004
|
-
}, [control, name]);
|
1005
|
-
const append$1 = (value, options) => {
|
1006
|
-
const appendValue = convertToArrayPayload(cloneObject(value));
|
1007
|
-
const updatedFieldArrayValues = append(control._getFieldArray(name), appendValue);
|
1008
|
-
control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
|
1009
|
-
ids.current = append(ids.current, appendValue.map(generateId));
|
1010
|
-
updateValues(updatedFieldArrayValues);
|
1011
|
-
setFields(updatedFieldArrayValues);
|
1012
|
-
control._updateFieldArray(name, updatedFieldArrayValues, append, {
|
1013
|
-
argA: fillEmptyArray(value),
|
1014
|
-
});
|
1015
|
-
};
|
1016
|
-
const prepend$1 = (value, options) => {
|
1017
|
-
const prependValue = convertToArrayPayload(cloneObject(value));
|
1018
|
-
const updatedFieldArrayValues = prepend(control._getFieldArray(name), prependValue);
|
1019
|
-
control._names.focus = getFocusFieldName(name, 0, options);
|
1020
|
-
ids.current = prepend(ids.current, prependValue.map(generateId));
|
1021
|
-
updateValues(updatedFieldArrayValues);
|
1022
|
-
setFields(updatedFieldArrayValues);
|
1023
|
-
control._updateFieldArray(name, updatedFieldArrayValues, prepend, {
|
1024
|
-
argA: fillEmptyArray(value),
|
1025
|
-
});
|
1026
|
-
};
|
1027
|
-
const remove = (index) => {
|
1028
|
-
const updatedFieldArrayValues = removeArrayAt(control._getFieldArray(name), index);
|
1029
|
-
ids.current = removeArrayAt(ids.current, index);
|
1030
|
-
updateValues(updatedFieldArrayValues);
|
1031
|
-
setFields(updatedFieldArrayValues);
|
1032
|
-
control._updateFieldArray(name, updatedFieldArrayValues, removeArrayAt, {
|
1033
|
-
argA: index,
|
1034
|
-
});
|
1035
|
-
};
|
1036
|
-
const insert$1 = (index, value, options) => {
|
1037
|
-
const insertValue = convertToArrayPayload(cloneObject(value));
|
1038
|
-
const updatedFieldArrayValues = insert(control._getFieldArray(name), index, insertValue);
|
1039
|
-
control._names.focus = getFocusFieldName(name, index, options);
|
1040
|
-
ids.current = insert(ids.current, index, insertValue.map(generateId));
|
1041
|
-
updateValues(updatedFieldArrayValues);
|
1042
|
-
setFields(updatedFieldArrayValues);
|
1043
|
-
control._updateFieldArray(name, updatedFieldArrayValues, insert, {
|
1044
|
-
argA: index,
|
1045
|
-
argB: fillEmptyArray(value),
|
1046
|
-
});
|
1047
|
-
};
|
1048
|
-
const swap = (indexA, indexB) => {
|
1049
|
-
const updatedFieldArrayValues = control._getFieldArray(name);
|
1050
|
-
swapArrayAt(updatedFieldArrayValues, indexA, indexB);
|
1051
|
-
swapArrayAt(ids.current, indexA, indexB);
|
1052
|
-
updateValues(updatedFieldArrayValues);
|
1053
|
-
setFields(updatedFieldArrayValues);
|
1054
|
-
control._updateFieldArray(name, updatedFieldArrayValues, swapArrayAt, {
|
1055
|
-
argA: indexA,
|
1056
|
-
argB: indexB,
|
1057
|
-
}, false);
|
1058
|
-
};
|
1059
|
-
const move = (from, to) => {
|
1060
|
-
const updatedFieldArrayValues = control._getFieldArray(name);
|
1061
|
-
moveArrayAt(updatedFieldArrayValues, from, to);
|
1062
|
-
moveArrayAt(ids.current, from, to);
|
1063
|
-
updateValues(updatedFieldArrayValues);
|
1064
|
-
setFields(updatedFieldArrayValues);
|
1065
|
-
control._updateFieldArray(name, updatedFieldArrayValues, moveArrayAt, {
|
1066
|
-
argA: from,
|
1067
|
-
argB: to,
|
1068
|
-
}, false);
|
1069
|
-
};
|
1070
|
-
const update = (index, value) => {
|
1071
|
-
const updateValue = cloneObject(value);
|
1072
|
-
const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
|
1073
|
-
ids.current = [...updatedFieldArrayValues].map((item, i) => !item || i === index ? generateId() : ids.current[i]);
|
1074
|
-
updateValues(updatedFieldArrayValues);
|
1075
|
-
setFields([...updatedFieldArrayValues]);
|
1076
|
-
control._updateFieldArray(name, updatedFieldArrayValues, updateAt, {
|
1077
|
-
argA: index,
|
1078
|
-
argB: updateValue,
|
1079
|
-
}, true, false);
|
1080
|
-
};
|
1081
|
-
const replace = (value) => {
|
1082
|
-
const updatedFieldArrayValues = convertToArrayPayload(cloneObject(value));
|
1083
|
-
ids.current = updatedFieldArrayValues.map(generateId);
|
1084
|
-
updateValues([...updatedFieldArrayValues]);
|
1085
|
-
setFields([...updatedFieldArrayValues]);
|
1086
|
-
control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
1087
|
-
};
|
1088
|
-
React.useEffect(() => {
|
1089
|
-
control._state.action = false;
|
1090
|
-
isWatched(name, control._names) &&
|
1091
|
-
control._subjects.state.next({
|
1092
|
-
...control._formState,
|
1093
|
-
});
|
1094
|
-
if (_actioned.current &&
|
1095
|
-
(!getValidationModes(control._options.mode).isOnSubmit ||
|
1096
|
-
control._formState.isSubmitted)) {
|
1097
|
-
if (control._options.resolver) {
|
1098
|
-
control._executeSchema([name]).then((result) => {
|
1099
|
-
const error = get(result.errors, name);
|
1100
|
-
const existingError = get(control._formState.errors, name);
|
1101
|
-
if (existingError ? !error && existingError.type : error && error.type) {
|
1102
|
-
error
|
1103
|
-
? set(control._formState.errors, name, error)
|
1104
|
-
: unset(control._formState.errors, name);
|
1105
|
-
control._subjects.state.next({
|
1106
|
-
errors: control._formState.errors,
|
1107
|
-
});
|
1108
|
-
}
|
1109
|
-
});
|
1110
|
-
}
|
1111
|
-
else {
|
1112
|
-
const field = get(control._fields, name);
|
1113
|
-
if (field && field._f) {
|
1114
|
-
validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
|
1115
|
-
control._subjects.state.next({
|
1116
|
-
errors: updateFieldArrayRootError(control._formState.errors, error, name),
|
1117
|
-
}));
|
1118
|
-
}
|
1119
|
-
}
|
1120
|
-
}
|
1121
|
-
control._subjects.values.next({
|
1122
|
-
name,
|
1123
|
-
values: { ...control._formValues },
|
1124
|
-
});
|
1125
|
-
control._names.focus &&
|
1126
|
-
focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
|
1127
|
-
control._names.focus = '';
|
1128
|
-
control._updateValid();
|
1129
|
-
}, [fields, name, control]);
|
1130
|
-
React.useEffect(() => {
|
1131
|
-
!get(control._formValues, name) && control._updateFieldArray(name);
|
1132
|
-
return () => {
|
1133
|
-
(control._options.shouldUnregister || shouldUnregister) &&
|
1134
|
-
control.unregister(name);
|
1135
|
-
};
|
1136
|
-
}, [name, control, keyName, shouldUnregister]);
|
1137
|
-
return {
|
1138
|
-
swap: React.useCallback(swap, [updateValues, name, control]),
|
1139
|
-
move: React.useCallback(move, [updateValues, name, control]),
|
1140
|
-
prepend: React.useCallback(prepend$1, [updateValues, name, control]),
|
1141
|
-
append: React.useCallback(append$1, [updateValues, name, control]),
|
1142
|
-
remove: React.useCallback(remove, [updateValues, name, control]),
|
1143
|
-
insert: React.useCallback(insert$1, [updateValues, name, control]),
|
1144
|
-
update: React.useCallback(update, [updateValues, name, control]),
|
1145
|
-
replace: React.useCallback(replace, [updateValues, name, control]),
|
1146
|
-
fields: React.useMemo(() => fields.map((field, index) => ({
|
1147
|
-
...field,
|
1148
|
-
[keyName]: ids.current[index] || generateId(),
|
1149
|
-
})), [fields, keyName]),
|
1150
|
-
};
|
943
|
+
/**
|
944
|
+
* A custom hook that exposes convenient methods to perform operations with a list of dynamic inputs that need to be appended, updated, removed etc. • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn) • [Video](https://youtu.be/4MrbfGSFY2A)
|
945
|
+
*
|
946
|
+
* @remarks
|
947
|
+
* [API](https://react-hook-form.com/api/usefieldarray) • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn)
|
948
|
+
*
|
949
|
+
* @param props - useFieldArray props
|
950
|
+
*
|
951
|
+
* @returns methods - functions to manipulate with the Field Arrays (dynamic inputs) {@link UseFieldArrayReturn}
|
952
|
+
*
|
953
|
+
* @example
|
954
|
+
* ```tsx
|
955
|
+
* function App() {
|
956
|
+
* const { register, control, handleSubmit, reset, trigger, setError } = useForm({
|
957
|
+
* defaultValues: {
|
958
|
+
* test: []
|
959
|
+
* }
|
960
|
+
* });
|
961
|
+
* const { fields, append } = useFieldArray({
|
962
|
+
* control,
|
963
|
+
* name: "test"
|
964
|
+
* });
|
965
|
+
*
|
966
|
+
* return (
|
967
|
+
* <form onSubmit={handleSubmit(data => console.log(data))}>
|
968
|
+
* {fields.map((item, index) => (
|
969
|
+
* <input key={item.id} {...register(`test.${index}.firstName`)} />
|
970
|
+
* ))}
|
971
|
+
* <button type="button" onClick={() => append({ firstName: "bill" })}>
|
972
|
+
* append
|
973
|
+
* </button>
|
974
|
+
* <input type="submit" />
|
975
|
+
* </form>
|
976
|
+
* );
|
977
|
+
* }
|
978
|
+
* ```
|
979
|
+
*/
|
980
|
+
function useFieldArray(props) {
|
981
|
+
const methods = useFormContext();
|
982
|
+
const { control = methods.control, name, keyName = 'id', shouldUnregister, } = props;
|
983
|
+
const [fields, setFields] = React.useState(control._getFieldArray(name));
|
984
|
+
const ids = React.useRef(control._getFieldArray(name).map(generateId));
|
985
|
+
const _fieldIds = React.useRef(fields);
|
986
|
+
const _name = React.useRef(name);
|
987
|
+
const _actioned = React.useRef(false);
|
988
|
+
_name.current = name;
|
989
|
+
_fieldIds.current = fields;
|
990
|
+
control._names.array.add(name);
|
991
|
+
props.rules &&
|
992
|
+
control.register(name, props.rules);
|
993
|
+
useSubscribe({
|
994
|
+
next: ({ values, name: fieldArrayName, }) => {
|
995
|
+
if (fieldArrayName === _name.current || !fieldArrayName) {
|
996
|
+
const fieldValues = get(values, _name.current);
|
997
|
+
if (Array.isArray(fieldValues)) {
|
998
|
+
setFields(fieldValues);
|
999
|
+
ids.current = fieldValues.map(generateId);
|
1000
|
+
}
|
1001
|
+
}
|
1002
|
+
},
|
1003
|
+
subject: control._subjects.array,
|
1004
|
+
});
|
1005
|
+
const updateValues = React.useCallback((updatedFieldArrayValues) => {
|
1006
|
+
_actioned.current = true;
|
1007
|
+
control._updateFieldArray(name, updatedFieldArrayValues);
|
1008
|
+
}, [control, name]);
|
1009
|
+
const append$1 = (value, options) => {
|
1010
|
+
const appendValue = convertToArrayPayload(cloneObject(value));
|
1011
|
+
const updatedFieldArrayValues = append(control._getFieldArray(name), appendValue);
|
1012
|
+
control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
|
1013
|
+
ids.current = append(ids.current, appendValue.map(generateId));
|
1014
|
+
updateValues(updatedFieldArrayValues);
|
1015
|
+
setFields(updatedFieldArrayValues);
|
1016
|
+
control._updateFieldArray(name, updatedFieldArrayValues, append, {
|
1017
|
+
argA: fillEmptyArray(value),
|
1018
|
+
});
|
1019
|
+
};
|
1020
|
+
const prepend$1 = (value, options) => {
|
1021
|
+
const prependValue = convertToArrayPayload(cloneObject(value));
|
1022
|
+
const updatedFieldArrayValues = prepend(control._getFieldArray(name), prependValue);
|
1023
|
+
control._names.focus = getFocusFieldName(name, 0, options);
|
1024
|
+
ids.current = prepend(ids.current, prependValue.map(generateId));
|
1025
|
+
updateValues(updatedFieldArrayValues);
|
1026
|
+
setFields(updatedFieldArrayValues);
|
1027
|
+
control._updateFieldArray(name, updatedFieldArrayValues, prepend, {
|
1028
|
+
argA: fillEmptyArray(value),
|
1029
|
+
});
|
1030
|
+
};
|
1031
|
+
const remove = (index) => {
|
1032
|
+
const updatedFieldArrayValues = removeArrayAt(control._getFieldArray(name), index);
|
1033
|
+
ids.current = removeArrayAt(ids.current, index);
|
1034
|
+
updateValues(updatedFieldArrayValues);
|
1035
|
+
setFields(updatedFieldArrayValues);
|
1036
|
+
control._updateFieldArray(name, updatedFieldArrayValues, removeArrayAt, {
|
1037
|
+
argA: index,
|
1038
|
+
});
|
1039
|
+
};
|
1040
|
+
const insert$1 = (index, value, options) => {
|
1041
|
+
const insertValue = convertToArrayPayload(cloneObject(value));
|
1042
|
+
const updatedFieldArrayValues = insert(control._getFieldArray(name), index, insertValue);
|
1043
|
+
control._names.focus = getFocusFieldName(name, index, options);
|
1044
|
+
ids.current = insert(ids.current, index, insertValue.map(generateId));
|
1045
|
+
updateValues(updatedFieldArrayValues);
|
1046
|
+
setFields(updatedFieldArrayValues);
|
1047
|
+
control._updateFieldArray(name, updatedFieldArrayValues, insert, {
|
1048
|
+
argA: index,
|
1049
|
+
argB: fillEmptyArray(value),
|
1050
|
+
});
|
1051
|
+
};
|
1052
|
+
const swap = (indexA, indexB) => {
|
1053
|
+
const updatedFieldArrayValues = control._getFieldArray(name);
|
1054
|
+
swapArrayAt(updatedFieldArrayValues, indexA, indexB);
|
1055
|
+
swapArrayAt(ids.current, indexA, indexB);
|
1056
|
+
updateValues(updatedFieldArrayValues);
|
1057
|
+
setFields(updatedFieldArrayValues);
|
1058
|
+
control._updateFieldArray(name, updatedFieldArrayValues, swapArrayAt, {
|
1059
|
+
argA: indexA,
|
1060
|
+
argB: indexB,
|
1061
|
+
}, false);
|
1062
|
+
};
|
1063
|
+
const move = (from, to) => {
|
1064
|
+
const updatedFieldArrayValues = control._getFieldArray(name);
|
1065
|
+
moveArrayAt(updatedFieldArrayValues, from, to);
|
1066
|
+
moveArrayAt(ids.current, from, to);
|
1067
|
+
updateValues(updatedFieldArrayValues);
|
1068
|
+
setFields(updatedFieldArrayValues);
|
1069
|
+
control._updateFieldArray(name, updatedFieldArrayValues, moveArrayAt, {
|
1070
|
+
argA: from,
|
1071
|
+
argB: to,
|
1072
|
+
}, false);
|
1073
|
+
};
|
1074
|
+
const update = (index, value) => {
|
1075
|
+
const updateValue = cloneObject(value);
|
1076
|
+
const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
|
1077
|
+
ids.current = [...updatedFieldArrayValues].map((item, i) => !item || i === index ? generateId() : ids.current[i]);
|
1078
|
+
updateValues(updatedFieldArrayValues);
|
1079
|
+
setFields([...updatedFieldArrayValues]);
|
1080
|
+
control._updateFieldArray(name, updatedFieldArrayValues, updateAt, {
|
1081
|
+
argA: index,
|
1082
|
+
argB: updateValue,
|
1083
|
+
}, true, false);
|
1084
|
+
};
|
1085
|
+
const replace = (value) => {
|
1086
|
+
const updatedFieldArrayValues = convertToArrayPayload(cloneObject(value));
|
1087
|
+
ids.current = updatedFieldArrayValues.map(generateId);
|
1088
|
+
updateValues([...updatedFieldArrayValues]);
|
1089
|
+
setFields([...updatedFieldArrayValues]);
|
1090
|
+
control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
1091
|
+
};
|
1092
|
+
React.useEffect(() => {
|
1093
|
+
control._state.action = false;
|
1094
|
+
isWatched(name, control._names) &&
|
1095
|
+
control._subjects.state.next({
|
1096
|
+
...control._formState,
|
1097
|
+
});
|
1098
|
+
if (_actioned.current &&
|
1099
|
+
(!getValidationModes(control._options.mode).isOnSubmit ||
|
1100
|
+
control._formState.isSubmitted)) {
|
1101
|
+
if (control._options.resolver) {
|
1102
|
+
control._executeSchema([name]).then((result) => {
|
1103
|
+
const error = get(result.errors, name);
|
1104
|
+
const existingError = get(control._formState.errors, name);
|
1105
|
+
if (existingError ? !error && existingError.type : error && error.type) {
|
1106
|
+
error
|
1107
|
+
? set(control._formState.errors, name, error)
|
1108
|
+
: unset(control._formState.errors, name);
|
1109
|
+
control._subjects.state.next({
|
1110
|
+
errors: control._formState.errors,
|
1111
|
+
});
|
1112
|
+
}
|
1113
|
+
});
|
1114
|
+
}
|
1115
|
+
else {
|
1116
|
+
const field = get(control._fields, name);
|
1117
|
+
if (field && field._f) {
|
1118
|
+
validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
|
1119
|
+
control._subjects.state.next({
|
1120
|
+
errors: updateFieldArrayRootError(control._formState.errors, error, name),
|
1121
|
+
}));
|
1122
|
+
}
|
1123
|
+
}
|
1124
|
+
}
|
1125
|
+
control._subjects.values.next({
|
1126
|
+
name,
|
1127
|
+
values: { ...control._formValues },
|
1128
|
+
});
|
1129
|
+
control._names.focus &&
|
1130
|
+
focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
|
1131
|
+
control._names.focus = '';
|
1132
|
+
control._updateValid();
|
1133
|
+
}, [fields, name, control]);
|
1134
|
+
React.useEffect(() => {
|
1135
|
+
!get(control._formValues, name) && control._updateFieldArray(name);
|
1136
|
+
return () => {
|
1137
|
+
(control._options.shouldUnregister || shouldUnregister) &&
|
1138
|
+
control.unregister(name);
|
1139
|
+
};
|
1140
|
+
}, [name, control, keyName, shouldUnregister]);
|
1141
|
+
return {
|
1142
|
+
swap: React.useCallback(swap, [updateValues, name, control]),
|
1143
|
+
move: React.useCallback(move, [updateValues, name, control]),
|
1144
|
+
prepend: React.useCallback(prepend$1, [updateValues, name, control]),
|
1145
|
+
append: React.useCallback(append$1, [updateValues, name, control]),
|
1146
|
+
remove: React.useCallback(remove, [updateValues, name, control]),
|
1147
|
+
insert: React.useCallback(insert$1, [updateValues, name, control]),
|
1148
|
+
update: React.useCallback(update, [updateValues, name, control]),
|
1149
|
+
replace: React.useCallback(replace, [updateValues, name, control]),
|
1150
|
+
fields: React.useMemo(() => fields.map((field, index) => ({
|
1151
|
+
...field,
|
1152
|
+
[keyName]: ids.current[index] || generateId(),
|
1153
|
+
})), [fields, keyName]),
|
1154
|
+
};
|
1151
1155
|
}
|
1152
1156
|
|
1153
|
-
function createSubject() {
|
1154
|
-
let _observers = [];
|
1155
|
-
const next = (value) => {
|
1156
|
-
for (const observer of _observers) {
|
1157
|
-
observer.next && observer.next(value);
|
1158
|
-
}
|
1159
|
-
};
|
1160
|
-
const subscribe = (observer) => {
|
1161
|
-
_observers.push(observer);
|
1162
|
-
return {
|
1163
|
-
unsubscribe: () => {
|
1164
|
-
_observers = _observers.filter((o) => o !== observer);
|
1165
|
-
},
|
1166
|
-
};
|
1167
|
-
};
|
1168
|
-
const unsubscribe = () => {
|
1169
|
-
_observers = [];
|
1170
|
-
};
|
1171
|
-
return {
|
1172
|
-
get observers() {
|
1173
|
-
return _observers;
|
1174
|
-
},
|
1175
|
-
next,
|
1176
|
-
subscribe,
|
1177
|
-
unsubscribe,
|
1178
|
-
};
|
1157
|
+
function createSubject() {
|
1158
|
+
let _observers = [];
|
1159
|
+
const next = (value) => {
|
1160
|
+
for (const observer of _observers) {
|
1161
|
+
observer.next && observer.next(value);
|
1162
|
+
}
|
1163
|
+
};
|
1164
|
+
const subscribe = (observer) => {
|
1165
|
+
_observers.push(observer);
|
1166
|
+
return {
|
1167
|
+
unsubscribe: () => {
|
1168
|
+
_observers = _observers.filter((o) => o !== observer);
|
1169
|
+
},
|
1170
|
+
};
|
1171
|
+
};
|
1172
|
+
const unsubscribe = () => {
|
1173
|
+
_observers = [];
|
1174
|
+
};
|
1175
|
+
return {
|
1176
|
+
get observers() {
|
1177
|
+
return _observers;
|
1178
|
+
},
|
1179
|
+
next,
|
1180
|
+
subscribe,
|
1181
|
+
unsubscribe,
|
1182
|
+
};
|
1179
1183
|
}
|
1180
1184
|
|
1181
1185
|
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
1182
1186
|
|
1183
|
-
function deepEqual(object1, object2) {
|
1184
|
-
if (isPrimitive(object1) || isPrimitive(object2)) {
|
1185
|
-
return object1 === object2;
|
1186
|
-
}
|
1187
|
-
if (isDateObject(object1) && isDateObject(object2)) {
|
1188
|
-
return object1.getTime() === object2.getTime();
|
1189
|
-
}
|
1190
|
-
const keys1 = Object.keys(object1);
|
1191
|
-
const keys2 = Object.keys(object2);
|
1192
|
-
if (keys1.length !== keys2.length) {
|
1193
|
-
return false;
|
1194
|
-
}
|
1195
|
-
for (const key of keys1) {
|
1196
|
-
const val1 = object1[key];
|
1197
|
-
if (!keys2.includes(key)) {
|
1198
|
-
return false;
|
1199
|
-
}
|
1200
|
-
if (key !== 'ref') {
|
1201
|
-
const val2 = object2[key];
|
1202
|
-
if ((isDateObject(val1) && isDateObject(val2)) ||
|
1203
|
-
(isObject(val1) && isObject(val2)) ||
|
1204
|
-
(Array.isArray(val1) && Array.isArray(val2))
|
1205
|
-
? !deepEqual(val1, val2)
|
1206
|
-
: val1 !== val2) {
|
1207
|
-
return false;
|
1208
|
-
}
|
1209
|
-
}
|
1210
|
-
}
|
1211
|
-
return true;
|
1187
|
+
function deepEqual(object1, object2) {
|
1188
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
1189
|
+
return object1 === object2;
|
1190
|
+
}
|
1191
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
1192
|
+
return object1.getTime() === object2.getTime();
|
1193
|
+
}
|
1194
|
+
const keys1 = Object.keys(object1);
|
1195
|
+
const keys2 = Object.keys(object2);
|
1196
|
+
if (keys1.length !== keys2.length) {
|
1197
|
+
return false;
|
1198
|
+
}
|
1199
|
+
for (const key of keys1) {
|
1200
|
+
const val1 = object1[key];
|
1201
|
+
if (!keys2.includes(key)) {
|
1202
|
+
return false;
|
1203
|
+
}
|
1204
|
+
if (key !== 'ref') {
|
1205
|
+
const val2 = object2[key];
|
1206
|
+
if ((isDateObject(val1) && isDateObject(val2)) ||
|
1207
|
+
(isObject(val1) && isObject(val2)) ||
|
1208
|
+
(Array.isArray(val1) && Array.isArray(val2))
|
1209
|
+
? !deepEqual(val1, val2)
|
1210
|
+
: val1 !== val2) {
|
1211
|
+
return false;
|
1212
|
+
}
|
1213
|
+
}
|
1214
|
+
}
|
1215
|
+
return true;
|
1212
1216
|
}
|
1213
1217
|
|
1214
1218
|
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
@@ -1217,1105 +1221,1111 @@ var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
1217
1221
|
|
1218
1222
|
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
|
1219
1223
|
|
1220
|
-
var objectHasFunction = (data) => {
|
1221
|
-
for (const key in data) {
|
1222
|
-
if (isFunction(data[key])) {
|
1223
|
-
return true;
|
1224
|
-
}
|
1225
|
-
}
|
1226
|
-
return false;
|
1224
|
+
var objectHasFunction = (data) => {
|
1225
|
+
for (const key in data) {
|
1226
|
+
if (isFunction(data[key])) {
|
1227
|
+
return true;
|
1228
|
+
}
|
1229
|
+
}
|
1230
|
+
return false;
|
1227
1231
|
};
|
1228
1232
|
|
1229
|
-
function markFieldsDirty(data, fields = {}) {
|
1230
|
-
const isParentNodeArray = Array.isArray(data);
|
1231
|
-
if (isObject(data) || isParentNodeArray) {
|
1232
|
-
for (const key in data) {
|
1233
|
-
if (Array.isArray(data[key]) ||
|
1234
|
-
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
1235
|
-
fields[key] = Array.isArray(data[key]) ? [] : {};
|
1236
|
-
markFieldsDirty(data[key], fields[key]);
|
1237
|
-
}
|
1238
|
-
else if (!isNullOrUndefined(data[key])) {
|
1239
|
-
fields[key] = true;
|
1240
|
-
}
|
1241
|
-
}
|
1242
|
-
}
|
1243
|
-
return fields;
|
1244
|
-
}
|
1245
|
-
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
1246
|
-
const isParentNodeArray = Array.isArray(data);
|
1247
|
-
if (isObject(data) || isParentNodeArray) {
|
1248
|
-
for (const key in data) {
|
1249
|
-
if (Array.isArray(data[key]) ||
|
1250
|
-
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
1251
|
-
if (isUndefined(formValues) ||
|
1252
|
-
isPrimitive(dirtyFieldsFromValues[key])) {
|
1253
|
-
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
1254
|
-
? markFieldsDirty(data[key], [])
|
1255
|
-
: { ...markFieldsDirty(data[key]) };
|
1256
|
-
}
|
1257
|
-
else {
|
1258
|
-
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
1259
|
-
}
|
1260
|
-
}
|
1261
|
-
else {
|
1262
|
-
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
1263
|
-
}
|
1264
|
-
}
|
1265
|
-
}
|
1266
|
-
return dirtyFieldsFromValues;
|
1267
|
-
}
|
1233
|
+
function markFieldsDirty(data, fields = {}) {
|
1234
|
+
const isParentNodeArray = Array.isArray(data);
|
1235
|
+
if (isObject(data) || isParentNodeArray) {
|
1236
|
+
for (const key in data) {
|
1237
|
+
if (Array.isArray(data[key]) ||
|
1238
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
1239
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
1240
|
+
markFieldsDirty(data[key], fields[key]);
|
1241
|
+
}
|
1242
|
+
else if (!isNullOrUndefined(data[key])) {
|
1243
|
+
fields[key] = true;
|
1244
|
+
}
|
1245
|
+
}
|
1246
|
+
}
|
1247
|
+
return fields;
|
1248
|
+
}
|
1249
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
1250
|
+
const isParentNodeArray = Array.isArray(data);
|
1251
|
+
if (isObject(data) || isParentNodeArray) {
|
1252
|
+
for (const key in data) {
|
1253
|
+
if (Array.isArray(data[key]) ||
|
1254
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
1255
|
+
if (isUndefined(formValues) ||
|
1256
|
+
isPrimitive(dirtyFieldsFromValues[key])) {
|
1257
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
1258
|
+
? markFieldsDirty(data[key], [])
|
1259
|
+
: { ...markFieldsDirty(data[key]) };
|
1260
|
+
}
|
1261
|
+
else {
|
1262
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
1263
|
+
}
|
1264
|
+
}
|
1265
|
+
else {
|
1266
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
1267
|
+
}
|
1268
|
+
}
|
1269
|
+
}
|
1270
|
+
return dirtyFieldsFromValues;
|
1271
|
+
}
|
1268
1272
|
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
1269
1273
|
|
1270
|
-
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
|
1271
|
-
? value
|
1272
|
-
: valueAsNumber
|
1273
|
-
? value === ''
|
1274
|
-
? NaN
|
1275
|
-
: value
|
1276
|
-
? +value
|
1277
|
-
: value
|
1278
|
-
: valueAsDate && isString(value)
|
1279
|
-
? new Date(value)
|
1280
|
-
: setValueAs
|
1281
|
-
? setValueAs(value)
|
1274
|
+
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
|
1275
|
+
? value
|
1276
|
+
: valueAsNumber
|
1277
|
+
? value === ''
|
1278
|
+
? NaN
|
1279
|
+
: value
|
1280
|
+
? +value
|
1281
|
+
: value
|
1282
|
+
: valueAsDate && isString(value)
|
1283
|
+
? new Date(value)
|
1284
|
+
: setValueAs
|
1285
|
+
? setValueAs(value)
|
1282
1286
|
: value;
|
1283
1287
|
|
1284
|
-
function getFieldValue(_f) {
|
1285
|
-
const ref = _f.ref;
|
1286
|
-
if (_f.refs ? _f.refs.every((ref) => ref.disabled) : ref.disabled) {
|
1287
|
-
return;
|
1288
|
-
}
|
1289
|
-
if (isFileInput(ref)) {
|
1290
|
-
return ref.files;
|
1291
|
-
}
|
1292
|
-
if (isRadioInput(ref)) {
|
1293
|
-
return getRadioValue(_f.refs).value;
|
1294
|
-
}
|
1295
|
-
if (isMultipleSelect(ref)) {
|
1296
|
-
return [...ref.selectedOptions].map(({ value }) => value);
|
1297
|
-
}
|
1298
|
-
if (isCheckBoxInput(ref)) {
|
1299
|
-
return getCheckboxValue(_f.refs).value;
|
1300
|
-
}
|
1301
|
-
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
1288
|
+
function getFieldValue(_f) {
|
1289
|
+
const ref = _f.ref;
|
1290
|
+
if (_f.refs ? _f.refs.every((ref) => ref.disabled) : ref.disabled) {
|
1291
|
+
return;
|
1292
|
+
}
|
1293
|
+
if (isFileInput(ref)) {
|
1294
|
+
return ref.files;
|
1295
|
+
}
|
1296
|
+
if (isRadioInput(ref)) {
|
1297
|
+
return getRadioValue(_f.refs).value;
|
1298
|
+
}
|
1299
|
+
if (isMultipleSelect(ref)) {
|
1300
|
+
return [...ref.selectedOptions].map(({ value }) => value);
|
1301
|
+
}
|
1302
|
+
if (isCheckBoxInput(ref)) {
|
1303
|
+
return getCheckboxValue(_f.refs).value;
|
1304
|
+
}
|
1305
|
+
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
1302
1306
|
}
|
1303
1307
|
|
1304
|
-
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
|
1305
|
-
const fields = {};
|
1306
|
-
for (const name of fieldsNames) {
|
1307
|
-
const field = get(_fields, name);
|
1308
|
-
field && set(fields, name, field._f);
|
1309
|
-
}
|
1310
|
-
return {
|
1311
|
-
criteriaMode,
|
1312
|
-
names: [...fieldsNames],
|
1313
|
-
fields,
|
1314
|
-
shouldUseNativeValidation,
|
1315
|
-
};
|
1308
|
+
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
|
1309
|
+
const fields = {};
|
1310
|
+
for (const name of fieldsNames) {
|
1311
|
+
const field = get(_fields, name);
|
1312
|
+
field && set(fields, name, field._f);
|
1313
|
+
}
|
1314
|
+
return {
|
1315
|
+
criteriaMode,
|
1316
|
+
names: [...fieldsNames],
|
1317
|
+
fields,
|
1318
|
+
shouldUseNativeValidation,
|
1319
|
+
};
|
1316
1320
|
};
|
1317
1321
|
|
1318
|
-
var getRuleValue = (rule) => isUndefined(rule)
|
1319
|
-
? rule
|
1320
|
-
: isRegex(rule)
|
1321
|
-
? rule.source
|
1322
|
-
: isObject(rule)
|
1323
|
-
? isRegex(rule.value)
|
1324
|
-
? rule.value.source
|
1325
|
-
: rule.value
|
1322
|
+
var getRuleValue = (rule) => isUndefined(rule)
|
1323
|
+
? rule
|
1324
|
+
: isRegex(rule)
|
1325
|
+
? rule.source
|
1326
|
+
: isObject(rule)
|
1327
|
+
? isRegex(rule.value)
|
1328
|
+
? rule.value.source
|
1329
|
+
: rule.value
|
1326
1330
|
: rule;
|
1327
1331
|
|
1328
|
-
var hasValidation = (options) => options.mount &&
|
1329
|
-
(options.required ||
|
1330
|
-
options.min ||
|
1331
|
-
options.max ||
|
1332
|
-
options.maxLength ||
|
1333
|
-
options.minLength ||
|
1334
|
-
options.pattern ||
|
1332
|
+
var hasValidation = (options) => options.mount &&
|
1333
|
+
(options.required ||
|
1334
|
+
options.min ||
|
1335
|
+
options.max ||
|
1336
|
+
options.maxLength ||
|
1337
|
+
options.minLength ||
|
1338
|
+
options.pattern ||
|
1335
1339
|
options.validate);
|
1336
1340
|
|
1337
|
-
function schemaErrorLookup(errors, _fields, name) {
|
1338
|
-
const error = get(errors, name);
|
1339
|
-
if (error || isKey(name)) {
|
1340
|
-
return {
|
1341
|
-
error,
|
1342
|
-
name,
|
1343
|
-
};
|
1344
|
-
}
|
1345
|
-
const names = name.split('.');
|
1346
|
-
while (names.length) {
|
1347
|
-
const fieldName = names.join('.');
|
1348
|
-
const field = get(_fields, fieldName);
|
1349
|
-
const foundError = get(errors, fieldName);
|
1350
|
-
if (field && !Array.isArray(field) && name !== fieldName) {
|
1351
|
-
return { name };
|
1352
|
-
}
|
1353
|
-
if (foundError && foundError.type) {
|
1354
|
-
return {
|
1355
|
-
name: fieldName,
|
1356
|
-
error: foundError,
|
1357
|
-
};
|
1358
|
-
}
|
1359
|
-
names.pop();
|
1360
|
-
}
|
1361
|
-
return {
|
1362
|
-
name,
|
1363
|
-
};
|
1341
|
+
function schemaErrorLookup(errors, _fields, name) {
|
1342
|
+
const error = get(errors, name);
|
1343
|
+
if (error || isKey(name)) {
|
1344
|
+
return {
|
1345
|
+
error,
|
1346
|
+
name,
|
1347
|
+
};
|
1348
|
+
}
|
1349
|
+
const names = name.split('.');
|
1350
|
+
while (names.length) {
|
1351
|
+
const fieldName = names.join('.');
|
1352
|
+
const field = get(_fields, fieldName);
|
1353
|
+
const foundError = get(errors, fieldName);
|
1354
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
1355
|
+
return { name };
|
1356
|
+
}
|
1357
|
+
if (foundError && foundError.type) {
|
1358
|
+
return {
|
1359
|
+
name: fieldName,
|
1360
|
+
error: foundError,
|
1361
|
+
};
|
1362
|
+
}
|
1363
|
+
names.pop();
|
1364
|
+
}
|
1365
|
+
return {
|
1366
|
+
name,
|
1367
|
+
};
|
1364
1368
|
}
|
1365
1369
|
|
1366
|
-
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
1367
|
-
if (mode.isOnAll) {
|
1368
|
-
return false;
|
1369
|
-
}
|
1370
|
-
else if (!isSubmitted && mode.isOnTouch) {
|
1371
|
-
return !(isTouched || isBlurEvent);
|
1372
|
-
}
|
1373
|
-
else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
1374
|
-
return !isBlurEvent;
|
1375
|
-
}
|
1376
|
-
else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
1377
|
-
return isBlurEvent;
|
1378
|
-
}
|
1379
|
-
return true;
|
1370
|
+
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
1371
|
+
if (mode.isOnAll) {
|
1372
|
+
return false;
|
1373
|
+
}
|
1374
|
+
else if (!isSubmitted && mode.isOnTouch) {
|
1375
|
+
return !(isTouched || isBlurEvent);
|
1376
|
+
}
|
1377
|
+
else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
1378
|
+
return !isBlurEvent;
|
1379
|
+
}
|
1380
|
+
else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
1381
|
+
return isBlurEvent;
|
1382
|
+
}
|
1383
|
+
return true;
|
1380
1384
|
};
|
1381
1385
|
|
1382
1386
|
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
|
1383
1387
|
|
1384
|
-
const defaultOptions = {
|
1385
|
-
mode: VALIDATION_MODE.onSubmit,
|
1386
|
-
reValidateMode: VALIDATION_MODE.onChange,
|
1387
|
-
shouldFocusError: true,
|
1388
|
-
};
|
1389
|
-
function createFormControl(props = {}, flushRootRender) {
|
1390
|
-
let _options = {
|
1391
|
-
...defaultOptions,
|
1392
|
-
...props,
|
1393
|
-
};
|
1394
|
-
let _formState = {
|
1395
|
-
submitCount: 0,
|
1396
|
-
isDirty: false,
|
1397
|
-
isLoading:
|
1398
|
-
isValidating: false,
|
1399
|
-
isSubmitted: false,
|
1400
|
-
isSubmitting: false,
|
1401
|
-
isSubmitSuccessful: false,
|
1402
|
-
isValid: false,
|
1403
|
-
touchedFields: {},
|
1404
|
-
dirtyFields: {},
|
1405
|
-
errors: {},
|
1406
|
-
};
|
1407
|
-
let _fields = {};
|
1408
|
-
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
|
1409
|
-
? cloneObject(_options.defaultValues || _options.values) || {}
|
1410
|
-
: {};
|
1411
|
-
let _formValues = _options.shouldUnregister
|
1412
|
-
? {}
|
1413
|
-
: cloneObject(_defaultValues);
|
1414
|
-
let _state = {
|
1415
|
-
action: false,
|
1416
|
-
mount: false,
|
1417
|
-
watch: false,
|
1418
|
-
};
|
1419
|
-
let _names = {
|
1420
|
-
mount: new Set(),
|
1421
|
-
unMount: new Set(),
|
1422
|
-
array: new Set(),
|
1423
|
-
watch: new Set(),
|
1424
|
-
};
|
1425
|
-
let delayErrorCallback;
|
1426
|
-
let timer = 0;
|
1427
|
-
const _proxyFormState = {
|
1428
|
-
isDirty: false,
|
1429
|
-
dirtyFields: false,
|
1430
|
-
touchedFields: false,
|
1431
|
-
isValidating: false,
|
1432
|
-
isValid: false,
|
1433
|
-
errors: false,
|
1434
|
-
};
|
1435
|
-
const _subjects = {
|
1436
|
-
values: createSubject(),
|
1437
|
-
array: createSubject(),
|
1438
|
-
state: createSubject(),
|
1439
|
-
};
|
1440
|
-
const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
|
1441
|
-
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
1442
|
-
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
1443
|
-
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
1444
|
-
const debounce = (callback) => (wait) => {
|
1445
|
-
clearTimeout(timer);
|
1446
|
-
timer = setTimeout(callback, wait);
|
1447
|
-
};
|
1448
|
-
const _updateValid = async (shouldUpdateValid) => {
|
1449
|
-
if (_proxyFormState.isValid || shouldUpdateValid) {
|
1450
|
-
const isValid = _options.resolver
|
1451
|
-
? isEmptyObject((await _executeSchema()).errors)
|
1452
|
-
: await executeBuiltInValidation(_fields, true);
|
1453
|
-
if (isValid !== _formState.isValid) {
|
1454
|
-
_subjects.state.next({
|
1455
|
-
isValid,
|
1456
|
-
});
|
1457
|
-
}
|
1458
|
-
}
|
1459
|
-
};
|
1460
|
-
const _updateIsValidating = (value) => _proxyFormState.isValidating &&
|
1461
|
-
_subjects.state.next({
|
1462
|
-
isValidating: value,
|
1463
|
-
});
|
1464
|
-
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
1465
|
-
if (args && method) {
|
1466
|
-
_state.action = true;
|
1467
|
-
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
1468
|
-
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
1469
|
-
shouldSetValues && set(_fields, name, fieldValues);
|
1470
|
-
}
|
1471
|
-
if (shouldUpdateFieldsAndState &&
|
1472
|
-
Array.isArray(get(_formState.errors, name))) {
|
1473
|
-
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
1474
|
-
shouldSetValues && set(_formState.errors, name, errors);
|
1475
|
-
unsetEmptyArray(_formState.errors, name);
|
1476
|
-
}
|
1477
|
-
if (_proxyFormState.touchedFields &&
|
1478
|
-
shouldUpdateFieldsAndState &&
|
1479
|
-
Array.isArray(get(_formState.touchedFields, name))) {
|
1480
|
-
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
1481
|
-
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
1482
|
-
}
|
1483
|
-
if (_proxyFormState.dirtyFields) {
|
1484
|
-
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
1485
|
-
}
|
1486
|
-
_subjects.state.next({
|
1487
|
-
name,
|
1488
|
-
isDirty: _getDirty(name, values),
|
1489
|
-
dirtyFields: _formState.dirtyFields,
|
1490
|
-
errors: _formState.errors,
|
1491
|
-
isValid: _formState.isValid,
|
1492
|
-
});
|
1493
|
-
}
|
1494
|
-
else {
|
1495
|
-
set(_formValues, name, values);
|
1496
|
-
}
|
1497
|
-
};
|
1498
|
-
const updateErrors = (name, error) => {
|
1499
|
-
set(_formState.errors, name, error);
|
1500
|
-
_subjects.state.next({
|
1501
|
-
errors: _formState.errors,
|
1502
|
-
});
|
1503
|
-
};
|
1504
|
-
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
|
1505
|
-
const field = get(_fields, name);
|
1506
|
-
if (field) {
|
1507
|
-
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
|
1508
|
-
isUndefined(defaultValue) ||
|
1509
|
-
(ref && ref.defaultChecked) ||
|
1510
|
-
shouldSkipSetValueAs
|
1511
|
-
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
1512
|
-
: setFieldValue(name, defaultValue);
|
1513
|
-
_state.mount && _updateValid();
|
1514
|
-
}
|
1515
|
-
};
|
1516
|
-
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
1517
|
-
let shouldUpdateField = false;
|
1518
|
-
let isPreviousDirty = false;
|
1519
|
-
const output = {
|
1520
|
-
name,
|
1521
|
-
};
|
1522
|
-
if (!isBlurEvent || shouldDirty) {
|
1523
|
-
if (_proxyFormState.isDirty) {
|
1524
|
-
isPreviousDirty = _formState.isDirty;
|
1525
|
-
_formState.isDirty = output.isDirty = _getDirty();
|
1526
|
-
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
1527
|
-
}
|
1528
|
-
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
1529
|
-
isPreviousDirty = get(_formState.dirtyFields, name);
|
1530
|
-
isCurrentFieldPristine
|
1531
|
-
? unset(_formState.dirtyFields, name)
|
1532
|
-
: set(_formState.dirtyFields, name, true);
|
1533
|
-
output.dirtyFields = _formState.dirtyFields;
|
1534
|
-
shouldUpdateField =
|
1535
|
-
shouldUpdateField ||
|
1536
|
-
(_proxyFormState.dirtyFields &&
|
1537
|
-
isPreviousDirty !== !isCurrentFieldPristine);
|
1538
|
-
}
|
1539
|
-
if (isBlurEvent) {
|
1540
|
-
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
1541
|
-
if (!isPreviousFieldTouched) {
|
1542
|
-
set(_formState.touchedFields, name, isBlurEvent);
|
1543
|
-
output.touchedFields = _formState.touchedFields;
|
1544
|
-
shouldUpdateField =
|
1545
|
-
shouldUpdateField ||
|
1546
|
-
(_proxyFormState.touchedFields &&
|
1547
|
-
isPreviousFieldTouched !== isBlurEvent);
|
1548
|
-
}
|
1549
|
-
}
|
1550
|
-
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
1551
|
-
return shouldUpdateField ? output : {};
|
1552
|
-
};
|
1553
|
-
const shouldRenderByError = (name, isValid, error, fieldState) => {
|
1554
|
-
const previousFieldError = get(_formState.errors, name);
|
1555
|
-
const shouldUpdateValid = _proxyFormState.isValid &&
|
1556
|
-
isBoolean(isValid) &&
|
1557
|
-
_formState.isValid !== isValid;
|
1558
|
-
if (props.delayError && error) {
|
1559
|
-
delayErrorCallback = debounce(() => updateErrors(name, error));
|
1560
|
-
delayErrorCallback(props.delayError);
|
1561
|
-
}
|
1562
|
-
else {
|
1563
|
-
clearTimeout(timer);
|
1564
|
-
delayErrorCallback = null;
|
1565
|
-
error
|
1566
|
-
? set(_formState.errors, name, error)
|
1567
|
-
: unset(_formState.errors, name);
|
1568
|
-
}
|
1569
|
-
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) ||
|
1570
|
-
!isEmptyObject(fieldState) ||
|
1571
|
-
shouldUpdateValid) {
|
1572
|
-
const updatedFormState = {
|
1573
|
-
...fieldState,
|
1574
|
-
...(shouldUpdateValid && isBoolean(isValid) ? { isValid } : {}),
|
1575
|
-
errors: _formState.errors,
|
1576
|
-
name,
|
1577
|
-
};
|
1578
|
-
_formState = {
|
1579
|
-
..._formState,
|
1580
|
-
...updatedFormState,
|
1581
|
-
};
|
1582
|
-
_subjects.state.next(updatedFormState);
|
1583
|
-
}
|
1584
|
-
_updateIsValidating(false);
|
1585
|
-
};
|
1586
|
-
const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
1587
|
-
const executeSchemaAndUpdateState = async (names) => {
|
1588
|
-
const { errors } = await _executeSchema();
|
1589
|
-
if (names) {
|
1590
|
-
for (const name of names) {
|
1591
|
-
const error = get(errors, name);
|
1592
|
-
error
|
1593
|
-
? set(_formState.errors, name, error)
|
1594
|
-
: unset(_formState.errors, name);
|
1595
|
-
}
|
1596
|
-
}
|
1597
|
-
else {
|
1598
|
-
_formState.errors = errors;
|
1599
|
-
}
|
1600
|
-
return errors;
|
1601
|
-
};
|
1602
|
-
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
1603
|
-
valid: true,
|
1604
|
-
}) => {
|
1605
|
-
for (const name in fields) {
|
1606
|
-
const field = fields[name];
|
1607
|
-
if (field) {
|
1608
|
-
const { _f, ...fieldValue } = field;
|
1609
|
-
if (_f) {
|
1610
|
-
const isFieldArrayRoot = _names.array.has(_f.name);
|
1611
|
-
const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
|
1612
|
-
if (fieldError[_f.name]) {
|
1613
|
-
context.valid = false;
|
1614
|
-
if (shouldOnlyCheckValid) {
|
1615
|
-
break;
|
1616
|
-
}
|
1617
|
-
}
|
1618
|
-
!shouldOnlyCheckValid &&
|
1619
|
-
(get(fieldError, _f.name)
|
1620
|
-
? isFieldArrayRoot
|
1621
|
-
? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
|
1622
|
-
: set(_formState.errors, _f.name, fieldError[_f.name])
|
1623
|
-
: unset(_formState.errors, _f.name));
|
1624
|
-
}
|
1625
|
-
fieldValue &&
|
1626
|
-
(await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
|
1627
|
-
}
|
1628
|
-
}
|
1629
|
-
return context.valid;
|
1630
|
-
};
|
1631
|
-
const _removeUnmounted = () => {
|
1632
|
-
for (const name of _names.unMount) {
|
1633
|
-
const field = get(_fields, name);
|
1634
|
-
field &&
|
1635
|
-
(field._f.refs
|
1636
|
-
? field._f.refs.every((ref) => !live(ref))
|
1637
|
-
: !live(field._f.ref)) &&
|
1638
|
-
unregister(name);
|
1639
|
-
}
|
1640
|
-
_names.unMount = new Set();
|
1641
|
-
};
|
1642
|
-
const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
|
1643
|
-
!deepEqual(getValues(), _defaultValues));
|
1644
|
-
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
1645
|
-
...(_state.mount
|
1646
|
-
? _formValues
|
1647
|
-
: isUndefined(defaultValue)
|
1648
|
-
? _defaultValues
|
1649
|
-
: isString(names)
|
1650
|
-
? { [names]: defaultValue }
|
1651
|
-
: defaultValue),
|
1652
|
-
}, isGlobal, defaultValue);
|
1653
|
-
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
|
1654
|
-
const setFieldValue = (name, value, options = {}) => {
|
1655
|
-
const field = get(_fields, name);
|
1656
|
-
let fieldValue = value;
|
1657
|
-
if (field) {
|
1658
|
-
const fieldReference = field._f;
|
1659
|
-
if (fieldReference) {
|
1660
|
-
!fieldReference.disabled &&
|
1661
|
-
set(_formValues, name, getFieldValueAs(value, fieldReference));
|
1662
|
-
fieldValue =
|
1663
|
-
isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
1664
|
-
? ''
|
1665
|
-
: value;
|
1666
|
-
if (isMultipleSelect(fieldReference.ref)) {
|
1667
|
-
[...fieldReference.ref.options].forEach((optionRef) => (optionRef.selected = fieldValue.includes(optionRef.value)));
|
1668
|
-
}
|
1669
|
-
else if (fieldReference.refs) {
|
1670
|
-
if (isCheckBoxInput(fieldReference.ref)) {
|
1671
|
-
fieldReference.refs.length > 1
|
1672
|
-
? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) &&
|
1673
|
-
(checkboxRef.checked = Array.isArray(fieldValue)
|
1674
|
-
? !!fieldValue.find((data) => data === checkboxRef.value)
|
1675
|
-
: fieldValue === checkboxRef.value))
|
1676
|
-
: fieldReference.refs[0] &&
|
1677
|
-
(fieldReference.refs[0].checked = !!fieldValue);
|
1678
|
-
}
|
1679
|
-
else {
|
1680
|
-
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
|
1681
|
-
}
|
1682
|
-
}
|
1683
|
-
else if (isFileInput(fieldReference.ref)) {
|
1684
|
-
fieldReference.ref.value = '';
|
1685
|
-
}
|
1686
|
-
else {
|
1687
|
-
fieldReference.ref.value = fieldValue;
|
1688
|
-
if (!fieldReference.ref.type) {
|
1689
|
-
_subjects.values.next({
|
1690
|
-
name,
|
1691
|
-
values: { ..._formValues },
|
1692
|
-
});
|
1693
|
-
}
|
1694
|
-
}
|
1695
|
-
}
|
1696
|
-
}
|
1697
|
-
(options.shouldDirty || options.shouldTouch) &&
|
1698
|
-
updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
1699
|
-
options.shouldValidate && trigger(name);
|
1700
|
-
};
|
1701
|
-
const setValues = (name, value, options) => {
|
1702
|
-
for (const fieldKey in value) {
|
1703
|
-
const fieldValue = value[fieldKey];
|
1704
|
-
const fieldName = `${name}.${fieldKey}`;
|
1705
|
-
const field = get(_fields, fieldName);
|
1706
|
-
(_names.array.has(name) ||
|
1707
|
-
!isPrimitive(fieldValue) ||
|
1708
|
-
(field && !field._f)) &&
|
1709
|
-
!isDateObject(fieldValue)
|
1710
|
-
? setValues(fieldName, fieldValue, options)
|
1711
|
-
: setFieldValue(fieldName, fieldValue, options);
|
1712
|
-
}
|
1713
|
-
};
|
1714
|
-
const setValue = (name, value, options = {}) => {
|
1715
|
-
const field = get(_fields, name);
|
1716
|
-
const isFieldArray = _names.array.has(name);
|
1717
|
-
const cloneValue = cloneObject(value);
|
1718
|
-
set(_formValues, name, cloneValue);
|
1719
|
-
if (isFieldArray) {
|
1720
|
-
_subjects.array.next({
|
1721
|
-
name,
|
1722
|
-
values: { ..._formValues },
|
1723
|
-
});
|
1724
|
-
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
|
1725
|
-
options.shouldDirty) {
|
1726
|
-
_subjects.state.next({
|
1727
|
-
name,
|
1728
|
-
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
1729
|
-
isDirty: _getDirty(name, cloneValue),
|
1730
|
-
});
|
1731
|
-
}
|
1732
|
-
}
|
1733
|
-
else {
|
1734
|
-
field && !field._f && !isNullOrUndefined(cloneValue)
|
1735
|
-
? setValues(name, cloneValue, options)
|
1736
|
-
: setFieldValue(name, cloneValue, options);
|
1737
|
-
}
|
1738
|
-
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
1739
|
-
_subjects.values.next({
|
1740
|
-
name,
|
1741
|
-
values: { ..._formValues },
|
1742
|
-
});
|
1743
|
-
!_state.mount && flushRootRender();
|
1744
|
-
};
|
1745
|
-
const onChange = async (event) => {
|
1746
|
-
const target = event.target;
|
1747
|
-
let name = target.name;
|
1748
|
-
|
1749
|
-
const
|
1750
|
-
|
1751
|
-
|
1752
|
-
let
|
1753
|
-
|
1754
|
-
const
|
1755
|
-
const
|
1756
|
-
|
1757
|
-
!
|
1758
|
-
!
|
1759
|
-
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1767
|
-
|
1768
|
-
|
1769
|
-
|
1770
|
-
const
|
1771
|
-
!
|
1772
|
-
|
1773
|
-
|
1774
|
-
|
1775
|
-
|
1776
|
-
|
1777
|
-
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
const
|
1787
|
-
const
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1800
|
-
|
1801
|
-
|
1802
|
-
|
1803
|
-
|
1804
|
-
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1810
|
-
|
1811
|
-
|
1812
|
-
|
1813
|
-
|
1814
|
-
|
1815
|
-
|
1816
|
-
|
1817
|
-
|
1818
|
-
|
1819
|
-
|
1820
|
-
|
1821
|
-
|
1822
|
-
|
1823
|
-
|
1824
|
-
|
1825
|
-
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
|
1833
|
-
|
1834
|
-
|
1835
|
-
|
1836
|
-
|
1837
|
-
|
1838
|
-
|
1839
|
-
|
1840
|
-
|
1841
|
-
|
1842
|
-
|
1843
|
-
|
1844
|
-
|
1845
|
-
|
1846
|
-
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
|
1851
|
-
|
1852
|
-
|
1853
|
-
|
1854
|
-
|
1855
|
-
|
1856
|
-
|
1857
|
-
|
1858
|
-
|
1859
|
-
|
1860
|
-
|
1861
|
-
|
1862
|
-
|
1863
|
-
|
1864
|
-
|
1865
|
-
|
1866
|
-
|
1867
|
-
|
1868
|
-
|
1869
|
-
|
1870
|
-
|
1871
|
-
|
1872
|
-
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
|
1878
|
-
|
1879
|
-
|
1880
|
-
|
1881
|
-
|
1882
|
-
|
1883
|
-
|
1884
|
-
|
1885
|
-
|
1886
|
-
|
1887
|
-
|
1888
|
-
|
1889
|
-
|
1890
|
-
|
1891
|
-
|
1892
|
-
|
1893
|
-
|
1894
|
-
|
1895
|
-
|
1896
|
-
|
1897
|
-
|
1898
|
-
|
1899
|
-
}
|
1900
|
-
|
1901
|
-
|
1902
|
-
|
1903
|
-
|
1904
|
-
|
1905
|
-
|
1906
|
-
|
1907
|
-
|
1908
|
-
|
1909
|
-
|
1910
|
-
|
1911
|
-
|
1912
|
-
|
1913
|
-
|
1914
|
-
|
1915
|
-
|
1916
|
-
|
1917
|
-
|
1918
|
-
|
1919
|
-
|
1920
|
-
},
|
1921
|
-
|
1922
|
-
|
1923
|
-
|
1924
|
-
|
1925
|
-
|
1926
|
-
|
1927
|
-
|
1928
|
-
|
1929
|
-
|
1930
|
-
|
1931
|
-
|
1932
|
-
|
1933
|
-
|
1934
|
-
|
1935
|
-
|
1936
|
-
|
1937
|
-
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
|
1944
|
-
|
1945
|
-
|
1946
|
-
|
1947
|
-
|
1948
|
-
|
1949
|
-
|
1950
|
-
|
1951
|
-
|
1952
|
-
|
1953
|
-
|
1954
|
-
const
|
1955
|
-
|
1956
|
-
|
1957
|
-
|
1958
|
-
|
1959
|
-
|
1960
|
-
|
1961
|
-
|
1962
|
-
|
1963
|
-
|
1964
|
-
|
1965
|
-
|
1966
|
-
|
1967
|
-
|
1968
|
-
|
1969
|
-
|
1970
|
-
|
1971
|
-
|
1972
|
-
|
1973
|
-
|
1974
|
-
|
1975
|
-
|
1976
|
-
|
1977
|
-
|
1978
|
-
|
1979
|
-
|
1980
|
-
|
1981
|
-
|
1982
|
-
|
1983
|
-
|
1984
|
-
|
1985
|
-
|
1986
|
-
|
1987
|
-
|
1988
|
-
|
1989
|
-
|
1990
|
-
|
1991
|
-
|
1992
|
-
|
1993
|
-
|
1994
|
-
|
1995
|
-
|
1996
|
-
|
1997
|
-
|
1998
|
-
|
1999
|
-
|
2000
|
-
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
|
2005
|
-
|
2006
|
-
|
2007
|
-
|
2008
|
-
|
2009
|
-
|
2010
|
-
|
2011
|
-
|
2012
|
-
|
2013
|
-
|
2014
|
-
|
2015
|
-
|
2016
|
-
|
2017
|
-
|
2018
|
-
}
|
2019
|
-
|
2020
|
-
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2026
|
-
|
2027
|
-
|
2028
|
-
|
2029
|
-
|
2030
|
-
|
2031
|
-
|
2032
|
-
|
2033
|
-
|
2034
|
-
|
2035
|
-
|
2036
|
-
|
2037
|
-
|
2038
|
-
|
2039
|
-
|
2040
|
-
|
2041
|
-
|
2042
|
-
|
2043
|
-
|
2044
|
-
|
2045
|
-
|
2046
|
-
|
2047
|
-
}
|
2048
|
-
if (!options.
|
2049
|
-
unset(_formState.
|
2050
|
-
|
2051
|
-
|
2052
|
-
|
2053
|
-
|
2054
|
-
|
2055
|
-
|
2056
|
-
|
2057
|
-
|
2058
|
-
|
2059
|
-
|
2060
|
-
|
2061
|
-
|
2062
|
-
|
2063
|
-
|
2064
|
-
|
2065
|
-
|
2066
|
-
|
2067
|
-
|
2068
|
-
|
2069
|
-
|
2070
|
-
|
2071
|
-
|
2072
|
-
|
2073
|
-
|
2074
|
-
|
2075
|
-
|
2076
|
-
|
2077
|
-
|
2078
|
-
|
2079
|
-
|
2080
|
-
|
2081
|
-
|
2082
|
-
|
2083
|
-
|
2084
|
-
|
2085
|
-
|
2086
|
-
|
2087
|
-
|
2088
|
-
|
2089
|
-
|
2090
|
-
|
2091
|
-
|
2092
|
-
|
2093
|
-
|
2094
|
-
|
2095
|
-
|
2096
|
-
|
2097
|
-
|
2098
|
-
|
2099
|
-
|
2100
|
-
|
2101
|
-
|
2102
|
-
|
2103
|
-
|
2104
|
-
|
2105
|
-
|
2106
|
-
|
2107
|
-
|
2108
|
-
|
2109
|
-
|
2110
|
-
|
2111
|
-
|
2112
|
-
|
2113
|
-
|
2114
|
-
|
2115
|
-
|
2116
|
-
|
2117
|
-
|
2118
|
-
|
2119
|
-
|
2120
|
-
|
2121
|
-
|
2122
|
-
|
2123
|
-
|
2124
|
-
|
2125
|
-
|
2126
|
-
|
2127
|
-
|
2128
|
-
|
2129
|
-
|
2130
|
-
|
2131
|
-
|
2132
|
-
|
2133
|
-
|
2134
|
-
|
2135
|
-
|
2136
|
-
|
2137
|
-
|
2138
|
-
|
2139
|
-
|
2140
|
-
|
2141
|
-
|
2142
|
-
|
2143
|
-
|
2144
|
-
|
2145
|
-
|
2146
|
-
|
2147
|
-
|
2148
|
-
|
2149
|
-
|
2150
|
-
|
2151
|
-
|
2152
|
-
|
2153
|
-
|
2154
|
-
|
2155
|
-
|
2156
|
-
|
2157
|
-
|
2158
|
-
|
2159
|
-
}
|
2160
|
-
};
|
2161
|
-
|
2162
|
-
|
2163
|
-
|
2164
|
-
|
2165
|
-
|
2166
|
-
|
2167
|
-
|
2168
|
-
|
2169
|
-
|
2170
|
-
|
2171
|
-
|
2172
|
-
|
2173
|
-
|
2174
|
-
|
2175
|
-
|
2176
|
-
|
2177
|
-
|
2178
|
-
|
2179
|
-
|
2180
|
-
|
2181
|
-
|
2182
|
-
|
2183
|
-
|
2184
|
-
|
2185
|
-
|
2186
|
-
|
2187
|
-
|
2188
|
-
|
2189
|
-
|
2190
|
-
|
2191
|
-
get
|
2192
|
-
return
|
2193
|
-
},
|
2194
|
-
|
2195
|
-
|
2196
|
-
},
|
2197
|
-
get
|
2198
|
-
return
|
2199
|
-
},
|
2200
|
-
|
2201
|
-
|
2202
|
-
},
|
2203
|
-
|
2204
|
-
|
2205
|
-
},
|
2206
|
-
get
|
2207
|
-
return
|
2208
|
-
},
|
2209
|
-
set
|
2210
|
-
|
2211
|
-
},
|
2212
|
-
get
|
2213
|
-
return
|
2214
|
-
},
|
2215
|
-
set
|
2216
|
-
|
2217
|
-
|
2218
|
-
|
2219
|
-
|
2220
|
-
},
|
2221
|
-
|
2222
|
-
|
2223
|
-
|
2224
|
-
|
2225
|
-
|
2226
|
-
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2230
|
-
|
2231
|
-
|
2232
|
-
|
2233
|
-
|
2234
|
-
|
2235
|
-
|
1388
|
+
const defaultOptions = {
|
1389
|
+
mode: VALIDATION_MODE.onSubmit,
|
1390
|
+
reValidateMode: VALIDATION_MODE.onChange,
|
1391
|
+
shouldFocusError: true,
|
1392
|
+
};
|
1393
|
+
function createFormControl(props = {}, flushRootRender) {
|
1394
|
+
let _options = {
|
1395
|
+
...defaultOptions,
|
1396
|
+
...props,
|
1397
|
+
};
|
1398
|
+
let _formState = {
|
1399
|
+
submitCount: 0,
|
1400
|
+
isDirty: false,
|
1401
|
+
isLoading: isFunction(_options.defaultValues),
|
1402
|
+
isValidating: false,
|
1403
|
+
isSubmitted: false,
|
1404
|
+
isSubmitting: false,
|
1405
|
+
isSubmitSuccessful: false,
|
1406
|
+
isValid: false,
|
1407
|
+
touchedFields: {},
|
1408
|
+
dirtyFields: {},
|
1409
|
+
errors: {},
|
1410
|
+
};
|
1411
|
+
let _fields = {};
|
1412
|
+
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
|
1413
|
+
? cloneObject(_options.defaultValues || _options.values) || {}
|
1414
|
+
: {};
|
1415
|
+
let _formValues = _options.shouldUnregister
|
1416
|
+
? {}
|
1417
|
+
: cloneObject(_defaultValues);
|
1418
|
+
let _state = {
|
1419
|
+
action: false,
|
1420
|
+
mount: false,
|
1421
|
+
watch: false,
|
1422
|
+
};
|
1423
|
+
let _names = {
|
1424
|
+
mount: new Set(),
|
1425
|
+
unMount: new Set(),
|
1426
|
+
array: new Set(),
|
1427
|
+
watch: new Set(),
|
1428
|
+
};
|
1429
|
+
let delayErrorCallback;
|
1430
|
+
let timer = 0;
|
1431
|
+
const _proxyFormState = {
|
1432
|
+
isDirty: false,
|
1433
|
+
dirtyFields: false,
|
1434
|
+
touchedFields: false,
|
1435
|
+
isValidating: false,
|
1436
|
+
isValid: false,
|
1437
|
+
errors: false,
|
1438
|
+
};
|
1439
|
+
const _subjects = {
|
1440
|
+
values: createSubject(),
|
1441
|
+
array: createSubject(),
|
1442
|
+
state: createSubject(),
|
1443
|
+
};
|
1444
|
+
const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
|
1445
|
+
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
1446
|
+
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
1447
|
+
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
1448
|
+
const debounce = (callback) => (wait) => {
|
1449
|
+
clearTimeout(timer);
|
1450
|
+
timer = setTimeout(callback, wait);
|
1451
|
+
};
|
1452
|
+
const _updateValid = async (shouldUpdateValid) => {
|
1453
|
+
if (_proxyFormState.isValid || shouldUpdateValid) {
|
1454
|
+
const isValid = _options.resolver
|
1455
|
+
? isEmptyObject((await _executeSchema()).errors)
|
1456
|
+
: await executeBuiltInValidation(_fields, true);
|
1457
|
+
if (isValid !== _formState.isValid) {
|
1458
|
+
_subjects.state.next({
|
1459
|
+
isValid,
|
1460
|
+
});
|
1461
|
+
}
|
1462
|
+
}
|
1463
|
+
};
|
1464
|
+
const _updateIsValidating = (value) => _proxyFormState.isValidating &&
|
1465
|
+
_subjects.state.next({
|
1466
|
+
isValidating: value,
|
1467
|
+
});
|
1468
|
+
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
1469
|
+
if (args && method) {
|
1470
|
+
_state.action = true;
|
1471
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
1472
|
+
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
1473
|
+
shouldSetValues && set(_fields, name, fieldValues);
|
1474
|
+
}
|
1475
|
+
if (shouldUpdateFieldsAndState &&
|
1476
|
+
Array.isArray(get(_formState.errors, name))) {
|
1477
|
+
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
1478
|
+
shouldSetValues && set(_formState.errors, name, errors);
|
1479
|
+
unsetEmptyArray(_formState.errors, name);
|
1480
|
+
}
|
1481
|
+
if (_proxyFormState.touchedFields &&
|
1482
|
+
shouldUpdateFieldsAndState &&
|
1483
|
+
Array.isArray(get(_formState.touchedFields, name))) {
|
1484
|
+
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
1485
|
+
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
1486
|
+
}
|
1487
|
+
if (_proxyFormState.dirtyFields) {
|
1488
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
1489
|
+
}
|
1490
|
+
_subjects.state.next({
|
1491
|
+
name,
|
1492
|
+
isDirty: _getDirty(name, values),
|
1493
|
+
dirtyFields: _formState.dirtyFields,
|
1494
|
+
errors: _formState.errors,
|
1495
|
+
isValid: _formState.isValid,
|
1496
|
+
});
|
1497
|
+
}
|
1498
|
+
else {
|
1499
|
+
set(_formValues, name, values);
|
1500
|
+
}
|
1501
|
+
};
|
1502
|
+
const updateErrors = (name, error) => {
|
1503
|
+
set(_formState.errors, name, error);
|
1504
|
+
_subjects.state.next({
|
1505
|
+
errors: _formState.errors,
|
1506
|
+
});
|
1507
|
+
};
|
1508
|
+
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
|
1509
|
+
const field = get(_fields, name);
|
1510
|
+
if (field) {
|
1511
|
+
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
|
1512
|
+
isUndefined(defaultValue) ||
|
1513
|
+
(ref && ref.defaultChecked) ||
|
1514
|
+
shouldSkipSetValueAs
|
1515
|
+
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
1516
|
+
: setFieldValue(name, defaultValue);
|
1517
|
+
_state.mount && _updateValid();
|
1518
|
+
}
|
1519
|
+
};
|
1520
|
+
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
1521
|
+
let shouldUpdateField = false;
|
1522
|
+
let isPreviousDirty = false;
|
1523
|
+
const output = {
|
1524
|
+
name,
|
1525
|
+
};
|
1526
|
+
if (!isBlurEvent || shouldDirty) {
|
1527
|
+
if (_proxyFormState.isDirty) {
|
1528
|
+
isPreviousDirty = _formState.isDirty;
|
1529
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
1530
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
1531
|
+
}
|
1532
|
+
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
1533
|
+
isPreviousDirty = get(_formState.dirtyFields, name);
|
1534
|
+
isCurrentFieldPristine
|
1535
|
+
? unset(_formState.dirtyFields, name)
|
1536
|
+
: set(_formState.dirtyFields, name, true);
|
1537
|
+
output.dirtyFields = _formState.dirtyFields;
|
1538
|
+
shouldUpdateField =
|
1539
|
+
shouldUpdateField ||
|
1540
|
+
(_proxyFormState.dirtyFields &&
|
1541
|
+
isPreviousDirty !== !isCurrentFieldPristine);
|
1542
|
+
}
|
1543
|
+
if (isBlurEvent) {
|
1544
|
+
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
1545
|
+
if (!isPreviousFieldTouched) {
|
1546
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
1547
|
+
output.touchedFields = _formState.touchedFields;
|
1548
|
+
shouldUpdateField =
|
1549
|
+
shouldUpdateField ||
|
1550
|
+
(_proxyFormState.touchedFields &&
|
1551
|
+
isPreviousFieldTouched !== isBlurEvent);
|
1552
|
+
}
|
1553
|
+
}
|
1554
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
1555
|
+
return shouldUpdateField ? output : {};
|
1556
|
+
};
|
1557
|
+
const shouldRenderByError = (name, isValid, error, fieldState) => {
|
1558
|
+
const previousFieldError = get(_formState.errors, name);
|
1559
|
+
const shouldUpdateValid = _proxyFormState.isValid &&
|
1560
|
+
isBoolean(isValid) &&
|
1561
|
+
_formState.isValid !== isValid;
|
1562
|
+
if (props.delayError && error) {
|
1563
|
+
delayErrorCallback = debounce(() => updateErrors(name, error));
|
1564
|
+
delayErrorCallback(props.delayError);
|
1565
|
+
}
|
1566
|
+
else {
|
1567
|
+
clearTimeout(timer);
|
1568
|
+
delayErrorCallback = null;
|
1569
|
+
error
|
1570
|
+
? set(_formState.errors, name, error)
|
1571
|
+
: unset(_formState.errors, name);
|
1572
|
+
}
|
1573
|
+
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) ||
|
1574
|
+
!isEmptyObject(fieldState) ||
|
1575
|
+
shouldUpdateValid) {
|
1576
|
+
const updatedFormState = {
|
1577
|
+
...fieldState,
|
1578
|
+
...(shouldUpdateValid && isBoolean(isValid) ? { isValid } : {}),
|
1579
|
+
errors: _formState.errors,
|
1580
|
+
name,
|
1581
|
+
};
|
1582
|
+
_formState = {
|
1583
|
+
..._formState,
|
1584
|
+
...updatedFormState,
|
1585
|
+
};
|
1586
|
+
_subjects.state.next(updatedFormState);
|
1587
|
+
}
|
1588
|
+
_updateIsValidating(false);
|
1589
|
+
};
|
1590
|
+
const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
1591
|
+
const executeSchemaAndUpdateState = async (names) => {
|
1592
|
+
const { errors } = await _executeSchema();
|
1593
|
+
if (names) {
|
1594
|
+
for (const name of names) {
|
1595
|
+
const error = get(errors, name);
|
1596
|
+
error
|
1597
|
+
? set(_formState.errors, name, error)
|
1598
|
+
: unset(_formState.errors, name);
|
1599
|
+
}
|
1600
|
+
}
|
1601
|
+
else {
|
1602
|
+
_formState.errors = errors;
|
1603
|
+
}
|
1604
|
+
return errors;
|
1605
|
+
};
|
1606
|
+
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
1607
|
+
valid: true,
|
1608
|
+
}) => {
|
1609
|
+
for (const name in fields) {
|
1610
|
+
const field = fields[name];
|
1611
|
+
if (field) {
|
1612
|
+
const { _f, ...fieldValue } = field;
|
1613
|
+
if (_f) {
|
1614
|
+
const isFieldArrayRoot = _names.array.has(_f.name);
|
1615
|
+
const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
1616
|
+
if (fieldError[_f.name]) {
|
1617
|
+
context.valid = false;
|
1618
|
+
if (shouldOnlyCheckValid) {
|
1619
|
+
break;
|
1620
|
+
}
|
1621
|
+
}
|
1622
|
+
!shouldOnlyCheckValid &&
|
1623
|
+
(get(fieldError, _f.name)
|
1624
|
+
? isFieldArrayRoot
|
1625
|
+
? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
|
1626
|
+
: set(_formState.errors, _f.name, fieldError[_f.name])
|
1627
|
+
: unset(_formState.errors, _f.name));
|
1628
|
+
}
|
1629
|
+
fieldValue &&
|
1630
|
+
(await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
|
1631
|
+
}
|
1632
|
+
}
|
1633
|
+
return context.valid;
|
1634
|
+
};
|
1635
|
+
const _removeUnmounted = () => {
|
1636
|
+
for (const name of _names.unMount) {
|
1637
|
+
const field = get(_fields, name);
|
1638
|
+
field &&
|
1639
|
+
(field._f.refs
|
1640
|
+
? field._f.refs.every((ref) => !live(ref))
|
1641
|
+
: !live(field._f.ref)) &&
|
1642
|
+
unregister(name);
|
1643
|
+
}
|
1644
|
+
_names.unMount = new Set();
|
1645
|
+
};
|
1646
|
+
const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
|
1647
|
+
!deepEqual(getValues(), _defaultValues));
|
1648
|
+
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
1649
|
+
...(_state.mount
|
1650
|
+
? _formValues
|
1651
|
+
: isUndefined(defaultValue)
|
1652
|
+
? _defaultValues
|
1653
|
+
: isString(names)
|
1654
|
+
? { [names]: defaultValue }
|
1655
|
+
: defaultValue),
|
1656
|
+
}, isGlobal, defaultValue);
|
1657
|
+
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
|
1658
|
+
const setFieldValue = (name, value, options = {}) => {
|
1659
|
+
const field = get(_fields, name);
|
1660
|
+
let fieldValue = value;
|
1661
|
+
if (field) {
|
1662
|
+
const fieldReference = field._f;
|
1663
|
+
if (fieldReference) {
|
1664
|
+
!fieldReference.disabled &&
|
1665
|
+
set(_formValues, name, getFieldValueAs(value, fieldReference));
|
1666
|
+
fieldValue =
|
1667
|
+
isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
1668
|
+
? ''
|
1669
|
+
: value;
|
1670
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
1671
|
+
[...fieldReference.ref.options].forEach((optionRef) => (optionRef.selected = fieldValue.includes(optionRef.value)));
|
1672
|
+
}
|
1673
|
+
else if (fieldReference.refs) {
|
1674
|
+
if (isCheckBoxInput(fieldReference.ref)) {
|
1675
|
+
fieldReference.refs.length > 1
|
1676
|
+
? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) &&
|
1677
|
+
(checkboxRef.checked = Array.isArray(fieldValue)
|
1678
|
+
? !!fieldValue.find((data) => data === checkboxRef.value)
|
1679
|
+
: fieldValue === checkboxRef.value))
|
1680
|
+
: fieldReference.refs[0] &&
|
1681
|
+
(fieldReference.refs[0].checked = !!fieldValue);
|
1682
|
+
}
|
1683
|
+
else {
|
1684
|
+
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
|
1685
|
+
}
|
1686
|
+
}
|
1687
|
+
else if (isFileInput(fieldReference.ref)) {
|
1688
|
+
fieldReference.ref.value = '';
|
1689
|
+
}
|
1690
|
+
else {
|
1691
|
+
fieldReference.ref.value = fieldValue;
|
1692
|
+
if (!fieldReference.ref.type) {
|
1693
|
+
_subjects.values.next({
|
1694
|
+
name,
|
1695
|
+
values: { ..._formValues },
|
1696
|
+
});
|
1697
|
+
}
|
1698
|
+
}
|
1699
|
+
}
|
1700
|
+
}
|
1701
|
+
(options.shouldDirty || options.shouldTouch) &&
|
1702
|
+
updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
1703
|
+
options.shouldValidate && trigger(name);
|
1704
|
+
};
|
1705
|
+
const setValues = (name, value, options) => {
|
1706
|
+
for (const fieldKey in value) {
|
1707
|
+
const fieldValue = value[fieldKey];
|
1708
|
+
const fieldName = `${name}.${fieldKey}`;
|
1709
|
+
const field = get(_fields, fieldName);
|
1710
|
+
(_names.array.has(name) ||
|
1711
|
+
!isPrimitive(fieldValue) ||
|
1712
|
+
(field && !field._f)) &&
|
1713
|
+
!isDateObject(fieldValue)
|
1714
|
+
? setValues(fieldName, fieldValue, options)
|
1715
|
+
: setFieldValue(fieldName, fieldValue, options);
|
1716
|
+
}
|
1717
|
+
};
|
1718
|
+
const setValue = (name, value, options = {}) => {
|
1719
|
+
const field = get(_fields, name);
|
1720
|
+
const isFieldArray = _names.array.has(name);
|
1721
|
+
const cloneValue = cloneObject(value);
|
1722
|
+
set(_formValues, name, cloneValue);
|
1723
|
+
if (isFieldArray) {
|
1724
|
+
_subjects.array.next({
|
1725
|
+
name,
|
1726
|
+
values: { ..._formValues },
|
1727
|
+
});
|
1728
|
+
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
|
1729
|
+
options.shouldDirty) {
|
1730
|
+
_subjects.state.next({
|
1731
|
+
name,
|
1732
|
+
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
1733
|
+
isDirty: _getDirty(name, cloneValue),
|
1734
|
+
});
|
1735
|
+
}
|
1736
|
+
}
|
1737
|
+
else {
|
1738
|
+
field && !field._f && !isNullOrUndefined(cloneValue)
|
1739
|
+
? setValues(name, cloneValue, options)
|
1740
|
+
: setFieldValue(name, cloneValue, options);
|
1741
|
+
}
|
1742
|
+
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
1743
|
+
_subjects.values.next({
|
1744
|
+
name,
|
1745
|
+
values: { ..._formValues },
|
1746
|
+
});
|
1747
|
+
!_state.mount && flushRootRender();
|
1748
|
+
};
|
1749
|
+
const onChange = async (event) => {
|
1750
|
+
const target = event.target;
|
1751
|
+
let name = target.name;
|
1752
|
+
let isFieldValueUpdated = true;
|
1753
|
+
const field = get(_fields, name);
|
1754
|
+
const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
|
1755
|
+
if (field) {
|
1756
|
+
let error;
|
1757
|
+
let isValid;
|
1758
|
+
const fieldValue = getCurrentFieldValue();
|
1759
|
+
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
1760
|
+
const shouldSkipValidation = (!hasValidation(field._f) &&
|
1761
|
+
!_options.resolver &&
|
1762
|
+
!get(_formState.errors, name) &&
|
1763
|
+
!field._f.deps) ||
|
1764
|
+
skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
1765
|
+
const watched = isWatched(name, _names, isBlurEvent);
|
1766
|
+
set(_formValues, name, fieldValue);
|
1767
|
+
if (isBlurEvent) {
|
1768
|
+
field._f.onBlur && field._f.onBlur(event);
|
1769
|
+
delayErrorCallback && delayErrorCallback(0);
|
1770
|
+
}
|
1771
|
+
else if (field._f.onChange) {
|
1772
|
+
field._f.onChange(event);
|
1773
|
+
}
|
1774
|
+
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
|
1775
|
+
const shouldRender = !isEmptyObject(fieldState) || watched;
|
1776
|
+
!isBlurEvent &&
|
1777
|
+
_subjects.values.next({
|
1778
|
+
name,
|
1779
|
+
type: event.type,
|
1780
|
+
values: { ..._formValues },
|
1781
|
+
});
|
1782
|
+
if (shouldSkipValidation) {
|
1783
|
+
_proxyFormState.isValid && _updateValid();
|
1784
|
+
return (shouldRender &&
|
1785
|
+
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
|
1786
|
+
}
|
1787
|
+
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
1788
|
+
_updateIsValidating(true);
|
1789
|
+
if (_options.resolver) {
|
1790
|
+
const { errors } = await _executeSchema([name]);
|
1791
|
+
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
1792
|
+
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
1793
|
+
error = errorLookupResult.error;
|
1794
|
+
name = errorLookupResult.name;
|
1795
|
+
isValid = isEmptyObject(errors);
|
1796
|
+
}
|
1797
|
+
else {
|
1798
|
+
error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
1799
|
+
isFieldValueUpdated =
|
1800
|
+
isNaN(fieldValue) ||
|
1801
|
+
fieldValue === get(_formValues, name, fieldValue);
|
1802
|
+
if (isFieldValueUpdated) {
|
1803
|
+
if (error) {
|
1804
|
+
isValid = false;
|
1805
|
+
}
|
1806
|
+
else if (_proxyFormState.isValid) {
|
1807
|
+
isValid = await executeBuiltInValidation(_fields, true);
|
1808
|
+
}
|
1809
|
+
}
|
1810
|
+
}
|
1811
|
+
if (isFieldValueUpdated) {
|
1812
|
+
field._f.deps &&
|
1813
|
+
trigger(field._f.deps);
|
1814
|
+
shouldRenderByError(name, isValid, error, fieldState);
|
1815
|
+
}
|
1816
|
+
}
|
1817
|
+
};
|
1818
|
+
const trigger = async (name, options = {}) => {
|
1819
|
+
let isValid;
|
1820
|
+
let validationResult;
|
1821
|
+
const fieldNames = convertToArrayPayload(name);
|
1822
|
+
_updateIsValidating(true);
|
1823
|
+
if (_options.resolver) {
|
1824
|
+
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
1825
|
+
isValid = isEmptyObject(errors);
|
1826
|
+
validationResult = name
|
1827
|
+
? !fieldNames.some((name) => get(errors, name))
|
1828
|
+
: isValid;
|
1829
|
+
}
|
1830
|
+
else if (name) {
|
1831
|
+
validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
|
1832
|
+
const field = get(_fields, fieldName);
|
1833
|
+
return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
|
1834
|
+
}))).every(Boolean);
|
1835
|
+
!(!validationResult && !_formState.isValid) && _updateValid();
|
1836
|
+
}
|
1837
|
+
else {
|
1838
|
+
validationResult = isValid = await executeBuiltInValidation(_fields);
|
1839
|
+
}
|
1840
|
+
_subjects.state.next({
|
1841
|
+
...(!isString(name) ||
|
1842
|
+
(_proxyFormState.isValid && isValid !== _formState.isValid)
|
1843
|
+
? {}
|
1844
|
+
: { name }),
|
1845
|
+
...(_options.resolver || !name ? { isValid } : {}),
|
1846
|
+
errors: _formState.errors,
|
1847
|
+
isValidating: false,
|
1848
|
+
});
|
1849
|
+
options.shouldFocus &&
|
1850
|
+
!validationResult &&
|
1851
|
+
focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
|
1852
|
+
return validationResult;
|
1853
|
+
};
|
1854
|
+
const getValues = (fieldNames) => {
|
1855
|
+
const values = {
|
1856
|
+
..._defaultValues,
|
1857
|
+
...(_state.mount ? _formValues : {}),
|
1858
|
+
};
|
1859
|
+
return isUndefined(fieldNames)
|
1860
|
+
? values
|
1861
|
+
: isString(fieldNames)
|
1862
|
+
? get(values, fieldNames)
|
1863
|
+
: fieldNames.map((name) => get(values, name));
|
1864
|
+
};
|
1865
|
+
const getFieldState = (name, formState) => ({
|
1866
|
+
invalid: !!get((formState || _formState).errors, name),
|
1867
|
+
isDirty: !!get((formState || _formState).dirtyFields, name),
|
1868
|
+
isTouched: !!get((formState || _formState).touchedFields, name),
|
1869
|
+
error: get((formState || _formState).errors, name),
|
1870
|
+
});
|
1871
|
+
const clearErrors = (name) => {
|
1872
|
+
name &&
|
1873
|
+
convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
|
1874
|
+
_subjects.state.next({
|
1875
|
+
errors: name ? _formState.errors : {},
|
1876
|
+
});
|
1877
|
+
};
|
1878
|
+
const setError = (name, error, options) => {
|
1879
|
+
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
|
1880
|
+
set(_formState.errors, name, {
|
1881
|
+
...error,
|
1882
|
+
ref,
|
1883
|
+
});
|
1884
|
+
_subjects.state.next({
|
1885
|
+
name,
|
1886
|
+
errors: _formState.errors,
|
1887
|
+
isValid: false,
|
1888
|
+
});
|
1889
|
+
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
1890
|
+
};
|
1891
|
+
const watch = (name, defaultValue) => isFunction(name)
|
1892
|
+
? _subjects.values.subscribe({
|
1893
|
+
next: (payload) => name(_getWatch(undefined, defaultValue), payload),
|
1894
|
+
})
|
1895
|
+
: _getWatch(name, defaultValue, true);
|
1896
|
+
const unregister = (name, options = {}) => {
|
1897
|
+
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
|
1898
|
+
_names.mount.delete(fieldName);
|
1899
|
+
_names.array.delete(fieldName);
|
1900
|
+
if (!options.keepValue) {
|
1901
|
+
unset(_fields, fieldName);
|
1902
|
+
unset(_formValues, fieldName);
|
1903
|
+
}
|
1904
|
+
!options.keepError && unset(_formState.errors, fieldName);
|
1905
|
+
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
1906
|
+
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
1907
|
+
!_options.shouldUnregister &&
|
1908
|
+
!options.keepDefaultValue &&
|
1909
|
+
unset(_defaultValues, fieldName);
|
1910
|
+
}
|
1911
|
+
_subjects.values.next({
|
1912
|
+
values: { ..._formValues },
|
1913
|
+
});
|
1914
|
+
_subjects.state.next({
|
1915
|
+
..._formState,
|
1916
|
+
...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
|
1917
|
+
});
|
1918
|
+
!options.keepIsValid && _updateValid();
|
1919
|
+
};
|
1920
|
+
const register = (name, options = {}) => {
|
1921
|
+
let field = get(_fields, name);
|
1922
|
+
const disabledIsDefined = isBoolean(options.disabled);
|
1923
|
+
set(_fields, name, {
|
1924
|
+
...(field || {}),
|
1925
|
+
_f: {
|
1926
|
+
...(field && field._f ? field._f : { ref: { name } }),
|
1927
|
+
name,
|
1928
|
+
mount: true,
|
1929
|
+
...options,
|
1930
|
+
},
|
1931
|
+
});
|
1932
|
+
_names.mount.add(name);
|
1933
|
+
field
|
1934
|
+
? disabledIsDefined &&
|
1935
|
+
set(_formValues, name, options.disabled
|
1936
|
+
? undefined
|
1937
|
+
: get(_formValues, name, getFieldValue(field._f)))
|
1938
|
+
: updateValidAndValue(name, true, options.value);
|
1939
|
+
return {
|
1940
|
+
...(disabledIsDefined ? { disabled: options.disabled } : {}),
|
1941
|
+
...(_options.shouldUseNativeValidation
|
1942
|
+
? {
|
1943
|
+
required: !!options.required,
|
1944
|
+
min: getRuleValue(options.min),
|
1945
|
+
max: getRuleValue(options.max),
|
1946
|
+
minLength: getRuleValue(options.minLength),
|
1947
|
+
maxLength: getRuleValue(options.maxLength),
|
1948
|
+
pattern: getRuleValue(options.pattern),
|
1949
|
+
}
|
1950
|
+
: {}),
|
1951
|
+
name,
|
1952
|
+
onChange,
|
1953
|
+
onBlur: onChange,
|
1954
|
+
ref: (ref) => {
|
1955
|
+
if (ref) {
|
1956
|
+
register(name, options);
|
1957
|
+
field = get(_fields, name);
|
1958
|
+
const fieldRef = isUndefined(ref.value)
|
1959
|
+
? ref.querySelectorAll
|
1960
|
+
? ref.querySelectorAll('input,select,textarea')[0] || ref
|
1961
|
+
: ref
|
1962
|
+
: ref;
|
1963
|
+
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
1964
|
+
const refs = field._f.refs || [];
|
1965
|
+
if (radioOrCheckbox
|
1966
|
+
? refs.find((option) => option === fieldRef)
|
1967
|
+
: fieldRef === field._f.ref) {
|
1968
|
+
return;
|
1969
|
+
}
|
1970
|
+
set(_fields, name, {
|
1971
|
+
_f: {
|
1972
|
+
...field._f,
|
1973
|
+
...(radioOrCheckbox
|
1974
|
+
? {
|
1975
|
+
refs: [
|
1976
|
+
...refs.filter(live),
|
1977
|
+
fieldRef,
|
1978
|
+
...(Array.isArray(get(_defaultValues, name)) ? [{}] : []),
|
1979
|
+
],
|
1980
|
+
ref: { type: fieldRef.type, name },
|
1981
|
+
}
|
1982
|
+
: { ref: fieldRef }),
|
1983
|
+
},
|
1984
|
+
});
|
1985
|
+
updateValidAndValue(name, false, undefined, fieldRef);
|
1986
|
+
}
|
1987
|
+
else {
|
1988
|
+
field = get(_fields, name, {});
|
1989
|
+
if (field._f) {
|
1990
|
+
field._f.mount = false;
|
1991
|
+
}
|
1992
|
+
(_options.shouldUnregister || options.shouldUnregister) &&
|
1993
|
+
!(isNameInFieldArray(_names.array, name) && _state.action) &&
|
1994
|
+
_names.unMount.add(name);
|
1995
|
+
}
|
1996
|
+
},
|
1997
|
+
};
|
1998
|
+
};
|
1999
|
+
const _focusError = () => _options.shouldFocusError &&
|
2000
|
+
focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
|
2001
|
+
const handleSubmit = (onValid, onInvalid) => async (e) => {
|
2002
|
+
if (e) {
|
2003
|
+
e.preventDefault && e.preventDefault();
|
2004
|
+
e.persist && e.persist();
|
2005
|
+
}
|
2006
|
+
let fieldValues = cloneObject(_formValues);
|
2007
|
+
_subjects.state.next({
|
2008
|
+
isSubmitting: true,
|
2009
|
+
});
|
2010
|
+
if (_options.resolver) {
|
2011
|
+
const { errors, values } = await _executeSchema();
|
2012
|
+
_formState.errors = errors;
|
2013
|
+
fieldValues = values;
|
2014
|
+
}
|
2015
|
+
else {
|
2016
|
+
await executeBuiltInValidation(_fields);
|
2017
|
+
}
|
2018
|
+
unset(_formState.errors, 'root');
|
2019
|
+
if (isEmptyObject(_formState.errors)) {
|
2020
|
+
_subjects.state.next({
|
2021
|
+
errors: {},
|
2022
|
+
});
|
2023
|
+
await onValid(fieldValues, e);
|
2024
|
+
}
|
2025
|
+
else {
|
2026
|
+
if (onInvalid) {
|
2027
|
+
await onInvalid({ ..._formState.errors }, e);
|
2028
|
+
}
|
2029
|
+
_focusError();
|
2030
|
+
setTimeout(_focusError);
|
2031
|
+
}
|
2032
|
+
_subjects.state.next({
|
2033
|
+
isSubmitted: true,
|
2034
|
+
isSubmitting: false,
|
2035
|
+
isSubmitSuccessful: isEmptyObject(_formState.errors),
|
2036
|
+
submitCount: _formState.submitCount + 1,
|
2037
|
+
errors: _formState.errors,
|
2038
|
+
});
|
2039
|
+
};
|
2040
|
+
const resetField = (name, options = {}) => {
|
2041
|
+
if (get(_fields, name)) {
|
2042
|
+
if (isUndefined(options.defaultValue)) {
|
2043
|
+
setValue(name, get(_defaultValues, name));
|
2044
|
+
}
|
2045
|
+
else {
|
2046
|
+
setValue(name, options.defaultValue);
|
2047
|
+
set(_defaultValues, name, options.defaultValue);
|
2048
|
+
}
|
2049
|
+
if (!options.keepTouched) {
|
2050
|
+
unset(_formState.touchedFields, name);
|
2051
|
+
}
|
2052
|
+
if (!options.keepDirty) {
|
2053
|
+
unset(_formState.dirtyFields, name);
|
2054
|
+
_formState.isDirty = options.defaultValue
|
2055
|
+
? _getDirty(name, get(_defaultValues, name))
|
2056
|
+
: _getDirty();
|
2057
|
+
}
|
2058
|
+
if (!options.keepError) {
|
2059
|
+
unset(_formState.errors, name);
|
2060
|
+
_proxyFormState.isValid && _updateValid();
|
2061
|
+
}
|
2062
|
+
_subjects.state.next({ ..._formState });
|
2063
|
+
}
|
2064
|
+
};
|
2065
|
+
const _reset = (formValues, keepStateOptions = {}) => {
|
2066
|
+
const updatedValues = formValues || _defaultValues;
|
2067
|
+
const cloneUpdatedValues = cloneObject(updatedValues);
|
2068
|
+
const values = formValues && !isEmptyObject(formValues)
|
2069
|
+
? cloneUpdatedValues
|
2070
|
+
: _defaultValues;
|
2071
|
+
if (!keepStateOptions.keepDefaultValues) {
|
2072
|
+
_defaultValues = updatedValues;
|
2073
|
+
}
|
2074
|
+
if (!keepStateOptions.keepValues) {
|
2075
|
+
if (keepStateOptions.keepDirtyValues || shouldCaptureDirtyFields) {
|
2076
|
+
for (const fieldName of _names.mount) {
|
2077
|
+
get(_formState.dirtyFields, fieldName)
|
2078
|
+
? set(values, fieldName, get(_formValues, fieldName))
|
2079
|
+
: setValue(fieldName, get(values, fieldName));
|
2080
|
+
}
|
2081
|
+
}
|
2082
|
+
else {
|
2083
|
+
if (isWeb && isUndefined(formValues)) {
|
2084
|
+
for (const name of _names.mount) {
|
2085
|
+
const field = get(_fields, name);
|
2086
|
+
if (field && field._f) {
|
2087
|
+
const fieldReference = Array.isArray(field._f.refs)
|
2088
|
+
? field._f.refs[0]
|
2089
|
+
: field._f.ref;
|
2090
|
+
if (isHTMLElement(fieldReference)) {
|
2091
|
+
const form = fieldReference.closest('form');
|
2092
|
+
if (form) {
|
2093
|
+
form.reset();
|
2094
|
+
break;
|
2095
|
+
}
|
2096
|
+
}
|
2097
|
+
}
|
2098
|
+
}
|
2099
|
+
}
|
2100
|
+
_fields = {};
|
2101
|
+
}
|
2102
|
+
_formValues = props.shouldUnregister
|
2103
|
+
? keepStateOptions.keepDefaultValues
|
2104
|
+
? cloneObject(_defaultValues)
|
2105
|
+
: {}
|
2106
|
+
: cloneUpdatedValues;
|
2107
|
+
_subjects.array.next({
|
2108
|
+
values: { ...values },
|
2109
|
+
});
|
2110
|
+
_subjects.values.next({
|
2111
|
+
values: { ...values },
|
2112
|
+
});
|
2113
|
+
}
|
2114
|
+
_names = {
|
2115
|
+
mount: new Set(),
|
2116
|
+
unMount: new Set(),
|
2117
|
+
array: new Set(),
|
2118
|
+
watch: new Set(),
|
2119
|
+
watchAll: false,
|
2120
|
+
focus: '',
|
2121
|
+
};
|
2122
|
+
!_state.mount && flushRootRender();
|
2123
|
+
_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
|
2124
|
+
_state.watch = !!props.shouldUnregister;
|
2125
|
+
_subjects.state.next({
|
2126
|
+
submitCount: keepStateOptions.keepSubmitCount
|
2127
|
+
? _formState.submitCount
|
2128
|
+
: 0,
|
2129
|
+
isDirty: keepStateOptions.keepDirty
|
2130
|
+
? _formState.isDirty
|
2131
|
+
: !!(keepStateOptions.keepDefaultValues &&
|
2132
|
+
!deepEqual(formValues, _defaultValues)),
|
2133
|
+
isSubmitted: keepStateOptions.keepIsSubmitted
|
2134
|
+
? _formState.isSubmitted
|
2135
|
+
: false,
|
2136
|
+
dirtyFields: keepStateOptions.keepDirtyValues
|
2137
|
+
? _formState.dirtyFields
|
2138
|
+
: keepStateOptions.keepDefaultValues && formValues
|
2139
|
+
? getDirtyFields(_defaultValues, formValues)
|
2140
|
+
: {},
|
2141
|
+
touchedFields: keepStateOptions.keepTouched
|
2142
|
+
? _formState.touchedFields
|
2143
|
+
: {},
|
2144
|
+
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
2145
|
+
isSubmitting: false,
|
2146
|
+
isSubmitSuccessful: false,
|
2147
|
+
});
|
2148
|
+
};
|
2149
|
+
const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
|
2150
|
+
? formValues(_formValues)
|
2151
|
+
: formValues, keepStateOptions);
|
2152
|
+
const setFocus = (name, options = {}) => {
|
2153
|
+
const field = get(_fields, name);
|
2154
|
+
const fieldReference = field && field._f;
|
2155
|
+
if (fieldReference) {
|
2156
|
+
const fieldRef = fieldReference.refs
|
2157
|
+
? fieldReference.refs[0]
|
2158
|
+
: fieldReference.ref;
|
2159
|
+
if (fieldRef.focus) {
|
2160
|
+
fieldRef.focus();
|
2161
|
+
options.shouldSelect && fieldRef.select();
|
2162
|
+
}
|
2163
|
+
}
|
2164
|
+
};
|
2165
|
+
const _updateFormState = (updatedFormState) => {
|
2166
|
+
_formState = {
|
2167
|
+
..._formState,
|
2168
|
+
...updatedFormState,
|
2169
|
+
};
|
2170
|
+
};
|
2171
|
+
if (isFunction(_options.defaultValues)) {
|
2172
|
+
_options.defaultValues().then((values) => {
|
2173
|
+
reset(values, _options.resetOptions);
|
2174
|
+
_subjects.state.next({
|
2175
|
+
isLoading: false,
|
2176
|
+
});
|
2177
|
+
});
|
2178
|
+
}
|
2179
|
+
return {
|
2180
|
+
control: {
|
2181
|
+
register,
|
2182
|
+
unregister,
|
2183
|
+
getFieldState,
|
2184
|
+
_executeSchema,
|
2185
|
+
_getWatch,
|
2186
|
+
_getDirty,
|
2187
|
+
_updateValid,
|
2188
|
+
_removeUnmounted,
|
2189
|
+
_updateFieldArray,
|
2190
|
+
_getFieldArray,
|
2191
|
+
_reset,
|
2192
|
+
_updateFormState,
|
2193
|
+
_subjects,
|
2194
|
+
_proxyFormState,
|
2195
|
+
get _fields() {
|
2196
|
+
return _fields;
|
2197
|
+
},
|
2198
|
+
get _formValues() {
|
2199
|
+
return _formValues;
|
2200
|
+
},
|
2201
|
+
get _state() {
|
2202
|
+
return _state;
|
2203
|
+
},
|
2204
|
+
set _state(value) {
|
2205
|
+
_state = value;
|
2206
|
+
},
|
2207
|
+
get _defaultValues() {
|
2208
|
+
return _defaultValues;
|
2209
|
+
},
|
2210
|
+
get _names() {
|
2211
|
+
return _names;
|
2212
|
+
},
|
2213
|
+
set _names(value) {
|
2214
|
+
_names = value;
|
2215
|
+
},
|
2216
|
+
get _formState() {
|
2217
|
+
return _formState;
|
2218
|
+
},
|
2219
|
+
set _formState(value) {
|
2220
|
+
_formState = value;
|
2221
|
+
},
|
2222
|
+
get _options() {
|
2223
|
+
return _options;
|
2224
|
+
},
|
2225
|
+
set _options(value) {
|
2226
|
+
_options = {
|
2227
|
+
..._options,
|
2228
|
+
...value,
|
2229
|
+
};
|
2230
|
+
},
|
2231
|
+
},
|
2232
|
+
trigger,
|
2233
|
+
register,
|
2234
|
+
handleSubmit,
|
2235
|
+
watch,
|
2236
|
+
setValue,
|
2237
|
+
getValues,
|
2238
|
+
reset,
|
2239
|
+
resetField,
|
2240
|
+
clearErrors,
|
2241
|
+
unregister,
|
2242
|
+
setError,
|
2243
|
+
setFocus,
|
2244
|
+
getFieldState,
|
2245
|
+
};
|
2236
2246
|
}
|
2237
2247
|
|
2238
|
-
/**
|
2239
|
-
* Custom hook to manage the entire form.
|
2240
|
-
*
|
2241
|
-
* @remarks
|
2242
|
-
* [API](https://react-hook-form.com/api/useform) • [Demo](https://codesandbox.io/s/react-hook-form-get-started-ts-5ksmm) • [Video](https://www.youtube.com/watch?v=RkXv4AXXC_4)
|
2243
|
-
*
|
2244
|
-
* @param props - form configuration and validation parameters.
|
2245
|
-
*
|
2246
|
-
* @returns methods - individual functions to manage the form state. {@link UseFormReturn}
|
2247
|
-
*
|
2248
|
-
* @example
|
2249
|
-
* ```tsx
|
2250
|
-
* function App() {
|
2251
|
-
* const { register, handleSubmit, watch, formState: { errors } } = useForm();
|
2252
|
-
* const onSubmit = data => console.log(data);
|
2253
|
-
*
|
2254
|
-
* console.log(watch("example"));
|
2255
|
-
*
|
2256
|
-
* return (
|
2257
|
-
* <form onSubmit={handleSubmit(onSubmit)}>
|
2258
|
-
* <input defaultValue="test" {...register("example")} />
|
2259
|
-
* <input {...register("exampleRequired", { required: true })} />
|
2260
|
-
* {errors.exampleRequired && <span>This field is required</span>}
|
2261
|
-
* <input type="submit" />
|
2262
|
-
* </form>
|
2263
|
-
* );
|
2264
|
-
* }
|
2265
|
-
* ```
|
2266
|
-
*/
|
2267
|
-
function useForm(props = {}) {
|
2268
|
-
const _formControl = React.useRef();
|
2269
|
-
const [formState, updateFormState] = React.useState({
|
2270
|
-
isDirty: false,
|
2271
|
-
isValidating: false,
|
2272
|
-
isLoading:
|
2273
|
-
isSubmitted: false,
|
2274
|
-
isSubmitting: false,
|
2275
|
-
isSubmitSuccessful: false,
|
2276
|
-
isValid: false,
|
2277
|
-
submitCount: 0,
|
2278
|
-
dirtyFields: {},
|
2279
|
-
touchedFields: {},
|
2280
|
-
errors: {},
|
2281
|
-
defaultValues: isFunction(props.defaultValues)
|
2282
|
-
? undefined
|
2283
|
-
: props.defaultValues,
|
2284
|
-
});
|
2285
|
-
if (!_formControl.current) {
|
2286
|
-
_formControl.current = {
|
2287
|
-
...createFormControl(props, () => updateFormState((formState) => ({ ...formState }))),
|
2288
|
-
formState,
|
2289
|
-
};
|
2290
|
-
}
|
2291
|
-
const control = _formControl.current.control;
|
2292
|
-
control._options = props;
|
2293
|
-
useSubscribe({
|
2294
|
-
subject: control._subjects.state,
|
2295
|
-
next: (value) => {
|
2296
|
-
if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
|
2297
|
-
updateFormState({ ...control._formState });
|
2298
|
-
}
|
2299
|
-
},
|
2300
|
-
});
|
2301
|
-
React.useEffect(() => {
|
2302
|
-
if (props.values && !deepEqual(props.values, control._defaultValues)) {
|
2303
|
-
control._reset(props.values, control._options.resetOptions);
|
2304
|
-
}
|
2305
|
-
}, [props.values, control]);
|
2306
|
-
React.useEffect(() => {
|
2307
|
-
if (!control._state.mount) {
|
2308
|
-
control._updateValid();
|
2309
|
-
control._state.mount = true;
|
2310
|
-
}
|
2311
|
-
if (control._state.watch) {
|
2312
|
-
control._state.watch = false;
|
2313
|
-
control._subjects.state.next({ ...control._formState });
|
2314
|
-
}
|
2315
|
-
control._removeUnmounted();
|
2316
|
-
});
|
2317
|
-
_formControl.current.formState = getProxyFormState(formState, control);
|
2318
|
-
return _formControl.current;
|
2248
|
+
/**
|
2249
|
+
* Custom hook to manage the entire form.
|
2250
|
+
*
|
2251
|
+
* @remarks
|
2252
|
+
* [API](https://react-hook-form.com/api/useform) • [Demo](https://codesandbox.io/s/react-hook-form-get-started-ts-5ksmm) • [Video](https://www.youtube.com/watch?v=RkXv4AXXC_4)
|
2253
|
+
*
|
2254
|
+
* @param props - form configuration and validation parameters.
|
2255
|
+
*
|
2256
|
+
* @returns methods - individual functions to manage the form state. {@link UseFormReturn}
|
2257
|
+
*
|
2258
|
+
* @example
|
2259
|
+
* ```tsx
|
2260
|
+
* function App() {
|
2261
|
+
* const { register, handleSubmit, watch, formState: { errors } } = useForm();
|
2262
|
+
* const onSubmit = data => console.log(data);
|
2263
|
+
*
|
2264
|
+
* console.log(watch("example"));
|
2265
|
+
*
|
2266
|
+
* return (
|
2267
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
2268
|
+
* <input defaultValue="test" {...register("example")} />
|
2269
|
+
* <input {...register("exampleRequired", { required: true })} />
|
2270
|
+
* {errors.exampleRequired && <span>This field is required</span>}
|
2271
|
+
* <input type="submit" />
|
2272
|
+
* </form>
|
2273
|
+
* );
|
2274
|
+
* }
|
2275
|
+
* ```
|
2276
|
+
*/
|
2277
|
+
function useForm(props = {}) {
|
2278
|
+
const _formControl = React.useRef();
|
2279
|
+
const [formState, updateFormState] = React.useState({
|
2280
|
+
isDirty: false,
|
2281
|
+
isValidating: false,
|
2282
|
+
isLoading: isFunction(props.defaultValues),
|
2283
|
+
isSubmitted: false,
|
2284
|
+
isSubmitting: false,
|
2285
|
+
isSubmitSuccessful: false,
|
2286
|
+
isValid: false,
|
2287
|
+
submitCount: 0,
|
2288
|
+
dirtyFields: {},
|
2289
|
+
touchedFields: {},
|
2290
|
+
errors: {},
|
2291
|
+
defaultValues: isFunction(props.defaultValues)
|
2292
|
+
? undefined
|
2293
|
+
: props.defaultValues,
|
2294
|
+
});
|
2295
|
+
if (!_formControl.current) {
|
2296
|
+
_formControl.current = {
|
2297
|
+
...createFormControl(props, () => updateFormState((formState) => ({ ...formState }))),
|
2298
|
+
formState,
|
2299
|
+
};
|
2300
|
+
}
|
2301
|
+
const control = _formControl.current.control;
|
2302
|
+
control._options = props;
|
2303
|
+
useSubscribe({
|
2304
|
+
subject: control._subjects.state,
|
2305
|
+
next: (value) => {
|
2306
|
+
if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
|
2307
|
+
updateFormState({ ...control._formState });
|
2308
|
+
}
|
2309
|
+
},
|
2310
|
+
});
|
2311
|
+
React.useEffect(() => {
|
2312
|
+
if (props.values && !deepEqual(props.values, control._defaultValues)) {
|
2313
|
+
control._reset(props.values, control._options.resetOptions);
|
2314
|
+
}
|
2315
|
+
}, [props.values, control]);
|
2316
|
+
React.useEffect(() => {
|
2317
|
+
if (!control._state.mount) {
|
2318
|
+
control._updateValid();
|
2319
|
+
control._state.mount = true;
|
2320
|
+
}
|
2321
|
+
if (control._state.watch) {
|
2322
|
+
control._state.watch = false;
|
2323
|
+
control._subjects.state.next({ ...control._formState });
|
2324
|
+
}
|
2325
|
+
control._removeUnmounted();
|
2326
|
+
});
|
2327
|
+
_formControl.current.formState = getProxyFormState(formState, control);
|
2328
|
+
return _formControl.current;
|
2319
2329
|
}
|
2320
2330
|
|
2321
2331
|
export { Controller, FormProvider, appendErrors, get, set, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch };
|