react-hook-form 7.52.1 → 7.53.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.
@@ -407,6 +407,7 @@ function useController(props) {
407
407
  const formState = useFormState({
408
408
  control,
409
409
  name,
410
+ exact: true,
410
411
  });
411
412
  const _registerProps = React.useRef(control.register(name, {
412
413
  ...props.rules,
@@ -468,7 +469,7 @@ function useController(props) {
468
469
  },
469
470
  type: EVENTS.BLUR,
470
471
  }), [name, control]),
471
- ref: (elm) => {
472
+ ref: React.useCallback((elm) => {
472
473
  const field = get(control._fields, name);
473
474
  if (field && elm) {
474
475
  field._f.ref = {
@@ -478,7 +479,7 @@ function useController(props) {
478
479
  reportValidity: () => elm.reportValidity(),
479
480
  };
480
481
  }
481
- },
482
+ }, [control._fields, name]),
482
483
  },
483
484
  formState,
484
485
  fieldState: Object.defineProperties({}, {
@@ -692,20 +693,25 @@ const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
692
693
  const { _f, ...currentField } = field;
693
694
  if (_f) {
694
695
  if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
695
- break;
696
+ return true;
696
697
  }
697
698
  else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
698
- break;
699
+ return true;
699
700
  }
700
701
  else {
701
- iterateFieldsByAction(currentField, action);
702
+ if (iterateFieldsByAction(currentField, action)) {
703
+ break;
704
+ }
702
705
  }
703
706
  }
704
707
  else if (isObject(currentField)) {
705
- iterateFieldsByAction(currentField, action);
708
+ if (iterateFieldsByAction(currentField, action)) {
709
+ break;
710
+ }
706
711
  }
707
712
  }
708
713
  }
714
+ return;
709
715
  };
710
716
 
711
717
  var updateFieldArrayRootError = (errors, error, name) => {
@@ -1465,6 +1471,13 @@ var getRuleValue = (rule) => isUndefined(rule)
1465
1471
  : rule.value
1466
1472
  : rule;
1467
1473
 
1474
+ const ASYNC_FUNCTION = 'AsyncFunction';
1475
+ var hasPromiseValidation = (fieldReference) => (!fieldReference || !fieldReference.validate) &&
1476
+ !!((isFunction(fieldReference.validate) &&
1477
+ fieldReference.validate.constructor.name === ASYNC_FUNCTION) ||
1478
+ (isObject(fieldReference.validate) &&
1479
+ Object.values(fieldReference.validate).find((validateFunction) => validateFunction.constructor.name === ASYNC_FUNCTION)));
1480
+
1468
1481
  var hasValidation = (options) => options.mount &&
1469
1482
  (options.required ||
1470
1483
  options.min ||
@@ -1775,9 +1788,14 @@ function createFormControl(props = {}) {
1775
1788
  const { _f, ...fieldValue } = field;
1776
1789
  if (_f) {
1777
1790
  const isFieldArrayRoot = _names.array.has(_f.name);
1778
- _updateIsValidating([name], true);
1791
+ const isPromiseFunction = field._f && hasPromiseValidation(field._f);
1792
+ if (isPromiseFunction && _proxyFormState.validatingFields) {
1793
+ _updateIsValidating([name], true);
1794
+ }
1779
1795
  const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
1780
- _updateIsValidating([name]);
1796
+ if (isPromiseFunction && _proxyFormState.validatingFields) {
1797
+ _updateIsValidating([name]);
1798
+ }
1781
1799
  if (fieldError[_f.name]) {
1782
1800
  context.valid = false;
1783
1801
  if (shouldOnlyCheckValid) {
@@ -1791,7 +1809,7 @@ function createFormControl(props = {}) {
1791
1809
  : set(_formState.errors, _f.name, fieldError[_f.name])
1792
1810
  : unset(_formState.errors, _f.name));
1793
1811
  }
1794
- fieldValue &&
1812
+ !isEmptyObject(fieldValue) &&
1795
1813
  (await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
1796
1814
  }
1797
1815
  }
@@ -1920,7 +1938,7 @@ function createFormControl(props = {}) {
1920
1938
  const _updateIsFieldValueUpdated = (fieldValue) => {
1921
1939
  isFieldValueUpdated =
1922
1940
  Number.isNaN(fieldValue) ||
1923
- fieldValue === get(_formValues, name, fieldValue);
1941
+ deepEqual(fieldValue, get(_formValues, name, fieldValue));
1924
1942
  };
1925
1943
  if (field) {
1926
1944
  let error;
@@ -1950,7 +1968,16 @@ function createFormControl(props = {}) {
1950
1968
  values: { ..._formValues },
1951
1969
  });
1952
1970
  if (shouldSkipValidation) {
1953
- _proxyFormState.isValid && _updateValid();
1971
+ if (_proxyFormState.isValid) {
1972
+ if (props.mode === 'onBlur') {
1973
+ if (isBlurEvent) {
1974
+ _updateValid();
1975
+ }
1976
+ }
1977
+ else {
1978
+ _updateValid();
1979
+ }
1980
+ }
1954
1981
  return (shouldRender &&
1955
1982
  _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1956
1983
  }
@@ -2113,7 +2140,7 @@ function createFormControl(props = {}) {
2113
2140
  };
2114
2141
  const register = (name, options = {}) => {
2115
2142
  let field = get(_fields, name);
2116
- const disabledIsDefined = isBoolean(options.disabled);
2143
+ const disabledIsDefined = isBoolean(options.disabled) || isBoolean(props.disabled);
2117
2144
  set(_fields, name, {
2118
2145
  ...(field || {}),
2119
2146
  _f: {
@@ -2127,7 +2154,9 @@ function createFormControl(props = {}) {
2127
2154
  if (field) {
2128
2155
  _updateDisabledField({
2129
2156
  field,
2130
- disabled: options.disabled,
2157
+ disabled: isBoolean(options.disabled)
2158
+ ? options.disabled
2159
+ : props.disabled,
2131
2160
  name,
2132
2161
  value: options.value,
2133
2162
  });
@@ -2136,7 +2165,9 @@ function createFormControl(props = {}) {
2136
2165
  updateValidAndValue(name, true, options.value);
2137
2166
  }
2138
2167
  return {
2139
- ...(disabledIsDefined ? { disabled: options.disabled } : {}),
2168
+ ...(disabledIsDefined
2169
+ ? { disabled: options.disabled || props.disabled }
2170
+ : {}),
2140
2171
  ...(_options.progressive
2141
2172
  ? {
2142
2173
  required: !!options.required,