react-hook-form 6.9.4 → 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.
@@ -1142,13 +1142,14 @@ function useForm(_a) {
1142
1142
  }
1143
1143
  else if (!isPrimitive(value)) {
1144
1144
  setInternalValues(name, value, config);
1145
- if (fieldArrayNamesRef.current.has(name) && config.shouldDirty) {
1145
+ if (fieldArrayNamesRef.current.has(name)) {
1146
1146
  fieldArrayDefaultValuesRef.current[name] = value;
1147
1147
  resetFieldArrayFunctionRef.current[name]((_a = {},
1148
1148
  _a[name] = value,
1149
1149
  _a));
1150
- if (readFormStateRef.current.isDirty ||
1151
- readFormStateRef.current.dirtyFields) {
1150
+ if ((readFormStateRef.current.isDirty ||
1151
+ readFormStateRef.current.dirtyFields) &&
1152
+ config.shouldDirty) {
1152
1153
  set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(value, get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
1153
1154
  updateFormState({
1154
1155
  isDirty: !deepEqual(__assign(__assign({}, getValues()), (_b = {}, _b[name] = value, _b)), defaultValuesRef.current),
@@ -1308,6 +1309,33 @@ function useForm(_a) {
1308
1309
  var removeFieldEventListener = React.useCallback(function (field, forceDelete) {
1309
1310
  return findRemovedFieldAndRemoveListener(fieldsRef, handleChangeRef.current, field, shallowFieldsStateRef, shouldUnregister, forceDelete);
1310
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
+ };
1311
1339
  var removeFieldEventListenerAndRef = React.useCallback(function (field, forceDelete) {
1312
1340
  if (field) {
1313
1341
  removeFieldEventListener(field, forceDelete);
@@ -1323,6 +1351,7 @@ function useForm(_a) {
1323
1351
  dirtyFields: formStateRef.current.dirtyFields,
1324
1352
  });
1325
1353
  resolverRef.current && validateResolver();
1354
+ updateWatchedValue(field.ref.name);
1326
1355
  }
1327
1356
  }
1328
1357
  }, [validateResolver, removeFieldEventListener]);
@@ -1375,19 +1404,19 @@ function useForm(_a) {
1375
1404
  return watchInternal(fieldNames, defaultValue);
1376
1405
  }
1377
1406
  function unregister(name) {
1378
- var e_4, _a;
1407
+ var e_5, _a;
1379
1408
  try {
1380
1409
  for (var _b = __values(Array.isArray(name) ? name : [name]), _c = _b.next(); !_c.done; _c = _b.next()) {
1381
1410
  var fieldName = _c.value;
1382
1411
  removeFieldEventListenerAndRef(fieldsRef.current[fieldName], true);
1383
1412
  }
1384
1413
  }
1385
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1414
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1386
1415
  finally {
1387
1416
  try {
1388
1417
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1389
1418
  }
1390
- finally { if (e_4) throw e_4.error; }
1419
+ finally { if (e_5) throw e_5.error; }
1391
1420
  }
1392
1421
  }
1393
1422
  function registerFieldRef(ref, validateOptions) {
@@ -1492,8 +1521,8 @@ function useForm(_a) {
1492
1521
  }
1493
1522
  }
1494
1523
  var handleSubmit = React.useCallback(function (onValid, onInvalid) { return function (e) { return __awaiter(_this, void 0, void 0, function () {
1495
- var fieldErrors, fieldValues, _a, errors, values, _b, _c, field, name_2, fieldError, e_5_1, _d;
1496
- var e_5, _e;
1524
+ var fieldErrors, fieldValues, _a, errors, values, _b, _c, field, name_2, fieldError, e_6_1, _d;
1525
+ var e_6, _e;
1497
1526
  return __generator(this, function (_f) {
1498
1527
  switch (_f.label) {
1499
1528
  case 0:
@@ -1503,11 +1532,10 @@ function useForm(_a) {
1503
1532
  }
1504
1533
  fieldErrors = {};
1505
1534
  fieldValues = setFieldArrayDefaultValues(getFieldsValues(fieldsRef, shallowFieldsStateRef, true));
1506
- if (readFormStateRef.current.isSubmitting) {
1535
+ readFormStateRef.current.isSubmitting &&
1507
1536
  updateFormState({
1508
1537
  isSubmitting: true,
1509
1538
  });
1510
- }
1511
1539
  _f.label = 1;
1512
1540
  case 1:
1513
1541
  _f.trys.push([1, , 16, 17]);
@@ -1515,8 +1543,7 @@ function useForm(_a) {
1515
1543
  return [4 /*yield*/, resolverRef.current(fieldValues, contextRef.current, isValidateAllFieldCriteria)];
1516
1544
  case 2:
1517
1545
  _a = _f.sent(), errors = _a.errors, values = _a.values;
1518
- formStateRef.current.errors = errors;
1519
- fieldErrors = errors;
1546
+ formStateRef.current.errors = fieldErrors = errors;
1520
1547
  fieldValues = values;
1521
1548
  return [3 /*break*/, 10];
1522
1549
  case 3:
@@ -1545,14 +1572,14 @@ function useForm(_a) {
1545
1572
  return [3 /*break*/, 4];
1546
1573
  case 7: return [3 /*break*/, 10];
1547
1574
  case 8:
1548
- e_5_1 = _f.sent();
1549
- e_5 = { error: e_5_1 };
1575
+ e_6_1 = _f.sent();
1576
+ e_6 = { error: e_6_1 };
1550
1577
  return [3 /*break*/, 10];
1551
1578
  case 9:
1552
1579
  try {
1553
1580
  if (_c && !_c.done && (_e = _b.return)) _e.call(_b);
1554
1581
  }
1555
- finally { if (e_5) throw e_5.error; }
1582
+ finally { if (e_6) throw e_6.error; }
1556
1583
  return [7 /*endfinally*/];
1557
1584
  case 10:
1558
1585
  if (!(isEmptyObject(fieldErrors) &&
@@ -1613,7 +1640,7 @@ function useForm(_a) {
1613
1640
  });
1614
1641
  };
1615
1642
  var reset = function (values, omitResetState) {
1616
- var e_6, _a;
1643
+ var e_7, _a;
1617
1644
  if (omitResetState === void 0) { omitResetState = {}; }
1618
1645
  if (isWeb) {
1619
1646
  try {
@@ -1634,23 +1661,21 @@ function useForm(_a) {
1634
1661
  }
1635
1662
  }
1636
1663
  }
1637
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
1664
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
1638
1665
  finally {
1639
1666
  try {
1640
1667
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1641
1668
  }
1642
- finally { if (e_6) throw e_6.error; }
1669
+ finally { if (e_7) throw e_7.error; }
1643
1670
  }
1644
1671
  }
1645
1672
  fieldsRef.current = {};
1646
1673
  defaultValuesRef.current = cloneObject(values || defaultValuesRef.current);
1647
- if (values) {
1648
- renderWatchedInputs('');
1649
- }
1674
+ values && renderWatchedInputs('');
1675
+ Object.values(resetFieldArrayFunctionRef.current).forEach(function (resetFieldArray) { return isFunction(resetFieldArray) && resetFieldArray(); });
1650
1676
  shallowFieldsStateRef.current = shouldUnregister
1651
1677
  ? {}
1652
1678
  : cloneObject(values) || {};
1653
- Object.values(resetFieldArrayFunctionRef.current).forEach(function (resetFieldArray) { return isFunction(resetFieldArray) && resetFieldArray(); });
1654
1679
  resetRefs(omitResetState);
1655
1680
  };
1656
1681
  React.useEffect(function () {
@@ -1681,15 +1706,13 @@ function useForm(_a) {
1681
1706
  register: React.useCallback(register, [defaultValuesRef.current]),
1682
1707
  unregister: React.useCallback(unregister, []),
1683
1708
  };
1684
- var control = __assign({ renderWatchedInputs: renderWatchedInputs,
1709
+ var control = __assign({ updateWatchedValue: updateWatchedValue,
1685
1710
  shouldUnregister: shouldUnregister,
1686
1711
  removeFieldEventListener: removeFieldEventListener,
1687
1712
  watchInternal: watchInternal, mode: modeRef.current, reValidateMode: {
1688
1713
  isReValidateOnBlur: isReValidateOnBlur,
1689
1714
  isReValidateOnChange: isReValidateOnChange,
1690
1715
  }, fieldsRef: fieldsRef,
1691
- isWatchAllRef: isWatchAllRef,
1692
- watchFieldsRef: watchFieldsRef,
1693
1716
  resetFieldArrayFunctionRef: resetFieldArrayFunctionRef,
1694
1717
  useWatchFieldsRef: useWatchFieldsRef,
1695
1718
  useWatchRenderFunctionsRef: useWatchRenderFunctionsRef,
@@ -1817,7 +1840,7 @@ var useFieldArray = function (_a) {
1817
1840
  }
1818
1841
  }
1819
1842
  var focusIndexRef = React.useRef(-1);
1820
- var _c = control || methods.control, isWatchAllRef = _c.isWatchAllRef, 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, watchFieldsRef = _c.watchFieldsRef, validFieldsRef = _c.validFieldsRef, fieldsWithValidationRef = _c.fieldsWithValidationRef, fieldArrayDefaultValuesRef = _c.fieldArrayDefaultValuesRef, validateResolver = _c.validateResolver, renderWatchedInputs = _c.renderWatchedInputs, getValues = _c.getValues, shouldUnregister = _c.shouldUnregister;
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;
1821
1844
  var fieldArrayParentName = getFieldArrayParentName(name);
1822
1845
  var memoizedDefaultValues = React.useRef(__spread((get(fieldArrayDefaultValuesRef.current, fieldArrayParentName)
1823
1846
  ? get(fieldArrayDefaultValuesRef.current, name, [])
@@ -2015,7 +2038,6 @@ var useFieldArray = function (_a) {
2015
2038
  setFields(mapIds(memoizedDefaultValues.current, keyName));
2016
2039
  };
2017
2040
  React.useEffect(function () {
2018
- var e_1, _a;
2019
2041
  {
2020
2042
  if (!name) {
2021
2043
  console.warn('📋 useFieldArray is missing `name` attribute. https://react-hook-form.com/api#useFieldArray');
@@ -2026,30 +2048,7 @@ var useFieldArray = function (_a) {
2026
2048
  defaultValues.pop();
2027
2049
  set(fieldArrayDefaultValuesRef.current, name, defaultValues);
2028
2050
  }
2029
- if (isWatchAllRef.current) {
2030
- updateFormState();
2031
- }
2032
- else if (watchFieldsRef) {
2033
- var shouldRenderUseWatch = true;
2034
- try {
2035
- for (var _b = __values(watchFieldsRef.current), _c = _b.next(); !_c.done; _c = _b.next()) {
2036
- var watchField = _c.value;
2037
- if (watchField.startsWith(name)) {
2038
- updateFormState();
2039
- shouldRenderUseWatch = false;
2040
- break;
2041
- }
2042
- }
2043
- }
2044
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2045
- finally {
2046
- try {
2047
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2048
- }
2049
- finally { if (e_1) throw e_1.error; }
2050
- }
2051
- shouldRenderUseWatch && renderWatchedInputs(name);
2052
- }
2051
+ updateWatchedValue(name);
2053
2052
  if (focusIndexRef.current > -1) {
2054
2053
  for (var key in fieldsRef.current) {
2055
2054
  var field = fieldsRef.current[key];
@@ -2105,7 +2104,7 @@ function useWatch(_a) {
2105
2104
  : defaultValuesRef.current
2106
2105
  : defaultValue), 2), value = _c[0], setValue = _c[1];
2107
2106
  var idRef = React.useRef();
2108
- var defaultValueRef = React.useRef(value);
2107
+ var defaultValueRef = React.useRef(defaultValue);
2109
2108
  var updateWatchValue = React.useCallback(function () {
2110
2109
  var value = watchInternal(name, defaultValueRef.current, idRef.current);
2111
2110
  setValue(isObject(value)