@bombillazo/rhf-plus 7.62.0-plus.0 → 7.62.0-plus.2

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.
@@ -469,27 +469,50 @@ function useController(props) {
469
469
  },
470
470
  type: EVENTS.BLUR,
471
471
  }), [name, control._formValues]);
472
+ const elementRef = React.useRef(null);
472
473
  const ref = React.useCallback((elm) => {
474
+ elementRef.current = elm;
473
475
  const field = get(control._fields, name);
474
476
  if (field && elm) {
475
477
  field._f.ref = {
476
- focus: () => elm.focus && elm.focus(),
477
- select: () => elm.select && elm.select(),
478
+ focus: () => { var _a; return (_a = elm.focus) === null || _a === void 0 ? void 0 : _a.call(elm); },
479
+ select: () => { var _a; return (_a = elm.select) === null || _a === void 0 ? void 0 : _a.call(elm); },
478
480
  setCustomValidity: (message) => elm.setCustomValidity(message),
479
481
  reportValidity: () => elm.reportValidity(),
480
482
  };
481
483
  }
482
484
  }, [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]);
485
+ // Add scrollIntoView method to the ref callback function
486
+ ref.scrollIntoView = React.useCallback((options) => {
487
+ if (elementRef.current &&
488
+ typeof elementRef.current.scrollIntoView === 'function') {
489
+ elementRef.current.scrollIntoView(options);
490
+ }
491
+ }, []);
492
+ const field = React.useMemo(() => {
493
+ // Calculate if this specific field should be disabled
494
+ let isFieldDisabled;
495
+ if (isBoolean(disabled)) {
496
+ // Field-level disabled prop takes precedence
497
+ isFieldDisabled = disabled;
498
+ }
499
+ else if (isBoolean(control._options.disabled)) {
500
+ // Form-level boolean disabled
501
+ isFieldDisabled = control._options.disabled;
502
+ }
503
+ else if (Array.isArray(control._options.disabled)) {
504
+ // Form-level array disabled - check if this field is in the array
505
+ isFieldDisabled = control._options.disabled.includes(name);
506
+ }
507
+ return {
508
+ name,
509
+ value,
510
+ ...(isBoolean(isFieldDisabled) ? { disabled: isFieldDisabled } : {}),
511
+ onChange,
512
+ onBlur,
513
+ ref,
514
+ };
515
+ }, [name, disabled, control._options.disabled, onChange, onBlur, ref, value]);
493
516
  React.useEffect(() => {
494
517
  const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
495
518
  control.register(name, {
@@ -1122,7 +1145,17 @@ var updateFieldArrayRootError = (errors, error, name) => {
1122
1145
  return errors;
1123
1146
  };
1124
1147
 
1125
- var isMessage = (value) => isString(value);
1148
+ var isMessage = (value) => {
1149
+ // Support strings (existing functionality)
1150
+ if (isString(value)) {
1151
+ return true;
1152
+ }
1153
+ // Support React elements only (not primitives like null, undefined, numbers)
1154
+ if (React.isValidElement(value)) {
1155
+ return true;
1156
+ }
1157
+ return false;
1158
+ };
1126
1159
 
1127
1160
  function getValidateError(result, ref, type = 'validate') {
1128
1161
  if (isMessage(result) ||
@@ -1152,7 +1185,16 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
1152
1185
  const inputRef = refs ? refs[0] : ref;
1153
1186
  const setCustomValidity = (message) => {
1154
1187
  if (shouldUseNativeValidation && inputRef.reportValidity) {
1155
- inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
1188
+ if (isBoolean(message)) {
1189
+ inputRef.setCustomValidity('');
1190
+ }
1191
+ else if (typeof message === 'string') {
1192
+ inputRef.setCustomValidity(message || '');
1193
+ }
1194
+ else {
1195
+ // For ReactNode messages, convert to string or use empty string for native validation
1196
+ inputRef.setCustomValidity('');
1197
+ }
1156
1198
  inputRef.reportValidity();
1157
1199
  }
1158
1200
  };
@@ -1347,7 +1389,9 @@ function createFormControl(props = {}) {
1347
1389
  dirtyFields: {},
1348
1390
  validatingFields: {},
1349
1391
  errors: _options.errors || {},
1350
- disabled: _options.disabled || false,
1392
+ disabled: Array.isArray(_options.disabled)
1393
+ ? false
1394
+ : _options.disabled || false,
1351
1395
  metadata: _options.defaultMetadata || {},
1352
1396
  };
1353
1397
  let _fields = {};
@@ -1394,7 +1438,7 @@ function createFormControl(props = {}) {
1394
1438
  timer = setTimeout(callback, wait);
1395
1439
  };
1396
1440
  const _setValid = async (shouldUpdateValid) => {
1397
- if (!_options.disabled &&
1441
+ if ((Array.isArray(_options.disabled) || !_options.disabled) &&
1398
1442
  (_proxyFormState.isValid ||
1399
1443
  _proxySubscribeFormState.isValid ||
1400
1444
  shouldUpdateValid)) {
@@ -1756,6 +1800,22 @@ function createFormControl(props = {}) {
1756
1800
  const validationModeBeforeSubmit = getValidationModes(_options.mode);
1757
1801
  const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
1758
1802
  if (field) {
1803
+ // Check if field is disabled and should not process events
1804
+ const isFieldDisabled = isBoolean(field._f.disabled)
1805
+ ? field._f.disabled
1806
+ : isBoolean(_options.disabled)
1807
+ ? _options.disabled
1808
+ : Array.isArray(_options.disabled)
1809
+ ? new Set(_options.disabled).has(name)
1810
+ : false;
1811
+ if (isFieldDisabled) {
1812
+ // Restore the original value if the field is disabled
1813
+ const originalValue = get(_formValues, name);
1814
+ if (target.value !== originalValue) {
1815
+ target.value = originalValue;
1816
+ }
1817
+ return;
1818
+ }
1759
1819
  let error;
1760
1820
  let isValid;
1761
1821
  const fieldValue = target.type
@@ -1982,7 +2042,9 @@ function createFormControl(props = {}) {
1982
2042
  };
1983
2043
  const register = (name, options = {}) => {
1984
2044
  let field = get(_fields, name);
1985
- const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
2045
+ const disabledIsDefined = isBoolean(options.disabled) ||
2046
+ isBoolean(_options.disabled) ||
2047
+ Array.isArray(_options.disabled);
1986
2048
  set(_fields, name, {
1987
2049
  ...(field || {}),
1988
2050
  _f: {
@@ -1997,7 +2059,9 @@ function createFormControl(props = {}) {
1997
2059
  _setDisabledField({
1998
2060
  disabled: isBoolean(options.disabled)
1999
2061
  ? options.disabled
2000
- : _options.disabled,
2062
+ : Array.isArray(_options.disabled)
2063
+ ? new Set(_options.disabled).has(name)
2064
+ : _options.disabled,
2001
2065
  name,
2002
2066
  });
2003
2067
  }
@@ -2006,7 +2070,13 @@ function createFormControl(props = {}) {
2006
2070
  }
2007
2071
  return {
2008
2072
  ...(disabledIsDefined
2009
- ? { disabled: options.disabled || _options.disabled }
2073
+ ? {
2074
+ disabled: isBoolean(options.disabled)
2075
+ ? options.disabled
2076
+ : Array.isArray(_options.disabled)
2077
+ ? new Set(_options.disabled).has(name)
2078
+ : !!_options.disabled,
2079
+ }
2010
2080
  : {}),
2011
2081
  ...(_options.progressive
2012
2082
  ? {
@@ -2074,10 +2144,33 @@ function createFormControl(props = {}) {
2074
2144
  iterateFieldsByAction(_fields, (ref, name) => {
2075
2145
  const currentField = get(_fields, name);
2076
2146
  if (currentField) {
2077
- ref.disabled = currentField._f.disabled || disabled;
2147
+ ref.disabled = isBoolean(currentField._f.disabled)
2148
+ ? currentField._f.disabled
2149
+ : disabled;
2150
+ if (Array.isArray(currentField._f.refs)) {
2151
+ currentField._f.refs.forEach((inputRef) => {
2152
+ inputRef.disabled = isBoolean(currentField._f.disabled)
2153
+ ? currentField._f.disabled
2154
+ : disabled;
2155
+ });
2156
+ }
2157
+ }
2158
+ }, 0, false);
2159
+ }
2160
+ else if (Array.isArray(disabled)) {
2161
+ // For array mode, we don't set the global disabled state
2162
+ // but we update individual fields based on their inclusion in the array
2163
+ iterateFieldsByAction(_fields, (ref, name) => {
2164
+ const currentField = get(_fields, name);
2165
+ if (currentField) {
2166
+ // Field-level disabled takes precedence over array disabled
2167
+ const isFieldDisabled = isBoolean(currentField._f.disabled)
2168
+ ? currentField._f.disabled
2169
+ : new Set(disabled).has(name);
2170
+ ref.disabled = isFieldDisabled;
2078
2171
  if (Array.isArray(currentField._f.refs)) {
2079
2172
  currentField._f.refs.forEach((inputRef) => {
2080
- inputRef.disabled = currentField._f.disabled || disabled;
2173
+ inputRef.disabled = isFieldDisabled;
2081
2174
  });
2082
2175
  }
2083
2176
  }
@@ -2781,7 +2874,9 @@ function useForm(props = {}) {
2781
2874
  touchedFields: {},
2782
2875
  validatingFields: {},
2783
2876
  errors: props.errors || {},
2784
- disabled: props.disabled || false,
2877
+ // If it's an array, set formState.disabled to false because when using array mode,
2878
+ // the form itself isn't disabled - only specific fields are
2879
+ disabled: Array.isArray(props.disabled) ? false : props.disabled || false,
2785
2880
  isReady: false,
2786
2881
  defaultValues: isFunction(props.defaultValues)
2787
2882
  ? undefined