@bombillazo/rhf-plus 7.62.0-plus.6 → 7.62.0-plus.7

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.
@@ -1236,10 +1236,10 @@ var getValueAndMessage = (validationData) => isObject(validationData) && !isRege
1236
1236
  message: '',
1237
1237
  };
1238
1238
 
1239
- var validateField = async (field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
1239
+ var validateField = async (field, skippedFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
1240
1240
  const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, } = field._f;
1241
1241
  const inputValue = get(formValues, name);
1242
- if (!mount || disabledFieldNames.has(name)) {
1242
+ if (!mount || skippedFieldNames.has(name)) {
1243
1243
  return {};
1244
1244
  }
1245
1245
  const inputRef = refs ? refs[0] : ref;
@@ -1428,6 +1428,7 @@ const defaultOptions = {
1428
1428
  mode: VALIDATION_MODE.onSubmit,
1429
1429
  reValidateMode: VALIDATION_MODE.onChange,
1430
1430
  shouldFocusError: true,
1431
+ shouldSkipReadOnlyValidation: false,
1431
1432
  };
1432
1433
  function createFormControl(props = {}) {
1433
1434
  let _options = {
@@ -1472,6 +1473,7 @@ function createFormControl(props = {}) {
1472
1473
  let _names = {
1473
1474
  mount: new Set(),
1474
1475
  disabled: new Set(),
1476
+ readonly: new Set(),
1475
1477
  unMount: new Set(),
1476
1478
  array: new Set(),
1477
1479
  watch: new Set(),
@@ -1739,7 +1741,12 @@ function createFormControl(props = {}) {
1739
1741
  if (isPromiseFunction && _proxyFormState.validatingFields) {
1740
1742
  _updateIsValidating([name], true);
1741
1743
  }
1742
- const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
1744
+ // Combine disabled and readonly field names for validation skipping
1745
+ const skipValidationFields = new Set([
1746
+ ..._names.disabled,
1747
+ ..._names.readonly,
1748
+ ]);
1749
+ const fieldError = await validateField(field, skipValidationFields, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
1743
1750
  if (isPromiseFunction && _proxyFormState.validatingFields) {
1744
1751
  _updateIsValidating([name]);
1745
1752
  }
@@ -1945,6 +1952,36 @@ function createFormControl(props = {}) {
1945
1952
  }
1946
1953
  return;
1947
1954
  }
1955
+ // Check if field is readonly and should skip validation (only when flag is enabled)
1956
+ if (_options.shouldSkipReadOnlyValidation && target && target.readOnly) {
1957
+ // Add to readonly fields set for validation skipping
1958
+ _names.readonly.add(name);
1959
+ // For readonly fields, we still want to update the form values
1960
+ // but skip validation (similar to disabled fields behavior)
1961
+ const fieldValue = target.type
1962
+ ? getFieldValue(field._f)
1963
+ : getEventValue(event);
1964
+ const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
1965
+ const isFocusEvent = event.type === EVENTS.FOCUS || event.type === EVENTS.FOCUS_IN;
1966
+ const watched = isWatched(name, _names, isBlurEvent || isFocusEvent);
1967
+ // Update form values but skip validation and error handling
1968
+ set(_formValues, name, fieldValue);
1969
+ // Update touch and dirty state
1970
+ const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, isFocusEvent, !isBlurEvent);
1971
+ const shouldRender = !isEmptyObject(fieldState) || watched;
1972
+ !isBlurEvent &&
1973
+ _subjects.state.next({
1974
+ name,
1975
+ type: event.type,
1976
+ values: cloneObject(_formValues),
1977
+ });
1978
+ return (shouldRender &&
1979
+ _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1980
+ }
1981
+ else if (_options.shouldSkipReadOnlyValidation) {
1982
+ // Remove from readonly fields set if not readonly anymore (only when flag is enabled)
1983
+ _names.readonly.delete(name);
1984
+ }
1948
1985
  let error;
1949
1986
  let isValid;
1950
1987
  const fieldValue = target.type
@@ -2004,7 +2041,12 @@ function createFormControl(props = {}) {
2004
2041
  }
2005
2042
  else {
2006
2043
  _updateIsValidating([name], true);
2007
- error = (await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
2044
+ // Combine disabled and readonly field names for validation skipping
2045
+ const skipValidationFields = new Set([
2046
+ ..._names.disabled,
2047
+ ..._names.readonly,
2048
+ ]);
2049
+ error = (await validateField(field, skipValidationFields, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, false))[name];
2008
2050
  _updateIsValidating([name]);
2009
2051
  _updateIsFieldValueUpdated(fieldValue);
2010
2052
  if (isFieldValueUpdated) {
@@ -2255,6 +2297,13 @@ function createFormControl(props = {}) {
2255
2297
  },
2256
2298
  });
2257
2299
  updateValidAndValue(name, false, undefined, fieldRef);
2300
+ // Check if field is readonly and should skip validation (only when flag is enabled)
2301
+ if (_options.shouldSkipReadOnlyValidation &&
2302
+ fieldRef &&
2303
+ 'readOnly' in fieldRef &&
2304
+ fieldRef.readOnly) {
2305
+ _names.readonly.add(name);
2306
+ }
2258
2307
  }
2259
2308
  else {
2260
2309
  field = get(_fields, name, {});
@@ -2455,6 +2504,7 @@ function createFormControl(props = {}) {
2455
2504
  unMount: new Set(),
2456
2505
  array: new Set(),
2457
2506
  disabled: new Set(),
2507
+ readonly: new Set(),
2458
2508
  watch: new Set(),
2459
2509
  watchAll: false,
2460
2510
  focus: '',
@@ -2554,6 +2604,28 @@ function createFormControl(props = {}) {
2554
2604
  });
2555
2605
  }
2556
2606
  };
2607
+ const _updateReadonlyFieldTracking = () => {
2608
+ // Re-evaluate all registered fields and update readonly tracking
2609
+ // based on current shouldSkipReadOnlyValidation flag and field readonly state
2610
+ Object.keys(_fields).forEach((fieldName) => {
2611
+ const field = get(_fields, fieldName);
2612
+ if (field && field._f) {
2613
+ // Get the actual DOM element reference
2614
+ const fieldRef = field._f.refs ? field._f.refs[0] : field._f.ref;
2615
+ if (fieldRef && 'readOnly' in fieldRef) {
2616
+ const isFieldReadonly = Boolean(fieldRef.readOnly);
2617
+ const shouldTrackAsReadonly = _options.shouldSkipReadOnlyValidation && isFieldReadonly;
2618
+ // Update readonly tracking set
2619
+ if (shouldTrackAsReadonly) {
2620
+ _names.readonly.add(fieldName);
2621
+ }
2622
+ else {
2623
+ _names.readonly.delete(fieldName);
2624
+ }
2625
+ }
2626
+ }
2627
+ });
2628
+ };
2557
2629
  const setMetadata = (metadata) => {
2558
2630
  let _metadata;
2559
2631
  if (!metadata) {
@@ -2598,6 +2670,7 @@ function createFormControl(props = {}) {
2598
2670
  _removeUnmounted,
2599
2671
  _disableForm,
2600
2672
  _updateIsLoading,
2673
+ _updateReadonlyFieldTracking,
2601
2674
  _subjects,
2602
2675
  _proxyFormState,
2603
2676
  get _fields() {
@@ -3060,6 +3133,11 @@ function useForm(props = {}) {
3060
3133
  return sub;
3061
3134
  }, [control]);
3062
3135
  React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
3136
+ // Handle shouldSkipReadOnlyValidation flag changes
3137
+ React.useEffect(() => {
3138
+ // Re-evaluate readonly field tracking when the flag changes
3139
+ control._updateReadonlyFieldTracking();
3140
+ }, [control, props.shouldSkipReadOnlyValidation]);
3063
3141
  React.useEffect(() => {
3064
3142
  control._updateIsLoading(props.isLoading);
3065
3143
  }, [control, props.isLoading]);