react-hook-form 7.50.1 → 7.51.1

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.
@@ -263,6 +263,7 @@ function useFormState(props) {
263
263
  isLoading: false,
264
264
  dirtyFields: false,
265
265
  touchedFields: false,
266
+ validatingFields: false,
266
267
  isValidating: false,
267
268
  isValid: false,
268
269
  errors: false,
@@ -490,6 +491,10 @@ function useController(props) {
490
491
  enumerable: true,
491
492
  get: () => !!get(formState.touchedFields, name),
492
493
  },
494
+ isValidating: {
495
+ enumerable: true,
496
+ get: () => !!get(formState.validatingFields, name),
497
+ },
493
498
  error: {
494
499
  enumerable: true,
495
500
  get: () => get(formState.errors, name),
@@ -1518,7 +1523,7 @@ const defaultOptions = {
1518
1523
  reValidateMode: VALIDATION_MODE.onChange,
1519
1524
  shouldFocusError: true,
1520
1525
  };
1521
- function createFormControl(props = {}, flushRootRender) {
1526
+ function createFormControl(props = {}) {
1522
1527
  let _options = {
1523
1528
  ...defaultOptions,
1524
1529
  ...props,
@@ -1534,12 +1539,13 @@ function createFormControl(props = {}, flushRootRender) {
1534
1539
  isValid: false,
1535
1540
  touchedFields: {},
1536
1541
  dirtyFields: {},
1542
+ validatingFields: {},
1537
1543
  errors: _options.errors || {},
1538
1544
  disabled: _options.disabled || false,
1539
1545
  };
1540
1546
  let _fields = {};
1541
- let _defaultValues = isObject(_options.values) || isObject(_options.defaultValues)
1542
- ? cloneObject(_options.values || _options.defaultValues) || {}
1547
+ let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
1548
+ ? cloneObject(_options.defaultValues || _options.values) || {}
1543
1549
  : {};
1544
1550
  let _formValues = _options.shouldUnregister
1545
1551
  ? {}
@@ -1560,6 +1566,7 @@ function createFormControl(props = {}, flushRootRender) {
1560
1566
  const _proxyFormState = {
1561
1567
  isDirty: false,
1562
1568
  dirtyFields: false,
1569
+ validatingFields: false,
1563
1570
  touchedFields: false,
1564
1571
  isValidating: false,
1565
1572
  isValid: false,
@@ -1589,10 +1596,16 @@ function createFormControl(props = {}, flushRootRender) {
1589
1596
  }
1590
1597
  }
1591
1598
  };
1592
- const _updateIsValidating = (value) => _proxyFormState.isValidating &&
1593
- _subjects.state.next({
1594
- isValidating: value,
1595
- });
1599
+ const _updateIsValidating = (names, isValidating) => {
1600
+ if (_proxyFormState.isValidating || _proxyFormState.validatingFields) {
1601
+ (names || Array.from(_names.mount)).forEach((name) => name && set(_formState.validatingFields, name, !!isValidating));
1602
+ _formState.isValidating = Object.values(_formState.validatingFields).some((val) => val);
1603
+ _subjects.state.next({
1604
+ validatingFields: _formState.validatingFields,
1605
+ isValidating: _formState.isValidating,
1606
+ });
1607
+ }
1608
+ };
1596
1609
  const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
1597
1610
  if (args && method) {
1598
1611
  _state.action = true;
@@ -1721,9 +1734,13 @@ function createFormControl(props = {}, flushRootRender) {
1721
1734
  };
1722
1735
  _subjects.state.next(updatedFormState);
1723
1736
  }
1724
- _updateIsValidating(false);
1725
1737
  };
1726
- const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1738
+ const _executeSchema = async (name) => {
1739
+ _updateIsValidating(name, true);
1740
+ const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1741
+ _updateIsValidating(name);
1742
+ return result;
1743
+ };
1727
1744
  const executeSchemaAndUpdateState = async (names) => {
1728
1745
  const { errors } = await _executeSchema(names);
1729
1746
  if (names) {
@@ -1748,7 +1765,9 @@ function createFormControl(props = {}, flushRootRender) {
1748
1765
  const { _f, ...fieldValue } = field;
1749
1766
  if (_f) {
1750
1767
  const isFieldArrayRoot = _names.array.has(_f.name);
1768
+ _updateIsValidating([name], true);
1751
1769
  const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
1770
+ _updateIsValidating([name]);
1752
1771
  if (fieldError[_f.name]) {
1753
1772
  context.valid = false;
1754
1773
  if (shouldOnlyCheckValid) {
@@ -1877,10 +1896,9 @@ function createFormControl(props = {}, flushRootRender) {
1877
1896
  }
1878
1897
  isWatched(name, _names) && _subjects.state.next({ ..._formState });
1879
1898
  _subjects.values.next({
1880
- name,
1899
+ name: _state.mount ? name : undefined,
1881
1900
  values: { ..._formValues },
1882
1901
  });
1883
- !_state.mount && flushRootRender();
1884
1902
  };
1885
1903
  const onChange = async (event) => {
1886
1904
  const target = event.target;
@@ -1926,7 +1944,6 @@ function createFormControl(props = {}, flushRootRender) {
1926
1944
  _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1927
1945
  }
1928
1946
  !isBlurEvent && watched && _subjects.state.next({ ..._formState });
1929
- _updateIsValidating(true);
1930
1947
  if (_options.resolver) {
1931
1948
  const { errors } = await _executeSchema([name]);
1932
1949
  _updateIsFieldValueUpdated(fieldValue);
@@ -1939,7 +1956,9 @@ function createFormControl(props = {}, flushRootRender) {
1939
1956
  }
1940
1957
  }
1941
1958
  else {
1959
+ _updateIsValidating([name], true);
1942
1960
  error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1961
+ _updateIsValidating([name]);
1943
1962
  _updateIsFieldValueUpdated(fieldValue);
1944
1963
  if (isFieldValueUpdated) {
1945
1964
  if (error) {
@@ -1968,7 +1987,6 @@ function createFormControl(props = {}, flushRootRender) {
1968
1987
  let isValid;
1969
1988
  let validationResult;
1970
1989
  const fieldNames = convertToArrayPayload(name);
1971
- _updateIsValidating(true);
1972
1990
  if (_options.resolver) {
1973
1991
  const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
1974
1992
  isValid = isEmptyObject(errors);
@@ -1993,7 +2011,6 @@ function createFormControl(props = {}, flushRootRender) {
1993
2011
  : { name }),
1994
2012
  ...(_options.resolver || !name ? { isValid } : {}),
1995
2013
  errors: _formState.errors,
1996
- isValidating: false,
1997
2014
  });
1998
2015
  options.shouldFocus &&
1999
2016
  !validationResult &&
@@ -2015,6 +2032,7 @@ function createFormControl(props = {}, flushRootRender) {
2015
2032
  invalid: !!get((formState || _formState).errors, name),
2016
2033
  isDirty: !!get((formState || _formState).dirtyFields, name),
2017
2034
  isTouched: !!get((formState || _formState).touchedFields, name),
2035
+ isValidating: !!get((formState || _formState).validatingFields, name),
2018
2036
  error: get((formState || _formState).errors, name),
2019
2037
  });
2020
2038
  const clearErrors = (name) => {
@@ -2053,6 +2071,8 @@ function createFormControl(props = {}, flushRootRender) {
2053
2071
  !options.keepError && unset(_formState.errors, fieldName);
2054
2072
  !options.keepDirty && unset(_formState.dirtyFields, fieldName);
2055
2073
  !options.keepTouched && unset(_formState.touchedFields, fieldName);
2074
+ !options.keepIsValidating &&
2075
+ unset(_formState.validatingFields, fieldName);
2056
2076
  !_options.shouldUnregister &&
2057
2077
  !options.keepDefaultValue &&
2058
2078
  unset(_defaultValues, fieldName);
@@ -2252,9 +2272,8 @@ function createFormControl(props = {}, flushRootRender) {
2252
2272
  const _reset = (formValues, keepStateOptions = {}) => {
2253
2273
  const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
2254
2274
  const cloneUpdatedValues = cloneObject(updatedValues);
2255
- const values = formValues && !isEmptyObject(formValues)
2256
- ? cloneUpdatedValues
2257
- : _defaultValues;
2275
+ const isEmptyResetValues = isEmptyObject(formValues);
2276
+ const values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues;
2258
2277
  if (!keepStateOptions.keepDefaultValues) {
2259
2278
  _defaultValues = updatedValues;
2260
2279
  }
@@ -2299,14 +2318,13 @@ function createFormControl(props = {}, flushRootRender) {
2299
2318
  });
2300
2319
  }
2301
2320
  _names = {
2302
- mount: new Set(),
2321
+ mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
2303
2322
  unMount: new Set(),
2304
2323
  array: new Set(),
2305
2324
  watch: new Set(),
2306
2325
  watchAll: false,
2307
2326
  focus: '',
2308
2327
  };
2309
- !_state.mount && flushRootRender();
2310
2328
  _state.mount =
2311
2329
  !_proxyFormState.isValid ||
2312
2330
  !!keepStateOptions.keepIsValid ||
@@ -2316,20 +2334,24 @@ function createFormControl(props = {}, flushRootRender) {
2316
2334
  submitCount: keepStateOptions.keepSubmitCount
2317
2335
  ? _formState.submitCount
2318
2336
  : 0,
2319
- isDirty: keepStateOptions.keepDirty
2320
- ? _formState.isDirty
2321
- : !!(keepStateOptions.keepDefaultValues &&
2322
- !deepEqual(formValues, _defaultValues)),
2337
+ isDirty: isEmptyResetValues
2338
+ ? false
2339
+ : keepStateOptions.keepDirty
2340
+ ? _formState.isDirty
2341
+ : !!(keepStateOptions.keepDefaultValues &&
2342
+ !deepEqual(formValues, _defaultValues)),
2323
2343
  isSubmitted: keepStateOptions.keepIsSubmitted
2324
2344
  ? _formState.isSubmitted
2325
2345
  : false,
2326
- dirtyFields: keepStateOptions.keepDirtyValues
2327
- ? keepStateOptions.keepDefaultValues && _formValues
2328
- ? getDirtyFields(_defaultValues, _formValues)
2329
- : _formState.dirtyFields
2330
- : keepStateOptions.keepDefaultValues && formValues
2331
- ? getDirtyFields(_defaultValues, formValues)
2332
- : {},
2346
+ dirtyFields: isEmptyResetValues
2347
+ ? []
2348
+ : keepStateOptions.keepDirtyValues
2349
+ ? keepStateOptions.keepDefaultValues && _formValues
2350
+ ? getDirtyFields(_defaultValues, _formValues)
2351
+ : _formState.dirtyFields
2352
+ : keepStateOptions.keepDefaultValues && formValues
2353
+ ? getDirtyFields(_defaultValues, formValues)
2354
+ : {},
2333
2355
  touchedFields: keepStateOptions.keepTouched
2334
2356
  ? _formState.touchedFields
2335
2357
  : {},
@@ -2487,6 +2509,7 @@ function useForm(props = {}) {
2487
2509
  submitCount: 0,
2488
2510
  dirtyFields: {},
2489
2511
  touchedFields: {},
2512
+ validatingFields: {},
2490
2513
  errors: props.errors || {},
2491
2514
  disabled: props.disabled || false,
2492
2515
  defaultValues: isFunction(props.defaultValues)
@@ -2495,7 +2518,7 @@ function useForm(props = {}) {
2495
2518
  });
2496
2519
  if (!_formControl.current) {
2497
2520
  _formControl.current = {
2498
- ...createFormControl(props, () => updateFormState((formState) => ({ ...formState }))),
2521
+ ...createFormControl(props),
2499
2522
  formState,
2500
2523
  };
2501
2524
  }