react-hook-form 6.9.2 → 6.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -42
- package/dist/index.cjs.development.js +171 -144
- package/dist/index.cjs.development.js.map +1 -1
- package/dist/index.cjs.production.min.js +1 -1
- package/dist/index.cjs.production.min.js.map +1 -1
- package/dist/index.esm.js +172 -146
- package/dist/index.esm.js.map +1 -1
- package/dist/index.ie11.development.js +199 -170
- package/dist/index.ie11.development.js.map +1 -1
- package/dist/index.ie11.production.min.js +1 -1
- package/dist/index.ie11.production.min.js.map +1 -1
- package/dist/index.umd.development.js +171 -144
- package/dist/index.umd.development.js.map +1 -1
- package/dist/index.umd.production.min.js +1 -1
- package/dist/index.umd.production.min.js.map +1 -1
- package/dist/logic/transformToNestObject.d.ts +1 -1
- package/dist/types/fieldArray.d.ts +9 -0
- package/dist/types/form.d.ts +3 -5
- package/dist/types/utils.d.ts +4 -4
- package/dist/useFieldArray.d.ts +2 -10
- package/dist/utils/cloneObject.d.ts +1 -0
- package/dist/utils/{filterBooleanArray.test.d.ts → cloneObject.test.d.ts} +0 -0
- package/dist/utils/{filterOutFalsy.d.ts → compact.d.ts} +0 -0
- package/dist/utils/{filterOutFalsy.test.d.ts → compact.test.d.ts} +0 -0
- package/dist/utils/{filterBooleanArray.d.ts → fillBooleanArray.d.ts} +0 -0
- package/dist/utils/{isArray.test.d.ts → fillBooleanArray.test.d.ts} +0 -0
- package/package.json +1 -1
- package/dist/utils/isArray.d.ts +0 -2
@@ -154,30 +154,28 @@ function attachEventListeners(_a, shouldAttachChangeEvent, handleChange) {
|
|
154
154
|
|
155
155
|
var isNullOrUndefined = (function (value) { return value == null; });
|
156
156
|
|
157
|
-
var isArray = (function (value) { return Array.isArray(value); });
|
158
|
-
|
159
157
|
var isObjectType = function (value) { return typeof value === 'object'; };
|
160
158
|
var isObject = (function (value) {
|
161
159
|
return !isNullOrUndefined(value) &&
|
162
|
-
!isArray(value) &&
|
160
|
+
!Array.isArray(value) &&
|
163
161
|
isObjectType(value) &&
|
164
162
|
!(value instanceof Date);
|
165
163
|
});
|
166
164
|
|
167
165
|
var isKey = (function (value) {
|
168
|
-
return !isArray(value) &&
|
166
|
+
return !Array.isArray(value) &&
|
169
167
|
(/^\w*$/.test(value) ||
|
170
168
|
!/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/.test(value));
|
171
169
|
});
|
172
170
|
|
171
|
+
var compact = (function (value) { return value.filter(Boolean); });
|
172
|
+
|
173
173
|
var stringToPath = (function (input) {
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
});
|
180
|
-
return result;
|
174
|
+
return compact(input
|
175
|
+
.replace(/["|']/g, '')
|
176
|
+
.replace(/\[/g, '.')
|
177
|
+
.replace(/\]/g, '')
|
178
|
+
.split('.'));
|
181
179
|
});
|
182
180
|
|
183
181
|
function set(object, path, value) {
|
@@ -191,7 +189,7 @@ function set(object, path, value) {
|
|
191
189
|
if (index !== lastIndex) {
|
192
190
|
var objValue = object[key];
|
193
191
|
newValue =
|
194
|
-
isObject(objValue) || isArray(objValue)
|
192
|
+
isObject(objValue) || Array.isArray(objValue)
|
195
193
|
? objValue
|
196
194
|
: !isNaN(+tempPath[index + 1])
|
197
195
|
? []
|
@@ -203,24 +201,18 @@ function set(object, path, value) {
|
|
203
201
|
return object;
|
204
202
|
}
|
205
203
|
|
206
|
-
var transformToNestObject = (function (data) {
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
return previous;
|
213
|
-
}
|
214
|
-
return __assign(__assign({}, previous), (_b = {}, _b[key] = value, _b));
|
215
|
-
}, {});
|
204
|
+
var transformToNestObject = (function (data, value) {
|
205
|
+
if (value === void 0) { value = {}; }
|
206
|
+
for (var key in data) {
|
207
|
+
!isKey(key) ? set(value, key, data[key]) : (value[key] = data[key]);
|
208
|
+
}
|
209
|
+
return value;
|
216
210
|
});
|
217
211
|
|
218
212
|
var isUndefined = (function (val) { return val === undefined; });
|
219
213
|
|
220
|
-
var filterOutFalsy = (function (value) { return value.filter(Boolean); });
|
221
|
-
|
222
214
|
var get = (function (obj, path, defaultValue) {
|
223
|
-
var result =
|
215
|
+
var result = compact(path.split(/[,[\].]+?/)).reduce(function (result, key) { return (isNullOrUndefined(result) ? result : result[key]); }, obj);
|
224
216
|
return isUndefined(result) || result === obj
|
225
217
|
? isUndefined(obj[path])
|
226
218
|
? defaultValue
|
@@ -259,7 +251,7 @@ var defaultReturn = {
|
|
259
251
|
value: '',
|
260
252
|
};
|
261
253
|
var getRadioValue = (function (options) {
|
262
|
-
return isArray(options)
|
254
|
+
return Array.isArray(options)
|
263
255
|
? options.reduce(function (previous, option) {
|
264
256
|
return option && option.ref.checked
|
265
257
|
? {
|
@@ -304,7 +296,7 @@ var defaultResult = {
|
|
304
296
|
};
|
305
297
|
var validResult = { value: true, isValid: true };
|
306
298
|
var getCheckboxValue = (function (options) {
|
307
|
-
if (isArray(options)) {
|
299
|
+
if (Array.isArray(options)) {
|
308
300
|
if (options.length > 1) {
|
309
301
|
var values = options
|
310
302
|
.filter(function (option) { return option && option.ref.checked; })
|
@@ -399,7 +391,7 @@ function unset(object, path) {
|
|
399
391
|
objectRef = objectRef ? objectRef[item] : object[item];
|
400
392
|
if (currentPathsLength === index &&
|
401
393
|
((isObject(objectRef) && isEmptyObject(objectRef)) ||
|
402
|
-
(isArray(objectRef) &&
|
394
|
+
(Array.isArray(objectRef) &&
|
403
395
|
!objectRef.filter(function (data) {
|
404
396
|
return (isObject(data) && !isEmptyObject(data)) || isBoolean(data);
|
405
397
|
}).length))) {
|
@@ -429,15 +421,15 @@ function findRemovedFieldAndRemoveListener(fieldsRef, handleChange, field, shall
|
|
429
421
|
}
|
430
422
|
if ((isRadioInput(ref) || isCheckBoxInput(ref)) && fieldRef) {
|
431
423
|
var options_1 = fieldRef.options;
|
432
|
-
if (isArray(options_1) && options_1.length) {
|
433
|
-
|
424
|
+
if (Array.isArray(options_1) && options_1.length) {
|
425
|
+
compact(options_1).forEach(function (option, index) {
|
434
426
|
var ref = option.ref;
|
435
427
|
if ((ref && isDetached(ref) && isSameRef(option, ref)) || forceDelete) {
|
436
428
|
removeAllEventListeners(ref, handleChange);
|
437
429
|
unset(options_1, "[" + index + "]");
|
438
430
|
}
|
439
431
|
});
|
440
|
-
if (options_1 && !
|
432
|
+
if (options_1 && !compact(options_1).length) {
|
441
433
|
delete fieldsRef.current[name];
|
442
434
|
}
|
443
435
|
}
|
@@ -456,7 +448,7 @@ function setFieldArrayDirtyFields(values, defaultValues, dirtyFields, parentNode
|
|
456
448
|
var index = -1;
|
457
449
|
while (++index < values.length) {
|
458
450
|
for (var key in values[index]) {
|
459
|
-
if (isArray(values[index][key])) {
|
451
|
+
if (Array.isArray(values[index][key])) {
|
460
452
|
!dirtyFields[index] && (dirtyFields[index] = {});
|
461
453
|
dirtyFields[index][key] = [];
|
462
454
|
setFieldArrayDirtyFields(values[index][key], get(defaultValues[index] || {}, key, []), dirtyFields[index][key], dirtyFields[index], key);
|
@@ -488,13 +480,11 @@ function deepMerge(target, source) {
|
|
488
480
|
var targetValue = target[key];
|
489
481
|
var sourceValue = source[key];
|
490
482
|
try {
|
491
|
-
|
492
|
-
(
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
target[key] = sourceValue;
|
497
|
-
}
|
483
|
+
target[key] =
|
484
|
+
(isObject(targetValue) && isObject(sourceValue)) ||
|
485
|
+
(Array.isArray(targetValue) && Array.isArray(sourceValue))
|
486
|
+
? deepMerge(targetValue, sourceValue)
|
487
|
+
: sourceValue;
|
498
488
|
}
|
499
489
|
catch (_a) { }
|
500
490
|
}
|
@@ -507,7 +497,7 @@ var getFieldsValues = (function (fieldsRef, shallowFieldsStateRef, excludeDisabl
|
|
507
497
|
if (isUndefined(search) ||
|
508
498
|
(isString(search)
|
509
499
|
? name_1.startsWith(search)
|
510
|
-
: isArray(search) && search.find(function (data) { return name_1.startsWith(data); }))) {
|
500
|
+
: Array.isArray(search) && search.find(function (data) { return name_1.startsWith(data); }))) {
|
511
501
|
output[name_1] = getFieldValue(fieldsRef, name_1, undefined, excludeDisabled);
|
512
502
|
}
|
513
503
|
};
|
@@ -532,7 +522,8 @@ function deepEqual(object1, object2, isErrorObject) {
|
|
532
522
|
if (!(isErrorObject && ['ref', 'context'].includes(key))) {
|
533
523
|
var val1 = object1[key];
|
534
524
|
var val2 = object2[key];
|
535
|
-
if ((isObject(val1) || isArray(val1)) &&
|
525
|
+
if ((isObject(val1) || Array.isArray(val1)) &&
|
526
|
+
(isObject(val2) || Array.isArray(val2))
|
536
527
|
? !deepEqual(val1, val2, isErrorObject)
|
537
528
|
: val1 !== val2) {
|
538
529
|
return false;
|
@@ -782,7 +773,7 @@ var assignWatchFields = (function (fieldValues, fieldName, watchFields, inputVal
|
|
782
773
|
}
|
783
774
|
else {
|
784
775
|
value = get(fieldValues, fieldName);
|
785
|
-
if (isObject(value) || isArray(value)) {
|
776
|
+
if (isObject(value) || Array.isArray(value)) {
|
786
777
|
getPath(fieldName, value).forEach(function (name) { return watchFields.add(name); });
|
787
778
|
}
|
788
779
|
}
|
@@ -864,6 +855,22 @@ function onDomRemove(fieldsRef, removeFieldEventListenerAndRef) {
|
|
864
855
|
return observer;
|
865
856
|
}
|
866
857
|
|
858
|
+
function cloneObject(object) {
|
859
|
+
var copy;
|
860
|
+
if (isPrimitive(object)) {
|
861
|
+
return object;
|
862
|
+
}
|
863
|
+
if (object instanceof Date) {
|
864
|
+
copy = new Date(object.getTime());
|
865
|
+
return copy;
|
866
|
+
}
|
867
|
+
copy = Array.isArray(object) ? [] : {};
|
868
|
+
for (var key in object) {
|
869
|
+
copy[key] = cloneObject(object[key]);
|
870
|
+
}
|
871
|
+
return copy;
|
872
|
+
}
|
873
|
+
|
867
874
|
var modeChecker = (function (mode) { return ({
|
868
875
|
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
869
876
|
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
@@ -896,7 +903,7 @@ function useForm(_a) {
|
|
896
903
|
var isUnMount = React.useRef(false);
|
897
904
|
var isWatchAllRef = React.useRef(false);
|
898
905
|
var handleChangeRef = React.useRef();
|
899
|
-
var shallowFieldsStateRef = React.useRef(shouldUnregister ? {} :
|
906
|
+
var shallowFieldsStateRef = React.useRef(shouldUnregister ? {} : cloneObject(defaultValues));
|
900
907
|
var resetFieldArrayFunctionRef = React.useRef({});
|
901
908
|
var contextRef = React.useRef(context);
|
902
909
|
var resolverRef = React.useRef(resolver);
|
@@ -988,7 +995,7 @@ function useForm(_a) {
|
|
988
995
|
options.length > 1
|
989
996
|
? options.forEach(function (_a) {
|
990
997
|
var checkboxRef = _a.ref;
|
991
|
-
return (checkboxRef.checked = isArray(value)
|
998
|
+
return (checkboxRef.checked = Array.isArray(value)
|
992
999
|
? !!value.find(function (data) { return data === checkboxRef.value; })
|
993
1000
|
: value === checkboxRef.value);
|
994
1001
|
})
|
@@ -1001,7 +1008,7 @@ function useForm(_a) {
|
|
1001
1008
|
var isFormDirty = function () {
|
1002
1009
|
return !deepEqual(getValues(), isEmptyObject(defaultValuesRef.current)
|
1003
1010
|
? defaultValuesAtRenderRef.current
|
1004
|
-
: defaultValuesRef.current)
|
1011
|
+
: defaultValuesRef.current);
|
1005
1012
|
};
|
1006
1013
|
var updateAndGetDirtyState = React.useCallback(function (name, shouldRender) {
|
1007
1014
|
if (shouldRender === void 0) { shouldRender = true; }
|
@@ -1053,7 +1060,7 @@ function useForm(_a) {
|
|
1053
1060
|
case 1:
|
1054
1061
|
errors = (_a.sent()).errors;
|
1055
1062
|
previousFormIsValid = formStateRef.current.isValid;
|
1056
|
-
if (isArray(names)) {
|
1063
|
+
if (Array.isArray(names)) {
|
1057
1064
|
isInputsValid = names
|
1058
1065
|
.map(function (name) {
|
1059
1066
|
var error = get(errors, name);
|
@@ -1087,7 +1094,7 @@ function useForm(_a) {
|
|
1087
1094
|
if (resolverRef.current) {
|
1088
1095
|
return [2 /*return*/, executeSchemaOrResolverValidation(fields)];
|
1089
1096
|
}
|
1090
|
-
if (!isArray(fields)) return [3 /*break*/, 2];
|
1097
|
+
if (!Array.isArray(fields)) return [3 /*break*/, 2];
|
1091
1098
|
return [4 /*yield*/, Promise.all(fields.map(function (data) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
1092
1099
|
switch (_a.label) {
|
1093
1100
|
case 0: return [4 /*yield*/, executeValidation(data, null)];
|
@@ -1140,8 +1147,9 @@ function useForm(_a) {
|
|
1140
1147
|
resetFieldArrayFunctionRef.current[name]((_a = {},
|
1141
1148
|
_a[name] = value,
|
1142
1149
|
_a));
|
1143
|
-
if (readFormStateRef.current.isDirty ||
|
1144
|
-
readFormStateRef.current.dirtyFields)
|
1150
|
+
if ((readFormStateRef.current.isDirty ||
|
1151
|
+
readFormStateRef.current.dirtyFields) &&
|
1152
|
+
config.shouldDirty) {
|
1145
1153
|
set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(value, get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
|
1146
1154
|
updateFormState({
|
1147
1155
|
isDirty: !deepEqual(__assign(__assign({}, getValues()), (_b = {}, _b[name] = value, _b)), defaultValuesRef.current),
|
@@ -1235,11 +1243,12 @@ function useForm(_a) {
|
|
1235
1243
|
function setFieldArrayDefaultValues(data) {
|
1236
1244
|
var e_2, _a, _b;
|
1237
1245
|
if (!shouldUnregister) {
|
1246
|
+
var copy = cloneObject(data);
|
1238
1247
|
try {
|
1239
1248
|
for (var _c = __values(fieldArrayNamesRef.current), _d = _c.next(); !_d.done; _d = _c.next()) {
|
1240
1249
|
var value = _d.value;
|
1241
|
-
if (isKey(value) && !
|
1242
|
-
|
1250
|
+
if (isKey(value) && !copy[value]) {
|
1251
|
+
copy = __assign(__assign({}, copy), (_b = {}, _b[value] = [], _b));
|
1243
1252
|
}
|
1244
1253
|
}
|
1245
1254
|
}
|
@@ -1250,6 +1259,7 @@ function useForm(_a) {
|
|
1250
1259
|
}
|
1251
1260
|
finally { if (e_2) throw e_2.error; }
|
1252
1261
|
}
|
1262
|
+
return copy;
|
1253
1263
|
}
|
1254
1264
|
return data;
|
1255
1265
|
}
|
@@ -1258,7 +1268,7 @@ function useForm(_a) {
|
|
1258
1268
|
if (isString(payload)) {
|
1259
1269
|
return getFieldValue(fieldsRef, payload, shallowFieldsStateRef);
|
1260
1270
|
}
|
1261
|
-
if (isArray(payload)) {
|
1271
|
+
if (Array.isArray(payload)) {
|
1262
1272
|
var data = {};
|
1263
1273
|
try {
|
1264
1274
|
for (var payload_1 = __values(payload), payload_1_1 = payload_1.next(); !payload_1_1.done; payload_1_1 = payload_1.next()) {
|
@@ -1299,10 +1309,37 @@ function useForm(_a) {
|
|
1299
1309
|
var removeFieldEventListener = React.useCallback(function (field, forceDelete) {
|
1300
1310
|
return findRemovedFieldAndRemoveListener(fieldsRef, handleChangeRef.current, field, shallowFieldsStateRef, shouldUnregister, forceDelete);
|
1301
1311
|
}, [shouldUnregister]);
|
1312
|
+
var updateWatchedValue = function (name) {
|
1313
|
+
var e_4, _a;
|
1314
|
+
if (isWatchAllRef.current) {
|
1315
|
+
updateFormState();
|
1316
|
+
}
|
1317
|
+
else if (watchFieldsRef) {
|
1318
|
+
var shouldRenderUseWatch = true;
|
1319
|
+
try {
|
1320
|
+
for (var _b = __values(watchFieldsRef.current), _c = _b.next(); !_c.done; _c = _b.next()) {
|
1321
|
+
var watchField = _c.value;
|
1322
|
+
if (watchField.startsWith(name)) {
|
1323
|
+
updateFormState();
|
1324
|
+
shouldRenderUseWatch = false;
|
1325
|
+
break;
|
1326
|
+
}
|
1327
|
+
}
|
1328
|
+
}
|
1329
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
1330
|
+
finally {
|
1331
|
+
try {
|
1332
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
1333
|
+
}
|
1334
|
+
finally { if (e_4) throw e_4.error; }
|
1335
|
+
}
|
1336
|
+
shouldRenderUseWatch && renderWatchedInputs(name);
|
1337
|
+
}
|
1338
|
+
};
|
1302
1339
|
var removeFieldEventListenerAndRef = React.useCallback(function (field, forceDelete) {
|
1303
1340
|
if (field) {
|
1304
1341
|
removeFieldEventListener(field, forceDelete);
|
1305
|
-
if (shouldUnregister && !
|
1342
|
+
if (shouldUnregister && !compact(field.options || []).length) {
|
1306
1343
|
unset(defaultValuesAtRenderRef.current, field.ref.name);
|
1307
1344
|
unset(validFieldsRef.current, field.ref.name);
|
1308
1345
|
unset(fieldsWithValidationRef.current, field.ref.name);
|
@@ -1314,12 +1351,13 @@ function useForm(_a) {
|
|
1314
1351
|
dirtyFields: formStateRef.current.dirtyFields,
|
1315
1352
|
});
|
1316
1353
|
resolverRef.current && validateResolver();
|
1354
|
+
updateWatchedValue(field.ref.name);
|
1317
1355
|
}
|
1318
1356
|
}
|
1319
1357
|
}, [validateResolver, removeFieldEventListener]);
|
1320
1358
|
function clearErrors(name) {
|
1321
1359
|
name &&
|
1322
|
-
(isArray(name) ? name : [name]).forEach(function (inputName) {
|
1360
|
+
(Array.isArray(name) ? name : [name]).forEach(function (inputName) {
|
1323
1361
|
return fieldsRef.current[inputName]
|
1324
1362
|
? isKey(inputName)
|
1325
1363
|
? delete formStateRef.current.errors[inputName]
|
@@ -1352,7 +1390,7 @@ function useForm(_a) {
|
|
1352
1390
|
? get(combinedDefaultValues, fieldNames)
|
1353
1391
|
: defaultValue, true);
|
1354
1392
|
}
|
1355
|
-
if (isArray(fieldNames)) {
|
1393
|
+
if (Array.isArray(fieldNames)) {
|
1356
1394
|
return fieldNames.reduce(function (previous, name) {
|
1357
1395
|
var _a;
|
1358
1396
|
return (__assign(__assign({}, previous), (_a = {}, _a[name] = assignWatchFields(fieldValues, name, watchFields, combinedDefaultValues), _a)));
|
@@ -1366,19 +1404,19 @@ function useForm(_a) {
|
|
1366
1404
|
return watchInternal(fieldNames, defaultValue);
|
1367
1405
|
}
|
1368
1406
|
function unregister(name) {
|
1369
|
-
var
|
1407
|
+
var e_5, _a;
|
1370
1408
|
try {
|
1371
|
-
for (var _b = __values(isArray(name) ? name : [name]), _c = _b.next(); !_c.done; _c = _b.next()) {
|
1409
|
+
for (var _b = __values(Array.isArray(name) ? name : [name]), _c = _b.next(); !_c.done; _c = _b.next()) {
|
1372
1410
|
var fieldName = _c.value;
|
1373
1411
|
removeFieldEventListenerAndRef(fieldsRef.current[fieldName], true);
|
1374
1412
|
}
|
1375
1413
|
}
|
1376
|
-
catch (
|
1414
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
1377
1415
|
finally {
|
1378
1416
|
try {
|
1379
1417
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
1380
1418
|
}
|
1381
|
-
finally { if (
|
1419
|
+
finally { if (e_5) throw e_5.error; }
|
1382
1420
|
}
|
1383
1421
|
}
|
1384
1422
|
function registerFieldRef(ref, validateOptions) {
|
@@ -1407,8 +1445,8 @@ function useForm(_a) {
|
|
1407
1445
|
var defaultValue;
|
1408
1446
|
if (field &&
|
1409
1447
|
(isRadioOrCheckbox
|
1410
|
-
? isArray(field.options) &&
|
1411
|
-
|
1448
|
+
? Array.isArray(field.options) &&
|
1449
|
+
compact(field.options).find(function (option) {
|
1412
1450
|
return value === option.ref.value && compareRef(option.ref);
|
1413
1451
|
})
|
1414
1452
|
: compareRef(field.ref))) {
|
@@ -1417,7 +1455,7 @@ function useForm(_a) {
|
|
1417
1455
|
}
|
1418
1456
|
if (type) {
|
1419
1457
|
field = isRadioOrCheckbox
|
1420
|
-
? __assign({ options: __spread(
|
1458
|
+
? __assign({ options: __spread(compact((field && field.options) || []), [
|
1421
1459
|
{
|
1422
1460
|
ref: ref,
|
1423
1461
|
},
|
@@ -1483,8 +1521,8 @@ function useForm(_a) {
|
|
1483
1521
|
}
|
1484
1522
|
}
|
1485
1523
|
var handleSubmit = React.useCallback(function (onValid, onInvalid) { return function (e) { return __awaiter(_this, void 0, void 0, function () {
|
1486
|
-
var fieldErrors, fieldValues, _a, errors, values, _b, _c, field, name_2, fieldError,
|
1487
|
-
var
|
1524
|
+
var fieldErrors, fieldValues, _a, errors, values, _b, _c, field, name_2, fieldError, e_6_1, _d;
|
1525
|
+
var e_6, _e;
|
1488
1526
|
return __generator(this, function (_f) {
|
1489
1527
|
switch (_f.label) {
|
1490
1528
|
case 0:
|
@@ -1494,11 +1532,10 @@ function useForm(_a) {
|
|
1494
1532
|
}
|
1495
1533
|
fieldErrors = {};
|
1496
1534
|
fieldValues = setFieldArrayDefaultValues(getFieldsValues(fieldsRef, shallowFieldsStateRef, true));
|
1497
|
-
|
1535
|
+
readFormStateRef.current.isSubmitting &&
|
1498
1536
|
updateFormState({
|
1499
1537
|
isSubmitting: true,
|
1500
1538
|
});
|
1501
|
-
}
|
1502
1539
|
_f.label = 1;
|
1503
1540
|
case 1:
|
1504
1541
|
_f.trys.push([1, , 16, 17]);
|
@@ -1506,8 +1543,7 @@ function useForm(_a) {
|
|
1506
1543
|
return [4 /*yield*/, resolverRef.current(fieldValues, contextRef.current, isValidateAllFieldCriteria)];
|
1507
1544
|
case 2:
|
1508
1545
|
_a = _f.sent(), errors = _a.errors, values = _a.values;
|
1509
|
-
formStateRef.current.errors = errors;
|
1510
|
-
fieldErrors = errors;
|
1546
|
+
formStateRef.current.errors = fieldErrors = errors;
|
1511
1547
|
fieldValues = values;
|
1512
1548
|
return [3 /*break*/, 10];
|
1513
1549
|
case 3:
|
@@ -1536,14 +1572,14 @@ function useForm(_a) {
|
|
1536
1572
|
return [3 /*break*/, 4];
|
1537
1573
|
case 7: return [3 /*break*/, 10];
|
1538
1574
|
case 8:
|
1539
|
-
|
1540
|
-
|
1575
|
+
e_6_1 = _f.sent();
|
1576
|
+
e_6 = { error: e_6_1 };
|
1541
1577
|
return [3 /*break*/, 10];
|
1542
1578
|
case 9:
|
1543
1579
|
try {
|
1544
1580
|
if (_c && !_c.done && (_e = _b.return)) _e.call(_b);
|
1545
1581
|
}
|
1546
|
-
finally { if (
|
1582
|
+
finally { if (e_6) throw e_6.error; }
|
1547
1583
|
return [7 /*endfinally*/];
|
1548
1584
|
case 10:
|
1549
1585
|
if (!(isEmptyObject(fieldErrors) &&
|
@@ -1604,7 +1640,7 @@ function useForm(_a) {
|
|
1604
1640
|
});
|
1605
1641
|
};
|
1606
1642
|
var reset = function (values, omitResetState) {
|
1607
|
-
var
|
1643
|
+
var e_7, _a;
|
1608
1644
|
if (omitResetState === void 0) { omitResetState = {}; }
|
1609
1645
|
if (isWeb) {
|
1610
1646
|
try {
|
@@ -1612,7 +1648,7 @@ function useForm(_a) {
|
|
1612
1648
|
var field = _c.value;
|
1613
1649
|
if (field) {
|
1614
1650
|
var ref = field.ref, options = field.options;
|
1615
|
-
var inputRef = isRadioOrCheckboxFunction(ref) && isArray(options)
|
1651
|
+
var inputRef = isRadioOrCheckboxFunction(ref) && Array.isArray(options)
|
1616
1652
|
? options[0].ref
|
1617
1653
|
: ref;
|
1618
1654
|
if (isHTMLElement(inputRef)) {
|
@@ -1625,30 +1661,30 @@ function useForm(_a) {
|
|
1625
1661
|
}
|
1626
1662
|
}
|
1627
1663
|
}
|
1628
|
-
catch (
|
1664
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
1629
1665
|
finally {
|
1630
1666
|
try {
|
1631
1667
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
1632
1668
|
}
|
1633
|
-
finally { if (
|
1669
|
+
finally { if (e_7) throw e_7.error; }
|
1634
1670
|
}
|
1635
1671
|
}
|
1636
1672
|
fieldsRef.current = {};
|
1637
|
-
defaultValuesRef.current =
|
1638
|
-
|
1639
|
-
renderWatchedInputs('');
|
1640
|
-
}
|
1641
|
-
shallowFieldsStateRef.current = shouldUnregister ? {} : __assign({}, values) || {};
|
1673
|
+
defaultValuesRef.current = cloneObject(values || defaultValuesRef.current);
|
1674
|
+
values && renderWatchedInputs('');
|
1642
1675
|
Object.values(resetFieldArrayFunctionRef.current).forEach(function (resetFieldArray) { return isFunction(resetFieldArray) && resetFieldArray(); });
|
1676
|
+
shallowFieldsStateRef.current = shouldUnregister
|
1677
|
+
? {}
|
1678
|
+
: cloneObject(values) || {};
|
1643
1679
|
resetRefs(omitResetState);
|
1644
1680
|
};
|
1645
|
-
observerRef.current =
|
1646
|
-
observerRef.current || !isWeb
|
1647
|
-
? observerRef.current
|
1648
|
-
: onDomRemove(fieldsRef, removeFieldEventListenerAndRef);
|
1649
1681
|
React.useEffect(function () {
|
1650
1682
|
isUnMount.current = false;
|
1651
1683
|
resolver && readFormStateRef.current.isValid && validateResolver();
|
1684
|
+
observerRef.current =
|
1685
|
+
observerRef.current || !isWeb
|
1686
|
+
? observerRef.current
|
1687
|
+
: onDomRemove(fieldsRef, removeFieldEventListenerAndRef);
|
1652
1688
|
return function () {
|
1653
1689
|
isUnMount.current = true;
|
1654
1690
|
observerRef.current && observerRef.current.disconnect();
|
@@ -1670,15 +1706,13 @@ function useForm(_a) {
|
|
1670
1706
|
register: React.useCallback(register, [defaultValuesRef.current]),
|
1671
1707
|
unregister: React.useCallback(unregister, []),
|
1672
1708
|
};
|
1673
|
-
var control = __assign({
|
1709
|
+
var control = __assign({ updateWatchedValue: updateWatchedValue,
|
1674
1710
|
shouldUnregister: shouldUnregister,
|
1675
1711
|
removeFieldEventListener: removeFieldEventListener,
|
1676
1712
|
watchInternal: watchInternal, mode: modeRef.current, reValidateMode: {
|
1677
1713
|
isReValidateOnBlur: isReValidateOnBlur,
|
1678
1714
|
isReValidateOnChange: isReValidateOnChange,
|
1679
1715
|
}, fieldsRef: fieldsRef,
|
1680
|
-
isWatchAllRef: isWatchAllRef,
|
1681
|
-
watchFieldsRef: watchFieldsRef,
|
1682
1716
|
resetFieldArrayFunctionRef: resetFieldArrayFunctionRef,
|
1683
1717
|
useWatchFieldsRef: useWatchFieldsRef,
|
1684
1718
|
useWatchRenderFunctionsRef: useWatchRenderFunctionsRef,
|
@@ -1736,18 +1770,18 @@ function removeAtIndexes(data, index) {
|
|
1736
1770
|
delete data[k];
|
1737
1771
|
}
|
1738
1772
|
}
|
1739
|
-
return
|
1773
|
+
return compact(data);
|
1740
1774
|
}
|
1741
1775
|
var removeArrayAt = (function (data, index) {
|
1742
1776
|
return isUndefined(index)
|
1743
1777
|
? []
|
1744
|
-
: isArray(index)
|
1778
|
+
: Array.isArray(index)
|
1745
1779
|
? removeAtIndexes(data, index)
|
1746
1780
|
: removeAt(data, index);
|
1747
1781
|
});
|
1748
1782
|
|
1749
1783
|
var moveArrayAt = (function (data, from, to) {
|
1750
|
-
if (isArray(data)) {
|
1784
|
+
if (Array.isArray(data)) {
|
1751
1785
|
if (isUndefined(data[to])) {
|
1752
1786
|
data[to] = undefined;
|
1753
1787
|
}
|
@@ -1764,15 +1798,15 @@ var swapArrayAt = (function (data, indexA, indexB) {
|
|
1764
1798
|
});
|
1765
1799
|
|
1766
1800
|
function prepend(data, value) {
|
1767
|
-
return __spread((isArray(value) ? value : [value || undefined]), data);
|
1801
|
+
return __spread((Array.isArray(value) ? value : [value || undefined]), data);
|
1768
1802
|
}
|
1769
1803
|
|
1770
1804
|
function insert(data, index, value) {
|
1771
|
-
return __spread(data.slice(0, index), (isArray(value) ? value : [value || undefined]), data.slice(index));
|
1805
|
+
return __spread(data.slice(0, index), (Array.isArray(value) ? value : [value || undefined]), data.slice(index));
|
1772
1806
|
}
|
1773
1807
|
|
1774
1808
|
var fillEmptyArray = (function (value) {
|
1775
|
-
return isArray(value) ? Array(value.length).fill(undefined) : undefined;
|
1809
|
+
return Array.isArray(value) ? Array(value.length).fill(undefined) : undefined;
|
1776
1810
|
});
|
1777
1811
|
|
1778
1812
|
function mapValueToBoolean(value) {
|
@@ -1785,15 +1819,18 @@ function mapValueToBoolean(value) {
|
|
1785
1819
|
}
|
1786
1820
|
return [true];
|
1787
1821
|
}
|
1788
|
-
var
|
1789
|
-
return (isArray(value) ? value : [value])
|
1822
|
+
var fillBooleanArray = (function (value) {
|
1823
|
+
return (Array.isArray(value) ? value : [value])
|
1824
|
+
.map(mapValueToBoolean)
|
1825
|
+
.flat();
|
1790
1826
|
});
|
1791
1827
|
|
1792
|
-
var
|
1793
|
-
|
1794
|
-
|
1828
|
+
var mapIds = function (values, keyName) {
|
1829
|
+
return values.map(function (value) {
|
1830
|
+
var _a;
|
1831
|
+
return (__assign((_a = {}, _a[keyName] = generateId(), _a), value));
|
1832
|
+
});
|
1795
1833
|
};
|
1796
|
-
var mapIds = function (data, keyName) { return (isArray(data) ? data : []).map(function (value) { return appendId(value, keyName); }); };
|
1797
1834
|
var useFieldArray = function (_a) {
|
1798
1835
|
var control = _a.control, name = _a.name, _b = _a.keyName, keyName = _b === void 0 ? 'id' : _b;
|
1799
1836
|
var methods = useFormContext();
|
@@ -1803,27 +1840,23 @@ var useFieldArray = function (_a) {
|
|
1803
1840
|
}
|
1804
1841
|
}
|
1805
1842
|
var focusIndexRef = React.useRef(-1);
|
1806
|
-
var _c = control || methods.control,
|
1843
|
+
var _c = control || methods.control, updateWatchedValue = _c.updateWatchedValue, resetFieldArrayFunctionRef = _c.resetFieldArrayFunctionRef, fieldArrayNamesRef = _c.fieldArrayNamesRef, fieldsRef = _c.fieldsRef, defaultValuesRef = _c.defaultValuesRef, removeFieldEventListener = _c.removeFieldEventListener, formStateRef = _c.formStateRef, shallowFieldsStateRef = _c.shallowFieldsStateRef, updateFormState = _c.updateFormState, readFormStateRef = _c.readFormStateRef, validFieldsRef = _c.validFieldsRef, fieldsWithValidationRef = _c.fieldsWithValidationRef, fieldArrayDefaultValuesRef = _c.fieldArrayDefaultValuesRef, validateResolver = _c.validateResolver, getValues = _c.getValues, shouldUnregister = _c.shouldUnregister;
|
1807
1844
|
var fieldArrayParentName = getFieldArrayParentName(name);
|
1808
|
-
var
|
1845
|
+
var memoizedDefaultValues = React.useRef(__spread((get(fieldArrayDefaultValuesRef.current, fieldArrayParentName)
|
1809
1846
|
? get(fieldArrayDefaultValuesRef.current, name, [])
|
1810
1847
|
: get(shouldUnregister
|
1811
1848
|
? defaultValuesRef.current
|
1812
|
-
: shallowFieldsStateRef.current, name, [])));
|
1813
|
-
var
|
1814
|
-
var _e = __read(React.useState(mapIds(memoizedDefaultValues.current, keyName)), 2), fields = _e[0], setFields = _e[1];
|
1849
|
+
: shallowFieldsStateRef.current, name, []))));
|
1850
|
+
var _d = __read(React.useState(mapIds(memoizedDefaultValues.current, keyName)), 2), fields = _d[0], setFields = _d[1];
|
1815
1851
|
var allFields = React.useRef(fields);
|
1816
1852
|
var getCurrentFieldsValues = function () {
|
1817
|
-
return get(getValues()
|
1853
|
+
return get(getValues(), name, allFields.current).map(function (item, index) { return (__assign(__assign({}, allFields.current[index]), item)); });
|
1818
1854
|
};
|
1819
1855
|
allFields.current = fields;
|
1820
1856
|
fieldArrayNamesRef.current.add(name);
|
1821
1857
|
if (!get(fieldArrayDefaultValuesRef.current, fieldArrayParentName)) {
|
1822
1858
|
set(fieldArrayDefaultValuesRef.current, fieldArrayParentName, get(defaultValuesRef.current, fieldArrayParentName));
|
1823
1859
|
}
|
1824
|
-
var appendValueWithKey = function (values) {
|
1825
|
-
return values.map(function (value) { return appendId(value, keyName); });
|
1826
|
-
};
|
1827
1860
|
var setFieldAndValidState = function (fieldsValues) {
|
1828
1861
|
setFields(fieldsValues);
|
1829
1862
|
if (readFormStateRef.current.isValid && validateResolver) {
|
@@ -1844,15 +1877,34 @@ var useFieldArray = function (_a) {
|
|
1844
1877
|
};
|
1845
1878
|
var resetFields = function () {
|
1846
1879
|
for (var key in fieldsRef.current) {
|
1847
|
-
|
1880
|
+
isMatchFieldArrayName(key, name) &&
|
1848
1881
|
removeFieldEventListener(fieldsRef.current[key], true);
|
1849
|
-
}
|
1850
1882
|
}
|
1851
1883
|
};
|
1852
1884
|
var cleanup = function (ref) {
|
1853
|
-
return !
|
1885
|
+
return !compact(get(ref, name, [])).length && unset(ref, name);
|
1854
1886
|
};
|
1855
|
-
var
|
1887
|
+
var updateDirtyFieldsWithDefaultValues = function (updatedFieldArrayValues) {
|
1888
|
+
var defaultFieldArrayValues = get(defaultValuesRef.current, name, []);
|
1889
|
+
var updateDirtyFieldsBaseOnDefaultValues = function (base, target) {
|
1890
|
+
var _a;
|
1891
|
+
for (var key in base) {
|
1892
|
+
for (var innerKey in base[key]) {
|
1893
|
+
if (innerKey !== keyName &&
|
1894
|
+
(!target[key] ||
|
1895
|
+
!base[key] ||
|
1896
|
+
base[key][innerKey] !== target[key][innerKey])) {
|
1897
|
+
set(formStateRef.current.dirtyFields, name + "[" + key + "]", __assign(__assign({}, get(formStateRef.current.dirtyFields, name + "[" + key + "]", {})), (_a = {}, _a[innerKey] = true, _a)));
|
1898
|
+
}
|
1899
|
+
}
|
1900
|
+
}
|
1901
|
+
};
|
1902
|
+
if (updatedFieldArrayValues) {
|
1903
|
+
updateDirtyFieldsBaseOnDefaultValues(defaultFieldArrayValues, updatedFieldArrayValues);
|
1904
|
+
updateDirtyFieldsBaseOnDefaultValues(updatedFieldArrayValues, defaultFieldArrayValues);
|
1905
|
+
}
|
1906
|
+
};
|
1907
|
+
var batchStateUpdate = function (method, args, updatedFieldValues, isDirty, shouldSet, shouldUpdateValid) {
|
1856
1908
|
if (isDirty === void 0) { isDirty = true; }
|
1857
1909
|
if (shouldSet === void 0) { shouldSet = true; }
|
1858
1910
|
if (shouldUpdateValid === void 0) { shouldUpdateValid = false; }
|
@@ -1865,21 +1917,23 @@ var useFieldArray = function (_a) {
|
|
1865
1917
|
shouldSet && set(fieldArrayDefaultValuesRef.current, name, output);
|
1866
1918
|
cleanup(fieldArrayDefaultValuesRef.current);
|
1867
1919
|
}
|
1868
|
-
if (isArray(get(formStateRef.current.errors, name))) {
|
1920
|
+
if (Array.isArray(get(formStateRef.current.errors, name))) {
|
1869
1921
|
var output = method(get(formStateRef.current.errors, name), args.argA, args.argB);
|
1870
1922
|
shouldSet && set(formStateRef.current.errors, name, output);
|
1871
1923
|
cleanup(formStateRef.current.errors);
|
1872
1924
|
}
|
1873
|
-
if (readFormStateRef.current.touched &&
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1925
|
+
if (readFormStateRef.current.touched &&
|
1926
|
+
get(formStateRef.current.touched, name)) {
|
1927
|
+
var output = method(get(formStateRef.current.touched, name), args.argA, args.argB);
|
1928
|
+
shouldSet && set(formStateRef.current.touched, name, output);
|
1929
|
+
cleanup(formStateRef.current.touched);
|
1877
1930
|
}
|
1878
1931
|
if (readFormStateRef.current.dirtyFields ||
|
1879
1932
|
readFormStateRef.current.isDirty) {
|
1880
|
-
var output = method(get(dirtyFields, name, []), args.argC, args.argD);
|
1881
|
-
shouldSet && set(dirtyFields, name, output);
|
1882
|
-
|
1933
|
+
var output = method(get(formStateRef.current.dirtyFields, name, []), args.argC, args.argD);
|
1934
|
+
shouldSet && set(formStateRef.current.dirtyFields, name, output);
|
1935
|
+
updateDirtyFieldsWithDefaultValues(updatedFieldValues);
|
1936
|
+
cleanup(formStateRef.current.dirtyFields);
|
1883
1937
|
}
|
1884
1938
|
if (shouldUpdateValid &&
|
1885
1939
|
readFormStateRef.current.isValid &&
|
@@ -1891,25 +1945,21 @@ var useFieldArray = function (_a) {
|
|
1891
1945
|
}
|
1892
1946
|
updateFormState({
|
1893
1947
|
errors: formStateRef.current.errors,
|
1894
|
-
dirtyFields: dirtyFields,
|
1948
|
+
dirtyFields: formStateRef.current.dirtyFields,
|
1895
1949
|
isDirty: isDirty,
|
1896
|
-
touched: touched,
|
1950
|
+
touched: formStateRef.current.touched,
|
1897
1951
|
});
|
1898
1952
|
};
|
1899
1953
|
var append = function (value, shouldFocus) {
|
1900
1954
|
if (shouldFocus === void 0) { shouldFocus = true; }
|
1901
|
-
|
1902
|
-
|
1903
|
-
: [appendId(value, keyName)])));
|
1955
|
+
var updateFormValues = __spread(allFields.current, mapIds(Array.isArray(value) ? value : [value], keyName));
|
1956
|
+
setFieldAndValidState(updateFormValues);
|
1904
1957
|
if (readFormStateRef.current.dirtyFields ||
|
1905
1958
|
readFormStateRef.current.isDirty) {
|
1906
|
-
|
1907
|
-
set(dirtyFields, name, __spread((allFields.current.length > dirtyInputs_1.length
|
1908
|
-
? (fillEmptyArray(allFields.current) || []).map(function (_, index) { return dirtyInputs_1[index]; })
|
1909
|
-
: dirtyInputs_1), filterBooleanArray(value)));
|
1959
|
+
updateDirtyFieldsWithDefaultValues(updateFormValues);
|
1910
1960
|
updateFormState({
|
1911
1961
|
isDirty: true,
|
1912
|
-
dirtyFields: dirtyFields,
|
1962
|
+
dirtyFields: formStateRef.current.dirtyFields,
|
1913
1963
|
});
|
1914
1964
|
}
|
1915
1965
|
if (!shouldUnregister) {
|
@@ -1922,35 +1972,38 @@ var useFieldArray = function (_a) {
|
|
1922
1972
|
var prepend$1 = function (value, shouldFocus) {
|
1923
1973
|
if (shouldFocus === void 0) { shouldFocus = true; }
|
1924
1974
|
var emptyArray = fillEmptyArray(value);
|
1925
|
-
|
1975
|
+
var updatedFieldArrayValues = prepend(getCurrentFieldsValues(), mapIds(Array.isArray(value) ? value : [value], keyName));
|
1976
|
+
setFieldAndValidState(updatedFieldArrayValues);
|
1926
1977
|
resetFields();
|
1927
1978
|
batchStateUpdate(prepend, {
|
1928
1979
|
argA: emptyArray,
|
1929
|
-
argC:
|
1930
|
-
});
|
1980
|
+
argC: fillBooleanArray(value),
|
1981
|
+
}, updatedFieldArrayValues);
|
1931
1982
|
focusIndexRef.current = shouldFocus ? 0 : -1;
|
1932
1983
|
};
|
1933
1984
|
var remove = function (index) {
|
1934
1985
|
var fieldValues = getCurrentFieldsValues();
|
1935
|
-
|
1986
|
+
var updatedFieldValues = removeArrayAt(fieldValues, index);
|
1987
|
+
setFieldAndValidState(updatedFieldValues);
|
1936
1988
|
resetFields();
|
1937
1989
|
batchStateUpdate(removeArrayAt, {
|
1938
1990
|
argA: index,
|
1939
1991
|
argC: index,
|
1940
|
-
}, getIsDirtyState(removeArrayAt(fieldValues, index)), true, true);
|
1992
|
+
}, updatedFieldValues, getIsDirtyState(removeArrayAt(fieldValues, index)), true, true);
|
1941
1993
|
};
|
1942
1994
|
var insert$1 = function (index, value, shouldFocus) {
|
1943
1995
|
if (shouldFocus === void 0) { shouldFocus = true; }
|
1944
1996
|
var emptyArray = fillEmptyArray(value);
|
1945
1997
|
var fieldValues = getCurrentFieldsValues();
|
1946
|
-
|
1998
|
+
var updatedFieldArrayValues = insert(fieldValues, index, mapIds(Array.isArray(value) ? value : [value], keyName));
|
1999
|
+
setFieldAndValidState(updatedFieldArrayValues);
|
1947
2000
|
resetFields();
|
1948
2001
|
batchStateUpdate(insert, {
|
1949
2002
|
argA: index,
|
1950
2003
|
argB: emptyArray,
|
1951
2004
|
argC: index,
|
1952
|
-
argD:
|
1953
|
-
}, getIsDirtyState(insert(fieldValues, index)));
|
2005
|
+
argD: fillBooleanArray(value),
|
2006
|
+
}, updatedFieldArrayValues, getIsDirtyState(insert(fieldValues, index)));
|
1954
2007
|
focusIndexRef.current = shouldFocus ? index : -1;
|
1955
2008
|
};
|
1956
2009
|
var swap = function (indexA, indexB) {
|
@@ -1963,7 +2016,7 @@ var useFieldArray = function (_a) {
|
|
1963
2016
|
argB: indexB,
|
1964
2017
|
argC: indexA,
|
1965
2018
|
argD: indexB,
|
1966
|
-
}, getIsDirtyState(fieldValues), false);
|
2019
|
+
}, undefined, getIsDirtyState(fieldValues), false);
|
1967
2020
|
};
|
1968
2021
|
var move = function (from, to) {
|
1969
2022
|
var fieldValues = getCurrentFieldsValues();
|
@@ -1975,7 +2028,7 @@ var useFieldArray = function (_a) {
|
|
1975
2028
|
argB: to,
|
1976
2029
|
argC: from,
|
1977
2030
|
argD: to,
|
1978
|
-
}, getIsDirtyState(fieldValues), false);
|
2031
|
+
}, undefined, getIsDirtyState(fieldValues), false);
|
1979
2032
|
};
|
1980
2033
|
var reset = function (data) {
|
1981
2034
|
resetFields();
|
@@ -1985,7 +2038,6 @@ var useFieldArray = function (_a) {
|
|
1985
2038
|
setFields(mapIds(memoizedDefaultValues.current, keyName));
|
1986
2039
|
};
|
1987
2040
|
React.useEffect(function () {
|
1988
|
-
var e_1, _a;
|
1989
2041
|
{
|
1990
2042
|
if (!name) {
|
1991
2043
|
console.warn('📋 useFieldArray is missing `name` attribute. https://react-hook-form.com/api#useFieldArray');
|
@@ -1996,30 +2048,7 @@ var useFieldArray = function (_a) {
|
|
1996
2048
|
defaultValues.pop();
|
1997
2049
|
set(fieldArrayDefaultValuesRef.current, name, defaultValues);
|
1998
2050
|
}
|
1999
|
-
|
2000
|
-
updateFormState();
|
2001
|
-
}
|
2002
|
-
else if (watchFieldsRef) {
|
2003
|
-
var shouldRenderUseWatch = true;
|
2004
|
-
try {
|
2005
|
-
for (var _b = __values(watchFieldsRef.current), _c = _b.next(); !_c.done; _c = _b.next()) {
|
2006
|
-
var watchField = _c.value;
|
2007
|
-
if (watchField.startsWith(name)) {
|
2008
|
-
updateFormState();
|
2009
|
-
shouldRenderUseWatch = false;
|
2010
|
-
break;
|
2011
|
-
}
|
2012
|
-
}
|
2013
|
-
}
|
2014
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
2015
|
-
finally {
|
2016
|
-
try {
|
2017
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
2018
|
-
}
|
2019
|
-
finally { if (e_1) throw e_1.error; }
|
2020
|
-
}
|
2021
|
-
shouldRenderUseWatch && renderWatchedInputs(name);
|
2022
|
-
}
|
2051
|
+
updateWatchedValue(name);
|
2023
2052
|
if (focusIndexRef.current > -1) {
|
2024
2053
|
for (var key in fieldsRef.current) {
|
2025
2054
|
var field = fieldsRef.current[key];
|
@@ -2067,7 +2096,7 @@ function useWatch(_a) {
|
|
2067
2096
|
var _c = __read(React.useState(isUndefined(defaultValue)
|
2068
2097
|
? isString(name)
|
2069
2098
|
? get(defaultValuesRef.current, name)
|
2070
|
-
: isArray(name)
|
2099
|
+
: Array.isArray(name)
|
2071
2100
|
? name.reduce(function (previous, inputName) {
|
2072
2101
|
var _a;
|
2073
2102
|
return (__assign(__assign({}, previous), (_a = {}, _a[inputName] = get(defaultValuesRef.current, inputName), _a)));
|
@@ -2078,7 +2107,9 @@ function useWatch(_a) {
|
|
2078
2107
|
var defaultValueRef = React.useRef(defaultValue);
|
2079
2108
|
var updateWatchValue = React.useCallback(function () {
|
2080
2109
|
var value = watchInternal(name, defaultValueRef.current, idRef.current);
|
2081
|
-
setValue(isObject(value)
|
2110
|
+
setValue(isObject(value)
|
2111
|
+
? __assign({}, value) : Array.isArray(value)
|
2112
|
+
? __spread(value) : value);
|
2082
2113
|
}, [setValue, watchInternal, defaultValueRef, name, idRef]);
|
2083
2114
|
React.useEffect(function () {
|
2084
2115
|
{
|
@@ -2172,9 +2203,7 @@ var Controller = function (_a) {
|
|
2172
2203
|
}
|
2173
2204
|
}
|
2174
2205
|
}, [rules, name, register]);
|
2175
|
-
React.useEffect(function () { return function () {
|
2176
|
-
!isNameInFieldArray(fieldArrayNamesRef.current, name) && unregister(name);
|
2177
|
-
}; }, [unregister, name, fieldArrayNamesRef]);
|
2206
|
+
React.useEffect(function () { return function () { return unregister(name); }; }, [unregister, name]);
|
2178
2207
|
React.useEffect(function () {
|
2179
2208
|
{
|
2180
2209
|
if (isUndefined(value)) {
|