@bombillazo/rhf-plus 7.56.4-plus.0 → 7.59.0-plus.0

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.
Files changed (124) hide show
  1. package/README.md +1 -0
  2. package/dist/__typetest__/form.test-d.d.ts +1 -1
  3. package/dist/__typetest__/form.test-d.d.ts.map +1 -1
  4. package/dist/controller.d.ts +15 -14
  5. package/dist/controller.d.ts.map +1 -1
  6. package/dist/form.d.ts +1 -1
  7. package/dist/form.d.ts.map +1 -1
  8. package/dist/index.cjs.js +1 -1
  9. package/dist/index.cjs.js.map +1 -1
  10. package/dist/index.esm.mjs +76 -43
  11. package/dist/index.esm.mjs.map +1 -1
  12. package/dist/index.umd.js +1 -1
  13. package/dist/index.umd.js.map +1 -1
  14. package/dist/logic/appendErrors.d.ts +1 -1
  15. package/dist/logic/appendErrors.d.ts.map +1 -1
  16. package/dist/logic/createFormControl.d.ts +1 -1
  17. package/dist/logic/createFormControl.d.ts.map +1 -1
  18. package/dist/logic/generateId.d.ts.map +1 -1
  19. package/dist/logic/generateWatchOutput.d.ts +1 -1
  20. package/dist/logic/generateWatchOutput.d.ts.map +1 -1
  21. package/dist/logic/getFieldValue.d.ts +1 -1
  22. package/dist/logic/getFieldValue.d.ts.map +1 -1
  23. package/dist/logic/getFieldValueAs.d.ts +1 -1
  24. package/dist/logic/getFieldValueAs.d.ts.map +1 -1
  25. package/dist/logic/getFocusFieldName.d.ts +1 -1
  26. package/dist/logic/getFocusFieldName.d.ts.map +1 -1
  27. package/dist/logic/getProxyFormState.d.ts +1 -1
  28. package/dist/logic/getProxyFormState.d.ts.map +1 -1
  29. package/dist/logic/getResolverOptions.d.ts +1 -1
  30. package/dist/logic/getResolverOptions.d.ts.map +1 -1
  31. package/dist/logic/getRuleValue.d.ts +1 -1
  32. package/dist/logic/getRuleValue.d.ts.map +1 -1
  33. package/dist/logic/getValidateError.d.ts +1 -1
  34. package/dist/logic/getValidateError.d.ts.map +1 -1
  35. package/dist/logic/getValidationModes.d.ts +1 -1
  36. package/dist/logic/getValidationModes.d.ts.map +1 -1
  37. package/dist/logic/getValueAndMessage.d.ts +1 -1
  38. package/dist/logic/getValueAndMessage.d.ts.map +1 -1
  39. package/dist/logic/hasPromiseValidation.d.ts +1 -1
  40. package/dist/logic/hasPromiseValidation.d.ts.map +1 -1
  41. package/dist/logic/hasValidation.d.ts +1 -1
  42. package/dist/logic/hasValidation.d.ts.map +1 -1
  43. package/dist/logic/isNameInFieldArray.d.ts +1 -1
  44. package/dist/logic/isNameInFieldArray.d.ts.map +1 -1
  45. package/dist/logic/isWatched.d.ts +1 -1
  46. package/dist/logic/isWatched.d.ts.map +1 -1
  47. package/dist/logic/iterateFieldsByAction.d.ts +1 -1
  48. package/dist/logic/iterateFieldsByAction.d.ts.map +1 -1
  49. package/dist/logic/schemaErrorLookup.d.ts +1 -1
  50. package/dist/logic/schemaErrorLookup.d.ts.map +1 -1
  51. package/dist/logic/shouldRenderFormState.d.ts +1 -1
  52. package/dist/logic/shouldRenderFormState.d.ts.map +1 -1
  53. package/dist/logic/shouldSubscribeByName.d.ts +1 -1
  54. package/dist/logic/shouldSubscribeByName.d.ts.map +1 -1
  55. package/dist/logic/skipValidation.d.ts +1 -1
  56. package/dist/logic/skipValidation.d.ts.map +1 -1
  57. package/dist/logic/updateFieldArrayRootError.d.ts +1 -1
  58. package/dist/logic/updateFieldArrayRootError.d.ts.map +1 -1
  59. package/dist/logic/validateField.d.ts +1 -1
  60. package/dist/logic/validateField.d.ts.map +1 -1
  61. package/dist/react-server.esm.mjs +27 -11
  62. package/dist/react-server.esm.mjs.map +1 -1
  63. package/dist/types/controller.d.ts +13 -10
  64. package/dist/types/controller.d.ts.map +1 -1
  65. package/dist/types/errors.d.ts +3 -3
  66. package/dist/types/errors.d.ts.map +1 -1
  67. package/dist/types/fieldArray.d.ts +4 -4
  68. package/dist/types/fieldArray.d.ts.map +1 -1
  69. package/dist/types/fields.d.ts +2 -2
  70. package/dist/types/fields.d.ts.map +1 -1
  71. package/dist/types/form.d.ts +27 -22
  72. package/dist/types/form.d.ts.map +1 -1
  73. package/dist/types/path/common.d.ts +1 -1
  74. package/dist/types/path/common.d.ts.map +1 -1
  75. package/dist/types/path/eager.d.ts +16 -3
  76. package/dist/types/path/eager.d.ts.map +1 -1
  77. package/dist/types/path/index.d.ts +1 -1
  78. package/dist/types/path/index.d.ts.map +1 -1
  79. package/dist/types/resolvers.d.ts +3 -3
  80. package/dist/types/resolvers.d.ts.map +1 -1
  81. package/dist/types/utils.d.ts +1 -1
  82. package/dist/types/utils.d.ts.map +1 -1
  83. package/dist/types/validator.d.ts +4 -4
  84. package/dist/types/validator.d.ts.map +1 -1
  85. package/dist/useController.d.ts +1 -1
  86. package/dist/useController.d.ts.map +1 -1
  87. package/dist/useFieldArray.d.ts +1 -1
  88. package/dist/useFieldArray.d.ts.map +1 -1
  89. package/dist/useForm.d.ts +1 -1
  90. package/dist/useForm.d.ts.map +1 -1
  91. package/dist/useFormContext.d.ts +1 -1
  92. package/dist/useFormContext.d.ts.map +1 -1
  93. package/dist/useFormState.d.ts +1 -1
  94. package/dist/useFormState.d.ts.map +1 -1
  95. package/dist/useWatch.d.ts +15 -15
  96. package/dist/useWatch.d.ts.map +1 -1
  97. package/dist/utils/createSubject.d.ts +1 -1
  98. package/dist/utils/createSubject.d.ts.map +1 -1
  99. package/dist/utils/deepEqual.d.ts +1 -1
  100. package/dist/utils/deepEqual.d.ts.map +1 -1
  101. package/dist/utils/flatten.d.ts +1 -1
  102. package/dist/utils/flatten.d.ts.map +1 -1
  103. package/dist/utils/get.d.ts.map +1 -1
  104. package/dist/utils/isCheckBoxInput.d.ts +1 -1
  105. package/dist/utils/isCheckBoxInput.d.ts.map +1 -1
  106. package/dist/utils/isEmptyObject.d.ts +1 -1
  107. package/dist/utils/isEmptyObject.d.ts.map +1 -1
  108. package/dist/utils/isFileInput.d.ts +1 -1
  109. package/dist/utils/isFileInput.d.ts.map +1 -1
  110. package/dist/utils/isMessage.d.ts +1 -1
  111. package/dist/utils/isMessage.d.ts.map +1 -1
  112. package/dist/utils/isMultipleSelect.d.ts +1 -1
  113. package/dist/utils/isMultipleSelect.d.ts.map +1 -1
  114. package/dist/utils/isPrimitive.d.ts +1 -1
  115. package/dist/utils/isPrimitive.d.ts.map +1 -1
  116. package/dist/utils/isRadioInput.d.ts +1 -1
  117. package/dist/utils/isRadioInput.d.ts.map +1 -1
  118. package/dist/utils/isRadioOrCheckbox.d.ts +1 -1
  119. package/dist/utils/isRadioOrCheckbox.d.ts.map +1 -1
  120. package/dist/utils/live.d.ts +1 -1
  121. package/dist/utils/live.d.ts.map +1 -1
  122. package/dist/utils/set.d.ts +1 -1
  123. package/dist/utils/set.d.ts.map +1 -1
  124. package/package.json +13 -13
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import React__default from 'react';
3
- import crypto from 'crypto';
3
+ import crypto$1 from 'crypto';
4
4
 
5
5
  var isCheckBoxInput = (element) => element.type === 'checkbox';
6
6
 
@@ -63,15 +63,19 @@ function cloneObject(data) {
63
63
  return copy;
64
64
  }
65
65
 
66
- var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
66
+ var isKey = (value) => /^\w*$/.test(value);
67
67
 
68
68
  var isUndefined = (val) => val === undefined;
69
69
 
70
+ var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
71
+
72
+ var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
73
+
70
74
  var get = (object, path, defaultValue) => {
71
75
  if (!path || !isObject(object)) {
72
76
  return defaultValue;
73
77
  }
74
- const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], object);
78
+ const result = (isKey(path) ? [path] : stringToPath(path)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], object);
75
79
  return isUndefined(result) || result === object
76
80
  ? isUndefined(object[path])
77
81
  ? defaultValue
@@ -81,10 +85,6 @@ var get = (object, path, defaultValue) => {
81
85
 
82
86
  var isBoolean = (value) => typeof value === 'boolean';
83
87
 
84
- var isKey = (value) => /^\w*$/.test(value);
85
-
86
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
87
-
88
88
  var set = (object, path, value) => {
89
89
  let index = -1;
90
90
  const tempPath = isKey(path) ? [path] : stringToPath(path);
@@ -266,7 +266,7 @@ function useFormState(props) {
266
266
  errors: false,
267
267
  });
268
268
  useIsomorphicLayoutEffect(() => control._subscribe({
269
- name: name,
269
+ name,
270
270
  formState: _localProxyFormState.current,
271
271
  exact,
272
272
  callback: (formState) => {
@@ -319,7 +319,7 @@ function useWatch(props) {
319
319
  const _defaultValue = React__default.useRef(defaultValue);
320
320
  const [value, updateValue] = React__default.useState(control._getWatch(name, _defaultValue.current));
321
321
  useIsomorphicLayoutEffect(() => control._subscribe({
322
- name: name,
322
+ name,
323
323
  formState: {
324
324
  values: true,
325
325
  },
@@ -419,8 +419,8 @@ function useController(props) {
419
419
  const field = get(control._fields, name);
420
420
  if (field && elm) {
421
421
  field._f.ref = {
422
- focus: () => elm.focus(),
423
- select: () => elm.select(),
422
+ focus: () => elm.focus && elm.focus(),
423
+ select: () => elm.select && elm.select(),
424
424
  setCustomValidity: (message) => elm.setCustomValidity(message),
425
425
  reportValidity: () => elm.reportValidity(),
426
426
  };
@@ -504,25 +504,29 @@ function useController(props) {
504
504
  * <Controller
505
505
  * control={control}
506
506
  * name="test"
507
- * render={({ field: { onChange, onBlur, value, ref }, formState, fieldState }) => (
508
- * <>
509
- * <input
510
- * onChange={onChange} // send value to hook form
511
- * onBlur={onBlur} // notify when input is touched
512
- * value={value} // return updated value
513
- * ref={ref} // set ref for focus management
514
- * />
515
- * <p>{formState.isSubmitted ? "submitted" : ""}</p>
516
- * <p>{fieldState.isTouched ? "touched" : ""}</p>
517
- * </>
518
- * )}
519
- * />
507
+ * >
508
+ * ({ field: { onChange, onBlur, value, ref }, formState, fieldState }) => (
509
+ * <>
510
+ * <input
511
+ * onChange={onChange} // send value to hook form
512
+ * onBlur={onBlur} // notify when input is touched
513
+ * value={value} // return updated value
514
+ * ref={ref} // set ref for focus management
515
+ * />
516
+ * <p>{formState.isSubmitted ? "submitted" : ""}</p>
517
+ * <p>{fieldState.isTouched ? "touched" : ""}</p>
518
+ * </>
519
+ * )
520
+ * </Controller>
520
521
  * </form>
521
522
  * );
522
523
  * }
523
524
  * ```
524
525
  */
525
- const Controller = (props) => props.render(useController(props));
526
+ const Controller = (props) => {
527
+ const renderedController = useController(props);
528
+ return ('children' in props ? props.children : props.render)(renderedController);
529
+ };
526
530
 
527
531
  const flatten = (obj) => {
528
532
  const output = {};
@@ -656,7 +660,7 @@ var uuid = () => {
656
660
  typeof window.crypto.randomUUID === 'function') {
657
661
  return window.crypto.randomUUID();
658
662
  }
659
- return crypto.randomBytes(16).toString('hex');
663
+ return crypto$1.randomBytes(16).toString('hex');
660
664
  };
661
665
 
662
666
  var createId = (id) => id || `form-${uuid()}`;
@@ -691,7 +695,7 @@ var createSubject = () => {
691
695
 
692
696
  var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
693
697
 
694
- function deepEqual(object1, object2) {
698
+ function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
695
699
  if (isPrimitive(object1) || isPrimitive(object2)) {
696
700
  return object1 === object2;
697
701
  }
@@ -703,6 +707,11 @@ function deepEqual(object1, object2) {
703
707
  if (keys1.length !== keys2.length) {
704
708
  return false;
705
709
  }
710
+ if (_internal_visited.has(object1) || _internal_visited.has(object2)) {
711
+ return true;
712
+ }
713
+ _internal_visited.add(object1);
714
+ _internal_visited.add(object2);
706
715
  for (const key of keys1) {
707
716
  const val1 = object1[key];
708
717
  if (!keys2.includes(key)) {
@@ -713,7 +722,7 @@ function deepEqual(object1, object2) {
713
722
  if ((isDateObject(val1) && isDateObject(val2)) ||
714
723
  (isObject(val1) && isObject(val2)) ||
715
724
  (Array.isArray(val1) && Array.isArray(val2))
716
- ? !deepEqual(val1, val2)
725
+ ? !deepEqual(val1, val2, _internal_visited)
717
726
  : val1 !== val2) {
718
727
  return false;
719
728
  }
@@ -1039,6 +1048,12 @@ function schemaErrorLookup(errors, _fields, name) {
1039
1048
  error: foundError,
1040
1049
  };
1041
1050
  }
1051
+ if (foundError && foundError.root && foundError.root.type) {
1052
+ return {
1053
+ name: `${fieldName}.root`,
1054
+ error: foundError.root,
1055
+ };
1056
+ }
1042
1057
  names.pop();
1043
1058
  }
1044
1059
  return {
@@ -1665,7 +1680,7 @@ function createFormControl(props = {}) {
1665
1680
  return;
1666
1681
  }
1667
1682
  const fieldValue = value[fieldKey];
1668
- const fieldName = `${name}.${fieldKey}`;
1683
+ const fieldName = name + '.' + fieldKey;
1669
1684
  const field = get(_fields, fieldName);
1670
1685
  (_names.array.has(name) ||
1671
1686
  isObject(fieldValue) ||
@@ -2061,14 +2076,14 @@ function createFormControl(props = {}) {
2061
2076
  if (_options.resolver) {
2062
2077
  const { errors, values } = await _runSchema();
2063
2078
  _formState.errors = errors;
2064
- fieldValues = values;
2079
+ fieldValues = cloneObject(values);
2065
2080
  }
2066
2081
  else {
2067
2082
  await executeBuiltInValidation(_fields);
2068
2083
  }
2069
2084
  if (_names.disabled.size) {
2070
2085
  for (const name of _names.disabled) {
2071
- set(fieldValues, name, undefined);
2086
+ unset(fieldValues, name);
2072
2087
  }
2073
2088
  }
2074
2089
  unset(_formState.errors, 'root');
@@ -2165,7 +2180,11 @@ function createFormControl(props = {}) {
2165
2180
  }
2166
2181
  }
2167
2182
  for (const fieldName of _names.mount) {
2168
- setValue(fieldName, get(values, fieldName));
2183
+ const value = get(values, fieldName, get(_defaultValues, fieldName));
2184
+ if (!isUndefined(value)) {
2185
+ set(values, fieldName, value);
2186
+ setValue(fieldName, get(values, fieldName));
2187
+ }
2169
2188
  }
2170
2189
  }
2171
2190
  _formValues = cloneObject(values);
@@ -2307,6 +2326,7 @@ function createFormControl(props = {}) {
2307
2326
  setError,
2308
2327
  _subscribe,
2309
2328
  _runSchema,
2329
+ _focusError,
2310
2330
  _getWatch,
2311
2331
  _getDirty,
2312
2332
  _setValid,
@@ -2381,6 +2401,9 @@ function createFormControl(props = {}) {
2381
2401
  }
2382
2402
 
2383
2403
  var generateId = () => {
2404
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
2405
+ return crypto.randomUUID();
2406
+ }
2384
2407
  const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
2385
2408
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
2386
2409
  const r = (Math.random() * 16 + d) % 16 | 0;
@@ -2496,7 +2519,7 @@ function useFieldArray(props) {
2496
2519
  control._names.array.add(name);
2497
2520
  rules &&
2498
2521
  control.register(name, rules);
2499
- React__default.useEffect(() => control._subjects.array.subscribe({
2522
+ useIsomorphicLayoutEffect(() => control._subjects.array.subscribe({
2500
2523
  next: ({ values, name: fieldArrayName, }) => {
2501
2524
  if (fieldArrayName === _name.current || !fieldArrayName) {
2502
2525
  const fieldValues = get(values, _name.current);
@@ -2742,14 +2765,21 @@ function useForm(props = {}) {
2742
2765
  {},
2743
2766
  });
2744
2767
  if (!_formControl.current) {
2745
- _formControl.current = {
2746
- ...(props.formControl ? props.formControl : createFormControl(props)),
2747
- formState,
2748
- };
2749
- if (props.formControl &&
2750
- props.defaultValues &&
2751
- !isFunction(props.defaultValues)) {
2752
- props.formControl.reset(props.defaultValues, props.resetOptions);
2768
+ if (props.formControl) {
2769
+ _formControl.current = {
2770
+ ...props.formControl,
2771
+ formState,
2772
+ };
2773
+ if (props.defaultValues && !isFunction(props.defaultValues)) {
2774
+ props.formControl.reset(props.defaultValues, props.resetOptions);
2775
+ }
2776
+ }
2777
+ else {
2778
+ const { formControl, ...rest } = createFormControl(props);
2779
+ _formControl.current = {
2780
+ ...rest,
2781
+ formState,
2782
+ };
2753
2783
  }
2754
2784
  }
2755
2785
  const control = _formControl.current.control;
@@ -2778,10 +2808,13 @@ function useForm(props = {}) {
2778
2808
  if (props.reValidateMode) {
2779
2809
  control._options.reValidateMode = props.reValidateMode;
2780
2810
  }
2781
- if (props.errors && !isEmptyObject(props.errors)) {
2811
+ }, [control, props.mode, props.reValidateMode]);
2812
+ React__default.useEffect(() => {
2813
+ if (props.errors) {
2782
2814
  control._setErrors(props.errors);
2815
+ control._focusError();
2783
2816
  }
2784
- }, [control, props.errors, props.mode, props.reValidateMode]);
2817
+ }, [control, props.errors]);
2785
2818
  React__default.useEffect(() => {
2786
2819
  props.shouldUnregister &&
2787
2820
  control._subjects.state.next({