react-hook-form 7.46.0-next.0 → 7.46.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.
@@ -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,17 @@ 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,
439
447
  onChange: React.useCallback((event) => _registerProps.current.onChange({
440
448
  target: {
441
449
  value: getEventValue(event),
@@ -636,6 +644,28 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
636
644
  }
637
645
  : {};
638
646
 
647
+ const focusFieldBy = (fields, callback, fieldsNames) => {
648
+ for (const key of fieldsNames || Object.keys(fields)) {
649
+ const field = get(fields, key);
650
+ if (field) {
651
+ const { _f, ...currentField } = field;
652
+ if (_f && callback(_f.name)) {
653
+ if (_f.ref.focus) {
654
+ _f.ref.focus();
655
+ break;
656
+ }
657
+ else if (_f.refs && _f.refs[0].focus) {
658
+ _f.refs[0].focus();
659
+ break;
660
+ }
661
+ }
662
+ else if (isObject(currentField)) {
663
+ focusFieldBy(currentField, callback);
664
+ }
665
+ }
666
+ }
667
+ };
668
+
639
669
  var generateId = () => {
640
670
  const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
641
671
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@@ -663,26 +693,6 @@ var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
663
693
  [..._names.watch].some((watchName) => name.startsWith(watchName) &&
664
694
  /^\.\w+/.test(name.slice(watchName.length))));
665
695
 
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
696
  var updateFieldArrayRootError = (errors, error, name) => {
687
697
  const fieldArrayErrors = compact(get(errors, name));
688
698
  set(fieldArrayErrors, 'root', error[name]);
@@ -1226,15 +1236,7 @@ function useFieldArray(props) {
1226
1236
  values: { ...control._formValues },
1227
1237
  });
1228
1238
  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
- });
1239
+ focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
1238
1240
  control._names.focus = '';
1239
1241
  control._updateValid();
1240
1242
  _actioned.current = false;
@@ -1515,7 +1517,6 @@ function createFormControl(props = {}, flushRootRender) {
1515
1517
  touchedFields: {},
1516
1518
  dirtyFields: {},
1517
1519
  errors: {},
1518
- disabled: false,
1519
1520
  };
1520
1521
  let _fields = {};
1521
1522
  let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
@@ -1698,7 +1699,7 @@ function createFormControl(props = {}, flushRootRender) {
1698
1699
  };
1699
1700
  const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1700
1701
  const executeSchemaAndUpdateState = async (names) => {
1701
- const { errors } = await _executeSchema();
1702
+ const { errors } = await _executeSchema(names);
1702
1703
  if (names) {
1703
1704
  for (const name of names) {
1704
1705
  const error = get(errors, name);
@@ -1924,13 +1925,6 @@ function createFormControl(props = {}, flushRootRender) {
1924
1925
  }
1925
1926
  }
1926
1927
  };
1927
- const _focusInput = (ref, key) => {
1928
- if (get(_formState.errors, key) && ref.focus) {
1929
- ref.focus();
1930
- return 1;
1931
- }
1932
- return;
1933
- };
1934
1928
  const trigger = async (name, options = {}) => {
1935
1929
  let isValid;
1936
1930
  let validationResult;
@@ -1964,7 +1958,7 @@ function createFormControl(props = {}, flushRootRender) {
1964
1958
  });
1965
1959
  options.shouldFocus &&
1966
1960
  !validationResult &&
1967
- iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
1961
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
1968
1962
  return validationResult;
1969
1963
  };
1970
1964
  const getValues = (fieldNames) => {
@@ -2033,6 +2027,15 @@ function createFormControl(props = {}, flushRootRender) {
2033
2027
  });
2034
2028
  !options.keepIsValid && _updateValid();
2035
2029
  };
2030
+ const _updateDisabledField = ({ disabled, name, field, fields, }) => {
2031
+ if (isBoolean(disabled)) {
2032
+ const value = disabled
2033
+ ? undefined
2034
+ : get(_formValues, name, getFieldValue(field ? field._f : get(fields, name)._f));
2035
+ set(_formValues, name, value);
2036
+ updateTouchAndDirty(name, value, false, false, true);
2037
+ }
2038
+ };
2036
2039
  const register = (name, options = {}) => {
2037
2040
  let field = get(_fields, name);
2038
2041
  const disabledIsDefined = isBoolean(options.disabled);
@@ -2046,12 +2049,16 @@ function createFormControl(props = {}, flushRootRender) {
2046
2049
  },
2047
2050
  });
2048
2051
  _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);
2052
+ if (field) {
2053
+ _updateDisabledField({
2054
+ field,
2055
+ disabled: options.disabled,
2056
+ name,
2057
+ });
2058
+ }
2059
+ else {
2060
+ updateValidAndValue(name, true, options.value);
2061
+ }
2055
2062
  return {
2056
2063
  ...(disabledIsDefined ? { disabled: options.disabled } : {}),
2057
2064
  ...(_options.progressive
@@ -2113,15 +2120,7 @@ function createFormControl(props = {}, flushRootRender) {
2113
2120
  };
2114
2121
  };
2115
2122
  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
- };
2123
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
2125
2124
  const handleSubmit = (onValid, onInvalid) => async (e) => {
2126
2125
  if (e) {
2127
2126
  e.preventDefault && e.preventDefault();
@@ -2187,7 +2186,7 @@ function createFormControl(props = {}, flushRootRender) {
2187
2186
  }
2188
2187
  };
2189
2188
  const _reset = (formValues, keepStateOptions = {}) => {
2190
- const updatedValues = formValues || _defaultValues;
2189
+ const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
2191
2190
  const cloneUpdatedValues = cloneObject(updatedValues);
2192
2191
  const values = formValues && !isEmptyObject(formValues)
2193
2192
  ? cloneUpdatedValues
@@ -2312,11 +2311,11 @@ function createFormControl(props = {}, flushRootRender) {
2312
2311
  _updateValid,
2313
2312
  _removeUnmounted,
2314
2313
  _updateFieldArray,
2314
+ _updateDisabledField,
2315
2315
  _getFieldArray,
2316
2316
  _reset,
2317
2317
  _resetDefaultValues,
2318
2318
  _updateFormState,
2319
- _disableForm,
2320
2319
  _subjects,
2321
2320
  _proxyFormState,
2322
2321
  get _fields() {
@@ -2416,7 +2415,6 @@ function useForm(props = {}) {
2416
2415
  dirtyFields: {},
2417
2416
  touchedFields: {},
2418
2417
  errors: {},
2419
- disabled: false,
2420
2418
  defaultValues: isFunction(props.defaultValues)
2421
2419
  ? undefined
2422
2420
  : props.defaultValues,
@@ -2457,7 +2455,6 @@ function useForm(props = {}) {
2457
2455
  }
2458
2456
  control._removeUnmounted();
2459
2457
  });
2460
- React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
2461
2458
  _formControl.current.formState = getProxyFormState(formState, control);
2462
2459
  return _formControl.current;
2463
2460
  }