react-hook-form 7.5.2-beta.0 → 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/dist/index.esm.js CHANGED
@@ -279,13 +279,14 @@ const getFieldsValues = (fieldsRef, defaultValuesRef, output = {}) => {
279
279
  : Array.isArray(field)
280
280
  ? []
281
281
  : {});
282
- current &&
282
+ if (current) {
283
283
  getFieldsValues({
284
284
  current,
285
285
  }, defaultValuesRef, output[name]);
286
+ }
286
287
  }
287
288
  }
288
- return output;
289
+ return Object.assign(Object.assign({}, defaultValuesRef), output);
289
290
  };
290
291
 
291
292
  var generateId = () => {
@@ -385,8 +386,6 @@ function insert(data, index, value) {
385
386
  ];
386
387
  }
387
388
 
388
- var isString = (value) => typeof value === 'string';
389
-
390
389
  var moveArrayAt = (data, from, to) => {
391
390
  if (Array.isArray(data)) {
392
391
  if (isUndefined(data[to])) {
@@ -473,7 +472,7 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
473
472
  fieldArrayNamesRef.current.add(name);
474
473
  const omitKey = (fields) => fields.map((field) => omit((field || {}), keyName));
475
474
  const getCurrentFieldsValues = () => {
476
- const values = get(getFieldsValues(fieldsRef), name, []);
475
+ const values = get(getFieldsValues(fieldsRef, defaultValuesRef.current), name, []);
477
476
  return mapIds(get(fieldArrayDefaultValuesRef.current, name, []).map((item, index) => (Object.assign(Object.assign({}, item), values[index]))), keyName);
478
477
  };
479
478
  const getFocusDetail = (index, options) => options
@@ -611,10 +610,10 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
611
610
  }
612
611
  watchSubjectRef.current.next({
613
612
  name,
614
- value: get(getFieldsValues(fieldsRef), name, []),
613
+ value: get(getFieldsValues(fieldsRef, defaultValuesRef.current), name, []),
615
614
  });
616
615
  focusNameRef.current &&
617
- focusFieldBy(fieldsRef.current, (key) => isString(key) && key.startsWith(focusNameRef.current));
616
+ focusFieldBy(fieldsRef.current, (key) => key.startsWith(focusNameRef.current));
618
617
  focusNameRef.current = '';
619
618
  fieldArraySubjectRef.current.next({
620
619
  name,
@@ -771,6 +770,8 @@ var skipValidation = ({ isOnBlur, isOnChange, isOnTouch, isTouched, isReValidate
771
770
 
772
771
  var isFunction = (value) => typeof value === 'function';
773
772
 
773
+ var isString = (value) => typeof value === 'string';
774
+
774
775
  var isMessage = (value) => isString(value) || React.isValidElement(value);
775
776
 
776
777
  var isRegex = (value) => value instanceof RegExp;
@@ -1132,7 +1133,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1132
1133
  return isUndefined(error);
1133
1134
  }, [isValidateAllFieldCriteria]);
1134
1135
  const executeSchemaOrResolverValidation = React.useCallback(async (names, currentNames = []) => {
1135
- const { errors } = await resolverRef.current(getFieldsValues(fieldsRef), contextRef.current, {
1136
+ const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current), contextRef.current, {
1136
1137
  criteriaMode,
1137
1138
  names: currentNames,
1138
1139
  fields: getFields(fieldsNamesRef.current, fieldsRef.current),
@@ -1305,7 +1306,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1305
1306
  isValidating: true,
1306
1307
  });
1307
1308
  if (resolverRef.current) {
1308
- const { errors } = await resolverRef.current(getFieldsValues(fieldsRef), contextRef.current, {
1309
+ const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current), contextRef.current, {
1309
1310
  criteriaMode,
1310
1311
  fields: getFields([name], fieldsRef.current),
1311
1312
  names: [name],
@@ -1337,7 +1338,9 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1337
1338
  }
1338
1339
  }, []);
1339
1340
  const getValues = (fieldNames) => {
1340
- const values = Object.assign(Object.assign({}, defaultValuesRef.current), getFieldsValues(fieldsRef));
1341
+ const values = isMountedRef.current
1342
+ ? getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current)
1343
+ : defaultValuesRef.current;
1341
1344
  return isUndefined(fieldNames)
1342
1345
  ? values
1343
1346
  : isString(fieldNames)
@@ -1347,7 +1350,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1347
1350
  const updateIsValid = React.useCallback(async (values = {}) => {
1348
1351
  const previousIsValid = formStateRef.current.isValid;
1349
1352
  if (resolver) {
1350
- 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, {
1351
1354
  criteriaMode,
1352
1355
  fields: getFields(fieldsNamesRef.current, fieldsRef.current),
1353
1356
  });
@@ -1382,11 +1385,12 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1382
1385
  const watchInternal = React.useCallback((fieldNames, defaultValue, isGlobal) => {
1383
1386
  const isArrayNames = Array.isArray(fieldNames);
1384
1387
  const fieldValues = isMountedRef.current
1385
- ? Object.assign(Object.assign({}, defaultValuesRef.current), getFieldsValues(fieldsRef)) : isUndefined(defaultValue)
1386
- ? defaultValuesRef.current
1387
- : isArrayNames
1388
- ? defaultValue || {}
1389
- : { [fieldNames]: defaultValue };
1388
+ ? getFieldsValues(fieldsRef, defaultValuesRef.current)
1389
+ : isUndefined(defaultValue)
1390
+ ? defaultValuesRef.current
1391
+ : isArrayNames
1392
+ ? defaultValue || {}
1393
+ : { [fieldNames]: defaultValue };
1390
1394
  if (isUndefined(fieldNames)) {
1391
1395
  isGlobal && (isWatchAllRef.current = true);
1392
1396
  return fieldValues;
@@ -1495,7 +1499,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1495
1499
  e.persist && e.persist();
1496
1500
  }
1497
1501
  let hasNoPromiseError = true;
1498
- let fieldValues = getFieldsValues(fieldsRef);
1502
+ let fieldValues = getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current);
1499
1503
  formStateSubjectRef.current.next({
1500
1504
  isSubmitting: true,
1501
1505
  });
@@ -1564,26 +1568,6 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1564
1568
  isSubmitSuccessful: false,
1565
1569
  });
1566
1570
  }, []);
1567
- const registerAbsentFields = (value, name = '') => {
1568
- !get(fieldsRef.current, name) &&
1569
- (isPrimitive(value) ||
1570
- (isWeb && (value instanceof File || value instanceof Date))) &&
1571
- set(fieldsRef.current, name, {
1572
- _f: {
1573
- ref: { name, value },
1574
- value,
1575
- name,
1576
- },
1577
- });
1578
- if (Array.isArray(value) || isObject(value)) {
1579
- if (name && !get(fieldsRef.current, name)) {
1580
- set(fieldsRef.current, name, Array.isArray(value) ? [] : {});
1581
- }
1582
- for (const key in value) {
1583
- registerAbsentFields(value[key], name + (name ? '.' : '') + key);
1584
- }
1585
- }
1586
- };
1587
1571
  const reset = (values, keepStateOptions = {}) => {
1588
1572
  const updatedValues = values || defaultValuesRef.current;
1589
1573
  if (isWeb && !keepStateOptions.keepValues) {
@@ -1603,10 +1587,8 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1603
1587
  }
1604
1588
  }
1605
1589
  }
1606
- if (!keepStateOptions.keepDefaultValues) {
1607
- defaultValuesRef.current = Object.assign({}, updatedValues);
1608
- registerAbsentFields(updatedValues);
1609
- }
1590
+ !keepStateOptions.keepDefaultValues &&
1591
+ (defaultValuesRef.current = Object.assign({}, updatedValues));
1610
1592
  if (!keepStateOptions.keepValues) {
1611
1593
  fieldsRef.current = {};
1612
1594
  controllerSubjectRef.current.next({
@@ -1625,7 +1607,6 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1625
1607
  };
1626
1608
  const setFocus = (name) => get(fieldsRef.current, name)._f.ref.focus();
1627
1609
  React.useEffect(() => {
1628
- registerAbsentFields(defaultValuesRef.current);
1629
1610
  const formStateSubscription = formStateSubjectRef.current.subscribe({
1630
1611
  next(formState) {
1631
1612
  if (shouldRenderFormState(formState, readFormStateRef.current, true)) {