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