@paragrav/rhf-utils 0.0.18 → 0.0.20

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 (88) hide show
  1. package/dist/cjs/errors/flattenFieldErrors.d.ts +14 -0
  2. package/dist/cjs/errors/flattenFieldErrors.js +16 -0
  3. package/dist/cjs/errors/getFlatFieldErrors.d.ts +16 -0
  4. package/dist/cjs/errors/getFlatFieldErrors.js +40 -0
  5. package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.js +7 -4
  6. package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +3 -1
  7. package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.utils.js +7 -3
  8. package/dist/cjs/errors/nonfield/utils.d.ts +15 -7
  9. package/dist/cjs/errors/nonfield/utils.js +14 -72
  10. package/dist/cjs/errors/orphan/useFormErrorsHasOnlyOrphans.d.ts +2 -0
  11. package/dist/cjs/errors/orphan/useFormErrorsHasOnlyOrphans.js +19 -0
  12. package/dist/cjs/errors/orphan/useOrphanFormErrors.d.ts +3 -0
  13. package/dist/cjs/errors/orphan/useOrphanFormErrors.js +18 -0
  14. package/dist/cjs/errors/orphan/useOrphanFormErrorsToConsoleError.d.ts +7 -0
  15. package/dist/cjs/errors/{orphans/useFormErrorOrphansToConsoleError.js → orphan/useOrphanFormErrorsToConsoleError.js} +6 -8
  16. package/dist/cjs/errors/orphan/utils.d.ts +3 -0
  17. package/dist/cjs/errors/orphan/utils.js +21 -0
  18. package/dist/cjs/errors/root/useRootFormFieldErrors.d.ts +6 -0
  19. package/dist/cjs/errors/root/{useFormRootFieldErrors.js → useRootFormFieldErrors.js} +2 -2
  20. package/dist/cjs/errors/root/utils.d.ts +5 -1
  21. package/dist/cjs/errors/root/utils.js +15 -4
  22. package/dist/cjs/errors/useFormErrorConsoleDebug.d.ts +8 -0
  23. package/dist/cjs/errors/useFormErrorConsoleDebug.js +22 -0
  24. package/dist/cjs/errors/useFormErrorsIfAny.d.ts +6 -0
  25. package/dist/cjs/errors/useFormErrorsIfAny.js +13 -0
  26. package/dist/cjs/errors/useFormFlatFieldErrors.d.ts +5 -0
  27. package/dist/cjs/errors/useFormFlatFieldErrors.js +12 -0
  28. package/dist/cjs/main.d.ts +7 -4
  29. package/dist/cjs/main.js +14 -8
  30. package/dist/cjs/utils.d.ts +1 -0
  31. package/dist/cjs/utils.js +5 -0
  32. package/dist/esm/errors/flattenFieldErrors.mjs +5 -0
  33. package/dist/esm/errors/getFlatFieldErrors.mjs +21 -0
  34. package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.mjs +4 -4
  35. package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.utils.mjs +4 -3
  36. package/dist/esm/errors/nonfield/utils.mjs +2 -12
  37. package/dist/esm/errors/orphan/useFormErrorsHasOnlyOrphans.mjs +10 -0
  38. package/dist/esm/errors/orphan/useOrphanFormErrors.mjs +14 -0
  39. package/dist/esm/errors/{orphans/useFormErrorOrphansToConsoleError.mjs → orphan/useOrphanFormErrorsToConsoleError.mjs} +2 -2
  40. package/dist/esm/errors/orphan/utils.mjs +20 -0
  41. package/dist/esm/errors/root/{useFormRootFieldErrors.mjs → useRootFormFieldErrors.mjs} +1 -1
  42. package/dist/esm/errors/root/utils.mjs +8 -3
  43. package/dist/esm/errors/useFormErrorConsoleDebug.mjs +8 -0
  44. package/dist/esm/errors/useFormErrorsIfAny.mjs +9 -0
  45. package/dist/esm/errors/useFormFlatFieldErrors.mjs +6 -0
  46. package/dist/esm/main.mjs +20 -14
  47. package/dist/esm/utils.mjs +4 -0
  48. package/dist/types/errors/flattenFieldErrors.d.ts +15 -0
  49. package/dist/types/errors/getFlatFieldErrors.d.ts +17 -0
  50. package/dist/types/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +3 -1
  51. package/dist/types/errors/nonfield/utils.d.ts +15 -7
  52. package/dist/types/errors/orphan/useFormErrorsHasOnlyOrphans.d.ts +2 -0
  53. package/dist/types/errors/orphan/useOrphanFormErrors.d.ts +4 -0
  54. package/dist/types/errors/orphan/useOrphanFormErrorsToConsoleError.d.ts +8 -0
  55. package/dist/types/errors/orphan/utils.d.ts +4 -0
  56. package/dist/types/errors/root/useRootFormFieldErrors.d.ts +6 -0
  57. package/dist/types/errors/root/utils.d.ts +5 -1
  58. package/dist/types/errors/useFormErrorConsoleDebug.d.ts +8 -0
  59. package/dist/types/errors/useFormErrorsIfAny.d.ts +6 -0
  60. package/dist/types/errors/useFormFlatFieldErrors.d.ts +5 -0
  61. package/dist/types/main.d.ts +7 -4
  62. package/dist/types/utils.d.ts +1 -0
  63. package/package.json +63 -14
  64. package/dist/cjs/errors/orphans/FormErrorNonFieldMarker.d.ts +0 -14
  65. package/dist/cjs/errors/orphans/FormErrorNonFieldMarker.js +0 -20
  66. package/dist/cjs/errors/orphans/useFormErrorOrphansToConsoleError.d.ts +0 -9
  67. package/dist/cjs/errors/orphans/useFormOrphanErrors.d.ts +0 -3
  68. package/dist/cjs/errors/orphans/useFormOrphanErrors.js +0 -17
  69. package/dist/cjs/errors/orphans/utils.d.ts +0 -15
  70. package/dist/cjs/errors/orphans/utils.js +0 -27
  71. package/dist/cjs/errors/root/useFormRootFieldErrors.d.ts +0 -6
  72. package/dist/cjs/errors/roots/useFormRootFieldErrors.d.ts +0 -2
  73. package/dist/cjs/errors/roots/useFormRootFieldErrors.js +0 -9
  74. package/dist/cjs/errors/roots/utils.d.ts +0 -7
  75. package/dist/cjs/errors/roots/utils.js +0 -13
  76. package/dist/cjs/errors/utils.d.ts +0 -9
  77. package/dist/cjs/errors/utils.js +0 -65
  78. package/dist/esm/errors/orphans/useFormOrphanErrors.mjs +0 -13
  79. package/dist/esm/errors/orphans/utils.mjs +0 -17
  80. package/dist/esm/node_modules/flat/index.mjs +0 -22
  81. package/dist/types/errors/orphans/FormErrorNonFieldMarker.d.ts +0 -14
  82. package/dist/types/errors/orphans/useFormErrorOrphansToConsoleError.d.ts +0 -10
  83. package/dist/types/errors/orphans/useFormOrphanErrors.d.ts +0 -4
  84. package/dist/types/errors/orphans/utils.d.ts +0 -16
  85. package/dist/types/errors/root/useFormRootFieldErrors.d.ts +0 -6
  86. package/dist/types/errors/roots/useFormRootFieldErrors.d.ts +0 -2
  87. package/dist/types/errors/roots/utils.d.ts +0 -8
  88. package/dist/types/errors/utils.d.ts +0 -10
@@ -1,7 +0,0 @@
1
- import type { FieldErrors } from 'react-hook-form';
2
- /**
3
- * Determine if an error path is root. (e.g., `root` or `root.nested`)
4
- * @param path Flat path. (e.g., 'root.nested')
5
- */
6
- export declare const isFormErrorPathRoot: (path: string) => boolean;
7
- export declare const getRootFieldErrorList: (errors: FieldErrors) => import("react-hook-form").FieldError[];
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getRootFieldErrorList = exports.isFormErrorPathRoot = void 0;
4
- const utils_1 = require("../utils");
5
- const ROOT = 'root';
6
- /**
7
- * Determine if an error path is root. (e.g., `root` or `root.nested`)
8
- * @param path Flat path. (e.g., 'root.nested')
9
- */
10
- const isFormErrorPathRoot = (path) => path === ROOT || path.startsWith(ROOT + '.');
11
- exports.isFormErrorPathRoot = isFormErrorPathRoot;
12
- const getRootFieldErrorList = (errors) => (0, utils_1.getFieldErrorListByFilter)(errors, exports.isFormErrorPathRoot);
13
- exports.getRootFieldErrorList = getRootFieldErrorList;
@@ -1,9 +0,0 @@
1
- import type { FieldError, FieldErrors } from 'react-hook-form';
2
- /**
3
- * @returns A flattened {@link FieldErrors} record.
4
- */
5
- export declare const getFieldErrorsByFilter: (errors: FieldErrors, where?: ((path: string, flattenedErrors: Record<string, unknown>) => boolean) | undefined) => Record<string, FieldError>;
6
- /**
7
- * @returns An array of field errors. (Not a record.)
8
- */
9
- export declare const getFieldErrorListByFilter: (errors: FieldErrors, where?: ((path: string, flattenedErrors: Record<string, unknown>) => boolean) | undefined) => FieldError[];
@@ -1,65 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFieldErrorListByFilter = exports.getFieldErrorsByFilter = void 0;
4
- const flat_1 = require("flat");
5
- const react_hook_form_1 = require("react-hook-form");
6
- /**
7
- * @returns A flattened {@link FieldErrors} record.
8
- */
9
- const getFieldErrorsByFilter = (errors, where) => {
10
- const keys = getFieldErrorOrphanKeysByFilter(errors, where);
11
- const record = keys.reduce((acc, path) => {
12
- acc[path] = (0, react_hook_form_1.get)(errors, path);
13
- return acc;
14
- }, {});
15
- return record;
16
- };
17
- exports.getFieldErrorsByFilter = getFieldErrorsByFilter;
18
- /**
19
- * @returns An array of field errors. (Not a record.)
20
- */
21
- const getFieldErrorListByFilter = (errors, where) => {
22
- const keys = getFieldErrorOrphanKeysByFilter(errors, where);
23
- const fieldErrors = keys.map((key) => {
24
- return (0, react_hook_form_1.get)(errors, key);
25
- });
26
- return fieldErrors;
27
- };
28
- exports.getFieldErrorListByFilter = getFieldErrorListByFilter;
29
- /**
30
- * RHF uses deeply-nested object for errors, which reflects the structure of the form.
31
- * And the type is quite complex. All this complicates the process of finding errors.
32
- * This function automatically looks only for keys that end with `.ref`.
33
- * (This makes sure we only look at the leaf nodes of the object tree.)
34
- * This function also automatically removes the `.ref` suffix from the keys.
35
- *
36
- * @param where Filter callback.
37
- * @param map Transformer callback.
38
- * @returns Tuple of field errors and keys.
39
- */
40
- const getFieldErrorOrphanKeysByFilter = (errors, where) => {
41
- /**
42
- * e.g.:
43
- * ```js
44
- * { root: { ref: undefined, message: 'Form is invalid.' }, email: { ref: Object, message: 'Invalid.' } }
45
- * ```
46
- * flattens to:
47
- * ```js
48
- * { 'root.ref': undefined, 'root.message': 'Form is invalid.', 'email.ref': Object, 'email.message': 'Invalid.' }
49
- * ```
50
- * returns (if no additional filter/map specified)
51
- * ```js
52
- * [ 'root', 'email' ]
53
- * ```
54
- */
55
- const flattenedErrors = (0, flat_1.flatten)(errors);
56
- const flattenedErrorObjectKeys = Object.keys(flattenedErrors);
57
- // look for leaf node, which would have ref prop (e.g., `orphan_field.ref`)
58
- const flattenedErrorObjectKeysEndingWithDotRef = flattenedErrorObjectKeys.filter((key) => key.match(regexEndsWithDotRef));
59
- // remove `.ref` suffix to get flat field path (e.g., `name`, `address.street`)
60
- const paths = flattenedErrorObjectKeysEndingWithDotRef.map((flattenedKey) => flattenedKey.replace(regexEndsWithDotRef, ''));
61
- // filter via callback
62
- const flattenedKeysFiltered = paths.filter((path) => where ? where(path, flattenedErrors) : true);
63
- return flattenedKeysFiltered;
64
- };
65
- const regexEndsWithDotRef = /\.ref$/;
@@ -1,13 +0,0 @@
1
- import "react";
2
- import n from "../useFormErrorsOnChange.mjs";
3
- import { getOrphanFieldErrorsWithMarkerCheck as t } from "./utils.mjs";
4
- const e = (o) => n((r) => {
5
- if (JSON.stringify(r) === "{}")
6
- return {};
7
- if (!o.current)
8
- throw console.error(r), new Error("This should not happen.");
9
- return t(r, o.current);
10
- }), p = e;
11
- export {
12
- p as default
13
- };
@@ -1,17 +0,0 @@
1
- import { getFormNonFieldErrorMarkerQuerySelector as i } from "../nonfield/FormNonFieldErrorMarker.utils.mjs";
2
- import { getNonFieldErrorRecordByFilter as e } from "../nonfield/utils.mjs";
3
- import { isFormErrorPathRoot as l } from "../root/utils.mjs";
4
- const d = (r, o) => {
5
- const t = m(r);
6
- return e(
7
- t,
8
- (n) => (
9
- // if doesn't have alert, then it's an orphan
10
- !o.querySelector(i(n))
11
- )
12
- );
13
- }, m = (r) => e(r, (o) => !l(o));
14
- export {
15
- m as getNonFieldNonRootErrors,
16
- d as getOrphanFieldErrorsWithMarkerCheck
17
- };
@@ -1,22 +0,0 @@
1
- function d(t) {
2
- return t && t.constructor && typeof t.constructor.isBuffer == "function" && t.constructor.isBuffer(t);
3
- }
4
- function A(t) {
5
- return t;
6
- }
7
- function B(t, n) {
8
- n = n || {};
9
- const l = n.delimiter || ".", m = n.maxDepth, o = n.transformKey || A, f = {};
10
- function i(s, u, r) {
11
- r = r || 1, Object.keys(s).forEach(function(c) {
12
- const e = s[c], x = n.safe && Array.isArray(e), a = Object.prototype.toString.call(e), O = d(e), b = a === "[object Object]" || a === "[object Array]", y = u ? u + l + o(c) : o(c);
13
- if (!x && !O && b && Object.keys(e).length && (!n.maxDepth || r < m))
14
- return i(e, y, r + 1);
15
- f[y] = e;
16
- });
17
- }
18
- return i(t), f;
19
- }
20
- export {
21
- B as flatten
22
- };
@@ -1,14 +0,0 @@
1
- /// <reference types="react" />
2
- type Props = {
3
- /** Flat field name. (e.g., `address.street`) */
4
- path: string;
5
- };
6
- /**
7
- * Empty, hidden marker to indicate a non-field error is presented to user.
8
- */
9
- declare const FormErrorNonFieldMarker: React.FC<Props>;
10
- export default FormErrorNonFieldMarker;
11
- /**
12
- * HTML data attribute for {@link FormErrorNonFieldMarker}.
13
- */
14
- export declare const FormErrorNonFieldMarkerHtmlAttribute = "data-paragrav-rhf-utils-marker-path";
@@ -1,10 +0,0 @@
1
- import { default as React } from 'react';
2
-
3
- /**
4
- * Orphans are field errors that do not have a ref.
5
- * (Most commonly `root(.*)`, but these are filtered out. They are handled by `useFormRootFieldErrors`.)
6
- * Outputs to `console.error` all errors with `ref: undefined` that do not begin with `root(.*)`.
7
- * Sentry (or equivalent) should capture.
8
- */
9
- declare const useFormErrorOrphansToConsoleError: (formRef: React.RefObject<HTMLFormElement>) => void;
10
- export default useFormErrorOrphansToConsoleError;
@@ -1,4 +0,0 @@
1
- import { default as React } from 'react';
2
-
3
- declare const useFormOrphanErrors: (formRef: React.RefObject<HTMLFormElement>) => Record<string, import('react-hook-form').FieldError>;
4
- export default useFormOrphanErrors;
@@ -1,16 +0,0 @@
1
- import { FieldErrors } from 'react-hook-form';
2
-
3
- /**
4
- * Get flat record of orphan errors -- i.e., non-field, non-root and not displayed to user using {@link FormNonFieldErrorMarker}.
5
- * @param fieldErrors
6
- * @param formElement Ref to form element for scoped `querySelector`.
7
- * @returns
8
- */
9
- export declare const getOrphanFieldErrorsWithMarkerCheck: (fieldErrors: FieldErrors, formElement: HTMLFormElement) => Record<string, import('react-hook-form').FieldError>;
10
- /**
11
- * Get non-field errors that are not root errors.
12
- *
13
- * NOTE: This is an intermediary function used by {@link getOrphanFieldErrorsWithMarkerCheck}.
14
- * It is exported for sake of testing.
15
- */
16
- export declare const getNonFieldNonRootErrors: (errors: FieldErrors) => Record<string, import('react-hook-form').FieldError>;
@@ -1,6 +0,0 @@
1
- /**
2
- * Root field errors from form.
3
- * These are typically displayed at the bottom of the form.
4
- */
5
- declare const useFormRootFieldErrors: () => import('react-hook-form').FieldError[];
6
- export default useFormRootFieldErrors;
@@ -1,2 +0,0 @@
1
- declare const useFormRootFieldErrors: () => import('react-hook-form').FieldError[];
2
- export default useFormRootFieldErrors;
@@ -1,8 +0,0 @@
1
- import { FieldErrors } from 'react-hook-form';
2
-
3
- /**
4
- * Determine if an error path is root. (e.g., `root` or `root.nested`)
5
- * @param path Flat path. (e.g., 'root.nested')
6
- */
7
- export declare const isFormErrorPathRoot: (path: string) => boolean;
8
- export declare const getRootFieldErrorList: (errors: FieldErrors) => import('react-hook-form').FieldError[];
@@ -1,10 +0,0 @@
1
- import { FieldError, FieldErrors } from 'react-hook-form';
2
-
3
- /**
4
- * @returns A flattened {@link FieldErrors} record.
5
- */
6
- export declare const getFieldErrorsByFilter: (errors: FieldErrors, where?: ((path: string, flattenedErrors: Record<string, unknown>) => boolean) | undefined) => Record<string, FieldError>;
7
- /**
8
- * @returns An array of field errors. (Not a record.)
9
- */
10
- export declare const getFieldErrorListByFilter: (errors: FieldErrors, where?: ((path: string, flattenedErrors: Record<string, unknown>) => boolean) | undefined) => FieldError[];