react-hook-form 7.46.0-next.0 → 7.46.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.
@@ -198,7 +198,9 @@ var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, is
198
198
  var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
199
199
 
200
200
  var shouldSubscribeByName = (name, signalName, exact) => exact && signalName
201
- ? name === signalName
201
+ ? name === signalName ||
202
+ (Array.isArray(name) &&
203
+ name.some((currentName) => currentName && exact && currentName === signalName))
202
204
  : !name ||
203
205
  !signalName ||
204
206
  name === signalName ||
@@ -390,7 +392,7 @@ function set(object, path, value) {
390
392
  */
391
393
  function useController(props) {
392
394
  const methods = useFormContext();
393
- const { name, control = methods.control, shouldUnregister } = props;
395
+ const { name, disabled, control = methods.control, shouldUnregister } = props;
394
396
  const isArrayField = isNameInFieldArray(control._names.array, name);
395
397
  const value = useWatch({
396
398
  control,
@@ -431,11 +433,18 @@ function useController(props) {
431
433
  : updateMounted(name, false);
432
434
  };
433
435
  }, [name, control, isArrayField, shouldUnregister]);
436
+ React.useEffect(() => {
437
+ control._updateDisabledField({
438
+ disabled,
439
+ fields: control._fields,
440
+ name,
441
+ });
442
+ }, [disabled, name, control]);
434
443
  return {
435
444
  field: {
436
445
  name,
437
446
  value,
438
- disabled: formState.disabled,
447
+ disabled,
439
448
  onChange: React.useCallback((event) => _registerProps.current.onChange({
440
449
  target: {
441
450
  value: getEventValue(event),
@@ -636,6 +645,28 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
636
645
  }
637
646
  : {};
638
647
 
648
+ const focusFieldBy = (fields, callback, fieldsNames) => {
649
+ for (const key of fieldsNames || Object.keys(fields)) {
650
+ const field = get(fields, key);
651
+ if (field) {
652
+ const { _f, ...currentField } = field;
653
+ if (_f && callback(_f.name)) {
654
+ if (_f.ref.focus) {
655
+ _f.ref.focus();
656
+ break;
657
+ }
658
+ else if (_f.refs && _f.refs[0].focus) {
659
+ _f.refs[0].focus();
660
+ break;
661
+ }
662
+ }
663
+ else if (isObject(currentField)) {
664
+ focusFieldBy(currentField, callback);
665
+ }
666
+ }
667
+ }
668
+ };
669
+
639
670
  var generateId = () => {
640
671
  const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
641
672
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@@ -663,26 +694,6 @@ var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
663
694
  [..._names.watch].some((watchName) => name.startsWith(watchName) &&
664
695
  /^\.\w+/.test(name.slice(watchName.length))));
665
696
 
666
- const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
667
- for (const key of fieldsNames || Object.keys(fields)) {
668
- const field = get(fields, key);
669
- if (field) {
670
- const { _f, ...currentField } = field;
671
- if (_f) {
672
- if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
673
- break;
674
- }
675
- else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
676
- break;
677
- }
678
- }
679
- else if (isObject(currentField)) {
680
- iterateFieldsByAction(currentField, action);
681
- }
682
- }
683
- }
684
- };
685
-
686
697
  var updateFieldArrayRootError = (errors, error, name) => {
687
698
  const fieldArrayErrors = compact(get(errors, name));
688
699
  set(fieldArrayErrors, 'root', error[name]);
@@ -1226,15 +1237,7 @@ function useFieldArray(props) {
1226
1237
  values: { ...control._formValues },
1227
1238
  });
1228
1239
  control._names.focus &&
1229
- iterateFieldsByAction(control._fields, (ref, key) => {
1230
- if (control._names.focus &&
1231
- key.startsWith(control._names.focus) &&
1232
- ref.focus) {
1233
- ref.focus();
1234
- return 1;
1235
- }
1236
- return;
1237
- });
1240
+ focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
1238
1241
  control._names.focus = '';
1239
1242
  control._updateValid();
1240
1243
  _actioned.current = false;
@@ -1515,7 +1518,6 @@ function createFormControl(props = {}, flushRootRender) {
1515
1518
  touchedFields: {},
1516
1519
  dirtyFields: {},
1517
1520
  errors: {},
1518
- disabled: false,
1519
1521
  };
1520
1522
  let _fields = {};
1521
1523
  let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
@@ -1698,7 +1700,7 @@ function createFormControl(props = {}, flushRootRender) {
1698
1700
  };
1699
1701
  const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1700
1702
  const executeSchemaAndUpdateState = async (names) => {
1701
- const { errors } = await _executeSchema();
1703
+ const { errors } = await _executeSchema(names);
1702
1704
  if (names) {
1703
1705
  for (const name of names) {
1704
1706
  const error = get(errors, name);
@@ -1924,13 +1926,6 @@ function createFormControl(props = {}, flushRootRender) {
1924
1926
  }
1925
1927
  }
1926
1928
  };
1927
- const _focusInput = (ref, key) => {
1928
- if (get(_formState.errors, key) && ref.focus) {
1929
- ref.focus();
1930
- return 1;
1931
- }
1932
- return;
1933
- };
1934
1929
  const trigger = async (name, options = {}) => {
1935
1930
  let isValid;
1936
1931
  let validationResult;
@@ -1964,7 +1959,7 @@ function createFormControl(props = {}, flushRootRender) {
1964
1959
  });
1965
1960
  options.shouldFocus &&
1966
1961
  !validationResult &&
1967
- iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
1962
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
1968
1963
  return validationResult;
1969
1964
  };
1970
1965
  const getValues = (fieldNames) => {
@@ -2033,6 +2028,15 @@ function createFormControl(props = {}, flushRootRender) {
2033
2028
  });
2034
2029
  !options.keepIsValid && _updateValid();
2035
2030
  };
2031
+ const _updateDisabledField = ({ disabled, name, field, fields, }) => {
2032
+ if (isBoolean(disabled)) {
2033
+ const value = disabled
2034
+ ? undefined
2035
+ : get(_formValues, name, getFieldValue(field ? field._f : get(fields, name)._f));
2036
+ set(_formValues, name, value);
2037
+ updateTouchAndDirty(name, value, false, false, true);
2038
+ }
2039
+ };
2036
2040
  const register = (name, options = {}) => {
2037
2041
  let field = get(_fields, name);
2038
2042
  const disabledIsDefined = isBoolean(options.disabled);
@@ -2046,12 +2050,16 @@ function createFormControl(props = {}, flushRootRender) {
2046
2050
  },
2047
2051
  });
2048
2052
  _names.mount.add(name);
2049
- field
2050
- ? disabledIsDefined &&
2051
- set(_formValues, name, options.disabled
2052
- ? undefined
2053
- : get(_formValues, name, getFieldValue(field._f)))
2054
- : updateValidAndValue(name, true, options.value);
2053
+ if (field) {
2054
+ _updateDisabledField({
2055
+ field,
2056
+ disabled: options.disabled,
2057
+ name,
2058
+ });
2059
+ }
2060
+ else {
2061
+ updateValidAndValue(name, true, options.value);
2062
+ }
2055
2063
  return {
2056
2064
  ...(disabledIsDefined ? { disabled: options.disabled } : {}),
2057
2065
  ...(_options.progressive
@@ -2113,15 +2121,7 @@ function createFormControl(props = {}, flushRootRender) {
2113
2121
  };
2114
2122
  };
2115
2123
  const _focusError = () => _options.shouldFocusError &&
2116
- iterateFieldsByAction(_fields, _focusInput, _names.mount);
2117
- const _disableForm = (disabled) => {
2118
- if (isBoolean(props.disabled)) {
2119
- _subjects.state.next({ disabled: props.disabled });
2120
- iterateFieldsByAction(_fields, (ref) => {
2121
- ref.disabled = disabled;
2122
- }, 0, false);
2123
- }
2124
- };
2124
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
2125
2125
  const handleSubmit = (onValid, onInvalid) => async (e) => {
2126
2126
  if (e) {
2127
2127
  e.preventDefault && e.preventDefault();
@@ -2187,7 +2187,7 @@ function createFormControl(props = {}, flushRootRender) {
2187
2187
  }
2188
2188
  };
2189
2189
  const _reset = (formValues, keepStateOptions = {}) => {
2190
- const updatedValues = formValues || _defaultValues;
2190
+ const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
2191
2191
  const cloneUpdatedValues = cloneObject(updatedValues);
2192
2192
  const values = formValues && !isEmptyObject(formValues)
2193
2193
  ? cloneUpdatedValues
@@ -2312,11 +2312,11 @@ function createFormControl(props = {}, flushRootRender) {
2312
2312
  _updateValid,
2313
2313
  _removeUnmounted,
2314
2314
  _updateFieldArray,
2315
+ _updateDisabledField,
2315
2316
  _getFieldArray,
2316
2317
  _reset,
2317
2318
  _resetDefaultValues,
2318
2319
  _updateFormState,
2319
- _disableForm,
2320
2320
  _subjects,
2321
2321
  _proxyFormState,
2322
2322
  get _fields() {
@@ -2416,7 +2416,6 @@ function useForm(props = {}) {
2416
2416
  dirtyFields: {},
2417
2417
  touchedFields: {},
2418
2418
  errors: {},
2419
- disabled: false,
2420
2419
  defaultValues: isFunction(props.defaultValues)
2421
2420
  ? undefined
2422
2421
  : props.defaultValues,
@@ -2457,7 +2456,6 @@ function useForm(props = {}) {
2457
2456
  }
2458
2457
  control._removeUnmounted();
2459
2458
  });
2460
- React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
2461
2459
  _formControl.current.formState = getProxyFormState(formState, control);
2462
2460
  return _formControl.current;
2463
2461
  }