react-hook-form 7.46.2 → 7.48.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.
@@ -138,7 +138,7 @@ const useFormContext = () => React.useContext(HookFormContext);
138
138
  * @remarks
139
139
  * [API](https://react-hook-form.com/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
140
140
  *
141
- * @param props - all useFrom methods
141
+ * @param props - all useForm methods
142
142
  *
143
143
  * @example
144
144
  * ```tsx
@@ -199,15 +199,13 @@ var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, is
199
199
 
200
200
  var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
201
201
 
202
- var shouldSubscribeByName = (name, signalName, exact) => exact && signalName
203
- ? name === signalName ||
204
- (Array.isArray(name) &&
205
- name.some((currentName) => currentName && exact && currentName === signalName))
206
- : !name ||
207
- !signalName ||
208
- name === signalName ||
209
- convertToArrayPayload(name).some((currentName) => currentName &&
210
- (currentName.startsWith(signalName) ||
202
+ var shouldSubscribeByName = (name, signalName, exact) => !name ||
203
+ !signalName ||
204
+ name === signalName ||
205
+ convertToArrayPayload(name).some((currentName) => currentName &&
206
+ (exact
207
+ ? currentName === signalName
208
+ : currentName.startsWith(signalName) ||
211
209
  signalName.startsWith(currentName)));
212
210
 
213
211
  function useSubscribe(props) {
@@ -315,7 +313,7 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
315
313
  *
316
314
  * @example
317
315
  * ```tsx
318
- * const { watch } = useForm();
316
+ * const { control } = useForm();
319
317
  * const values = useWatch({
320
318
  * name: "fieldName"
321
319
  * control,
@@ -436,17 +434,22 @@ function useController(props) {
436
434
  };
437
435
  }, [name, control, isArrayField, shouldUnregister]);
438
436
  React.useEffect(() => {
439
- control._updateDisabledField({
440
- disabled,
441
- fields: control._fields,
442
- name,
443
- });
437
+ if (get(control._fields, name)) {
438
+ control._updateDisabledField({
439
+ disabled,
440
+ fields: control._fields,
441
+ name,
442
+ value: get(control._fields, name)._f.value,
443
+ });
444
+ }
444
445
  }, [disabled, name, control]);
445
446
  return {
446
447
  field: {
447
448
  name,
448
449
  value,
449
- ...(isBoolean(disabled) ? { disabled } : {}),
450
+ ...(isBoolean(disabled) || isBoolean(formState.disabled)
451
+ ? { disabled: formState.disabled || disabled }
452
+ : {}),
450
453
  onChange: React.useCallback((event) => _registerProps.current.onChange({
451
454
  target: {
452
455
  value: getEventValue(event),
@@ -647,28 +650,6 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
647
650
  }
648
651
  : {};
649
652
 
650
- const focusFieldBy = (fields, callback, fieldsNames) => {
651
- for (const key of fieldsNames || Object.keys(fields)) {
652
- const field = get(fields, key);
653
- if (field) {
654
- const { _f, ...currentField } = field;
655
- if (_f && callback(_f.name)) {
656
- if (_f.ref.focus) {
657
- _f.ref.focus();
658
- break;
659
- }
660
- else if (_f.refs && _f.refs[0].focus) {
661
- _f.refs[0].focus();
662
- break;
663
- }
664
- }
665
- else if (isObject(currentField)) {
666
- focusFieldBy(currentField, callback);
667
- }
668
- }
669
- }
670
- };
671
-
672
653
  var generateId = () => {
673
654
  const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
674
655
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@@ -696,6 +677,26 @@ var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
696
677
  [..._names.watch].some((watchName) => name.startsWith(watchName) &&
697
678
  /^\.\w+/.test(name.slice(watchName.length))));
698
679
 
680
+ const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
681
+ for (const key of fieldsNames || Object.keys(fields)) {
682
+ const field = get(fields, key);
683
+ if (field) {
684
+ const { _f, ...currentField } = field;
685
+ if (_f) {
686
+ if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
687
+ break;
688
+ }
689
+ else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
690
+ break;
691
+ }
692
+ }
693
+ else if (isObject(currentField)) {
694
+ iterateFieldsByAction(currentField, action);
695
+ }
696
+ }
697
+ }
698
+ };
699
+
699
700
  var updateFieldArrayRootError = (errors, error, name) => {
700
701
  const fieldArrayErrors = compact(get(errors, name));
701
702
  set(fieldArrayErrors, 'root', error[name]);
@@ -1237,7 +1238,15 @@ function useFieldArray(props) {
1237
1238
  values: { ...control._formValues },
1238
1239
  });
1239
1240
  control._names.focus &&
1240
- focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
1241
+ iterateFieldsByAction(control._fields, (ref, key) => {
1242
+ if (control._names.focus &&
1243
+ key.startsWith(control._names.focus) &&
1244
+ ref.focus) {
1245
+ ref.focus();
1246
+ return 1;
1247
+ }
1248
+ return;
1249
+ });
1241
1250
  control._names.focus = '';
1242
1251
  control._updateValid();
1243
1252
  _actioned.current = false;
@@ -1518,6 +1527,7 @@ function createFormControl(props = {}, flushRootRender) {
1518
1527
  touchedFields: {},
1519
1528
  dirtyFields: {},
1520
1529
  errors: {},
1530
+ disabled: false,
1521
1531
  };
1522
1532
  let _fields = {};
1523
1533
  let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
@@ -1863,6 +1873,11 @@ function createFormControl(props = {}, flushRootRender) {
1863
1873
  let isFieldValueUpdated = true;
1864
1874
  const field = get(_fields, name);
1865
1875
  const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
1876
+ const _updateIsFieldValueUpdated = (fieldValue) => {
1877
+ isFieldValueUpdated =
1878
+ Number.isNaN(fieldValue) ||
1879
+ fieldValue === get(_formValues, name, fieldValue);
1880
+ };
1866
1881
  if (field) {
1867
1882
  let error;
1868
1883
  let isValid;
@@ -1899,17 +1914,18 @@ function createFormControl(props = {}, flushRootRender) {
1899
1914
  _updateIsValidating(true);
1900
1915
  if (_options.resolver) {
1901
1916
  const { errors } = await _executeSchema([name]);
1902
- const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
1903
- const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
1904
- error = errorLookupResult.error;
1905
- name = errorLookupResult.name;
1906
- isValid = isEmptyObject(errors);
1917
+ _updateIsFieldValueUpdated(fieldValue);
1918
+ if (isFieldValueUpdated) {
1919
+ const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
1920
+ const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
1921
+ error = errorLookupResult.error;
1922
+ name = errorLookupResult.name;
1923
+ isValid = isEmptyObject(errors);
1924
+ }
1907
1925
  }
1908
1926
  else {
1909
1927
  error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1910
- isFieldValueUpdated =
1911
- Number.isNaN(fieldValue) ||
1912
- fieldValue === get(_formValues, name, fieldValue);
1928
+ _updateIsFieldValueUpdated(fieldValue);
1913
1929
  if (isFieldValueUpdated) {
1914
1930
  if (error) {
1915
1931
  isValid = false;
@@ -1926,6 +1942,13 @@ function createFormControl(props = {}, flushRootRender) {
1926
1942
  }
1927
1943
  }
1928
1944
  };
1945
+ const _focusInput = (ref, key) => {
1946
+ if (get(_formState.errors, key) && ref.focus) {
1947
+ ref.focus();
1948
+ return 1;
1949
+ }
1950
+ return;
1951
+ };
1929
1952
  const trigger = async (name, options = {}) => {
1930
1953
  let isValid;
1931
1954
  let validationResult;
@@ -1959,7 +1982,7 @@ function createFormControl(props = {}, flushRootRender) {
1959
1982
  });
1960
1983
  options.shouldFocus &&
1961
1984
  !validationResult &&
1962
- focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
1985
+ iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
1963
1986
  return validationResult;
1964
1987
  };
1965
1988
  const getValues = (fieldNames) => {
@@ -2028,13 +2051,15 @@ function createFormControl(props = {}, flushRootRender) {
2028
2051
  });
2029
2052
  !options.keepIsValid && _updateValid();
2030
2053
  };
2031
- const _updateDisabledField = ({ disabled, name, field, fields, }) => {
2054
+ const _updateDisabledField = ({ disabled, name, field, fields, value, }) => {
2032
2055
  if (isBoolean(disabled)) {
2033
- const value = disabled
2056
+ const inputValue = disabled
2034
2057
  ? 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);
2058
+ : isUndefined(value)
2059
+ ? getFieldValue(field ? field._f : get(fields, name)._f)
2060
+ : value;
2061
+ set(_formValues, name, inputValue);
2062
+ updateTouchAndDirty(name, inputValue, false, false, true);
2038
2063
  }
2039
2064
  };
2040
2065
  const register = (name, options = {}) => {
@@ -2121,7 +2146,15 @@ function createFormControl(props = {}, flushRootRender) {
2121
2146
  };
2122
2147
  };
2123
2148
  const _focusError = () => _options.shouldFocusError &&
2124
- focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
2149
+ iterateFieldsByAction(_fields, _focusInput, _names.mount);
2150
+ const _disableForm = (disabled) => {
2151
+ if (isBoolean(props.disabled)) {
2152
+ _subjects.state.next({ disabled: props.disabled });
2153
+ iterateFieldsByAction(_fields, (ref) => {
2154
+ ref.disabled = disabled;
2155
+ }, 0, false);
2156
+ }
2157
+ };
2125
2158
  const handleSubmit = (onValid, onInvalid) => async (e) => {
2126
2159
  if (e) {
2127
2160
  e.preventDefault && e.preventDefault();
@@ -2266,8 +2299,10 @@ function createFormControl(props = {}, flushRootRender) {
2266
2299
  ? _formState.touchedFields
2267
2300
  : {},
2268
2301
  errors: keepStateOptions.keepErrors ? _formState.errors : {},
2302
+ isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful
2303
+ ? _formState.isSubmitSuccessful
2304
+ : false,
2269
2305
  isSubmitting: false,
2270
- isSubmitSuccessful: false,
2271
2306
  });
2272
2307
  };
2273
2308
  const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
@@ -2317,6 +2352,7 @@ function createFormControl(props = {}, flushRootRender) {
2317
2352
  _reset,
2318
2353
  _resetDefaultValues,
2319
2354
  _updateFormState,
2355
+ _disableForm,
2320
2356
  _subjects,
2321
2357
  _proxyFormState,
2322
2358
  get _fields() {
@@ -2416,6 +2452,7 @@ function useForm(props = {}) {
2416
2452
  dirtyFields: {},
2417
2453
  touchedFields: {},
2418
2454
  errors: {},
2455
+ disabled: false,
2419
2456
  defaultValues: isFunction(props.defaultValues)
2420
2457
  ? undefined
2421
2458
  : props.defaultValues,
@@ -2436,6 +2473,7 @@ function useForm(props = {}) {
2436
2473
  }
2437
2474
  },
2438
2475
  });
2476
+ React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
2439
2477
  React.useEffect(() => {
2440
2478
  if (props.values && !deepEqual(props.values, _values.current)) {
2441
2479
  control._reset(props.values, control._options.resetOptions);