@bombillazo/rhf-plus 7.62.0-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.
@@ -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
@@ -1982,7 +2014,9 @@ function createFormControl(props = {}) {
1982
2014
  };
1983
2015
  const register = (name, options = {}) => {
1984
2016
  let field = get(_fields, name);
1985
- const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
2017
+ const disabledIsDefined = isBoolean(options.disabled) ||
2018
+ isBoolean(_options.disabled) ||
2019
+ Array.isArray(_options.disabled);
1986
2020
  set(_fields, name, {
1987
2021
  ...(field || {}),
1988
2022
  _f: {
@@ -1997,7 +2031,9 @@ function createFormControl(props = {}) {
1997
2031
  _setDisabledField({
1998
2032
  disabled: isBoolean(options.disabled)
1999
2033
  ? options.disabled
2000
- : _options.disabled,
2034
+ : Array.isArray(_options.disabled)
2035
+ ? new Set(_options.disabled).has(name)
2036
+ : _options.disabled,
2001
2037
  name,
2002
2038
  });
2003
2039
  }
@@ -2006,7 +2042,13 @@ function createFormControl(props = {}) {
2006
2042
  }
2007
2043
  return {
2008
2044
  ...(disabledIsDefined
2009
- ? { 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
+ }
2010
2052
  : {}),
2011
2053
  ...(_options.progressive
2012
2054
  ? {
@@ -2074,10 +2116,33 @@ function createFormControl(props = {}) {
2074
2116
  iterateFieldsByAction(_fields, (ref, name) => {
2075
2117
  const currentField = get(_fields, name);
2076
2118
  if (currentField) {
2077
- 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;
2078
2143
  if (Array.isArray(currentField._f.refs)) {
2079
2144
  currentField._f.refs.forEach((inputRef) => {
2080
- inputRef.disabled = currentField._f.disabled || disabled;
2145
+ inputRef.disabled = isFieldDisabled;
2081
2146
  });
2082
2147
  }
2083
2148
  }
@@ -2781,7 +2846,9 @@ function useForm(props = {}) {
2781
2846
  touchedFields: {},
2782
2847
  validatingFields: {},
2783
2848
  errors: props.errors || {},
2784
- 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,
2785
2852
  isReady: false,
2786
2853
  defaultValues: isFunction(props.defaultValues)
2787
2854
  ? undefined