@bombillazo/rhf-plus 7.60.1-plus.0 → 7.62.0-plus.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.
@@ -41,7 +41,7 @@ function cloneObject(data) {
41
41
  }
42
42
  else if (!(isWeb && (data instanceof Blob || isFileListInstance)) &&
43
43
  (isArray || isObject(data))) {
44
- copy = isArray ? [] : {};
44
+ copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));
45
45
  if (!isArray && !isPlainObject(data)) {
46
46
  copy = data;
47
47
  }
@@ -480,16 +480,30 @@ function useController(props) {
480
480
  };
481
481
  }
482
482
  }, [control._fields, name]);
483
- const field = React.useMemo(() => ({
484
- name,
485
- value,
486
- ...(isBoolean(disabled) || formState.disabled
487
- ? { disabled: formState.disabled || disabled }
488
- : {}),
489
- onChange,
490
- onBlur,
491
- ref,
492
- }), [name, disabled, formState.disabled, onChange, onBlur, ref, value]);
483
+ const field = React.useMemo(() => {
484
+ // Calculate if this specific field should be disabled
485
+ let isFieldDisabled;
486
+ if (isBoolean(disabled)) {
487
+ // Field-level disabled prop takes precedence
488
+ isFieldDisabled = disabled;
489
+ }
490
+ else if (isBoolean(control._options.disabled)) {
491
+ // Form-level boolean disabled
492
+ isFieldDisabled = control._options.disabled;
493
+ }
494
+ else if (Array.isArray(control._options.disabled)) {
495
+ // Form-level array disabled - check if this field is in the array
496
+ isFieldDisabled = control._options.disabled.includes(name);
497
+ }
498
+ return {
499
+ name,
500
+ value,
501
+ ...(isBoolean(isFieldDisabled) ? { disabled: isFieldDisabled } : {}),
502
+ onChange,
503
+ onBlur,
504
+ ref,
505
+ };
506
+ }, [name, disabled, control._options.disabled, onChange, onBlur, ref, value]);
493
507
  React.useEffect(() => {
494
508
  const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
495
509
  control.register(name, {
@@ -1347,7 +1361,9 @@ function createFormControl(props = {}) {
1347
1361
  dirtyFields: {},
1348
1362
  validatingFields: {},
1349
1363
  errors: _options.errors || {},
1350
- disabled: _options.disabled || false,
1364
+ disabled: Array.isArray(_options.disabled)
1365
+ ? false
1366
+ : _options.disabled || false,
1351
1367
  metadata: _options.defaultMetadata || {},
1352
1368
  };
1353
1369
  let _fields = {};
@@ -1394,7 +1410,7 @@ function createFormControl(props = {}) {
1394
1410
  timer = setTimeout(callback, wait);
1395
1411
  };
1396
1412
  const _setValid = async (shouldUpdateValid) => {
1397
- if (!_options.disabled &&
1413
+ if ((Array.isArray(_options.disabled) || !_options.disabled) &&
1398
1414
  (_proxyFormState.isValid ||
1399
1415
  _proxySubscribeFormState.isValid ||
1400
1416
  shouldUpdateValid)) {
@@ -1756,6 +1772,22 @@ function createFormControl(props = {}) {
1756
1772
  const validationModeBeforeSubmit = getValidationModes(_options.mode);
1757
1773
  const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
1758
1774
  if (field) {
1775
+ // Check if field is disabled and should not process events
1776
+ const isFieldDisabled = isBoolean(field._f.disabled)
1777
+ ? field._f.disabled
1778
+ : isBoolean(_options.disabled)
1779
+ ? _options.disabled
1780
+ : Array.isArray(_options.disabled)
1781
+ ? new Set(_options.disabled).has(name)
1782
+ : false;
1783
+ if (isFieldDisabled) {
1784
+ // Restore the original value if the field is disabled
1785
+ const originalValue = get(_formValues, name);
1786
+ if (target.value !== originalValue) {
1787
+ target.value = originalValue;
1788
+ }
1789
+ return;
1790
+ }
1759
1791
  let error;
1760
1792
  let isValid;
1761
1793
  const fieldValue = target.type
@@ -1770,8 +1802,10 @@ function createFormControl(props = {}) {
1770
1802
  const watched = isWatched(name, _names, isBlurEvent);
1771
1803
  set(_formValues, name, fieldValue);
1772
1804
  if (isBlurEvent) {
1773
- field._f.onBlur && field._f.onBlur(event);
1774
- delayErrorCallback && delayErrorCallback(0);
1805
+ if (!target || !target.readOnly) {
1806
+ field._f.onBlur && field._f.onBlur(event);
1807
+ delayErrorCallback && delayErrorCallback(0);
1808
+ }
1775
1809
  }
1776
1810
  else if (field._f.onChange) {
1777
1811
  field._f.onChange(event);
@@ -1980,7 +2014,9 @@ function createFormControl(props = {}) {
1980
2014
  };
1981
2015
  const register = (name, options = {}) => {
1982
2016
  let field = get(_fields, name);
1983
- const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
2017
+ const disabledIsDefined = isBoolean(options.disabled) ||
2018
+ isBoolean(_options.disabled) ||
2019
+ Array.isArray(_options.disabled);
1984
2020
  set(_fields, name, {
1985
2021
  ...(field || {}),
1986
2022
  _f: {
@@ -1995,7 +2031,9 @@ function createFormControl(props = {}) {
1995
2031
  _setDisabledField({
1996
2032
  disabled: isBoolean(options.disabled)
1997
2033
  ? options.disabled
1998
- : _options.disabled,
2034
+ : Array.isArray(_options.disabled)
2035
+ ? new Set(_options.disabled).has(name)
2036
+ : _options.disabled,
1999
2037
  name,
2000
2038
  });
2001
2039
  }
@@ -2004,7 +2042,13 @@ function createFormControl(props = {}) {
2004
2042
  }
2005
2043
  return {
2006
2044
  ...(disabledIsDefined
2007
- ? { disabled: options.disabled || _options.disabled }
2045
+ ? {
2046
+ disabled: isBoolean(options.disabled)
2047
+ ? options.disabled
2048
+ : Array.isArray(_options.disabled)
2049
+ ? new Set(_options.disabled).has(name)
2050
+ : !!_options.disabled,
2051
+ }
2008
2052
  : {}),
2009
2053
  ...(_options.progressive
2010
2054
  ? {
@@ -2072,10 +2116,33 @@ function createFormControl(props = {}) {
2072
2116
  iterateFieldsByAction(_fields, (ref, name) => {
2073
2117
  const currentField = get(_fields, name);
2074
2118
  if (currentField) {
2075
- ref.disabled = currentField._f.disabled || disabled;
2119
+ ref.disabled = isBoolean(currentField._f.disabled)
2120
+ ? currentField._f.disabled
2121
+ : disabled;
2122
+ if (Array.isArray(currentField._f.refs)) {
2123
+ currentField._f.refs.forEach((inputRef) => {
2124
+ inputRef.disabled = isBoolean(currentField._f.disabled)
2125
+ ? currentField._f.disabled
2126
+ : disabled;
2127
+ });
2128
+ }
2129
+ }
2130
+ }, 0, false);
2131
+ }
2132
+ else if (Array.isArray(disabled)) {
2133
+ // For array mode, we don't set the global disabled state
2134
+ // but we update individual fields based on their inclusion in the array
2135
+ iterateFieldsByAction(_fields, (ref, name) => {
2136
+ const currentField = get(_fields, name);
2137
+ if (currentField) {
2138
+ // Field-level disabled takes precedence over array disabled
2139
+ const isFieldDisabled = isBoolean(currentField._f.disabled)
2140
+ ? currentField._f.disabled
2141
+ : new Set(disabled).has(name);
2142
+ ref.disabled = isFieldDisabled;
2076
2143
  if (Array.isArray(currentField._f.refs)) {
2077
2144
  currentField._f.refs.forEach((inputRef) => {
2078
- inputRef.disabled = currentField._f.disabled || disabled;
2145
+ inputRef.disabled = isFieldDisabled;
2079
2146
  });
2080
2147
  }
2081
2148
  }
@@ -2266,6 +2333,7 @@ function createFormControl(props = {}) {
2266
2333
  ? _formState.isSubmitSuccessful
2267
2334
  : false,
2268
2335
  isSubmitting: false,
2336
+ defaultValues: _defaultValues,
2269
2337
  });
2270
2338
  };
2271
2339
  const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
@@ -2778,7 +2846,9 @@ function useForm(props = {}) {
2778
2846
  touchedFields: {},
2779
2847
  validatingFields: {},
2780
2848
  errors: props.errors || {},
2781
- disabled: props.disabled || false,
2849
+ // If it's an array, set formState.disabled to false because when using array mode,
2850
+ // the form itself isn't disabled - only specific fields are
2851
+ disabled: Array.isArray(props.disabled) ? false : props.disabled || false,
2782
2852
  isReady: false,
2783
2853
  defaultValues: isFunction(props.defaultValues)
2784
2854
  ? undefined