react-hook-form 7.43.1 → 7.43.3

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.
@@ -328,7 +328,7 @@ function useWatch(props) {
328
328
  _name.current = name;
329
329
  useSubscribe({
330
330
  disabled,
331
- subject: control._subjects.watch,
331
+ subject: control._subjects.values,
332
332
  next: (formState) => {
333
333
  if (shouldSubscribeByName(_name.current, formState.name, exact)) {
334
334
  updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
@@ -603,7 +603,7 @@ var isHTMLElement = (value) => {
603
603
  (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
604
604
  };
605
605
 
606
- var isMessage = (value) => isString(value) || React.isValidElement(value);
606
+ var isMessage = (value) => isString(value);
607
607
 
608
608
  var isRadioInput = (element) => element.type === 'radio';
609
609
 
@@ -1083,7 +1083,10 @@ function useFieldArray(props) {
1083
1083
  };
1084
1084
  React.useEffect(() => {
1085
1085
  control._stateFlags.action = false;
1086
- isWatched(name, control._names) && control._subjects.state.next({});
1086
+ isWatched(name, control._names) &&
1087
+ control._subjects.state.next({
1088
+ ...control._formState,
1089
+ });
1087
1090
  if (_actioned.current &&
1088
1091
  (!getValidationModes(control._options.mode).isOnSubmit ||
1089
1092
  control._formState.isSubmitted)) {
@@ -1111,7 +1114,7 @@ function useFieldArray(props) {
1111
1114
  }
1112
1115
  }
1113
1116
  }
1114
- control._subjects.watch.next({
1117
+ control._subjects.values.next({
1115
1118
  name,
1116
1119
  values: control._formValues,
1117
1120
  });
@@ -1146,8 +1149,11 @@ function useFieldArray(props) {
1146
1149
  function createSubject() {
1147
1150
  let _observers = [];
1148
1151
  const next = (value) => {
1149
- for (const observer of _observers) {
1150
- observer.next(value);
1152
+ let x = 0;
1153
+ const l = _observers.length;
1154
+ while (x < l) {
1155
+ _observers[x].next(value);
1156
+ ++x;
1151
1157
  }
1152
1158
  };
1153
1159
  const subscribe = (observer) => {
@@ -1252,9 +1258,7 @@ function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues
1252
1258
  }
1253
1259
  }
1254
1260
  else {
1255
- deepEqual(data[key], formValues[key])
1256
- ? delete dirtyFieldsFromValues[key]
1257
- : (dirtyFieldsFromValues[key] = true);
1261
+ dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
1258
1262
  }
1259
1263
  }
1260
1264
  }
@@ -1386,7 +1390,6 @@ function createFormControl(props = {}, flushRootRender) {
1386
1390
  ...defaultOptions,
1387
1391
  ...props,
1388
1392
  };
1389
- const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1390
1393
  let _formState = {
1391
1394
  submitCount: 0,
1392
1395
  isDirty: false,
@@ -1420,6 +1423,7 @@ function createFormControl(props = {}, flushRootRender) {
1420
1423
  };
1421
1424
  let delayErrorCallback;
1422
1425
  let timer = 0;
1426
+ const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1423
1427
  const _proxyFormState = {
1424
1428
  isDirty: false,
1425
1429
  dirtyFields: false,
@@ -1429,7 +1433,7 @@ function createFormControl(props = {}, flushRootRender) {
1429
1433
  errors: false,
1430
1434
  };
1431
1435
  const _subjects = {
1432
- watch: createSubject(),
1436
+ values: createSubject(),
1433
1437
  array: createSubject(),
1434
1438
  state: createSubject(),
1435
1439
  };
@@ -1438,7 +1442,7 @@ function createFormControl(props = {}, flushRootRender) {
1438
1442
  const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
1439
1443
  const debounce = (callback) => (wait) => {
1440
1444
  clearTimeout(timer);
1441
- timer = window.setTimeout(callback, wait);
1445
+ timer = setTimeout(callback, wait);
1442
1446
  };
1443
1447
  const _updateValid = async (shouldUpdateValid) => {
1444
1448
  if (_proxyFormState.isValid || shouldUpdateValid) {
@@ -1681,8 +1685,9 @@ function createFormControl(props = {}, flushRootRender) {
1681
1685
  else {
1682
1686
  fieldReference.ref.value = fieldValue;
1683
1687
  if (!fieldReference.ref.type) {
1684
- _subjects.watch.next({
1688
+ _subjects.values.next({
1685
1689
  name,
1690
+ values: { ..._formValues },
1686
1691
  });
1687
1692
  }
1688
1693
  }
@@ -1713,7 +1718,7 @@ function createFormControl(props = {}, flushRootRender) {
1713
1718
  if (isFieldArray) {
1714
1719
  _subjects.array.next({
1715
1720
  name,
1716
- values: _formValues,
1721
+ values: { ..._formValues },
1717
1722
  });
1718
1723
  if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
1719
1724
  options.shouldDirty) {
@@ -1729,9 +1734,10 @@ function createFormControl(props = {}, flushRootRender) {
1729
1734
  ? setValues(name, cloneValue, options)
1730
1735
  : setFieldValue(name, cloneValue, options);
1731
1736
  }
1732
- isWatched(name, _names) && _subjects.state.next({});
1733
- _subjects.watch.next({
1737
+ isWatched(name, _names) && _subjects.state.next({ ..._formState });
1738
+ _subjects.values.next({
1734
1739
  name,
1740
+ values: { ..._formValues },
1735
1741
  });
1736
1742
  !_stateFlags.mount && flushRootRender();
1737
1743
  };
@@ -1762,16 +1768,17 @@ function createFormControl(props = {}, flushRootRender) {
1762
1768
  const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
1763
1769
  const shouldRender = !isEmptyObject(fieldState) || watched;
1764
1770
  !isBlurEvent &&
1765
- _subjects.watch.next({
1771
+ _subjects.values.next({
1766
1772
  name,
1767
1773
  type: event.type,
1774
+ values: { ..._formValues },
1768
1775
  });
1769
1776
  if (shouldSkipValidation) {
1770
1777
  _proxyFormState.isValid && _updateValid();
1771
1778
  return (shouldRender &&
1772
1779
  _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1773
1780
  }
1774
- !isBlurEvent && watched && _subjects.state.next({});
1781
+ !isBlurEvent && watched && _subjects.state.next({ ..._formState });
1775
1782
  _updateIsValidating(true);
1776
1783
  if (_options.resolver) {
1777
1784
  const { errors } = await _executeSchema([name]);
@@ -1869,7 +1876,7 @@ function createFormControl(props = {}, flushRootRender) {
1869
1876
  options && options.shouldFocus && ref && ref.focus && ref.focus();
1870
1877
  };
1871
1878
  const watch = (name, defaultValue) => isFunction(name)
1872
- ? _subjects.watch.subscribe({
1879
+ ? _subjects.values.subscribe({
1873
1880
  next: (payload) => name(_getWatch(undefined, defaultValue), payload),
1874
1881
  })
1875
1882
  : _getWatch(name, defaultValue, true);
@@ -1890,7 +1897,9 @@ function createFormControl(props = {}, flushRootRender) {
1890
1897
  unset(_defaultValues, fieldName);
1891
1898
  }
1892
1899
  }
1893
- _subjects.watch.next({});
1900
+ _subjects.values.next({
1901
+ values: { ..._formValues },
1902
+ });
1894
1903
  _subjects.state.next({
1895
1904
  ..._formState,
1896
1905
  ...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
@@ -2007,6 +2016,7 @@ function createFormControl(props = {}, flushRootRender) {
2007
2016
  await onInvalid({ ..._formState.errors }, e);
2008
2017
  }
2009
2018
  _focusError();
2019
+ setTimeout(_focusError);
2010
2020
  }
2011
2021
  _subjects.state.next({
2012
2022
  isSubmitted: true,
@@ -2086,7 +2096,7 @@ function createFormControl(props = {}, flushRootRender) {
2086
2096
  _subjects.array.next({
2087
2097
  values,
2088
2098
  });
2089
- _subjects.watch.next({
2099
+ _subjects.values.next({
2090
2100
  values,
2091
2101
  });
2092
2102
  }
@@ -2106,14 +2116,14 @@ function createFormControl(props = {}, flushRootRender) {
2106
2116
  submitCount: keepStateOptions.keepSubmitCount
2107
2117
  ? _formState.submitCount
2108
2118
  : 0,
2109
- isDirty: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
2119
+ isDirty: keepStateOptions.keepDirty
2110
2120
  ? _formState.isDirty
2111
2121
  : !!(keepStateOptions.keepDefaultValues &&
2112
2122
  !deepEqual(formValues, _defaultValues)),
2113
2123
  isSubmitted: keepStateOptions.keepIsSubmitted
2114
2124
  ? _formState.isSubmitted
2115
2125
  : false,
2116
- dirtyFields: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
2126
+ dirtyFields: keepStateOptions.keepDirtyValues
2117
2127
  ? _formState.dirtyFields
2118
2128
  : keepStateOptions.keepDefaultValues && formValues
2119
2129
  ? getDirtyFields(_defaultValues, formValues)
@@ -2162,7 +2172,6 @@ function createFormControl(props = {}, flushRootRender) {
2162
2172
  unregister,
2163
2173
  getFieldState,
2164
2174
  _executeSchema,
2165
- _focusError,
2166
2175
  _getWatch,
2167
2176
  _getDirty,
2168
2177
  _updateValid,
@@ -2289,6 +2298,11 @@ function useForm(props = {}) {
2289
2298
  }
2290
2299
  },
2291
2300
  });
2301
+ React.useEffect(() => {
2302
+ if (props.values && !deepEqual(props.values, control._defaultValues)) {
2303
+ control._reset(props.values, control._options.resetOptions);
2304
+ }
2305
+ }, [props.values, control]);
2292
2306
  React.useEffect(() => {
2293
2307
  if (!control._stateFlags.mount) {
2294
2308
  control._updateValid();
@@ -2296,18 +2310,10 @@ function useForm(props = {}) {
2296
2310
  }
2297
2311
  if (control._stateFlags.watch) {
2298
2312
  control._stateFlags.watch = false;
2299
- control._subjects.state.next({});
2313
+ control._subjects.state.next({ ...control._formState });
2300
2314
  }
2301
2315
  control._removeUnmounted();
2302
2316
  });
2303
- React.useEffect(() => {
2304
- if (props.values && !deepEqual(props.values, control._defaultValues)) {
2305
- control._reset(props.values, control._options.resetOptions);
2306
- }
2307
- }, [props.values, control]);
2308
- React.useEffect(() => {
2309
- formState.submitCount && control._focusError();
2310
- }, [control, formState.submitCount]);
2311
2317
  _formControl.current.formState = getProxyFormState(formState, control);
2312
2318
  return _formControl.current;
2313
2319
  }