react-hook-form 7.42.0 → 7.43.0-next.0

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.
@@ -148,7 +148,8 @@ var getProxyFormState = (formState, control, localProxyFormState, isRoot = true)
148
148
 
149
149
  var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
150
150
 
151
- var shouldRenderFormState = (formStateData, _proxyFormState, isRoot) => {
151
+ var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
152
+ updateFormState(formStateData);
152
153
  const { name, ...formState } = formStateData;
153
154
  return (isEmptyObject(formState) ||
154
155
  Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
@@ -231,7 +232,7 @@ function useFormState(props) {
231
232
  disabled,
232
233
  next: (value) => _mounted.current &&
233
234
  shouldSubscribeByName(_name.current, value.name, exact) &&
234
- shouldRenderFormState(value, _localProxyFormState.current) &&
235
+ shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) &&
235
236
  updateFormState({
236
237
  ...control._formState,
237
238
  ...value,
@@ -246,7 +247,7 @@ function useFormState(props) {
246
247
  isDirty,
247
248
  });
248
249
  }
249
- control._updateValid();
250
+ _localProxyFormState.current.isValid && control._updateValid(true);
250
251
  return () => {
251
252
  _mounted.current = false;
252
253
  };
@@ -1117,7 +1118,7 @@ function useFieldArray(props) {
1117
1118
  control._names.focus &&
1118
1119
  focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
1119
1120
  control._names.focus = '';
1120
- control._proxyFormState.isValid && control._updateValid();
1121
+ control._updateValid();
1121
1122
  }, [fields, name, control]);
1122
1123
  React.useEffect(() => {
1123
1124
  !get(control._formValues, name) && control._updateFieldArray(name);
@@ -1400,8 +1401,8 @@ function createFormControl(props = {}, flushRootRender) {
1400
1401
  errors: {},
1401
1402
  };
1402
1403
  let _fields = {};
1403
- let _defaultValues = isObject(_options.defaultValues)
1404
- ? cloneObject(_options.defaultValues) || {}
1404
+ let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
1405
+ ? cloneObject(_options.defaultValues || _options.values) || {}
1405
1406
  : {};
1406
1407
  let _formValues = _options.shouldUnregister
1407
1408
  ? {}
@@ -1439,13 +1440,12 @@ function createFormControl(props = {}, flushRootRender) {
1439
1440
  clearTimeout(timer);
1440
1441
  timer = window.setTimeout(callback, wait);
1441
1442
  };
1442
- const _updateValid = async () => {
1443
- if (_proxyFormState.isValid) {
1443
+ const _updateValid = async (shouldUpdateValid) => {
1444
+ if (_proxyFormState.isValid || shouldUpdateValid) {
1444
1445
  const isValid = _options.resolver
1445
1446
  ? isEmptyObject((await _executeSchema()).errors)
1446
1447
  : await executeBuiltInValidation(_fields, true);
1447
1448
  if (isValid !== _formState.isValid) {
1448
- _formState.isValid = isValid;
1449
1449
  _subjects.state.next({
1450
1450
  isValid,
1451
1451
  });
@@ -1717,10 +1717,9 @@ function createFormControl(props = {}, flushRootRender) {
1717
1717
  });
1718
1718
  if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
1719
1719
  options.shouldDirty) {
1720
- _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
1721
1720
  _subjects.state.next({
1722
1721
  name,
1723
- dirtyFields: _formState.dirtyFields,
1722
+ dirtyFields: getDirtyFields(_defaultValues, _formValues),
1724
1723
  isDirty: _getDirty(name, cloneValue),
1725
1724
  });
1726
1725
  }
@@ -1850,11 +1849,10 @@ function createFormControl(props = {}, flushRootRender) {
1850
1849
  error: get((formState || _formState).errors, name),
1851
1850
  });
1852
1851
  const clearErrors = (name) => {
1853
- name
1854
- ? convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName))
1855
- : (_formState.errors = {});
1852
+ name &&
1853
+ convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
1856
1854
  _subjects.state.next({
1857
- errors: _formState.errors,
1855
+ errors: name ? _formState.errors : {},
1858
1856
  });
1859
1857
  };
1860
1858
  const setError = (name, error, options) => {
@@ -1997,6 +1995,7 @@ function createFormControl(props = {}, flushRootRender) {
1997
1995
  else {
1998
1996
  await executeBuiltInValidation(_fields);
1999
1997
  }
1998
+ unset(_formState.errors, 'root');
2000
1999
  if (isEmptyObject(_formState.errors)) {
2001
2000
  _subjects.state.next({
2002
2001
  errors: {},
@@ -2143,6 +2142,12 @@ function createFormControl(props = {}, flushRootRender) {
2143
2142
  }
2144
2143
  }
2145
2144
  };
2145
+ const _updateFormState = (updatedFormState) => {
2146
+ _formState = {
2147
+ ..._formState,
2148
+ ...updatedFormState,
2149
+ };
2150
+ };
2146
2151
  if (isFunction(_options.defaultValues)) {
2147
2152
  _options.defaultValues().then((values) => {
2148
2153
  reset(values, _options.resetOptions);
@@ -2165,6 +2170,7 @@ function createFormControl(props = {}, flushRootRender) {
2165
2170
  _updateFieldArray,
2166
2171
  _getFieldArray,
2167
2172
  _reset,
2173
+ _updateFormState,
2168
2174
  _subjects,
2169
2175
  _proxyFormState,
2170
2176
  get _fields() {
@@ -2278,18 +2284,14 @@ function useForm(props = {}) {
2278
2284
  useSubscribe({
2279
2285
  subject: control._subjects.state,
2280
2286
  next: (value) => {
2281
- if (shouldRenderFormState(value, control._proxyFormState, true)) {
2282
- control._formState = {
2283
- ...control._formState,
2284
- ...value,
2285
- };
2287
+ if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
2286
2288
  updateFormState({ ...control._formState });
2287
2289
  }
2288
2290
  },
2289
2291
  });
2290
2292
  React.useEffect(() => {
2291
2293
  if (!control._stateFlags.mount) {
2292
- control._proxyFormState.isValid && control._updateValid();
2294
+ control._updateValid();
2293
2295
  control._stateFlags.mount = true;
2294
2296
  }
2295
2297
  if (control._stateFlags.watch) {