@bombillazo/rhf-plus 7.56.1-plus.0
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/LICENSE +21 -0
- package/README.md +174 -0
- package/dist/__typetest__/__fixtures__/index.d.ts +5 -0
- package/dist/__typetest__/__fixtures__/index.d.ts.map +1 -0
- package/dist/__typetest__/__fixtures__/pathString.d.ts +4 -0
- package/dist/__typetest__/__fixtures__/pathString.d.ts.map +1 -0
- package/dist/__typetest__/__fixtures__/traversable.d.ts +14 -0
- package/dist/__typetest__/__fixtures__/traversable.d.ts.map +1 -0
- package/dist/__typetest__/__fixtures__/tuple.d.ts +15 -0
- package/dist/__typetest__/__fixtures__/tuple.d.ts.map +1 -0
- package/dist/__typetest__/__fixtures__/type.d.ts +12 -0
- package/dist/__typetest__/__fixtures__/type.d.ts.map +1 -0
- package/dist/__typetest__/errors.test-d.d.ts +2 -0
- package/dist/__typetest__/errors.test-d.d.ts.map +1 -0
- package/dist/__typetest__/form.test-d.d.ts +11 -0
- package/dist/__typetest__/form.test-d.d.ts.map +1 -0
- package/dist/__typetest__/path/common.test-d.d.ts +2 -0
- package/dist/__typetest__/path/common.test-d.d.ts.map +1 -0
- package/dist/__typetest__/path/eager.test-d.d.ts +2 -0
- package/dist/__typetest__/path/eager.test-d.d.ts.map +1 -0
- package/dist/__typetest__/use-form-context.test-d.d.ts +2 -0
- package/dist/__typetest__/use-form-context.test-d.d.ts.map +1 -0
- package/dist/__typetest__/util.test-d.d.ts +2 -0
- package/dist/__typetest__/util.test-d.d.ts.map +1 -0
- package/dist/constants.d.ts +22 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/controller.d.ts +46 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/form.d.ts +27 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.mjs +2764 -0
- package/dist/index.esm.mjs.map +1 -0
- package/dist/index.react-server.d.ts +3 -0
- package/dist/index.react-server.d.ts.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/logic/appendErrors.d.ts +4 -0
- package/dist/logic/appendErrors.d.ts.map +1 -0
- package/dist/logic/createFormControl.d.ts +5 -0
- package/dist/logic/createFormControl.d.ts.map +1 -0
- package/dist/logic/generateId.d.ts +3 -0
- package/dist/logic/generateId.d.ts.map +1 -0
- package/dist/logic/generateWatchOutput.d.ts +4 -0
- package/dist/logic/generateWatchOutput.d.ts.map +1 -0
- package/dist/logic/getCheckboxValue.d.ts +7 -0
- package/dist/logic/getCheckboxValue.d.ts.map +1 -0
- package/dist/logic/getDirtyFields.d.ts +3 -0
- package/dist/logic/getDirtyFields.d.ts.map +1 -0
- package/dist/logic/getEventValue.d.ts +3 -0
- package/dist/logic/getEventValue.d.ts.map +1 -0
- package/dist/logic/getFieldValue.d.ts +3 -0
- package/dist/logic/getFieldValue.d.ts.map +1 -0
- package/dist/logic/getFieldValueAs.d.ts +4 -0
- package/dist/logic/getFieldValueAs.d.ts.map +1 -0
- package/dist/logic/getFocusFieldName.d.ts +4 -0
- package/dist/logic/getFocusFieldName.d.ts.map +1 -0
- package/dist/logic/getNodeParentName.d.ts +3 -0
- package/dist/logic/getNodeParentName.d.ts.map +1 -0
- package/dist/logic/getProxyFormState.d.ts +4 -0
- package/dist/logic/getProxyFormState.d.ts.map +1 -0
- package/dist/logic/getRadioValue.d.ts +7 -0
- package/dist/logic/getRadioValue.d.ts.map +1 -0
- package/dist/logic/getResolverOptions.d.ts +14 -0
- package/dist/logic/getResolverOptions.d.ts.map +1 -0
- package/dist/logic/getRuleValue.d.ts +4 -0
- package/dist/logic/getRuleValue.d.ts.map +1 -0
- package/dist/logic/getValidateError.d.ts +3 -0
- package/dist/logic/getValidateError.d.ts.map +1 -0
- package/dist/logic/getValidationModes.d.ts +4 -0
- package/dist/logic/getValidationModes.d.ts.map +1 -0
- package/dist/logic/getValueAndMessage.d.ts +7 -0
- package/dist/logic/getValueAndMessage.d.ts.map +1 -0
- package/dist/logic/hasPromiseValidation.d.ts +4 -0
- package/dist/logic/hasPromiseValidation.d.ts.map +1 -0
- package/dist/logic/hasValidation.d.ts +4 -0
- package/dist/logic/hasValidation.d.ts.map +1 -0
- package/dist/logic/index.d.ts +4 -0
- package/dist/logic/index.d.ts.map +1 -0
- package/dist/logic/isNameInFieldArray.d.ts +4 -0
- package/dist/logic/isNameInFieldArray.d.ts.map +1 -0
- package/dist/logic/isWatched.d.ts +4 -0
- package/dist/logic/isWatched.d.ts.map +1 -0
- package/dist/logic/iterateFieldsByAction.d.ts +4 -0
- package/dist/logic/iterateFieldsByAction.d.ts.map +1 -0
- package/dist/logic/schemaErrorLookup.d.ts +6 -0
- package/dist/logic/schemaErrorLookup.d.ts.map +1 -0
- package/dist/logic/shouldRenderFormState.d.ts +7 -0
- package/dist/logic/shouldRenderFormState.d.ts.map +1 -0
- package/dist/logic/shouldSubscribeByName.d.ts +3 -0
- package/dist/logic/shouldSubscribeByName.d.ts.map +1 -0
- package/dist/logic/skipValidation.d.ts +7 -0
- package/dist/logic/skipValidation.d.ts.map +1 -0
- package/dist/logic/unsetEmptyArray.d.ts +3 -0
- package/dist/logic/unsetEmptyArray.d.ts.map +1 -0
- package/dist/logic/updateFieldArrayRootError.d.ts +4 -0
- package/dist/logic/updateFieldArrayRootError.d.ts.map +1 -0
- package/dist/logic/validateField.d.ts +4 -0
- package/dist/logic/validateField.d.ts.map +1 -0
- package/dist/react-server.esm.mjs +1816 -0
- package/dist/react-server.esm.mjs.map +1 -0
- package/dist/types/controller.d.ts +60 -0
- package/dist/types/controller.d.ts.map +1 -0
- package/dist/types/errors.d.ts +36 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/events.d.ts +2 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/fieldArray.d.ts +195 -0
- package/dist/types/fieldArray.d.ts.map +1 -0
- package/dist/types/fields.d.ts +31 -0
- package/dist/types/fields.d.ts.map +1 -0
- package/dist/types/form.d.ts +715 -0
- package/dist/types/form.d.ts.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/path/common.d.ts +316 -0
- package/dist/types/path/common.d.ts.map +1 -0
- package/dist/types/path/eager.d.ts +122 -0
- package/dist/types/path/eager.d.ts.map +1 -0
- package/dist/types/path/index.d.ts +4 -0
- package/dist/types/path/index.d.ts.map +1 -0
- package/dist/types/resolvers.d.ts +20 -0
- package/dist/types/resolvers.d.ts.map +1 -0
- package/dist/types/utils.d.ts +72 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/validator.d.ts +43 -0
- package/dist/types/validator.d.ts.map +1 -0
- package/dist/useController.d.ts +27 -0
- package/dist/useController.d.ts.map +1 -0
- package/dist/useDeepEqualEffect.d.ts +3 -0
- package/dist/useDeepEqualEffect.d.ts.map +1 -0
- package/dist/useFieldArray.d.ts +40 -0
- package/dist/useFieldArray.d.ts.map +1 -0
- package/dist/useForm.d.ts +32 -0
- package/dist/useForm.d.ts.map +1 -0
- package/dist/useFormContext.d.ts +65 -0
- package/dist/useFormContext.d.ts.map +1 -0
- package/dist/useFormState.d.ts +33 -0
- package/dist/useFormState.d.ts.map +1 -0
- package/dist/useWatch.d.ts +100 -0
- package/dist/useWatch.d.ts.map +1 -0
- package/dist/utils/append.d.ts +3 -0
- package/dist/utils/append.d.ts.map +1 -0
- package/dist/utils/cloneObject.d.ts +2 -0
- package/dist/utils/cloneObject.d.ts.map +1 -0
- package/dist/utils/compact.d.ts +3 -0
- package/dist/utils/compact.d.ts.map +1 -0
- package/dist/utils/convertToArrayPayload.d.ts +3 -0
- package/dist/utils/convertToArrayPayload.d.ts.map +1 -0
- package/dist/utils/createId.d.ts +3 -0
- package/dist/utils/createId.d.ts.map +1 -0
- package/dist/utils/createSubject.d.ts +15 -0
- package/dist/utils/createSubject.d.ts.map +1 -0
- package/dist/utils/deepEqual.d.ts +2 -0
- package/dist/utils/deepEqual.d.ts.map +1 -0
- package/dist/utils/deepMerge.d.ts +2 -0
- package/dist/utils/deepMerge.d.ts.map +1 -0
- package/dist/utils/fillEmptyArray.d.ts +3 -0
- package/dist/utils/fillEmptyArray.d.ts.map +1 -0
- package/dist/utils/flatten.d.ts +3 -0
- package/dist/utils/flatten.d.ts.map +1 -0
- package/dist/utils/get.d.ts +3 -0
- package/dist/utils/get.d.ts.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/insert.d.ts +3 -0
- package/dist/utils/insert.d.ts.map +1 -0
- package/dist/utils/isBoolean.d.ts +3 -0
- package/dist/utils/isBoolean.d.ts.map +1 -0
- package/dist/utils/isCheckBoxInput.d.ts +4 -0
- package/dist/utils/isCheckBoxInput.d.ts.map +1 -0
- package/dist/utils/isDateObject.d.ts +3 -0
- package/dist/utils/isDateObject.d.ts.map +1 -0
- package/dist/utils/isEmptyObject.d.ts +4 -0
- package/dist/utils/isEmptyObject.d.ts.map +1 -0
- package/dist/utils/isFileInput.d.ts +4 -0
- package/dist/utils/isFileInput.d.ts.map +1 -0
- package/dist/utils/isFunction.d.ts +3 -0
- package/dist/utils/isFunction.d.ts.map +1 -0
- package/dist/utils/isHTMLElement.d.ts +3 -0
- package/dist/utils/isHTMLElement.d.ts.map +1 -0
- package/dist/utils/isKey.d.ts +3 -0
- package/dist/utils/isKey.d.ts.map +1 -0
- package/dist/utils/isMessage.d.ts +4 -0
- package/dist/utils/isMessage.d.ts.map +1 -0
- package/dist/utils/isMultipleSelect.d.ts +4 -0
- package/dist/utils/isMultipleSelect.d.ts.map +1 -0
- package/dist/utils/isNullOrUndefined.d.ts +3 -0
- package/dist/utils/isNullOrUndefined.d.ts.map +1 -0
- package/dist/utils/isObject.d.ts +4 -0
- package/dist/utils/isObject.d.ts.map +1 -0
- package/dist/utils/isPlainObject.d.ts +3 -0
- package/dist/utils/isPlainObject.d.ts.map +1 -0
- package/dist/utils/isPrimitive.d.ts +4 -0
- package/dist/utils/isPrimitive.d.ts.map +1 -0
- package/dist/utils/isRadioInput.d.ts +4 -0
- package/dist/utils/isRadioInput.d.ts.map +1 -0
- package/dist/utils/isRadioOrCheckbox.d.ts +4 -0
- package/dist/utils/isRadioOrCheckbox.d.ts.map +1 -0
- package/dist/utils/isRegex.d.ts +3 -0
- package/dist/utils/isRegex.d.ts.map +1 -0
- package/dist/utils/isString.d.ts +3 -0
- package/dist/utils/isString.d.ts.map +1 -0
- package/dist/utils/isUndefined.d.ts +3 -0
- package/dist/utils/isUndefined.d.ts.map +1 -0
- package/dist/utils/isWeb.d.ts +3 -0
- package/dist/utils/isWeb.d.ts.map +1 -0
- package/dist/utils/live.d.ts +4 -0
- package/dist/utils/live.d.ts.map +1 -0
- package/dist/utils/move.d.ts +3 -0
- package/dist/utils/move.d.ts.map +1 -0
- package/dist/utils/noop.d.ts +2 -0
- package/dist/utils/noop.d.ts.map +1 -0
- package/dist/utils/objectHasFunction.d.ts +3 -0
- package/dist/utils/objectHasFunction.d.ts.map +1 -0
- package/dist/utils/omit.d.ts +3 -0
- package/dist/utils/omit.d.ts.map +1 -0
- package/dist/utils/prepend.d.ts +3 -0
- package/dist/utils/prepend.d.ts.map +1 -0
- package/dist/utils/remove.d.ts +3 -0
- package/dist/utils/remove.d.ts.map +1 -0
- package/dist/utils/set.d.ts +4 -0
- package/dist/utils/set.d.ts.map +1 -0
- package/dist/utils/sleep.d.ts +3 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/stringToPath.d.ts +3 -0
- package/dist/utils/stringToPath.d.ts.map +1 -0
- package/dist/utils/submit.d.ts +3 -0
- package/dist/utils/submit.d.ts.map +1 -0
- package/dist/utils/swap.d.ts +3 -0
- package/dist/utils/swap.d.ts.map +1 -0
- package/dist/utils/unset.d.ts +2 -0
- package/dist/utils/unset.d.ts.map +1 -0
- package/dist/utils/update.d.ts +3 -0
- package/dist/utils/update.d.ts.map +1 -0
- package/dist/utils/uuid.d.ts +3 -0
- package/dist/utils/uuid.d.ts.map +1 -0
- package/package.json +149 -0
|
@@ -0,0 +1,1816 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
|
4
|
+
? {
|
|
5
|
+
...errors[name],
|
|
6
|
+
types: {
|
|
7
|
+
...(errors[name] && errors[name].types ? errors[name].types : {}),
|
|
8
|
+
[type]: message || true,
|
|
9
|
+
},
|
|
10
|
+
}
|
|
11
|
+
: {};
|
|
12
|
+
|
|
13
|
+
const EVENTS = {
|
|
14
|
+
BLUR: 'blur',
|
|
15
|
+
FOCUS_OUT: 'focusout'};
|
|
16
|
+
const VALIDATION_MODE = {
|
|
17
|
+
onBlur: 'onBlur',
|
|
18
|
+
onChange: 'onChange',
|
|
19
|
+
onSubmit: 'onSubmit',
|
|
20
|
+
onTouched: 'onTouched',
|
|
21
|
+
all: 'all',
|
|
22
|
+
};
|
|
23
|
+
const INPUT_VALIDATION_RULES = {
|
|
24
|
+
max: 'max',
|
|
25
|
+
min: 'min',
|
|
26
|
+
maxLength: 'maxLength',
|
|
27
|
+
minLength: 'minLength',
|
|
28
|
+
pattern: 'pattern',
|
|
29
|
+
required: 'required',
|
|
30
|
+
validate: 'validate',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
var isDateObject = (value) => value instanceof Date;
|
|
34
|
+
|
|
35
|
+
var isNullOrUndefined = (value) => value == null;
|
|
36
|
+
|
|
37
|
+
const isObjectType = (value) => typeof value === 'object';
|
|
38
|
+
var isObject = (value) => !isNullOrUndefined(value) &&
|
|
39
|
+
!Array.isArray(value) &&
|
|
40
|
+
isObjectType(value) &&
|
|
41
|
+
!isDateObject(value);
|
|
42
|
+
|
|
43
|
+
var isPlainObject = (tempObject) => {
|
|
44
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
45
|
+
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
var isWeb = typeof window !== 'undefined' &&
|
|
49
|
+
typeof window.HTMLElement !== 'undefined' &&
|
|
50
|
+
typeof document !== 'undefined';
|
|
51
|
+
|
|
52
|
+
function cloneObject(data) {
|
|
53
|
+
let copy;
|
|
54
|
+
const isArray = Array.isArray(data);
|
|
55
|
+
const isFileListInstance = typeof FileList !== 'undefined' ? data instanceof FileList : false;
|
|
56
|
+
if (data instanceof Date) {
|
|
57
|
+
copy = new Date(data);
|
|
58
|
+
}
|
|
59
|
+
else if (data instanceof Set) {
|
|
60
|
+
copy = new Set(data);
|
|
61
|
+
}
|
|
62
|
+
else if (!(isWeb && (data instanceof Blob || isFileListInstance)) &&
|
|
63
|
+
(isArray || isObject(data))) {
|
|
64
|
+
copy = isArray ? [] : {};
|
|
65
|
+
if (!isArray && !isPlainObject(data)) {
|
|
66
|
+
copy = data;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
for (const key in data) {
|
|
70
|
+
if (data.hasOwnProperty(key)) {
|
|
71
|
+
copy[key] = cloneObject(data[key]);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return data;
|
|
78
|
+
}
|
|
79
|
+
return copy;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
|
83
|
+
|
|
84
|
+
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
|
85
|
+
|
|
86
|
+
var uuid = () => {
|
|
87
|
+
if (typeof window !== 'undefined' &&
|
|
88
|
+
typeof window.crypto.randomUUID === 'function') {
|
|
89
|
+
return window.crypto.randomUUID();
|
|
90
|
+
}
|
|
91
|
+
return crypto.randomBytes(16).toString('hex');
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
var createId = (id) => id || `form-${uuid()}`;
|
|
95
|
+
|
|
96
|
+
var createSubject = () => {
|
|
97
|
+
let _observers = [];
|
|
98
|
+
const next = (value) => {
|
|
99
|
+
for (const observer of _observers) {
|
|
100
|
+
observer.next && observer.next(value);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const subscribe = (observer) => {
|
|
104
|
+
_observers.push(observer);
|
|
105
|
+
return {
|
|
106
|
+
unsubscribe: () => {
|
|
107
|
+
_observers = _observers.filter((o) => o !== observer);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
const unsubscribe = () => {
|
|
112
|
+
_observers = [];
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
get observers() {
|
|
116
|
+
return _observers;
|
|
117
|
+
},
|
|
118
|
+
next,
|
|
119
|
+
subscribe,
|
|
120
|
+
unsubscribe,
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
125
|
+
|
|
126
|
+
function deepEqual(object1, object2) {
|
|
127
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
128
|
+
return object1 === object2;
|
|
129
|
+
}
|
|
130
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
|
131
|
+
return object1.getTime() === object2.getTime();
|
|
132
|
+
}
|
|
133
|
+
const keys1 = Object.keys(object1);
|
|
134
|
+
const keys2 = Object.keys(object2);
|
|
135
|
+
if (keys1.length !== keys2.length) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
for (const key of keys1) {
|
|
139
|
+
const val1 = object1[key];
|
|
140
|
+
if (!keys2.includes(key)) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
if (key !== 'ref') {
|
|
144
|
+
const val2 = object2[key];
|
|
145
|
+
if ((isDateObject(val1) && isDateObject(val2)) ||
|
|
146
|
+
(isObject(val1) && isObject(val2)) ||
|
|
147
|
+
(Array.isArray(val1) && Array.isArray(val2))
|
|
148
|
+
? !deepEqual(val1, val2)
|
|
149
|
+
: val1 !== val2) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
var isUndefined = (val) => val === undefined;
|
|
158
|
+
|
|
159
|
+
var get = (object, path, defaultValue) => {
|
|
160
|
+
if (!path || !isObject(object)) {
|
|
161
|
+
return defaultValue;
|
|
162
|
+
}
|
|
163
|
+
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], object);
|
|
164
|
+
return isUndefined(result) || result === object
|
|
165
|
+
? isUndefined(object[path])
|
|
166
|
+
? defaultValue
|
|
167
|
+
: object[path]
|
|
168
|
+
: result;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
var isBoolean = (value) => typeof value === 'boolean';
|
|
172
|
+
|
|
173
|
+
var isCheckBoxInput = (element) => element.type === 'checkbox';
|
|
174
|
+
|
|
175
|
+
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
|
176
|
+
|
|
177
|
+
var isFileInput = (element) => element.type === 'file';
|
|
178
|
+
|
|
179
|
+
var isFunction = (value) => typeof value === 'function';
|
|
180
|
+
|
|
181
|
+
var isHTMLElement = (value) => {
|
|
182
|
+
if (!isWeb) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
const owner = value ? value.ownerDocument : 0;
|
|
186
|
+
return (value instanceof
|
|
187
|
+
(owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
|
191
|
+
|
|
192
|
+
var isRadioInput = (element) => element.type === 'radio';
|
|
193
|
+
|
|
194
|
+
var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
195
|
+
|
|
196
|
+
var isString = (value) => typeof value === 'string';
|
|
197
|
+
|
|
198
|
+
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
|
|
199
|
+
|
|
200
|
+
var isKey = (value) => /^\w*$/.test(value);
|
|
201
|
+
|
|
202
|
+
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
|
203
|
+
|
|
204
|
+
var set = (object, path, value) => {
|
|
205
|
+
let index = -1;
|
|
206
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
207
|
+
const length = tempPath.length;
|
|
208
|
+
const lastIndex = length - 1;
|
|
209
|
+
while (++index < length) {
|
|
210
|
+
const key = tempPath[index];
|
|
211
|
+
let newValue = value;
|
|
212
|
+
if (index !== lastIndex) {
|
|
213
|
+
const objValue = object[key];
|
|
214
|
+
newValue =
|
|
215
|
+
isObject(objValue) || Array.isArray(objValue)
|
|
216
|
+
? objValue
|
|
217
|
+
: !isNaN(+tempPath[index + 1])
|
|
218
|
+
? []
|
|
219
|
+
: {};
|
|
220
|
+
}
|
|
221
|
+
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
object[key] = newValue;
|
|
225
|
+
object = object[key];
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
var submitForm = (id) => {
|
|
230
|
+
var _a;
|
|
231
|
+
try {
|
|
232
|
+
(_a = document === null || document === void 0 ? void 0 : document.getElementById(id)) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new Event('submit', {
|
|
233
|
+
bubbles: true,
|
|
234
|
+
cancelable: true,
|
|
235
|
+
}));
|
|
236
|
+
}
|
|
237
|
+
catch (_b) {
|
|
238
|
+
// noop
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
function baseGet(object, updatePath) {
|
|
243
|
+
const length = updatePath.slice(0, -1).length;
|
|
244
|
+
let index = 0;
|
|
245
|
+
while (index < length) {
|
|
246
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
247
|
+
}
|
|
248
|
+
return object;
|
|
249
|
+
}
|
|
250
|
+
function isEmptyArray(obj) {
|
|
251
|
+
for (const key in obj) {
|
|
252
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
function unset(object, path) {
|
|
259
|
+
const paths = Array.isArray(path)
|
|
260
|
+
? path
|
|
261
|
+
: isKey(path)
|
|
262
|
+
? [path]
|
|
263
|
+
: stringToPath(path);
|
|
264
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
265
|
+
const index = paths.length - 1;
|
|
266
|
+
const key = paths[index];
|
|
267
|
+
if (childObject) {
|
|
268
|
+
delete childObject[key];
|
|
269
|
+
}
|
|
270
|
+
if (index !== 0 &&
|
|
271
|
+
((isObject(childObject) && isEmptyObject(childObject)) ||
|
|
272
|
+
(Array.isArray(childObject) && isEmptyArray(childObject)))) {
|
|
273
|
+
unset(object, paths.slice(0, -1));
|
|
274
|
+
}
|
|
275
|
+
return object;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
|
279
|
+
if (isString(names)) {
|
|
280
|
+
isGlobal && _names.watch.add(names);
|
|
281
|
+
return get(formValues, names, defaultValue);
|
|
282
|
+
}
|
|
283
|
+
if (Array.isArray(names)) {
|
|
284
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
|
285
|
+
}
|
|
286
|
+
isGlobal && (_names.watchAll = true);
|
|
287
|
+
return formValues;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
var objectHasFunction = (data) => {
|
|
291
|
+
for (const key in data) {
|
|
292
|
+
if (isFunction(data[key])) {
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return false;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
function markFieldsDirty(data, fields = {}) {
|
|
300
|
+
const isParentNodeArray = Array.isArray(data);
|
|
301
|
+
if (isObject(data) || isParentNodeArray) {
|
|
302
|
+
for (const key in data) {
|
|
303
|
+
if (Array.isArray(data[key]) ||
|
|
304
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
|
305
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
|
306
|
+
markFieldsDirty(data[key], fields[key]);
|
|
307
|
+
}
|
|
308
|
+
else if (!isNullOrUndefined(data[key])) {
|
|
309
|
+
fields[key] = true;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return fields;
|
|
314
|
+
}
|
|
315
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
|
316
|
+
const isParentNodeArray = Array.isArray(data);
|
|
317
|
+
if (isObject(data) || isParentNodeArray) {
|
|
318
|
+
for (const key in data) {
|
|
319
|
+
if (Array.isArray(data[key]) ||
|
|
320
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
|
321
|
+
if (isUndefined(formValues) ||
|
|
322
|
+
isPrimitive(dirtyFieldsFromValues[key])) {
|
|
323
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
|
324
|
+
? markFieldsDirty(data[key], [])
|
|
325
|
+
: { ...markFieldsDirty(data[key]) };
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return dirtyFieldsFromValues;
|
|
337
|
+
}
|
|
338
|
+
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
|
339
|
+
|
|
340
|
+
var getEventValue = (event) => isObject(event) && event.target
|
|
341
|
+
? isCheckBoxInput(event.target)
|
|
342
|
+
? event.target.checked
|
|
343
|
+
: event.target.value
|
|
344
|
+
: event;
|
|
345
|
+
|
|
346
|
+
const defaultResult = {
|
|
347
|
+
value: false,
|
|
348
|
+
isValid: false,
|
|
349
|
+
};
|
|
350
|
+
const validResult = { value: true, isValid: true };
|
|
351
|
+
var getCheckboxValue = (options) => {
|
|
352
|
+
if (Array.isArray(options)) {
|
|
353
|
+
if (options.length > 1) {
|
|
354
|
+
const values = options
|
|
355
|
+
.filter((option) => option && option.checked && !option.disabled)
|
|
356
|
+
.map((option) => option.value);
|
|
357
|
+
return { value: values, isValid: !!values.length };
|
|
358
|
+
}
|
|
359
|
+
return options[0].checked && !options[0].disabled
|
|
360
|
+
? // @ts-expect-error expected to work in the browser
|
|
361
|
+
options[0].attributes && !isUndefined(options[0].attributes.value)
|
|
362
|
+
? isUndefined(options[0].value) || options[0].value === ''
|
|
363
|
+
? validResult
|
|
364
|
+
: { value: options[0].value, isValid: true }
|
|
365
|
+
: validResult
|
|
366
|
+
: defaultResult;
|
|
367
|
+
}
|
|
368
|
+
return defaultResult;
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
|
|
372
|
+
? value
|
|
373
|
+
: valueAsNumber
|
|
374
|
+
? value === ''
|
|
375
|
+
? NaN
|
|
376
|
+
: value
|
|
377
|
+
? +value
|
|
378
|
+
: value
|
|
379
|
+
: valueAsDate && isString(value)
|
|
380
|
+
? new Date(value)
|
|
381
|
+
: setValueAs
|
|
382
|
+
? setValueAs(value)
|
|
383
|
+
: value;
|
|
384
|
+
|
|
385
|
+
const defaultReturn = {
|
|
386
|
+
isValid: false,
|
|
387
|
+
value: null,
|
|
388
|
+
};
|
|
389
|
+
var getRadioValue = (options) => Array.isArray(options)
|
|
390
|
+
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
|
391
|
+
? {
|
|
392
|
+
isValid: true,
|
|
393
|
+
value: option.value,
|
|
394
|
+
}
|
|
395
|
+
: previous, defaultReturn)
|
|
396
|
+
: defaultReturn;
|
|
397
|
+
|
|
398
|
+
function getFieldValue(_f) {
|
|
399
|
+
const ref = _f.ref;
|
|
400
|
+
if (isFileInput(ref)) {
|
|
401
|
+
return ref.files;
|
|
402
|
+
}
|
|
403
|
+
if (isRadioInput(ref)) {
|
|
404
|
+
return getRadioValue(_f.refs).value;
|
|
405
|
+
}
|
|
406
|
+
if (isMultipleSelect(ref)) {
|
|
407
|
+
return [...ref.selectedOptions].map(({ value }) => value);
|
|
408
|
+
}
|
|
409
|
+
if (isCheckBoxInput(ref)) {
|
|
410
|
+
return getCheckboxValue(_f.refs).value;
|
|
411
|
+
}
|
|
412
|
+
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
|
|
416
|
+
const fields = {};
|
|
417
|
+
for (const name of fieldsNames) {
|
|
418
|
+
const field = get(_fields, name);
|
|
419
|
+
field && set(fields, name, field._f);
|
|
420
|
+
}
|
|
421
|
+
return {
|
|
422
|
+
criteriaMode,
|
|
423
|
+
names: [...fieldsNames],
|
|
424
|
+
fields,
|
|
425
|
+
shouldUseNativeValidation,
|
|
426
|
+
};
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
var isRegex = (value) => value instanceof RegExp;
|
|
430
|
+
|
|
431
|
+
var getRuleValue = (rule) => isUndefined(rule)
|
|
432
|
+
? rule
|
|
433
|
+
: isRegex(rule)
|
|
434
|
+
? rule.source
|
|
435
|
+
: isObject(rule)
|
|
436
|
+
? isRegex(rule.value)
|
|
437
|
+
? rule.value.source
|
|
438
|
+
: rule.value
|
|
439
|
+
: rule;
|
|
440
|
+
|
|
441
|
+
var getValidationModes = (mode) => ({
|
|
442
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
443
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
444
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
445
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
|
446
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
const ASYNC_FUNCTION = 'AsyncFunction';
|
|
450
|
+
var hasPromiseValidation = (fieldReference) => !!fieldReference &&
|
|
451
|
+
!!fieldReference.validate &&
|
|
452
|
+
!!((isFunction(fieldReference.validate) &&
|
|
453
|
+
fieldReference.validate.constructor.name === ASYNC_FUNCTION) ||
|
|
454
|
+
(isObject(fieldReference.validate) &&
|
|
455
|
+
Object.values(fieldReference.validate).find((validateFunction) => validateFunction.constructor.name === ASYNC_FUNCTION)));
|
|
456
|
+
|
|
457
|
+
var hasValidation = (options) => options.mount &&
|
|
458
|
+
(options.required ||
|
|
459
|
+
options.min ||
|
|
460
|
+
options.max ||
|
|
461
|
+
options.maxLength ||
|
|
462
|
+
options.minLength ||
|
|
463
|
+
options.pattern ||
|
|
464
|
+
options.validate);
|
|
465
|
+
|
|
466
|
+
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
467
|
+
|
|
468
|
+
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
|
469
|
+
|
|
470
|
+
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
|
471
|
+
(_names.watchAll ||
|
|
472
|
+
_names.watch.has(name) ||
|
|
473
|
+
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
|
474
|
+
/^\.\w+/.test(name.slice(watchName.length))));
|
|
475
|
+
|
|
476
|
+
const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
|
|
477
|
+
for (const key of fieldsNames || Object.keys(fields)) {
|
|
478
|
+
const field = get(fields, key);
|
|
479
|
+
if (field) {
|
|
480
|
+
const { _f, ...currentField } = field;
|
|
481
|
+
if (_f) {
|
|
482
|
+
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
else if (isObject(currentField)) {
|
|
495
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
function schemaErrorLookup(errors, _fields, name) {
|
|
505
|
+
const error = get(errors, name);
|
|
506
|
+
if (error || isKey(name)) {
|
|
507
|
+
return {
|
|
508
|
+
error,
|
|
509
|
+
name,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
const names = name.split('.');
|
|
513
|
+
while (names.length) {
|
|
514
|
+
const fieldName = names.join('.');
|
|
515
|
+
const field = get(_fields, fieldName);
|
|
516
|
+
const foundError = get(errors, fieldName);
|
|
517
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
|
518
|
+
return { name };
|
|
519
|
+
}
|
|
520
|
+
if (foundError && foundError.type) {
|
|
521
|
+
return {
|
|
522
|
+
name: fieldName,
|
|
523
|
+
error: foundError,
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
names.pop();
|
|
527
|
+
}
|
|
528
|
+
return {
|
|
529
|
+
name,
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
|
534
|
+
updateFormState(formStateData);
|
|
535
|
+
const { name, ...formState } = formStateData;
|
|
536
|
+
return (isEmptyObject(formState) ||
|
|
537
|
+
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
|
538
|
+
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
|
539
|
+
(!isRoot || VALIDATION_MODE.all)));
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
var shouldSubscribeByName = (name, signalName, exact) => !name ||
|
|
543
|
+
!signalName ||
|
|
544
|
+
name === signalName ||
|
|
545
|
+
convertToArrayPayload(name).some((currentName) => currentName &&
|
|
546
|
+
(exact
|
|
547
|
+
? currentName === signalName
|
|
548
|
+
: currentName.startsWith(signalName) ||
|
|
549
|
+
signalName.startsWith(currentName)));
|
|
550
|
+
|
|
551
|
+
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
|
552
|
+
if (mode.isOnAll) {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
else if (!isSubmitted && mode.isOnTouch) {
|
|
556
|
+
return !(isTouched || isBlurEvent);
|
|
557
|
+
}
|
|
558
|
+
else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
|
559
|
+
return !isBlurEvent;
|
|
560
|
+
}
|
|
561
|
+
else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
|
562
|
+
return isBlurEvent;
|
|
563
|
+
}
|
|
564
|
+
return true;
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
|
|
568
|
+
|
|
569
|
+
var updateFieldArrayRootError = (errors, error, name) => {
|
|
570
|
+
const fieldArrayErrors = convertToArrayPayload(get(errors, name));
|
|
571
|
+
set(fieldArrayErrors, 'root', error[name]);
|
|
572
|
+
set(errors, name, fieldArrayErrors);
|
|
573
|
+
return errors;
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
var isMessage = (value) => isString(value);
|
|
577
|
+
|
|
578
|
+
function getValidateError(result, ref, type = 'validate') {
|
|
579
|
+
if (isMessage(result) ||
|
|
580
|
+
(Array.isArray(result) && result.every(isMessage)) ||
|
|
581
|
+
(isBoolean(result) && !result)) {
|
|
582
|
+
return {
|
|
583
|
+
type,
|
|
584
|
+
message: isMessage(result) ? result : '',
|
|
585
|
+
ref,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData)
|
|
591
|
+
? validationData
|
|
592
|
+
: {
|
|
593
|
+
value: validationData,
|
|
594
|
+
message: '',
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
var validateField = async (field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
|
598
|
+
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, } = field._f;
|
|
599
|
+
const inputValue = get(formValues, name);
|
|
600
|
+
if (!mount || disabledFieldNames.has(name)) {
|
|
601
|
+
return {};
|
|
602
|
+
}
|
|
603
|
+
const inputRef = refs ? refs[0] : ref;
|
|
604
|
+
const setCustomValidity = (message) => {
|
|
605
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
606
|
+
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
|
607
|
+
inputRef.reportValidity();
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
const error = {};
|
|
611
|
+
const isRadio = isRadioInput(ref);
|
|
612
|
+
const isCheckBox = isCheckBoxInput(ref);
|
|
613
|
+
const isRadioOrCheckbox = isRadio || isCheckBox;
|
|
614
|
+
const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
|
|
615
|
+
isUndefined(ref.value) &&
|
|
616
|
+
isUndefined(inputValue)) ||
|
|
617
|
+
(isHTMLElement(ref) && ref.value === '') ||
|
|
618
|
+
inputValue === '' ||
|
|
619
|
+
(Array.isArray(inputValue) && !inputValue.length);
|
|
620
|
+
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
621
|
+
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
|
622
|
+
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
623
|
+
error[name] = {
|
|
624
|
+
type: exceedMax ? maxType : minType,
|
|
625
|
+
message,
|
|
626
|
+
ref,
|
|
627
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
|
628
|
+
};
|
|
629
|
+
};
|
|
630
|
+
if (isFieldArray
|
|
631
|
+
? !Array.isArray(inputValue) || !inputValue.length
|
|
632
|
+
: required &&
|
|
633
|
+
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
|
634
|
+
(isBoolean(inputValue) && !inputValue) ||
|
|
635
|
+
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
|
636
|
+
(isRadio && !getRadioValue(refs).isValid))) {
|
|
637
|
+
const { value, message } = isMessage(required)
|
|
638
|
+
? { value: !!required, message: required }
|
|
639
|
+
: getValueAndMessage(required);
|
|
640
|
+
if (value) {
|
|
641
|
+
error[name] = {
|
|
642
|
+
type: INPUT_VALIDATION_RULES.required,
|
|
643
|
+
message,
|
|
644
|
+
ref: inputRef,
|
|
645
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
|
646
|
+
};
|
|
647
|
+
if (!validateAllFieldCriteria) {
|
|
648
|
+
setCustomValidity(message);
|
|
649
|
+
return error;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
654
|
+
let exceedMax;
|
|
655
|
+
let exceedMin;
|
|
656
|
+
const maxOutput = getValueAndMessage(max);
|
|
657
|
+
const minOutput = getValueAndMessage(min);
|
|
658
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
659
|
+
const valueNumber = ref.valueAsNumber ||
|
|
660
|
+
(inputValue ? +inputValue : inputValue);
|
|
661
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
|
662
|
+
exceedMax = valueNumber > maxOutput.value;
|
|
663
|
+
}
|
|
664
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
|
665
|
+
exceedMin = valueNumber < minOutput.value;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
670
|
+
const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
|
|
671
|
+
const isTime = ref.type == 'time';
|
|
672
|
+
const isWeek = ref.type == 'week';
|
|
673
|
+
if (isString(maxOutput.value) && inputValue) {
|
|
674
|
+
exceedMax = isTime
|
|
675
|
+
? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
|
|
676
|
+
: isWeek
|
|
677
|
+
? inputValue > maxOutput.value
|
|
678
|
+
: valueDate > new Date(maxOutput.value);
|
|
679
|
+
}
|
|
680
|
+
if (isString(minOutput.value) && inputValue) {
|
|
681
|
+
exceedMin = isTime
|
|
682
|
+
? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
|
|
683
|
+
: isWeek
|
|
684
|
+
? inputValue < minOutput.value
|
|
685
|
+
: valueDate < new Date(minOutput.value);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if (exceedMax || exceedMin) {
|
|
689
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
690
|
+
if (!validateAllFieldCriteria) {
|
|
691
|
+
setCustomValidity(error[name].message);
|
|
692
|
+
return error;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if ((maxLength || minLength) &&
|
|
697
|
+
!isEmpty &&
|
|
698
|
+
(isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
|
|
699
|
+
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
700
|
+
const minLengthOutput = getValueAndMessage(minLength);
|
|
701
|
+
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
|
702
|
+
inputValue.length > +maxLengthOutput.value;
|
|
703
|
+
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
|
704
|
+
inputValue.length < +minLengthOutput.value;
|
|
705
|
+
if (exceedMax || exceedMin) {
|
|
706
|
+
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
707
|
+
if (!validateAllFieldCriteria) {
|
|
708
|
+
setCustomValidity(error[name].message);
|
|
709
|
+
return error;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
|
714
|
+
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
715
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
716
|
+
error[name] = {
|
|
717
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
|
718
|
+
message,
|
|
719
|
+
ref,
|
|
720
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
|
721
|
+
};
|
|
722
|
+
if (!validateAllFieldCriteria) {
|
|
723
|
+
setCustomValidity(message);
|
|
724
|
+
return error;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
if (validate) {
|
|
729
|
+
if (isFunction(validate)) {
|
|
730
|
+
const result = await validate(inputValue, formValues);
|
|
731
|
+
const validateError = getValidateError(result, inputRef);
|
|
732
|
+
if (validateError) {
|
|
733
|
+
error[name] = {
|
|
734
|
+
...validateError,
|
|
735
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
|
736
|
+
};
|
|
737
|
+
if (!validateAllFieldCriteria) {
|
|
738
|
+
setCustomValidity(validateError.message);
|
|
739
|
+
return error;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
else if (isObject(validate)) {
|
|
744
|
+
let validationResult = {};
|
|
745
|
+
for (const key in validate) {
|
|
746
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
747
|
+
break;
|
|
748
|
+
}
|
|
749
|
+
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
|
750
|
+
if (validateError) {
|
|
751
|
+
validationResult = {
|
|
752
|
+
...validateError,
|
|
753
|
+
...appendErrorsCurry(key, validateError.message),
|
|
754
|
+
};
|
|
755
|
+
setCustomValidity(validateError.message);
|
|
756
|
+
if (validateAllFieldCriteria) {
|
|
757
|
+
error[name] = validationResult;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (!isEmptyObject(validationResult)) {
|
|
762
|
+
error[name] = {
|
|
763
|
+
ref: inputRef,
|
|
764
|
+
...validationResult,
|
|
765
|
+
};
|
|
766
|
+
if (!validateAllFieldCriteria) {
|
|
767
|
+
return error;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
setCustomValidity(true);
|
|
773
|
+
return error;
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
const defaultOptions = {
|
|
777
|
+
mode: VALIDATION_MODE.onSubmit,
|
|
778
|
+
reValidateMode: VALIDATION_MODE.onChange,
|
|
779
|
+
shouldFocusError: true,
|
|
780
|
+
};
|
|
781
|
+
function createFormControl(props = {}) {
|
|
782
|
+
let _options = {
|
|
783
|
+
...defaultOptions,
|
|
784
|
+
...props,
|
|
785
|
+
};
|
|
786
|
+
let _formState = {
|
|
787
|
+
submitCount: 0,
|
|
788
|
+
isDirty: false,
|
|
789
|
+
isReady: false,
|
|
790
|
+
isLoading: isFunction(_options.defaultValues),
|
|
791
|
+
isValidating: false,
|
|
792
|
+
isSubmitted: false,
|
|
793
|
+
isSubmitting: false,
|
|
794
|
+
isSubmitSuccessful: false,
|
|
795
|
+
isValid: false,
|
|
796
|
+
touchedFields: {},
|
|
797
|
+
dirtyFields: {},
|
|
798
|
+
validatingFields: {},
|
|
799
|
+
errors: _options.errors || {},
|
|
800
|
+
disabled: _options.disabled || false,
|
|
801
|
+
};
|
|
802
|
+
const _fields = {};
|
|
803
|
+
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
|
|
804
|
+
? cloneObject(_options.values || _options.defaultValues) || {}
|
|
805
|
+
: {};
|
|
806
|
+
let _formValues = _options.shouldUnregister
|
|
807
|
+
? {}
|
|
808
|
+
: cloneObject(_defaultValues);
|
|
809
|
+
let _state = {
|
|
810
|
+
action: false,
|
|
811
|
+
mount: false,
|
|
812
|
+
watch: false,
|
|
813
|
+
};
|
|
814
|
+
let _names = {
|
|
815
|
+
mount: new Set(),
|
|
816
|
+
disabled: new Set(),
|
|
817
|
+
unMount: new Set(),
|
|
818
|
+
array: new Set(),
|
|
819
|
+
watch: new Set(),
|
|
820
|
+
};
|
|
821
|
+
let delayErrorCallback;
|
|
822
|
+
let timer = 0;
|
|
823
|
+
const _proxyFormState = {
|
|
824
|
+
isDirty: false,
|
|
825
|
+
dirtyFields: false,
|
|
826
|
+
validatingFields: false,
|
|
827
|
+
touchedFields: false,
|
|
828
|
+
isValidating: false,
|
|
829
|
+
isValid: false,
|
|
830
|
+
errors: false,
|
|
831
|
+
};
|
|
832
|
+
let _proxySubscribeFormState = {
|
|
833
|
+
..._proxyFormState,
|
|
834
|
+
};
|
|
835
|
+
const _subjects = {
|
|
836
|
+
array: createSubject(),
|
|
837
|
+
state: createSubject(),
|
|
838
|
+
};
|
|
839
|
+
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
|
840
|
+
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
|
841
|
+
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
|
842
|
+
const debounce = (callback) => (wait) => {
|
|
843
|
+
clearTimeout(timer);
|
|
844
|
+
timer = setTimeout(callback, wait);
|
|
845
|
+
};
|
|
846
|
+
const _setValid = async (shouldUpdateValid) => {
|
|
847
|
+
if (!_options.disabled &&
|
|
848
|
+
(_proxyFormState.isValid ||
|
|
849
|
+
_proxySubscribeFormState.isValid ||
|
|
850
|
+
shouldUpdateValid)) {
|
|
851
|
+
const isValid = _options.resolver
|
|
852
|
+
? isEmptyObject((await _runSchema()).errors)
|
|
853
|
+
: await executeBuiltInValidation(_fields, true);
|
|
854
|
+
if (isValid !== _formState.isValid) {
|
|
855
|
+
_subjects.state.next({
|
|
856
|
+
isValid,
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
const _updateIsValidating = (names, isValidating) => {
|
|
862
|
+
if (!_options.disabled &&
|
|
863
|
+
(_proxyFormState.isValidating ||
|
|
864
|
+
_proxyFormState.validatingFields ||
|
|
865
|
+
_proxySubscribeFormState.isValidating ||
|
|
866
|
+
_proxySubscribeFormState.validatingFields)) {
|
|
867
|
+
(names || Array.from(_names.mount)).forEach((name) => {
|
|
868
|
+
if (name) {
|
|
869
|
+
isValidating
|
|
870
|
+
? set(_formState.validatingFields, name, isValidating)
|
|
871
|
+
: unset(_formState.validatingFields, name);
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
_subjects.state.next({
|
|
875
|
+
validatingFields: _formState.validatingFields,
|
|
876
|
+
isValidating: !isEmptyObject(_formState.validatingFields),
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
const _setFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
|
881
|
+
if (args && method && !_options.disabled) {
|
|
882
|
+
_state.action = true;
|
|
883
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
|
884
|
+
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
|
885
|
+
shouldSetValues && set(_fields, name, fieldValues);
|
|
886
|
+
}
|
|
887
|
+
if (shouldUpdateFieldsAndState &&
|
|
888
|
+
Array.isArray(get(_formState.errors, name))) {
|
|
889
|
+
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
|
890
|
+
shouldSetValues && set(_formState.errors, name, errors);
|
|
891
|
+
unsetEmptyArray(_formState.errors, name);
|
|
892
|
+
}
|
|
893
|
+
if ((_proxyFormState.touchedFields ||
|
|
894
|
+
_proxySubscribeFormState.touchedFields) &&
|
|
895
|
+
shouldUpdateFieldsAndState &&
|
|
896
|
+
Array.isArray(get(_formState.touchedFields, name))) {
|
|
897
|
+
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
|
898
|
+
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
899
|
+
}
|
|
900
|
+
if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
|
|
901
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
902
|
+
}
|
|
903
|
+
_subjects.state.next({
|
|
904
|
+
name,
|
|
905
|
+
isDirty: _getDirty(name, values),
|
|
906
|
+
dirtyFields: _formState.dirtyFields,
|
|
907
|
+
errors: _formState.errors,
|
|
908
|
+
isValid: _formState.isValid,
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
else {
|
|
912
|
+
set(_formValues, name, values);
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
const updateErrors = (name, error) => {
|
|
916
|
+
set(_formState.errors, name, error);
|
|
917
|
+
_subjects.state.next({
|
|
918
|
+
errors: _formState.errors,
|
|
919
|
+
});
|
|
920
|
+
};
|
|
921
|
+
const _setErrors = (errors) => {
|
|
922
|
+
_formState.errors = errors;
|
|
923
|
+
_subjects.state.next({
|
|
924
|
+
errors: _formState.errors,
|
|
925
|
+
isValid: false,
|
|
926
|
+
});
|
|
927
|
+
};
|
|
928
|
+
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
|
|
929
|
+
const field = get(_fields, name);
|
|
930
|
+
if (field) {
|
|
931
|
+
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
|
|
932
|
+
isUndefined(defaultValue) ||
|
|
933
|
+
(ref && ref.defaultChecked) ||
|
|
934
|
+
shouldSkipSetValueAs
|
|
935
|
+
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
|
936
|
+
: setFieldValue(name, defaultValue);
|
|
937
|
+
_state.mount && _setValid();
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
|
941
|
+
let shouldUpdateField = false;
|
|
942
|
+
let isPreviousDirty = false;
|
|
943
|
+
const output = {
|
|
944
|
+
name,
|
|
945
|
+
};
|
|
946
|
+
if (!_options.disabled) {
|
|
947
|
+
if (!isBlurEvent || shouldDirty) {
|
|
948
|
+
if (_proxyFormState.isDirty || _proxySubscribeFormState.isDirty) {
|
|
949
|
+
isPreviousDirty = _formState.isDirty;
|
|
950
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
|
951
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
|
952
|
+
}
|
|
953
|
+
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
|
954
|
+
isPreviousDirty = !!get(_formState.dirtyFields, name);
|
|
955
|
+
isCurrentFieldPristine
|
|
956
|
+
? unset(_formState.dirtyFields, name)
|
|
957
|
+
: set(_formState.dirtyFields, name, true);
|
|
958
|
+
output.dirtyFields = _formState.dirtyFields;
|
|
959
|
+
shouldUpdateField =
|
|
960
|
+
shouldUpdateField ||
|
|
961
|
+
((_proxyFormState.dirtyFields ||
|
|
962
|
+
_proxySubscribeFormState.dirtyFields) &&
|
|
963
|
+
isPreviousDirty !== !isCurrentFieldPristine);
|
|
964
|
+
}
|
|
965
|
+
if (isBlurEvent) {
|
|
966
|
+
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
967
|
+
if (!isPreviousFieldTouched) {
|
|
968
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
|
969
|
+
output.touchedFields = _formState.touchedFields;
|
|
970
|
+
shouldUpdateField =
|
|
971
|
+
shouldUpdateField ||
|
|
972
|
+
((_proxyFormState.touchedFields ||
|
|
973
|
+
_proxySubscribeFormState.touchedFields) &&
|
|
974
|
+
isPreviousFieldTouched !== isBlurEvent);
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
978
|
+
}
|
|
979
|
+
return shouldUpdateField ? output : {};
|
|
980
|
+
};
|
|
981
|
+
const shouldRenderByError = (name, isValid, error, fieldState) => {
|
|
982
|
+
const previousFieldError = get(_formState.errors, name);
|
|
983
|
+
const shouldUpdateValid = (_proxyFormState.isValid || _proxySubscribeFormState.isValid) &&
|
|
984
|
+
isBoolean(isValid) &&
|
|
985
|
+
_formState.isValid !== isValid;
|
|
986
|
+
if (_options.delayError && error) {
|
|
987
|
+
delayErrorCallback = debounce(() => updateErrors(name, error));
|
|
988
|
+
delayErrorCallback(_options.delayError);
|
|
989
|
+
}
|
|
990
|
+
else {
|
|
991
|
+
clearTimeout(timer);
|
|
992
|
+
delayErrorCallback = null;
|
|
993
|
+
error
|
|
994
|
+
? set(_formState.errors, name, error)
|
|
995
|
+
: unset(_formState.errors, name);
|
|
996
|
+
}
|
|
997
|
+
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) ||
|
|
998
|
+
!isEmptyObject(fieldState) ||
|
|
999
|
+
shouldUpdateValid) {
|
|
1000
|
+
const updatedFormState = {
|
|
1001
|
+
...fieldState,
|
|
1002
|
+
...(shouldUpdateValid && isBoolean(isValid) ? { isValid } : {}),
|
|
1003
|
+
errors: _formState.errors,
|
|
1004
|
+
name,
|
|
1005
|
+
};
|
|
1006
|
+
_formState = {
|
|
1007
|
+
..._formState,
|
|
1008
|
+
...updatedFormState,
|
|
1009
|
+
};
|
|
1010
|
+
_subjects.state.next(updatedFormState);
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
const _runSchema = async (name) => {
|
|
1014
|
+
_updateIsValidating(name, true);
|
|
1015
|
+
const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
|
1016
|
+
_updateIsValidating(name);
|
|
1017
|
+
return result;
|
|
1018
|
+
};
|
|
1019
|
+
const executeSchemaAndUpdateState = async (names) => {
|
|
1020
|
+
const { errors } = await _runSchema(names);
|
|
1021
|
+
if (names) {
|
|
1022
|
+
for (const name of names) {
|
|
1023
|
+
const error = get(errors, name);
|
|
1024
|
+
error
|
|
1025
|
+
? set(_formState.errors, name, error)
|
|
1026
|
+
: unset(_formState.errors, name);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
else {
|
|
1030
|
+
_formState.errors = errors;
|
|
1031
|
+
}
|
|
1032
|
+
return errors;
|
|
1033
|
+
};
|
|
1034
|
+
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
|
1035
|
+
valid: true,
|
|
1036
|
+
}) => {
|
|
1037
|
+
for (const name in fields) {
|
|
1038
|
+
const field = fields[name];
|
|
1039
|
+
if (field) {
|
|
1040
|
+
const { _f, ...fieldValue } = field;
|
|
1041
|
+
if (_f) {
|
|
1042
|
+
const isFieldArrayRoot = _names.array.has(_f.name);
|
|
1043
|
+
const isPromiseFunction = field._f && hasPromiseValidation(field._f);
|
|
1044
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1045
|
+
_updateIsValidating([name], true);
|
|
1046
|
+
}
|
|
1047
|
+
const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
|
1048
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1049
|
+
_updateIsValidating([name]);
|
|
1050
|
+
}
|
|
1051
|
+
if (fieldError[_f.name]) {
|
|
1052
|
+
context.valid = false;
|
|
1053
|
+
if (shouldOnlyCheckValid) {
|
|
1054
|
+
break;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
!shouldOnlyCheckValid &&
|
|
1058
|
+
(get(fieldError, _f.name)
|
|
1059
|
+
? isFieldArrayRoot
|
|
1060
|
+
? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
|
|
1061
|
+
: set(_formState.errors, _f.name, fieldError[_f.name])
|
|
1062
|
+
: unset(_formState.errors, _f.name));
|
|
1063
|
+
}
|
|
1064
|
+
!isEmptyObject(fieldValue) &&
|
|
1065
|
+
(await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
return context.valid;
|
|
1069
|
+
};
|
|
1070
|
+
const _removeUnmounted = () => {
|
|
1071
|
+
for (const name of _names.unMount) {
|
|
1072
|
+
const field = get(_fields, name);
|
|
1073
|
+
field &&
|
|
1074
|
+
(field._f.refs
|
|
1075
|
+
? field._f.refs.every((ref) => !live(ref))
|
|
1076
|
+
: !live(field._f.ref)) &&
|
|
1077
|
+
unregister(name);
|
|
1078
|
+
}
|
|
1079
|
+
_names.unMount = new Set();
|
|
1080
|
+
};
|
|
1081
|
+
const _getDirty = (name, data) => !_options.disabled &&
|
|
1082
|
+
(name && data && set(_formValues, name, data),
|
|
1083
|
+
!deepEqual(getValues(), _defaultValues));
|
|
1084
|
+
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
|
1085
|
+
...(_state.mount
|
|
1086
|
+
? _formValues
|
|
1087
|
+
: isUndefined(defaultValue)
|
|
1088
|
+
? _defaultValues
|
|
1089
|
+
: isString(names)
|
|
1090
|
+
? { [names]: defaultValue }
|
|
1091
|
+
: defaultValue),
|
|
1092
|
+
}, isGlobal, defaultValue);
|
|
1093
|
+
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
|
|
1094
|
+
const setFieldValue = (name, value, options = {}) => {
|
|
1095
|
+
const field = get(_fields, name);
|
|
1096
|
+
let fieldValue = value;
|
|
1097
|
+
if (field) {
|
|
1098
|
+
const fieldReference = field._f;
|
|
1099
|
+
if (fieldReference) {
|
|
1100
|
+
!fieldReference.disabled &&
|
|
1101
|
+
set(_formValues, name, getFieldValueAs(value, fieldReference));
|
|
1102
|
+
fieldValue =
|
|
1103
|
+
isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
|
1104
|
+
? ''
|
|
1105
|
+
: value;
|
|
1106
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
|
1107
|
+
[...fieldReference.ref.options].forEach((optionRef) => (optionRef.selected = fieldValue.includes(optionRef.value)));
|
|
1108
|
+
}
|
|
1109
|
+
else if (fieldReference.refs) {
|
|
1110
|
+
if (isCheckBoxInput(fieldReference.ref)) {
|
|
1111
|
+
fieldReference.refs.length > 1
|
|
1112
|
+
? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) &&
|
|
1113
|
+
(checkboxRef.checked = Array.isArray(fieldValue)
|
|
1114
|
+
? !!fieldValue.find((data) => data === checkboxRef.value)
|
|
1115
|
+
: fieldValue === checkboxRef.value))
|
|
1116
|
+
: fieldReference.refs[0] &&
|
|
1117
|
+
(fieldReference.refs[0].checked = !!fieldValue);
|
|
1118
|
+
}
|
|
1119
|
+
else {
|
|
1120
|
+
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
else if (isFileInput(fieldReference.ref)) {
|
|
1124
|
+
fieldReference.ref.value = '';
|
|
1125
|
+
}
|
|
1126
|
+
else {
|
|
1127
|
+
fieldReference.ref.value = fieldValue;
|
|
1128
|
+
if (!fieldReference.ref.type) {
|
|
1129
|
+
_subjects.state.next({
|
|
1130
|
+
name,
|
|
1131
|
+
values: cloneObject(_formValues),
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
(options.shouldDirty || options.shouldTouch) &&
|
|
1138
|
+
updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
|
1139
|
+
options.shouldValidate && trigger(name);
|
|
1140
|
+
};
|
|
1141
|
+
const setValues = (name, value, options) => {
|
|
1142
|
+
for (const fieldKey in value) {
|
|
1143
|
+
const fieldValue = value[fieldKey];
|
|
1144
|
+
const fieldName = `${name}.${fieldKey}`;
|
|
1145
|
+
const field = get(_fields, fieldName);
|
|
1146
|
+
(_names.array.has(name) ||
|
|
1147
|
+
isObject(fieldValue) ||
|
|
1148
|
+
(field && !field._f)) &&
|
|
1149
|
+
!isDateObject(fieldValue)
|
|
1150
|
+
? setValues(fieldName, fieldValue, options)
|
|
1151
|
+
: setFieldValue(fieldName, fieldValue, options);
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
const setValue = (name, value, options = {}) => {
|
|
1155
|
+
const field = get(_fields, name);
|
|
1156
|
+
const isFieldArray = _names.array.has(name);
|
|
1157
|
+
const cloneValue = cloneObject(value);
|
|
1158
|
+
set(_formValues, name, cloneValue);
|
|
1159
|
+
if (isFieldArray) {
|
|
1160
|
+
_subjects.array.next({
|
|
1161
|
+
name,
|
|
1162
|
+
values: cloneObject(_formValues),
|
|
1163
|
+
});
|
|
1164
|
+
if ((_proxyFormState.isDirty ||
|
|
1165
|
+
_proxyFormState.dirtyFields ||
|
|
1166
|
+
_proxySubscribeFormState.isDirty ||
|
|
1167
|
+
_proxySubscribeFormState.dirtyFields) &&
|
|
1168
|
+
options.shouldDirty) {
|
|
1169
|
+
_subjects.state.next({
|
|
1170
|
+
name,
|
|
1171
|
+
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
|
1172
|
+
isDirty: _getDirty(name, cloneValue),
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
else {
|
|
1177
|
+
field && !field._f && !isNullOrUndefined(cloneValue)
|
|
1178
|
+
? setValues(name, cloneValue, options)
|
|
1179
|
+
: setFieldValue(name, cloneValue, options);
|
|
1180
|
+
}
|
|
1181
|
+
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
|
1182
|
+
_subjects.state.next({
|
|
1183
|
+
name: _state.mount ? name : undefined,
|
|
1184
|
+
values: cloneObject(_formValues),
|
|
1185
|
+
});
|
|
1186
|
+
};
|
|
1187
|
+
const onChange = async (event) => {
|
|
1188
|
+
_state.mount = true;
|
|
1189
|
+
const target = event.target;
|
|
1190
|
+
let name = target.name;
|
|
1191
|
+
let isFieldValueUpdated = true;
|
|
1192
|
+
const field = get(_fields, name);
|
|
1193
|
+
const _updateIsFieldValueUpdated = (fieldValue) => {
|
|
1194
|
+
isFieldValueUpdated =
|
|
1195
|
+
Number.isNaN(fieldValue) ||
|
|
1196
|
+
(isDateObject(fieldValue) && isNaN(fieldValue.getTime())) ||
|
|
1197
|
+
deepEqual(fieldValue, get(_formValues, name, fieldValue));
|
|
1198
|
+
};
|
|
1199
|
+
if (field) {
|
|
1200
|
+
let error;
|
|
1201
|
+
let isValid;
|
|
1202
|
+
const fieldValue = target.type
|
|
1203
|
+
? getFieldValue(field._f)
|
|
1204
|
+
: getEventValue(event);
|
|
1205
|
+
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
|
1206
|
+
const shouldSkipValidation = (!hasValidation(field._f) &&
|
|
1207
|
+
!_options.resolver &&
|
|
1208
|
+
!get(_formState.errors, name) &&
|
|
1209
|
+
!field._f.deps) ||
|
|
1210
|
+
skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
|
1211
|
+
const watched = isWatched(name, _names, isBlurEvent);
|
|
1212
|
+
set(_formValues, name, fieldValue);
|
|
1213
|
+
if (isBlurEvent) {
|
|
1214
|
+
field._f.onBlur && field._f.onBlur(event);
|
|
1215
|
+
delayErrorCallback && delayErrorCallback(0);
|
|
1216
|
+
}
|
|
1217
|
+
else if (field._f.onChange) {
|
|
1218
|
+
field._f.onChange(event);
|
|
1219
|
+
}
|
|
1220
|
+
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent);
|
|
1221
|
+
const shouldRender = !isEmptyObject(fieldState) || watched;
|
|
1222
|
+
!isBlurEvent &&
|
|
1223
|
+
_subjects.state.next({
|
|
1224
|
+
name,
|
|
1225
|
+
type: event.type,
|
|
1226
|
+
values: cloneObject(_formValues),
|
|
1227
|
+
});
|
|
1228
|
+
if (shouldSkipValidation) {
|
|
1229
|
+
if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
|
|
1230
|
+
if (_options.mode === 'onBlur') {
|
|
1231
|
+
if (isBlurEvent) {
|
|
1232
|
+
_setValid();
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
else if (!isBlurEvent) {
|
|
1236
|
+
_setValid();
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
return (shouldRender &&
|
|
1240
|
+
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
|
|
1241
|
+
}
|
|
1242
|
+
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
|
1243
|
+
if (_options.resolver) {
|
|
1244
|
+
const { errors } = await _runSchema([name]);
|
|
1245
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
1246
|
+
if (isFieldValueUpdated) {
|
|
1247
|
+
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
|
1248
|
+
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
|
1249
|
+
error = errorLookupResult.error;
|
|
1250
|
+
name = errorLookupResult.name;
|
|
1251
|
+
isValid = isEmptyObject(errors);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
else {
|
|
1255
|
+
_updateIsValidating([name], true);
|
|
1256
|
+
error = (await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
|
1257
|
+
_updateIsValidating([name]);
|
|
1258
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
1259
|
+
if (isFieldValueUpdated) {
|
|
1260
|
+
if (error) {
|
|
1261
|
+
isValid = false;
|
|
1262
|
+
}
|
|
1263
|
+
else if (_proxyFormState.isValid ||
|
|
1264
|
+
_proxySubscribeFormState.isValid) {
|
|
1265
|
+
isValid = await executeBuiltInValidation(_fields, true);
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
if (isFieldValueUpdated) {
|
|
1270
|
+
field._f.deps &&
|
|
1271
|
+
trigger(field._f.deps);
|
|
1272
|
+
shouldRenderByError(name, isValid, error, fieldState);
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
};
|
|
1276
|
+
const _focusInput = (ref, key) => {
|
|
1277
|
+
if (get(_formState.errors, key) && ref.focus) {
|
|
1278
|
+
ref.focus();
|
|
1279
|
+
return 1;
|
|
1280
|
+
}
|
|
1281
|
+
return;
|
|
1282
|
+
};
|
|
1283
|
+
const trigger = async (name, options = {}) => {
|
|
1284
|
+
let isValid;
|
|
1285
|
+
let validationResult;
|
|
1286
|
+
const fieldNames = convertToArrayPayload(name);
|
|
1287
|
+
if (_options.resolver) {
|
|
1288
|
+
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
|
1289
|
+
isValid = isEmptyObject(errors);
|
|
1290
|
+
validationResult = name
|
|
1291
|
+
? !fieldNames.some((name) => get(errors, name))
|
|
1292
|
+
: isValid;
|
|
1293
|
+
}
|
|
1294
|
+
else if (name) {
|
|
1295
|
+
validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
|
|
1296
|
+
const field = get(_fields, fieldName);
|
|
1297
|
+
return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
|
|
1298
|
+
}))).every(Boolean);
|
|
1299
|
+
!(!validationResult && !_formState.isValid) && _setValid();
|
|
1300
|
+
}
|
|
1301
|
+
else {
|
|
1302
|
+
validationResult = isValid = await executeBuiltInValidation(_fields);
|
|
1303
|
+
}
|
|
1304
|
+
_subjects.state.next({
|
|
1305
|
+
...(!isString(name) ||
|
|
1306
|
+
((_proxyFormState.isValid || _proxySubscribeFormState.isValid) &&
|
|
1307
|
+
isValid !== _formState.isValid)
|
|
1308
|
+
? {}
|
|
1309
|
+
: { name }),
|
|
1310
|
+
...(_options.resolver || !name ? { isValid } : {}),
|
|
1311
|
+
errors: _formState.errors,
|
|
1312
|
+
});
|
|
1313
|
+
options.shouldFocus &&
|
|
1314
|
+
!validationResult &&
|
|
1315
|
+
iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
|
|
1316
|
+
return validationResult;
|
|
1317
|
+
};
|
|
1318
|
+
const getValues = (fieldNames) => {
|
|
1319
|
+
const values = {
|
|
1320
|
+
...(_state.mount ? _formValues : _defaultValues),
|
|
1321
|
+
};
|
|
1322
|
+
return isUndefined(fieldNames)
|
|
1323
|
+
? values
|
|
1324
|
+
: isString(fieldNames)
|
|
1325
|
+
? get(values, fieldNames)
|
|
1326
|
+
: fieldNames.map((name) => get(values, name));
|
|
1327
|
+
};
|
|
1328
|
+
const getFieldState = (name, formState) => ({
|
|
1329
|
+
invalid: !!get((formState || _formState).errors, name),
|
|
1330
|
+
isDirty: !!get((formState || _formState).dirtyFields, name),
|
|
1331
|
+
error: get((formState || _formState).errors, name),
|
|
1332
|
+
isValidating: !!get(_formState.validatingFields, name),
|
|
1333
|
+
isTouched: !!get((formState || _formState).touchedFields, name),
|
|
1334
|
+
});
|
|
1335
|
+
const clearErrors = (name) => {
|
|
1336
|
+
name &&
|
|
1337
|
+
convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
|
|
1338
|
+
_subjects.state.next({
|
|
1339
|
+
errors: name ? _formState.errors : {},
|
|
1340
|
+
});
|
|
1341
|
+
};
|
|
1342
|
+
const setError = (name, error, options) => {
|
|
1343
|
+
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
|
|
1344
|
+
const currentError = get(_formState.errors, name) || {};
|
|
1345
|
+
// Don't override existing error messages elsewhere in the object tree.
|
|
1346
|
+
const { ref: currentRef, message, type, ...restOfErrorTree } = currentError;
|
|
1347
|
+
set(_formState.errors, name, {
|
|
1348
|
+
...restOfErrorTree,
|
|
1349
|
+
...error,
|
|
1350
|
+
ref,
|
|
1351
|
+
});
|
|
1352
|
+
_subjects.state.next({
|
|
1353
|
+
name,
|
|
1354
|
+
errors: _formState.errors,
|
|
1355
|
+
isValid: false,
|
|
1356
|
+
});
|
|
1357
|
+
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
1358
|
+
};
|
|
1359
|
+
const watch = (name, defaultValue) => isFunction(name)
|
|
1360
|
+
? _subjects.state.subscribe({
|
|
1361
|
+
next: (payload) => name(_getWatch(undefined, defaultValue), payload),
|
|
1362
|
+
})
|
|
1363
|
+
: _getWatch(name, defaultValue, true);
|
|
1364
|
+
const _subscribe = (props) => _subjects.state.subscribe({
|
|
1365
|
+
next: (formState) => {
|
|
1366
|
+
if (shouldSubscribeByName(props.name, formState.name, props.exact) &&
|
|
1367
|
+
shouldRenderFormState(formState, props.formState || _proxyFormState, _setFormState, props.reRenderRoot)) {
|
|
1368
|
+
props.callback({
|
|
1369
|
+
values: { ..._formValues },
|
|
1370
|
+
..._formState,
|
|
1371
|
+
...formState,
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
},
|
|
1375
|
+
}).unsubscribe;
|
|
1376
|
+
const subscribe = (props) => {
|
|
1377
|
+
_state.mount = true;
|
|
1378
|
+
_proxySubscribeFormState = {
|
|
1379
|
+
..._proxySubscribeFormState,
|
|
1380
|
+
...props.formState,
|
|
1381
|
+
};
|
|
1382
|
+
return _subscribe({
|
|
1383
|
+
...props,
|
|
1384
|
+
formState: _proxySubscribeFormState,
|
|
1385
|
+
});
|
|
1386
|
+
};
|
|
1387
|
+
const unregister = (name, options = {}) => {
|
|
1388
|
+
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
|
|
1389
|
+
_names.mount.delete(fieldName);
|
|
1390
|
+
_names.array.delete(fieldName);
|
|
1391
|
+
if (!options.keepValue) {
|
|
1392
|
+
unset(_fields, fieldName);
|
|
1393
|
+
unset(_formValues, fieldName);
|
|
1394
|
+
}
|
|
1395
|
+
!options.keepError && unset(_formState.errors, fieldName);
|
|
1396
|
+
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
|
1397
|
+
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
|
1398
|
+
!options.keepIsValidating &&
|
|
1399
|
+
unset(_formState.validatingFields, fieldName);
|
|
1400
|
+
!_options.shouldUnregister &&
|
|
1401
|
+
!options.keepDefaultValue &&
|
|
1402
|
+
unset(_defaultValues, fieldName);
|
|
1403
|
+
}
|
|
1404
|
+
_subjects.state.next({
|
|
1405
|
+
values: cloneObject(_formValues),
|
|
1406
|
+
});
|
|
1407
|
+
_subjects.state.next({
|
|
1408
|
+
..._formState,
|
|
1409
|
+
...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
|
|
1410
|
+
});
|
|
1411
|
+
!options.keepIsValid && _setValid();
|
|
1412
|
+
};
|
|
1413
|
+
const _setDisabledField = ({ disabled, name, }) => {
|
|
1414
|
+
if ((isBoolean(disabled) && _state.mount) ||
|
|
1415
|
+
!!disabled ||
|
|
1416
|
+
_names.disabled.has(name)) {
|
|
1417
|
+
disabled ? _names.disabled.add(name) : _names.disabled.delete(name);
|
|
1418
|
+
}
|
|
1419
|
+
};
|
|
1420
|
+
const register = (name, options = {}) => {
|
|
1421
|
+
let field = get(_fields, name);
|
|
1422
|
+
const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
|
|
1423
|
+
set(_fields, name, {
|
|
1424
|
+
...(field || {}),
|
|
1425
|
+
_f: {
|
|
1426
|
+
...(field && field._f ? field._f : { ref: { name } }),
|
|
1427
|
+
name,
|
|
1428
|
+
mount: true,
|
|
1429
|
+
...options,
|
|
1430
|
+
},
|
|
1431
|
+
});
|
|
1432
|
+
_names.mount.add(name);
|
|
1433
|
+
if (field) {
|
|
1434
|
+
_setDisabledField({
|
|
1435
|
+
disabled: isBoolean(options.disabled)
|
|
1436
|
+
? options.disabled
|
|
1437
|
+
: _options.disabled,
|
|
1438
|
+
name,
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
else {
|
|
1442
|
+
updateValidAndValue(name, true, options.value);
|
|
1443
|
+
}
|
|
1444
|
+
return {
|
|
1445
|
+
...(disabledIsDefined
|
|
1446
|
+
? { disabled: options.disabled || _options.disabled }
|
|
1447
|
+
: {}),
|
|
1448
|
+
...(_options.progressive
|
|
1449
|
+
? {
|
|
1450
|
+
required: !!options.required,
|
|
1451
|
+
min: getRuleValue(options.min),
|
|
1452
|
+
max: getRuleValue(options.max),
|
|
1453
|
+
minLength: getRuleValue(options.minLength),
|
|
1454
|
+
maxLength: getRuleValue(options.maxLength),
|
|
1455
|
+
pattern: getRuleValue(options.pattern),
|
|
1456
|
+
}
|
|
1457
|
+
: {}),
|
|
1458
|
+
name,
|
|
1459
|
+
onChange,
|
|
1460
|
+
onBlur: onChange,
|
|
1461
|
+
ref: (ref) => {
|
|
1462
|
+
if (ref) {
|
|
1463
|
+
register(name, options);
|
|
1464
|
+
field = get(_fields, name);
|
|
1465
|
+
const fieldRef = isUndefined(ref.value)
|
|
1466
|
+
? ref.querySelectorAll
|
|
1467
|
+
? ref.querySelectorAll('input,select,textarea')[0] || ref
|
|
1468
|
+
: ref
|
|
1469
|
+
: ref;
|
|
1470
|
+
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
|
1471
|
+
const refs = field._f.refs || [];
|
|
1472
|
+
if (radioOrCheckbox
|
|
1473
|
+
? refs.find((option) => option === fieldRef)
|
|
1474
|
+
: fieldRef === field._f.ref) {
|
|
1475
|
+
return;
|
|
1476
|
+
}
|
|
1477
|
+
set(_fields, name, {
|
|
1478
|
+
_f: {
|
|
1479
|
+
...field._f,
|
|
1480
|
+
...(radioOrCheckbox
|
|
1481
|
+
? {
|
|
1482
|
+
refs: [
|
|
1483
|
+
...refs.filter(live),
|
|
1484
|
+
fieldRef,
|
|
1485
|
+
...(Array.isArray(get(_defaultValues, name)) ? [{}] : []),
|
|
1486
|
+
],
|
|
1487
|
+
ref: { type: fieldRef.type, name },
|
|
1488
|
+
}
|
|
1489
|
+
: { ref: fieldRef }),
|
|
1490
|
+
},
|
|
1491
|
+
});
|
|
1492
|
+
updateValidAndValue(name, false, undefined, fieldRef);
|
|
1493
|
+
}
|
|
1494
|
+
else {
|
|
1495
|
+
field = get(_fields, name, {});
|
|
1496
|
+
if (field._f) {
|
|
1497
|
+
field._f.mount = false;
|
|
1498
|
+
}
|
|
1499
|
+
(_options.shouldUnregister || options.shouldUnregister) &&
|
|
1500
|
+
!(isNameInFieldArray(_names.array, name) && _state.action) &&
|
|
1501
|
+
_names.unMount.add(name);
|
|
1502
|
+
}
|
|
1503
|
+
},
|
|
1504
|
+
};
|
|
1505
|
+
};
|
|
1506
|
+
const _focusError = () => _options.shouldFocusError &&
|
|
1507
|
+
iterateFieldsByAction(_fields, _focusInput, _names.mount);
|
|
1508
|
+
const _disableForm = (disabled) => {
|
|
1509
|
+
if (isBoolean(disabled)) {
|
|
1510
|
+
_subjects.state.next({ disabled });
|
|
1511
|
+
iterateFieldsByAction(_fields, (ref, name) => {
|
|
1512
|
+
const currentField = get(_fields, name);
|
|
1513
|
+
if (currentField) {
|
|
1514
|
+
ref.disabled = currentField._f.disabled || disabled;
|
|
1515
|
+
if (Array.isArray(currentField._f.refs)) {
|
|
1516
|
+
currentField._f.refs.forEach((inputRef) => {
|
|
1517
|
+
inputRef.disabled = currentField._f.disabled || disabled;
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
}, 0, false);
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
const handleSubmit = (onValid, onInvalid) => async (e) => {
|
|
1525
|
+
let onValidError = undefined;
|
|
1526
|
+
if (e) {
|
|
1527
|
+
e.preventDefault && e.preventDefault();
|
|
1528
|
+
e.persist &&
|
|
1529
|
+
e.persist();
|
|
1530
|
+
}
|
|
1531
|
+
let fieldValues = cloneObject(_formValues);
|
|
1532
|
+
_subjects.state.next({
|
|
1533
|
+
isSubmitting: true,
|
|
1534
|
+
});
|
|
1535
|
+
if (_options.resolver) {
|
|
1536
|
+
const { errors, values } = await _runSchema();
|
|
1537
|
+
_formState.errors = errors;
|
|
1538
|
+
fieldValues = values;
|
|
1539
|
+
}
|
|
1540
|
+
else {
|
|
1541
|
+
await executeBuiltInValidation(_fields);
|
|
1542
|
+
}
|
|
1543
|
+
if (_names.disabled.size) {
|
|
1544
|
+
for (const name of _names.disabled) {
|
|
1545
|
+
set(fieldValues, name, undefined);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
unset(_formState.errors, 'root');
|
|
1549
|
+
if (isEmptyObject(_formState.errors)) {
|
|
1550
|
+
_subjects.state.next({
|
|
1551
|
+
errors: {},
|
|
1552
|
+
});
|
|
1553
|
+
try {
|
|
1554
|
+
await onValid(fieldValues, e);
|
|
1555
|
+
}
|
|
1556
|
+
catch (error) {
|
|
1557
|
+
onValidError = error;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
else {
|
|
1561
|
+
if (onInvalid) {
|
|
1562
|
+
await onInvalid({ ..._formState.errors }, e);
|
|
1563
|
+
}
|
|
1564
|
+
_focusError();
|
|
1565
|
+
setTimeout(_focusError);
|
|
1566
|
+
}
|
|
1567
|
+
_subjects.state.next({
|
|
1568
|
+
isSubmitted: true,
|
|
1569
|
+
isSubmitting: false,
|
|
1570
|
+
isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
|
|
1571
|
+
submitCount: _formState.submitCount + 1,
|
|
1572
|
+
errors: _formState.errors,
|
|
1573
|
+
});
|
|
1574
|
+
if (onValidError) {
|
|
1575
|
+
throw onValidError;
|
|
1576
|
+
}
|
|
1577
|
+
};
|
|
1578
|
+
const resetField = (name, options = {}) => {
|
|
1579
|
+
if (get(_fields, name)) {
|
|
1580
|
+
if (isUndefined(options.defaultValue)) {
|
|
1581
|
+
setValue(name, cloneObject(get(_defaultValues, name)));
|
|
1582
|
+
}
|
|
1583
|
+
else {
|
|
1584
|
+
setValue(name, options.defaultValue);
|
|
1585
|
+
set(_defaultValues, name, cloneObject(options.defaultValue));
|
|
1586
|
+
}
|
|
1587
|
+
if (!options.keepTouched) {
|
|
1588
|
+
unset(_formState.touchedFields, name);
|
|
1589
|
+
}
|
|
1590
|
+
if (!options.keepDirty) {
|
|
1591
|
+
unset(_formState.dirtyFields, name);
|
|
1592
|
+
_formState.isDirty = options.defaultValue
|
|
1593
|
+
? _getDirty(name, cloneObject(get(_defaultValues, name)))
|
|
1594
|
+
: _getDirty();
|
|
1595
|
+
}
|
|
1596
|
+
if (!options.keepError) {
|
|
1597
|
+
unset(_formState.errors, name);
|
|
1598
|
+
_proxyFormState.isValid && _setValid();
|
|
1599
|
+
}
|
|
1600
|
+
_subjects.state.next({ ..._formState });
|
|
1601
|
+
}
|
|
1602
|
+
};
|
|
1603
|
+
const _reset = (formValues, keepStateOptions = {}) => {
|
|
1604
|
+
const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
|
1605
|
+
const cloneUpdatedValues = cloneObject(updatedValues);
|
|
1606
|
+
const isEmptyResetValues = isEmptyObject(formValues);
|
|
1607
|
+
const values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues;
|
|
1608
|
+
if (!keepStateOptions.keepDefaultValues) {
|
|
1609
|
+
_defaultValues = updatedValues;
|
|
1610
|
+
}
|
|
1611
|
+
if (!keepStateOptions.keepValues) {
|
|
1612
|
+
if (keepStateOptions.keepDirtyValues) {
|
|
1613
|
+
const fieldsToCheck = new Set([
|
|
1614
|
+
..._names.mount,
|
|
1615
|
+
...Object.keys(getDirtyFields(_defaultValues, _formValues)),
|
|
1616
|
+
]);
|
|
1617
|
+
for (const fieldName of Array.from(fieldsToCheck)) {
|
|
1618
|
+
get(_formState.dirtyFields, fieldName)
|
|
1619
|
+
? set(values, fieldName, get(_formValues, fieldName))
|
|
1620
|
+
: setValue(fieldName, get(values, fieldName));
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
else {
|
|
1624
|
+
if (isWeb && isUndefined(formValues)) {
|
|
1625
|
+
for (const name of _names.mount) {
|
|
1626
|
+
const field = get(_fields, name);
|
|
1627
|
+
if (field && field._f) {
|
|
1628
|
+
const fieldReference = Array.isArray(field._f.refs)
|
|
1629
|
+
? field._f.refs[0]
|
|
1630
|
+
: field._f.ref;
|
|
1631
|
+
if (isHTMLElement(fieldReference)) {
|
|
1632
|
+
const form = fieldReference.closest('form');
|
|
1633
|
+
if (form) {
|
|
1634
|
+
form.reset();
|
|
1635
|
+
break;
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
for (const fieldName of _names.mount) {
|
|
1642
|
+
setValue(fieldName, get(values, fieldName));
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
_formValues = cloneObject(values);
|
|
1646
|
+
_subjects.array.next({
|
|
1647
|
+
values: { ...values },
|
|
1648
|
+
});
|
|
1649
|
+
_subjects.state.next({
|
|
1650
|
+
values: { ...values },
|
|
1651
|
+
});
|
|
1652
|
+
}
|
|
1653
|
+
_names = {
|
|
1654
|
+
mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
|
|
1655
|
+
unMount: new Set(),
|
|
1656
|
+
array: new Set(),
|
|
1657
|
+
disabled: new Set(),
|
|
1658
|
+
watch: new Set(),
|
|
1659
|
+
watchAll: false,
|
|
1660
|
+
focus: '',
|
|
1661
|
+
};
|
|
1662
|
+
_state.mount =
|
|
1663
|
+
!_proxyFormState.isValid ||
|
|
1664
|
+
!!keepStateOptions.keepIsValid ||
|
|
1665
|
+
!!keepStateOptions.keepDirtyValues;
|
|
1666
|
+
_state.watch = !!_options.shouldUnregister;
|
|
1667
|
+
_subjects.state.next({
|
|
1668
|
+
submitCount: keepStateOptions.keepSubmitCount
|
|
1669
|
+
? _formState.submitCount
|
|
1670
|
+
: 0,
|
|
1671
|
+
isDirty: isEmptyResetValues
|
|
1672
|
+
? false
|
|
1673
|
+
: keepStateOptions.keepDirty
|
|
1674
|
+
? _formState.isDirty
|
|
1675
|
+
: !!(keepStateOptions.keepDefaultValues &&
|
|
1676
|
+
!deepEqual(formValues, _defaultValues)),
|
|
1677
|
+
isSubmitted: keepStateOptions.keepIsSubmitted
|
|
1678
|
+
? _formState.isSubmitted
|
|
1679
|
+
: false,
|
|
1680
|
+
dirtyFields: isEmptyResetValues
|
|
1681
|
+
? {}
|
|
1682
|
+
: keepStateOptions.keepDirtyValues
|
|
1683
|
+
? keepStateOptions.keepDefaultValues && _formValues
|
|
1684
|
+
? getDirtyFields(_defaultValues, _formValues)
|
|
1685
|
+
: _formState.dirtyFields
|
|
1686
|
+
: keepStateOptions.keepDefaultValues && formValues
|
|
1687
|
+
? getDirtyFields(_defaultValues, formValues)
|
|
1688
|
+
: keepStateOptions.keepDirty
|
|
1689
|
+
? _formState.dirtyFields
|
|
1690
|
+
: {},
|
|
1691
|
+
touchedFields: keepStateOptions.keepTouched
|
|
1692
|
+
? _formState.touchedFields
|
|
1693
|
+
: {},
|
|
1694
|
+
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
|
1695
|
+
isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful
|
|
1696
|
+
? _formState.isSubmitSuccessful
|
|
1697
|
+
: false,
|
|
1698
|
+
isSubmitting: false,
|
|
1699
|
+
});
|
|
1700
|
+
};
|
|
1701
|
+
const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
|
|
1702
|
+
? formValues(_formValues)
|
|
1703
|
+
: formValues, keepStateOptions);
|
|
1704
|
+
const setFocus = (name, options = {}) => {
|
|
1705
|
+
const field = get(_fields, name);
|
|
1706
|
+
const fieldReference = field && field._f;
|
|
1707
|
+
if (fieldReference) {
|
|
1708
|
+
const fieldRef = fieldReference.refs
|
|
1709
|
+
? fieldReference.refs[0]
|
|
1710
|
+
: fieldReference.ref;
|
|
1711
|
+
if (fieldRef.focus) {
|
|
1712
|
+
fieldRef.focus();
|
|
1713
|
+
options.shouldSelect &&
|
|
1714
|
+
isFunction(fieldRef.select) &&
|
|
1715
|
+
fieldRef.select();
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
const _setFormState = (updatedFormState) => {
|
|
1720
|
+
_formState = {
|
|
1721
|
+
..._formState,
|
|
1722
|
+
...updatedFormState,
|
|
1723
|
+
};
|
|
1724
|
+
};
|
|
1725
|
+
const _resetDefaultValues = () => isFunction(_options.defaultValues) &&
|
|
1726
|
+
_options.defaultValues().then((values) => {
|
|
1727
|
+
reset(values, _options.resetOptions);
|
|
1728
|
+
_subjects.state.next({
|
|
1729
|
+
isLoading: false,
|
|
1730
|
+
});
|
|
1731
|
+
});
|
|
1732
|
+
const id = createId(props.id);
|
|
1733
|
+
const submit = () => {
|
|
1734
|
+
submitForm(id);
|
|
1735
|
+
};
|
|
1736
|
+
const methods = {
|
|
1737
|
+
control: {
|
|
1738
|
+
register,
|
|
1739
|
+
unregister,
|
|
1740
|
+
getFieldState,
|
|
1741
|
+
handleSubmit,
|
|
1742
|
+
setError,
|
|
1743
|
+
_subscribe,
|
|
1744
|
+
_runSchema,
|
|
1745
|
+
_getWatch,
|
|
1746
|
+
_getDirty,
|
|
1747
|
+
_setValid,
|
|
1748
|
+
_setFieldArray,
|
|
1749
|
+
_setDisabledField,
|
|
1750
|
+
_setErrors,
|
|
1751
|
+
_getFieldArray,
|
|
1752
|
+
_reset,
|
|
1753
|
+
_resetDefaultValues,
|
|
1754
|
+
_removeUnmounted,
|
|
1755
|
+
_disableForm,
|
|
1756
|
+
_subjects,
|
|
1757
|
+
_proxyFormState,
|
|
1758
|
+
get _fields() {
|
|
1759
|
+
return _fields;
|
|
1760
|
+
},
|
|
1761
|
+
get _formValues() {
|
|
1762
|
+
return _formValues;
|
|
1763
|
+
},
|
|
1764
|
+
get _state() {
|
|
1765
|
+
return _state;
|
|
1766
|
+
},
|
|
1767
|
+
set _state(value) {
|
|
1768
|
+
_state = value;
|
|
1769
|
+
},
|
|
1770
|
+
get _defaultValues() {
|
|
1771
|
+
return _defaultValues;
|
|
1772
|
+
},
|
|
1773
|
+
get _names() {
|
|
1774
|
+
return _names;
|
|
1775
|
+
},
|
|
1776
|
+
set _names(value) {
|
|
1777
|
+
_names = value;
|
|
1778
|
+
},
|
|
1779
|
+
get _formState() {
|
|
1780
|
+
return _formState;
|
|
1781
|
+
},
|
|
1782
|
+
get _options() {
|
|
1783
|
+
return _options;
|
|
1784
|
+
},
|
|
1785
|
+
set _options(value) {
|
|
1786
|
+
_options = {
|
|
1787
|
+
..._options,
|
|
1788
|
+
...value,
|
|
1789
|
+
};
|
|
1790
|
+
},
|
|
1791
|
+
},
|
|
1792
|
+
subscribe,
|
|
1793
|
+
trigger,
|
|
1794
|
+
register,
|
|
1795
|
+
handleSubmit,
|
|
1796
|
+
watch,
|
|
1797
|
+
setValue,
|
|
1798
|
+
getValues,
|
|
1799
|
+
reset,
|
|
1800
|
+
resetField,
|
|
1801
|
+
clearErrors,
|
|
1802
|
+
unregister,
|
|
1803
|
+
setError,
|
|
1804
|
+
setFocus,
|
|
1805
|
+
getFieldState,
|
|
1806
|
+
id,
|
|
1807
|
+
submit,
|
|
1808
|
+
};
|
|
1809
|
+
return {
|
|
1810
|
+
...methods,
|
|
1811
|
+
formControl: methods,
|
|
1812
|
+
};
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
export { appendErrors, createFormControl, get, set, submitForm as submit };
|
|
1816
|
+
//# sourceMappingURL=react-server.esm.mjs.map
|