react-hook-form 7.43.2 → 7.43.4

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)));
@@ -393,7 +393,7 @@ function useController(props) {
393
393
  return () => {
394
394
  const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
395
395
  (isArrayField
396
- ? _shouldUnregisterField && !control._stateFlags.action
396
+ ? _shouldUnregisterField && !control._state.action
397
397
  : _shouldUnregisterField)
398
398
  ? control.unregister(name)
399
399
  : updateMounted(name, false);
@@ -1082,8 +1082,11 @@ function useFieldArray(props) {
1082
1082
  control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
1083
1083
  };
1084
1084
  React.useEffect(() => {
1085
- control._stateFlags.action = false;
1086
- isWatched(name, control._names) && control._subjects.state.next({});
1085
+ control._state.action = false;
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,9 +1114,9 @@ function useFieldArray(props) {
1111
1114
  }
1112
1115
  }
1113
1116
  }
1114
- control._subjects.watch.next({
1117
+ control._subjects.values.next({
1115
1118
  name,
1116
- values: control._formValues,
1119
+ values: { ...control._formValues },
1117
1120
  });
1118
1121
  control._names.focus &&
1119
1122
  focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
@@ -1146,11 +1149,8 @@ function useFieldArray(props) {
1146
1149
  function createSubject() {
1147
1150
  let _observers = [];
1148
1151
  const next = (value) => {
1149
- let x = 0;
1150
- const l = _observers.length;
1151
- while (x < l) {
1152
- _observers[x].next(value);
1153
- ++x;
1152
+ for (const observer of _observers) {
1153
+ observer.next && observer.next(value);
1154
1154
  }
1155
1155
  };
1156
1156
  const subscribe = (observer) => {
@@ -1255,9 +1255,7 @@ function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues
1255
1255
  }
1256
1256
  }
1257
1257
  else {
1258
- deepEqual(data[key], formValues[key])
1259
- ? delete dirtyFieldsFromValues[key]
1260
- : (dirtyFieldsFromValues[key] = true);
1258
+ dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
1261
1259
  }
1262
1260
  }
1263
1261
  }
@@ -1409,7 +1407,7 @@ function createFormControl(props = {}, flushRootRender) {
1409
1407
  let _formValues = _options.shouldUnregister
1410
1408
  ? {}
1411
1409
  : cloneObject(_defaultValues);
1412
- let _stateFlags = {
1410
+ let _state = {
1413
1411
  action: false,
1414
1412
  mount: false,
1415
1413
  watch: false,
@@ -1422,7 +1420,6 @@ function createFormControl(props = {}, flushRootRender) {
1422
1420
  };
1423
1421
  let delayErrorCallback;
1424
1422
  let timer = 0;
1425
- const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1426
1423
  const _proxyFormState = {
1427
1424
  isDirty: false,
1428
1425
  dirtyFields: false,
@@ -1432,10 +1429,11 @@ function createFormControl(props = {}, flushRootRender) {
1432
1429
  errors: false,
1433
1430
  };
1434
1431
  const _subjects = {
1435
- watch: createSubject(),
1432
+ values: createSubject(),
1436
1433
  array: createSubject(),
1437
1434
  state: createSubject(),
1438
1435
  };
1436
+ const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1439
1437
  const validationModeBeforeSubmit = getValidationModes(_options.mode);
1440
1438
  const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
1441
1439
  const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
@@ -1461,7 +1459,7 @@ function createFormControl(props = {}, flushRootRender) {
1461
1459
  });
1462
1460
  const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
1463
1461
  if (args && method) {
1464
- _stateFlags.action = true;
1462
+ _state.action = true;
1465
1463
  if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
1466
1464
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1467
1465
  shouldSetValues && set(_fields, name, fieldValues);
@@ -1508,7 +1506,7 @@ function createFormControl(props = {}, flushRootRender) {
1508
1506
  shouldSkipSetValueAs
1509
1507
  ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
1510
1508
  : setFieldValue(name, defaultValue);
1511
- _stateFlags.mount && _updateValid();
1509
+ _state.mount && _updateValid();
1512
1510
  }
1513
1511
  };
1514
1512
  const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
@@ -1581,7 +1579,7 @@ function createFormControl(props = {}, flushRootRender) {
1581
1579
  }
1582
1580
  _updateIsValidating(false);
1583
1581
  };
1584
- const _executeSchema = async (name) => await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1582
+ const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1585
1583
  const executeSchemaAndUpdateState = async (names) => {
1586
1584
  const { errors } = await _executeSchema();
1587
1585
  if (names) {
@@ -1640,7 +1638,7 @@ function createFormControl(props = {}, flushRootRender) {
1640
1638
  const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
1641
1639
  !deepEqual(getValues(), _defaultValues));
1642
1640
  const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
1643
- ...(_stateFlags.mount
1641
+ ...(_state.mount
1644
1642
  ? _formValues
1645
1643
  : isUndefined(defaultValue)
1646
1644
  ? _defaultValues
@@ -1648,7 +1646,7 @@ function createFormControl(props = {}, flushRootRender) {
1648
1646
  ? { [names]: defaultValue }
1649
1647
  : defaultValue),
1650
1648
  }, isGlobal, defaultValue);
1651
- const _getFieldArray = (name) => compact(get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
1649
+ const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
1652
1650
  const setFieldValue = (name, value, options = {}) => {
1653
1651
  const field = get(_fields, name);
1654
1652
  let fieldValue = value;
@@ -1684,8 +1682,9 @@ function createFormControl(props = {}, flushRootRender) {
1684
1682
  else {
1685
1683
  fieldReference.ref.value = fieldValue;
1686
1684
  if (!fieldReference.ref.type) {
1687
- _subjects.watch.next({
1685
+ _subjects.values.next({
1688
1686
  name,
1687
+ values: { ..._formValues },
1689
1688
  });
1690
1689
  }
1691
1690
  }
@@ -1716,7 +1715,7 @@ function createFormControl(props = {}, flushRootRender) {
1716
1715
  if (isFieldArray) {
1717
1716
  _subjects.array.next({
1718
1717
  name,
1719
- values: _formValues,
1718
+ values: { ..._formValues },
1720
1719
  });
1721
1720
  if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
1722
1721
  options.shouldDirty) {
@@ -1732,11 +1731,12 @@ function createFormControl(props = {}, flushRootRender) {
1732
1731
  ? setValues(name, cloneValue, options)
1733
1732
  : setFieldValue(name, cloneValue, options);
1734
1733
  }
1735
- isWatched(name, _names) && _subjects.state.next({});
1736
- _subjects.watch.next({
1734
+ isWatched(name, _names) && _subjects.state.next({ ..._formState });
1735
+ _subjects.values.next({
1737
1736
  name,
1737
+ values: { ..._formValues },
1738
1738
  });
1739
- !_stateFlags.mount && flushRootRender();
1739
+ !_state.mount && flushRootRender();
1740
1740
  };
1741
1741
  const onChange = async (event) => {
1742
1742
  const target = event.target;
@@ -1765,16 +1765,17 @@ function createFormControl(props = {}, flushRootRender) {
1765
1765
  const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
1766
1766
  const shouldRender = !isEmptyObject(fieldState) || watched;
1767
1767
  !isBlurEvent &&
1768
- _subjects.watch.next({
1768
+ _subjects.values.next({
1769
1769
  name,
1770
1770
  type: event.type,
1771
+ values: { ..._formValues },
1771
1772
  });
1772
1773
  if (shouldSkipValidation) {
1773
1774
  _proxyFormState.isValid && _updateValid();
1774
1775
  return (shouldRender &&
1775
1776
  _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1776
1777
  }
1777
- !isBlurEvent && watched && _subjects.state.next({});
1778
+ !isBlurEvent && watched && _subjects.state.next({ ..._formState });
1778
1779
  _updateIsValidating(true);
1779
1780
  if (_options.resolver) {
1780
1781
  const { errors } = await _executeSchema([name]);
@@ -1837,7 +1838,7 @@ function createFormControl(props = {}, flushRootRender) {
1837
1838
  const getValues = (fieldNames) => {
1838
1839
  const values = {
1839
1840
  ..._defaultValues,
1840
- ...(_stateFlags.mount ? _formValues : {}),
1841
+ ...(_state.mount ? _formValues : {}),
1841
1842
  };
1842
1843
  return isUndefined(fieldNames)
1843
1844
  ? values
@@ -1872,7 +1873,7 @@ function createFormControl(props = {}, flushRootRender) {
1872
1873
  options && options.shouldFocus && ref && ref.focus && ref.focus();
1873
1874
  };
1874
1875
  const watch = (name, defaultValue) => isFunction(name)
1875
- ? _subjects.watch.subscribe({
1876
+ ? _subjects.values.subscribe({
1876
1877
  next: (payload) => name(_getWatch(undefined, defaultValue), payload),
1877
1878
  })
1878
1879
  : _getWatch(name, defaultValue, true);
@@ -1893,7 +1894,9 @@ function createFormControl(props = {}, flushRootRender) {
1893
1894
  unset(_defaultValues, fieldName);
1894
1895
  }
1895
1896
  }
1896
- _subjects.watch.next({});
1897
+ _subjects.values.next({
1898
+ values: { ..._formValues },
1899
+ });
1897
1900
  _subjects.state.next({
1898
1901
  ..._formState,
1899
1902
  ...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
@@ -1973,7 +1976,7 @@ function createFormControl(props = {}, flushRootRender) {
1973
1976
  field._f.mount = false;
1974
1977
  }
1975
1978
  (_options.shouldUnregister || options.shouldUnregister) &&
1976
- !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
1979
+ !(isNameInFieldArray(_names.array, name) && _state.action) &&
1977
1980
  _names.unMount.add(name);
1978
1981
  }
1979
1982
  },
@@ -2088,10 +2091,10 @@ function createFormControl(props = {}, flushRootRender) {
2088
2091
  : {}
2089
2092
  : cloneUpdatedValues;
2090
2093
  _subjects.array.next({
2091
- values,
2094
+ values: { ...values },
2092
2095
  });
2093
- _subjects.watch.next({
2094
- values,
2096
+ _subjects.values.next({
2097
+ values: { ...values },
2095
2098
  });
2096
2099
  }
2097
2100
  _names = {
@@ -2102,10 +2105,9 @@ function createFormControl(props = {}, flushRootRender) {
2102
2105
  watchAll: false,
2103
2106
  focus: '',
2104
2107
  };
2105
- !_stateFlags.mount && flushRootRender();
2106
- _stateFlags.mount =
2107
- !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
2108
- _stateFlags.watch = !!props.shouldUnregister;
2108
+ !_state.mount && flushRootRender();
2109
+ _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
2110
+ _state.watch = !!props.shouldUnregister;
2109
2111
  _subjects.state.next({
2110
2112
  submitCount: keepStateOptions.keepSubmitCount
2111
2113
  ? _formState.submitCount
@@ -2182,11 +2184,11 @@ function createFormControl(props = {}, flushRootRender) {
2182
2184
  get _formValues() {
2183
2185
  return _formValues;
2184
2186
  },
2185
- get _stateFlags() {
2186
- return _stateFlags;
2187
+ get _state() {
2188
+ return _state;
2187
2189
  },
2188
- set _stateFlags(value) {
2189
- _stateFlags = value;
2190
+ set _state(value) {
2191
+ _state = value;
2190
2192
  },
2191
2193
  get _defaultValues() {
2192
2194
  return _defaultValues;
@@ -2293,21 +2295,21 @@ function useForm(props = {}) {
2293
2295
  },
2294
2296
  });
2295
2297
  React.useEffect(() => {
2296
- if (!control._stateFlags.mount) {
2298
+ if (props.values && !deepEqual(props.values, control._defaultValues)) {
2299
+ control._reset(props.values, control._options.resetOptions);
2300
+ }
2301
+ }, [props.values, control]);
2302
+ React.useEffect(() => {
2303
+ if (!control._state.mount) {
2297
2304
  control._updateValid();
2298
- control._stateFlags.mount = true;
2305
+ control._state.mount = true;
2299
2306
  }
2300
- if (control._stateFlags.watch) {
2301
- control._stateFlags.watch = false;
2302
- control._subjects.state.next({});
2307
+ if (control._state.watch) {
2308
+ control._state.watch = false;
2309
+ control._subjects.state.next({ ...control._formState });
2303
2310
  }
2304
2311
  control._removeUnmounted();
2305
2312
  });
2306
- React.useEffect(() => {
2307
- if (props.values && !deepEqual(props.values, control._defaultValues)) {
2308
- control._reset(props.values, control._options.resetOptions);
2309
- }
2310
- }, [props.values, control]);
2311
2313
  _formControl.current.formState = getProxyFormState(formState, control);
2312
2314
  return _formControl.current;
2313
2315
  }