react-hook-form 7.4.3-beta.4 → 7.5.2
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/CHANGELOG.md +11 -4
- package/README.md +6 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +24 -51
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/logic/getFields.d.ts +1 -1
- package/dist/logic/getFieldsValues.d.ts +2 -2
- package/dist/logic/getValueAndMessage.d.ts +1 -1
- package/dist/logic/hasValidation.d.ts +2 -2
- package/dist/types/errors.d.ts +1 -1
- package/dist/types/validator.d.ts +1 -1
- package/dist/utils/isMessage.d.ts +1 -2
- package/package.json +11 -5
package/dist/index.esm.js
CHANGED
@@ -266,7 +266,7 @@ const focusFieldBy = (fields, callback, fieldsNames) => {
|
|
266
266
|
}
|
267
267
|
};
|
268
268
|
|
269
|
-
const
|
269
|
+
const getFieldsValues = (fieldsRef, defaultValuesRef, output = {}) => {
|
270
270
|
for (const name in fieldsRef.current) {
|
271
271
|
const field = fieldsRef.current[name];
|
272
272
|
if (field) {
|
@@ -279,18 +279,14 @@ const getFieldsValuesInternal = (fieldsRef, defaultValuesRef, output = {}) => {
|
|
279
279
|
: Array.isArray(field)
|
280
280
|
? []
|
281
281
|
: {});
|
282
|
-
current
|
283
|
-
|
282
|
+
if (current) {
|
283
|
+
getFieldsValues({
|
284
284
|
current,
|
285
285
|
}, defaultValuesRef, output[name]);
|
286
|
+
}
|
286
287
|
}
|
287
288
|
}
|
288
|
-
return output;
|
289
|
-
};
|
290
|
-
var getFieldsValues = (fieldsRef, defaultValuesRef) => {
|
291
|
-
const output = getFieldsValuesInternal(fieldsRef, defaultValuesRef);
|
292
|
-
return defaultValuesRef && defaultValuesRef.current
|
293
|
-
? Object.assign(Object.assign({}, defaultValuesRef.current), output) : output;
|
289
|
+
return Object.assign(Object.assign({}, defaultValuesRef), output);
|
294
290
|
};
|
295
291
|
|
296
292
|
var generateId = () => {
|
@@ -390,8 +386,6 @@ function insert(data, index, value) {
|
|
390
386
|
];
|
391
387
|
}
|
392
388
|
|
393
|
-
var isString = (value) => typeof value === 'string';
|
394
|
-
|
395
389
|
var moveArrayAt = (data, from, to) => {
|
396
390
|
if (Array.isArray(data)) {
|
397
391
|
if (isUndefined(data[to])) {
|
@@ -478,7 +472,7 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
|
|
478
472
|
fieldArrayNamesRef.current.add(name);
|
479
473
|
const omitKey = (fields) => fields.map((field) => omit((field || {}), keyName));
|
480
474
|
const getCurrentFieldsValues = () => {
|
481
|
-
const values = get(getFieldsValues(fieldsRef), name, []);
|
475
|
+
const values = get(getFieldsValues(fieldsRef, defaultValuesRef.current), name, []);
|
482
476
|
return mapIds(get(fieldArrayDefaultValuesRef.current, name, []).map((item, index) => (Object.assign(Object.assign({}, item), values[index]))), keyName);
|
483
477
|
};
|
484
478
|
const getFocusDetail = (index, options) => options
|
@@ -616,10 +610,10 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
|
|
616
610
|
}
|
617
611
|
watchSubjectRef.current.next({
|
618
612
|
name,
|
619
|
-
value: get(getFieldsValues(fieldsRef), name, []),
|
613
|
+
value: get(getFieldsValues(fieldsRef, defaultValuesRef.current), name, []),
|
620
614
|
});
|
621
615
|
focusNameRef.current &&
|
622
|
-
focusFieldBy(fieldsRef.current, (key) =>
|
616
|
+
focusFieldBy(fieldsRef.current, (key) => key.startsWith(focusNameRef.current));
|
623
617
|
focusNameRef.current = '';
|
624
618
|
fieldArraySubjectRef.current.next({
|
625
619
|
name,
|
@@ -776,6 +770,8 @@ var skipValidation = ({ isOnBlur, isOnChange, isOnTouch, isTouched, isReValidate
|
|
776
770
|
|
777
771
|
var isFunction = (value) => typeof value === 'function';
|
778
772
|
|
773
|
+
var isString = (value) => typeof value === 'string';
|
774
|
+
|
779
775
|
var isMessage = (value) => isString(value) || React.isValidElement(value);
|
780
776
|
|
781
777
|
var isRegex = (value) => value instanceof RegExp;
|
@@ -1137,7 +1133,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1137
1133
|
return isUndefined(error);
|
1138
1134
|
}, [isValidateAllFieldCriteria]);
|
1139
1135
|
const executeSchemaOrResolverValidation = React.useCallback(async (names, currentNames = []) => {
|
1140
|
-
const { errors } = await resolverRef.current(getFieldsValues(fieldsRef), contextRef.current, {
|
1136
|
+
const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current), contextRef.current, {
|
1141
1137
|
criteriaMode,
|
1142
1138
|
names: currentNames,
|
1143
1139
|
fields: getFields(fieldsNamesRef.current, fieldsRef.current),
|
@@ -1310,7 +1306,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1310
1306
|
isValidating: true,
|
1311
1307
|
});
|
1312
1308
|
if (resolverRef.current) {
|
1313
|
-
const { errors } = await resolverRef.current(getFieldsValues(fieldsRef), contextRef.current, {
|
1309
|
+
const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current), contextRef.current, {
|
1314
1310
|
criteriaMode,
|
1315
1311
|
fields: getFields([name], fieldsRef.current),
|
1316
1312
|
names: [name],
|
@@ -1342,7 +1338,9 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1342
1338
|
}
|
1343
1339
|
}, []);
|
1344
1340
|
const getValues = (fieldNames) => {
|
1345
|
-
const values =
|
1341
|
+
const values = isMountedRef.current
|
1342
|
+
? getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current)
|
1343
|
+
: defaultValuesRef.current;
|
1346
1344
|
return isUndefined(fieldNames)
|
1347
1345
|
? values
|
1348
1346
|
: isString(fieldNames)
|
@@ -1352,7 +1350,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1352
1350
|
const updateIsValid = React.useCallback(async (values = {}) => {
|
1353
1351
|
const previousIsValid = formStateRef.current.isValid;
|
1354
1352
|
if (resolver) {
|
1355
|
-
const { errors } = await resolverRef.current(Object.assign(Object.assign({}, getFieldsValues(fieldsRef)), values), contextRef.current, {
|
1353
|
+
const { errors } = await resolverRef.current(Object.assign(Object.assign({}, getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current)), values), contextRef.current, {
|
1356
1354
|
criteriaMode,
|
1357
1355
|
fields: getFields(fieldsNamesRef.current, fieldsRef.current),
|
1358
1356
|
});
|
@@ -1367,10 +1365,11 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1367
1365
|
});
|
1368
1366
|
}, [criteriaMode]);
|
1369
1367
|
const clearErrors = (name) => {
|
1370
|
-
name
|
1371
|
-
convertToArrayPayload(name).forEach((inputName) => unset(formStateRef.current.errors, inputName))
|
1368
|
+
name
|
1369
|
+
? convertToArrayPayload(name).forEach((inputName) => unset(formStateRef.current.errors, inputName))
|
1370
|
+
: (formStateRef.current.errors = {});
|
1372
1371
|
formStateSubjectRef.current.next({
|
1373
|
-
errors:
|
1372
|
+
errors: formStateRef.current.errors,
|
1374
1373
|
});
|
1375
1374
|
};
|
1376
1375
|
const setError = (name, error, options) => {
|
@@ -1386,7 +1385,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1386
1385
|
const watchInternal = React.useCallback((fieldNames, defaultValue, isGlobal) => {
|
1387
1386
|
const isArrayNames = Array.isArray(fieldNames);
|
1388
1387
|
const fieldValues = isMountedRef.current
|
1389
|
-
? getFieldsValues(fieldsRef, defaultValuesRef)
|
1388
|
+
? getFieldsValues(fieldsRef, defaultValuesRef.current)
|
1390
1389
|
: isUndefined(defaultValue)
|
1391
1390
|
? defaultValuesRef.current
|
1392
1391
|
: isArrayNames
|
@@ -1500,7 +1499,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1500
1499
|
e.persist && e.persist();
|
1501
1500
|
}
|
1502
1501
|
let hasNoPromiseError = true;
|
1503
|
-
let fieldValues = getFieldsValues(fieldsRef);
|
1502
|
+
let fieldValues = getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current);
|
1504
1503
|
formStateSubjectRef.current.next({
|
1505
1504
|
isSubmitting: true,
|
1506
1505
|
});
|
@@ -1569,29 +1568,6 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1569
1568
|
isSubmitSuccessful: false,
|
1570
1569
|
});
|
1571
1570
|
}, []);
|
1572
|
-
const registerAbsentFields = (value, name = '') => {
|
1573
|
-
!get(fieldsRef.current, name) &&
|
1574
|
-
(isPrimitive(value) ||
|
1575
|
-
(isWeb &&
|
1576
|
-
(value instanceof File ||
|
1577
|
-
value instanceof Date ||
|
1578
|
-
isHTMLElement(value)))) &&
|
1579
|
-
set(fieldsRef.current, name, {
|
1580
|
-
_f: {
|
1581
|
-
ref: { name, value },
|
1582
|
-
value,
|
1583
|
-
name,
|
1584
|
-
},
|
1585
|
-
});
|
1586
|
-
if (Array.isArray(value) || isObject(value)) {
|
1587
|
-
if (name && !get(fieldsRef.current, name)) {
|
1588
|
-
set(fieldsRef.current, name, Array.isArray(value) ? [] : {});
|
1589
|
-
}
|
1590
|
-
for (const key in value) {
|
1591
|
-
registerAbsentFields(value[key], name + (name ? '.' : '') + key);
|
1592
|
-
}
|
1593
|
-
}
|
1594
|
-
};
|
1595
1571
|
const reset = (values, keepStateOptions = {}) => {
|
1596
1572
|
const updatedValues = values || defaultValuesRef.current;
|
1597
1573
|
if (isWeb && !keepStateOptions.keepValues) {
|
@@ -1611,10 +1587,8 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1611
1587
|
}
|
1612
1588
|
}
|
1613
1589
|
}
|
1614
|
-
|
1615
|
-
defaultValuesRef.current = Object.assign({}, updatedValues);
|
1616
|
-
registerAbsentFields(updatedValues);
|
1617
|
-
}
|
1590
|
+
!keepStateOptions.keepDefaultValues &&
|
1591
|
+
(defaultValuesRef.current = Object.assign({}, updatedValues));
|
1618
1592
|
if (!keepStateOptions.keepValues) {
|
1619
1593
|
fieldsRef.current = {};
|
1620
1594
|
controllerSubjectRef.current.next({
|
@@ -1633,7 +1607,6 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1633
1607
|
};
|
1634
1608
|
const setFocus = (name) => get(fieldsRef.current, name)._f.ref.focus();
|
1635
1609
|
React.useEffect(() => {
|
1636
|
-
registerAbsentFields(defaultValuesRef.current);
|
1637
1610
|
const formStateSubscription = formStateSubjectRef.current.subscribe({
|
1638
1611
|
next(formState) {
|
1639
1612
|
if (shouldRenderFormState(formState, readFormStateRef.current, true)) {
|