@bombillazo/rhf-plus 7.62.0-plus.1 → 7.62.0-plus.3

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.
@@ -81,6 +81,21 @@ var get = (object, path, defaultValue) => {
81
81
 
82
82
  var isBoolean = (value) => typeof value === 'boolean';
83
83
 
84
+ function mergeMissingKeysAsUndefined(oldObject, newObject) {
85
+ if (!newObject) {
86
+ return newObject;
87
+ }
88
+ const result = { ...newObject };
89
+ if (oldObject) {
90
+ for (const key in oldObject) {
91
+ if (!(key in result)) {
92
+ result[key] = undefined;
93
+ }
94
+ }
95
+ }
96
+ return result;
97
+ }
98
+
84
99
  var set = (object, path, value) => {
85
100
  let index = -1;
86
101
  const tempPath = isKey(path) ? [path] : stringToPath(path);
@@ -427,11 +442,22 @@ function useController(props) {
427
442
  exact: true,
428
443
  });
429
444
  const _props = React.useRef(props);
445
+ const _previousRules = React.useRef(props.rules);
430
446
  const _registerProps = React.useRef(control.register(name, {
431
447
  ...props.rules,
432
448
  value,
433
449
  ...(isBoolean(props.disabled) ? { disabled: props.disabled } : {}),
434
450
  }));
451
+ const mergedRules = React.useMemo(() => mergeMissingKeysAsUndefined(_previousRules.current, props.rules), [props.rules]);
452
+ // Update register props when rules change
453
+ React.useEffect(() => {
454
+ _registerProps.current = control.register(name, {
455
+ ...mergedRules,
456
+ value,
457
+ ...(isBoolean(props.disabled) ? { disabled: props.disabled } : {}),
458
+ });
459
+ _previousRules.current = props.rules;
460
+ }, [mergedRules, props.rules, value, props.disabled, control, name]);
435
461
  _props.current = props;
436
462
  const fieldState = React.useMemo(() => Object.defineProperties({}, {
437
463
  invalid: {
@@ -469,17 +495,26 @@ function useController(props) {
469
495
  },
470
496
  type: EVENTS.BLUR,
471
497
  }), [name, control._formValues]);
498
+ const elementRef = React.useRef(null);
472
499
  const ref = React.useCallback((elm) => {
500
+ elementRef.current = elm;
473
501
  const field = get(control._fields, name);
474
502
  if (field && elm) {
475
503
  field._f.ref = {
476
- focus: () => elm.focus && elm.focus(),
477
- select: () => elm.select && elm.select(),
504
+ focus: () => { var _a; return (_a = elm.focus) === null || _a === void 0 ? void 0 : _a.call(elm); },
505
+ select: () => { var _a; return (_a = elm.select) === null || _a === void 0 ? void 0 : _a.call(elm); },
478
506
  setCustomValidity: (message) => elm.setCustomValidity(message),
479
507
  reportValidity: () => elm.reportValidity(),
480
508
  };
481
509
  }
482
510
  }, [control._fields, name]);
511
+ // Add scrollIntoView method to the ref callback function
512
+ ref.scrollIntoView = React.useCallback((options) => {
513
+ if (elementRef.current &&
514
+ typeof elementRef.current.scrollIntoView === 'function') {
515
+ elementRef.current.scrollIntoView(options);
516
+ }
517
+ }, []);
483
518
  const field = React.useMemo(() => {
484
519
  // Calculate if this specific field should be disabled
485
520
  let isFieldDisabled;
@@ -1136,7 +1171,17 @@ var updateFieldArrayRootError = (errors, error, name) => {
1136
1171
  return errors;
1137
1172
  };
1138
1173
 
1139
- var isMessage = (value) => isString(value);
1174
+ var isMessage = (value) => {
1175
+ // Support strings (existing functionality)
1176
+ if (isString(value)) {
1177
+ return true;
1178
+ }
1179
+ // Support React elements only (not primitives like null, undefined, numbers)
1180
+ if (React.isValidElement(value)) {
1181
+ return true;
1182
+ }
1183
+ return false;
1184
+ };
1140
1185
 
1141
1186
  function getValidateError(result, ref, type = 'validate') {
1142
1187
  if (isMessage(result) ||
@@ -1166,7 +1211,16 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
1166
1211
  const inputRef = refs ? refs[0] : ref;
1167
1212
  const setCustomValidity = (message) => {
1168
1213
  if (shouldUseNativeValidation && inputRef.reportValidity) {
1169
- inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
1214
+ if (isBoolean(message)) {
1215
+ inputRef.setCustomValidity('');
1216
+ }
1217
+ else if (typeof message === 'string') {
1218
+ inputRef.setCustomValidity(message || '');
1219
+ }
1220
+ else {
1221
+ // For ReactNode messages, convert to string or use empty string for native validation
1222
+ inputRef.setCustomValidity('');
1223
+ }
1170
1224
  inputRef.reportValidity();
1171
1225
  }
1172
1226
  };