@paragrav/rhf-utils 0.0.19 → 0.0.21

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 (73) hide show
  1. package/dist/cjs/errors/getFlatFieldErrors.d.ts +8 -0
  2. package/dist/cjs/errors/getFlatFieldErrors.js +11 -2
  3. package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.d.ts +1 -1
  4. package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.js +6 -3
  5. package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +2 -1
  6. package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.utils.js +5 -3
  7. package/dist/cjs/errors/nonfield/utils.d.ts +1 -1
  8. package/dist/cjs/errors/nonfield/utils.js +1 -1
  9. package/dist/cjs/errors/orphan/useFormErrorsHasOnlyOrphans.d.ts +2 -0
  10. package/dist/cjs/errors/orphan/useFormErrorsHasOnlyOrphans.js +19 -0
  11. package/dist/cjs/errors/{orphans/useFormOrphanErrors.d.ts → orphan/useOrphanFormErrors.d.ts} +2 -2
  12. package/dist/cjs/errors/{orphans/useFormOrphanErrors.js → orphan/useOrphanFormErrors.js} +6 -7
  13. package/dist/cjs/errors/orphan/useOrphanFormErrorsToConsoleError.d.ts +7 -0
  14. package/dist/cjs/errors/{orphans/useFormErrorOrphansToConsoleError.js → orphan/useOrphanFormErrorsToConsoleError.js} +6 -8
  15. package/dist/cjs/errors/orphan/utils.d.ts +3 -0
  16. package/dist/cjs/errors/{orphans → orphan}/utils.js +3 -3
  17. package/dist/cjs/errors/root/{useFormRootFieldErrors.d.ts → useRootFormFieldErrors.d.ts} +2 -2
  18. package/dist/cjs/errors/root/{useFormRootFieldErrors.js → useRootFormFieldErrors.js} +2 -2
  19. package/dist/cjs/errors/useFormErrorConsoleDebug.js +2 -2
  20. package/dist/cjs/errors/useFormErrorsIfAny.d.ts +6 -0
  21. package/dist/cjs/errors/{useFormContextErrorsIfAny.js → useFormErrorsIfAny.js} +2 -2
  22. package/dist/cjs/errors/useFormFlatFieldErrors.d.ts +5 -0
  23. package/dist/cjs/errors/useFormFlatFieldErrors.js +12 -0
  24. package/dist/cjs/main.d.ts +9 -4
  25. package/dist/cjs/main.js +18 -8
  26. package/dist/cjs/submit/useFormOnSubmitSuccessful.d.ts +2 -0
  27. package/dist/cjs/submit/useFormOnSubmitSuccessful.js +17 -0
  28. package/dist/cjs/submit/useFormOnSubmitSuccessfulAndReset.d.ts +10 -0
  29. package/dist/cjs/submit/useFormOnSubmitSuccessfulAndReset.js +39 -0
  30. package/dist/cjs/submit/useResetFormOnSubmitSuccessful.d.ts +11 -0
  31. package/dist/cjs/submit/useResetFormOnSubmitSuccessful.js +21 -0
  32. package/dist/esm/errors/getFlatFieldErrors.mjs +14 -13
  33. package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.mjs +1 -1
  34. package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.utils.mjs +3 -3
  35. package/dist/esm/errors/orphan/useFormErrorsHasOnlyOrphans.mjs +10 -0
  36. package/dist/esm/errors/orphan/useOrphanFormErrors.mjs +14 -0
  37. package/dist/esm/errors/{orphans/useFormErrorOrphansToConsoleError.mjs → orphan/useOrphanFormErrorsToConsoleError.mjs} +2 -2
  38. package/dist/esm/errors/{orphans → orphan}/utils.mjs +1 -1
  39. package/dist/esm/errors/useFormErrorConsoleDebug.mjs +3 -3
  40. package/dist/esm/errors/useFormErrorsIfAny.mjs +9 -0
  41. package/dist/esm/errors/useFormFlatFieldErrors.mjs +6 -0
  42. package/dist/esm/main.mjs +26 -16
  43. package/dist/esm/submit/useFormOnSubmitSuccessful.mjs +11 -0
  44. package/dist/esm/submit/useFormOnSubmitSuccessfulAndReset.mjs +14 -0
  45. package/dist/esm/submit/useResetFormOnSubmitSuccessful.mjs +11 -0
  46. package/dist/types/errors/getFlatFieldErrors.d.ts +8 -0
  47. package/dist/types/errors/nonfield/FormNonFieldErrorMarker.d.ts +1 -1
  48. package/dist/types/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +2 -1
  49. package/dist/types/errors/nonfield/utils.d.ts +1 -1
  50. package/dist/types/errors/orphan/useFormErrorsHasOnlyOrphans.d.ts +2 -0
  51. package/dist/types/errors/{orphans/useFormOrphanErrors.d.ts → orphan/useOrphanFormErrors.d.ts} +2 -2
  52. package/dist/types/errors/orphan/useOrphanFormErrorsToConsoleError.d.ts +8 -0
  53. package/dist/types/errors/orphan/utils.d.ts +4 -0
  54. package/dist/types/errors/root/{useFormRootFieldErrors.d.ts → useRootFormFieldErrors.d.ts} +2 -2
  55. package/dist/types/errors/useFormErrorsIfAny.d.ts +6 -0
  56. package/dist/types/errors/useFormFlatFieldErrors.d.ts +5 -0
  57. package/dist/types/main.d.ts +9 -4
  58. package/dist/types/submit/useFormOnSubmitSuccessful.d.ts +2 -0
  59. package/dist/types/submit/useFormOnSubmitSuccessfulAndReset.d.ts +10 -0
  60. package/dist/types/submit/useResetFormOnSubmitSuccessful.d.ts +12 -0
  61. package/package.json +73 -13
  62. package/dist/cjs/errors/orphans/useFormErrorOrphansToConsoleError.d.ts +0 -9
  63. package/dist/cjs/errors/orphans/useFormErrorsHasOnlyOrphan.d.ts +0 -2
  64. package/dist/cjs/errors/orphans/useFormErrorsHasOnlyOrphan.js +0 -21
  65. package/dist/cjs/errors/orphans/utils.d.ts +0 -3
  66. package/dist/cjs/errors/useFormContextErrorsIfAny.d.ts +0 -6
  67. package/dist/esm/errors/orphans/useFormOrphanErrors.mjs +0 -14
  68. package/dist/esm/errors/useFormContextErrorsIfAny.mjs +0 -9
  69. package/dist/types/errors/orphans/useFormErrorOrphansToConsoleError.d.ts +0 -10
  70. package/dist/types/errors/orphans/useFormErrorsHasOnlyOrphan.d.ts +0 -2
  71. package/dist/types/errors/orphans/utils.d.ts +0 -4
  72. package/dist/types/errors/useFormContextErrorsIfAny.d.ts +0 -6
  73. /package/dist/esm/errors/root/{useFormRootFieldErrors.mjs → useRootFormFieldErrors.mjs} +0 -0
@@ -14,3 +14,11 @@ export type FlatFieldErrors = Record<string, FieldError>;
14
14
  */
15
15
  declare const getFlatFieldErrors: (errors: FieldErrors) => FlatFieldErrors;
16
16
  export default getFlatFieldErrors;
17
+ /**
18
+ * Regular expression to maybe match the end of a leaf node path.
19
+ *
20
+ * i.e., `name.type`, `address.street.message`
21
+ *
22
+ * iow: dot [?: non-capturing]("type" | "message")[\b word boundary][$ end of string]
23
+ */
24
+ export declare const regexMaybeFieldErrorLeafNode: RegExp;
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.regexMaybeFieldErrorLeafNode = void 0;
6
7
  const react_hook_form_1 = require("react-hook-form");
7
8
  const flattenFieldErrors_1 = __importDefault(require("./flattenFieldErrors"));
8
9
  /**
@@ -23,7 +24,7 @@ const getFlatFieldErrors = (errors) => {
23
24
  return Object.fromEntries(keys.reduce((entries, key) => {
24
25
  // look for leaf node, which would have required `type` prop (e.g., `name.type`, `address.street.type`)
25
26
  // remove `regexEndsWithType` suffix to get flat field path (e.g., `name`, `address.street`)
26
- const path = key.replace(regexEndsWithType, '');
27
+ const path = key.replace(exports.regexMaybeFieldErrorLeafNode, '');
27
28
  // if nothing replaced, then it's not a leaf node
28
29
  if (path === key)
29
30
  return entries;
@@ -37,4 +38,12 @@ const getFlatFieldErrors = (errors) => {
37
38
  }, []));
38
39
  };
39
40
  exports.default = getFlatFieldErrors;
40
- const regexEndsWithType = /\.type$/;
41
+ //
42
+ /**
43
+ * Regular expression to maybe match the end of a leaf node path.
44
+ *
45
+ * i.e., `name.type`, `address.street.message`
46
+ *
47
+ * iow: dot [?: non-capturing]("type" | "message")[\b word boundary][$ end of string]
48
+ */
49
+ exports.regexMaybeFieldErrorLeafNode = /\.(?:type|message)\b$/;
@@ -4,7 +4,7 @@ type Props = {
4
4
  path: string;
5
5
  };
6
6
  /**
7
- * Empty, hidden marker to indicate a non-field error is presented to user.
7
+ * Empty, hidden marker to indicate a non-field/non-root error is being displayed to user.
8
8
  */
9
9
  declare const FormNonFieldErrorMarker: React.FC<Props>;
10
10
  export default FormNonFieldErrorMarker;
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const jsx_runtime_1 = require("react/jsx-runtime");
4
- const FormNonFieldErrorMarker_utils_1 = require("./FormNonFieldErrorMarker.utils");
7
+ const FormNonFieldErrorMarker_utils_1 = __importDefault(require("./FormNonFieldErrorMarker.utils"));
5
8
  /**
6
- * Empty, hidden marker to indicate a non-field error is presented to user.
9
+ * Empty, hidden marker to indicate a non-field/non-root error is being displayed to user.
7
10
  */
8
11
  const FormNonFieldErrorMarker = ({ path }) => {
9
12
  const dataAttribute = {
10
- [FormNonFieldErrorMarker_utils_1.FormNonFieldErrorMarkerHtmlAttribute]: path,
13
+ [FormNonFieldErrorMarker_utils_1.default]: path,
11
14
  };
12
15
  return ((0, jsx_runtime_1.jsx)("div", { role: "alert", "aria-hidden": "true" // hide from screen readers
13
16
  , style: { display: 'none' }, ...dataAttribute }));
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * HTML data attribute for {@link FormNonFieldErrorMarker}.
3
3
  */
4
- export declare const FormNonFieldErrorMarkerHtmlAttribute = "data-paragrav-rhf-utils-nonfield-error-marker-path";
4
+ declare const FormNonFieldErrorMarkerHtmlAttribute = "data-paragrav-rhf-utils-nonfield-error-marker-path";
5
+ export default FormNonFieldErrorMarkerHtmlAttribute;
5
6
  export declare const getFormNonFieldErrorMarkerQuerySelector: (path: string) => string;
6
7
  export declare const hasNonFieldErrorMarkerInDOM: (path: string, parentElement: Pick<HTMLElement, 'querySelector'>) => Element | null;
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasNonFieldErrorMarkerInDOM = exports.getFormNonFieldErrorMarkerQuerySelector = exports.FormNonFieldErrorMarkerHtmlAttribute = void 0;
3
+ exports.hasNonFieldErrorMarkerInDOM = exports.getFormNonFieldErrorMarkerQuerySelector = void 0;
4
4
  /**
5
5
  * HTML data attribute for {@link FormNonFieldErrorMarker}.
6
6
  */
7
- exports.FormNonFieldErrorMarkerHtmlAttribute = 'data-paragrav-rhf-utils-nonfield-error-marker-path';
8
- const getFormNonFieldErrorMarkerQuerySelector = (path) => `[role="alert"][${exports.FormNonFieldErrorMarkerHtmlAttribute}="${path}"]`;
7
+ const FormNonFieldErrorMarkerHtmlAttribute = 'data-paragrav-rhf-utils-nonfield-error-marker-path';
8
+ exports.default = FormNonFieldErrorMarkerHtmlAttribute;
9
+ // utils
10
+ const getFormNonFieldErrorMarkerQuerySelector = (path) => `[role="alert"][${FormNonFieldErrorMarkerHtmlAttribute}="${path}"]`;
9
11
  exports.getFormNonFieldErrorMarkerQuerySelector = getFormNonFieldErrorMarkerQuerySelector;
10
12
  const hasNonFieldErrorMarkerInDOM = (path, parentElement) => parentElement.querySelector((0, exports.getFormNonFieldErrorMarkerQuerySelector)(path));
11
13
  exports.hasNonFieldErrorMarkerInDOM = hasNonFieldErrorMarkerInDOM;
@@ -12,6 +12,6 @@ import type { FieldError } from 'react-hook-form';
12
12
  * - Orphan (undisplayed), which are typically caused by one of the following cases:
13
13
  * - The input is missing.
14
14
  * - The schema is incorrect.
15
- * - The non-field error is not displayed to user with `FormErrorNonFieldMarker`.
15
+ * - The error is not displayed to user with `FormErrorNonFieldMarker`.
16
16
  */
17
17
  export declare const isNonFieldError: (error: FieldError) => boolean;
@@ -14,7 +14,7 @@ exports.isNonFieldError = void 0;
14
14
  * - Orphan (undisplayed), which are typically caused by one of the following cases:
15
15
  * - The input is missing.
16
16
  * - The schema is incorrect.
17
- * - The non-field error is not displayed to user with `FormErrorNonFieldMarker`.
17
+ * - The error is not displayed to user with `FormErrorNonFieldMarker`.
18
18
  */
19
19
  const isNonFieldError = (error) => !error.ref;
20
20
  exports.isNonFieldError = isNonFieldError;
@@ -0,0 +1,2 @@
1
+ declare const useFormErrorsHasOnlyOrphans: (formRef: React.RefObject<HTMLFormElement>) => boolean;
2
+ export default useFormErrorsHasOnlyOrphans;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const utils_1 = require("@/utils");
7
+ const useFormFlatFieldErrors_1 = __importDefault(require("../useFormFlatFieldErrors"));
8
+ const useOrphanFormErrors_1 = __importDefault(require("./useOrphanFormErrors"));
9
+ const useFormErrorsHasOnlyOrphans = (formRef) => {
10
+ // get flat field errors of orphans and all errors
11
+ const orphans = (0, useOrphanFormErrors_1.default)(formRef);
12
+ const errors = (0, useFormFlatFieldErrors_1.default)();
13
+ // if either empty, early return
14
+ if ([orphans, errors].some(utils_1.isEmptyObject))
15
+ return false;
16
+ // equal length
17
+ return Object.keys(orphans).length === Object.keys(errors).length;
18
+ };
19
+ exports.default = useFormErrorsHasOnlyOrphans;
@@ -1,3 +1,3 @@
1
1
  import React from 'react';
2
- declare const useFormOrphanErrors: (formRef: React.RefObject<HTMLFormElement>) => import("@/errors/getFlatFieldErrors").FlatFieldErrors;
3
- export default useFormOrphanErrors;
2
+ declare const useOrphanFormErrors: (formRef: React.RefObject<HTMLFormElement>) => import("@/errors/getFlatFieldErrors").FlatFieldErrors;
3
+ export default useOrphanFormErrors;
@@ -6,14 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const utils_1 = require("@/utils");
7
7
  const useFormErrorsOnChange_1 = __importDefault(require("../useFormErrorsOnChange"));
8
8
  const utils_2 = require("./utils");
9
- const useFormOrphanErrors = (formRef) => (0, useFormErrorsOnChange_1.default)((errors) => {
9
+ const useOrphanFormErrors = (formRef) => (0, useFormErrorsOnChange_1.default)((errors) => {
10
10
  // if no errors, return early
11
11
  if ((0, utils_1.isEmptyObject)(errors))
12
12
  return {};
13
- if (!formRef.current) {
14
- console.error(errors);
15
- throw new Error('This should not happen.');
16
- }
17
- return (0, utils_2.getRootFlatFieldErrors)(errors, formRef.current);
13
+ // this should never happen
14
+ if (!formRef.current)
15
+ throw new Error();
16
+ return (0, utils_2.getOrphanFlatFieldErrors)(errors, formRef.current);
18
17
  });
19
- exports.default = useFormOrphanErrors;
18
+ exports.default = useOrphanFormErrors;
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ /**
3
+ * Outputs orphan errors to `console.error` all errors with `ref: undefined` that do not begin with `root(.*)`.
4
+ * Sentry (or equivalent) should capture.
5
+ */
6
+ declare const useOrphanFormErrorsToConsoleError: (formRef: React.RefObject<HTMLFormElement>) => void;
7
+ export default useOrphanFormErrorsToConsoleError;
@@ -5,25 +5,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const react_1 = __importDefault(require("react"));
7
7
  const react_hook_form_1 = require("react-hook-form");
8
- const useFormOrphanErrors_1 = __importDefault(require("./useFormOrphanErrors"));
8
+ const useOrphanFormErrors_1 = __importDefault(require("./useOrphanFormErrors"));
9
9
  /**
10
- * Orphans are field errors that do not have a ref.
11
- * (Most commonly `root(.*)`, but these are filtered out. They are handled by `useFormRootFieldErrors`.)
12
- * Outputs to `console.error` all errors with `ref: undefined` that do not begin with `root(.*)`.
10
+ * Outputs orphan errors to `console.error` all errors with `ref: undefined` that do not begin with `root(.*)`.
13
11
  * Sentry (or equivalent) should capture.
14
12
  */
15
- const useFormErrorOrphansToConsoleError = (formRef) => {
13
+ const useOrphanFormErrorsToConsoleError = (formRef) => {
16
14
  const { getValues } = (0, react_hook_form_1.useFormContext)();
17
15
  const { errors } = (0, react_hook_form_1.useFormState)();
18
- const orphans = (0, useFormOrphanErrors_1.default)(formRef);
16
+ const orphans = (0, useOrphanFormErrors_1.default)(formRef);
19
17
  react_1.default.useEffect(() => {
20
18
  if (!Object.keys(orphans).length)
21
19
  return;
22
- console.error('useFormErrorOrphansToConsoleError', {
20
+ console.error('useOrphanFormErrorsToConsoleError', {
23
21
  location: window.location,
24
22
  values: getValues(),
25
23
  orphans,
26
24
  });
27
25
  }, [errors, getValues, orphans]);
28
26
  };
29
- exports.default = useFormErrorOrphansToConsoleError;
27
+ exports.default = useOrphanFormErrorsToConsoleError;
@@ -0,0 +1,3 @@
1
+ import type { FieldErrors } from 'react-hook-form';
2
+ import type { FlatFieldErrors } from '../getFlatFieldErrors';
3
+ export declare const getOrphanFlatFieldErrors: (errors: FieldErrors, parentElement: Pick<HTMLElement, 'querySelector'>) => FlatFieldErrors;
@@ -3,12 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getRootFlatFieldErrors = void 0;
6
+ exports.getOrphanFlatFieldErrors = void 0;
7
7
  const getFlatFieldErrors_1 = __importDefault(require("../getFlatFieldErrors"));
8
8
  const FormNonFieldErrorMarker_utils_1 = require("../nonfield/FormNonFieldErrorMarker.utils");
9
9
  const utils_1 = require("../nonfield/utils");
10
10
  const utils_2 = require("../root/utils");
11
- const getRootFlatFieldErrors = (errors, parentElement) => {
11
+ const getOrphanFlatFieldErrors = (errors, parentElement) => {
12
12
  const flatFieldErrors = (0, getFlatFieldErrors_1.default)(errors);
13
13
  return Object.fromEntries(Object.entries(flatFieldErrors).filter(([path, error]) =>
14
14
  // not root path
@@ -18,4 +18,4 @@ const getRootFlatFieldErrors = (errors, parentElement) => {
18
18
  // does not have marker
19
19
  !(0, FormNonFieldErrorMarker_utils_1.hasNonFieldErrorMarkerInDOM)(path, parentElement)));
20
20
  };
21
- exports.getRootFlatFieldErrors = getRootFlatFieldErrors;
21
+ exports.getOrphanFlatFieldErrors = getOrphanFlatFieldErrors;
@@ -2,5 +2,5 @@
2
2
  * Root field errors from form.
3
3
  * These are typically displayed at the bottom of the form.
4
4
  */
5
- declare const useFormRootFieldErrors: () => import("@/errors/getFlatFieldErrors").FlatFieldErrors;
6
- export default useFormRootFieldErrors;
5
+ declare const useRootFormFieldErrors: () => import("@/errors/getFlatFieldErrors").FlatFieldErrors;
6
+ export default useRootFormFieldErrors;
@@ -9,5 +9,5 @@ const utils_1 = require("./utils");
9
9
  * Root field errors from form.
10
10
  * These are typically displayed at the bottom of the form.
11
11
  */
12
- const useFormRootFieldErrors = () => (0, useFormErrorsOnChange_1.default)(utils_1.getRootFlatFieldErrors);
13
- exports.default = useFormRootFieldErrors;
12
+ const useRootFormFieldErrors = () => (0, useFormErrorsOnChange_1.default)(utils_1.getRootFlatFieldErrors);
13
+ exports.default = useRootFormFieldErrors;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const useFormContextErrorsIfAny_1 = __importDefault(require("./useFormContextErrorsIfAny"));
6
+ const useFormErrorsIfAny_1 = __importDefault(require("./useFormErrorsIfAny"));
7
7
  /**
8
8
  * Output form errors to console.debug() for easier debugging for certain condition.
9
9
  * (e.g., in dev/staging environments or certain users)
@@ -11,7 +11,7 @@ const useFormContextErrorsIfAny_1 = __importDefault(require("./useFormContextErr
11
11
  * `console.debug()` is unlikely to trigger any automated console capture reporting.
12
12
  */
13
13
  const useFormErrorConsoleDebug = (condition) => {
14
- const errors = (0, useFormContextErrorsIfAny_1.default)();
14
+ const errors = (0, useFormErrorsIfAny_1.default)();
15
15
  if (!condition)
16
16
  return;
17
17
  if (!errors)
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Returns errors only if there are errors.
3
+ * Allows simple typeguard without `isEmpty` dependency.
4
+ */
5
+ declare const useFormErrorsIfAny: () => false | import("react-hook-form").FieldErrors<import("react-hook-form").FieldValues>;
6
+ export default useFormErrorsIfAny;
@@ -6,8 +6,8 @@ const utils_1 = require("@/utils");
6
6
  * Returns errors only if there are errors.
7
7
  * Allows simple typeguard without `isEmpty` dependency.
8
8
  */
9
- const useFormContextErrorsIfAny = () => {
9
+ const useFormErrorsIfAny = () => {
10
10
  const { errors } = (0, react_hook_form_1.useFormState)();
11
11
  return (0, utils_1.isEmptyObject)(errors) ? false : errors;
12
12
  };
13
- exports.default = useFormContextErrorsIfAny;
13
+ exports.default = useFormErrorsIfAny;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Use {@link FieldErrors} as {@link FlatFieldErrors}.
3
+ */
4
+ declare const useFormFlatFieldErrors: () => import("./getFlatFieldErrors").FlatFieldErrors;
5
+ export default useFormFlatFieldErrors;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getFlatFieldErrors_1 = __importDefault(require("./getFlatFieldErrors"));
7
+ const useFormErrorsOnChange_1 = __importDefault(require("./useFormErrorsOnChange"));
8
+ /**
9
+ * Use {@link FieldErrors} as {@link FlatFieldErrors}.
10
+ */
11
+ const useFormFlatFieldErrors = () => (0, useFormErrorsOnChange_1.default)(getFlatFieldErrors_1.default);
12
+ exports.default = useFormFlatFieldErrors;
@@ -1,9 +1,14 @@
1
1
  export { default as FormNonFieldErrorMarker } from './errors/nonfield/FormNonFieldErrorMarker';
2
- export { FormNonFieldErrorMarkerHtmlAttribute } from './errors/nonfield/FormNonFieldErrorMarker.utils';
3
- export { default as useFormErrorOrphansToConsoleError } from './errors/orphans/useFormErrorOrphansToConsoleError';
4
- export { default as useFormOrphanErrors } from './errors/orphans/useFormOrphanErrors';
5
- export { default as useFormRootFieldErrors } from './errors/root/useFormRootFieldErrors';
2
+ export { default as FormNonFieldErrorMarkerHtmlAttribute } from './errors/nonfield/FormNonFieldErrorMarker.utils';
3
+ export { default as useFormErrorsHasOnlyOrphans } from './errors/orphan/useFormErrorsHasOnlyOrphans';
4
+ export { default as useOrphanFormErrors } from './errors/orphan/useOrphanFormErrors';
5
+ export { default as useOrphanFormErrorsToConsoleError } from './errors/orphan/useOrphanFormErrorsToConsoleError';
6
+ export { default as useRootFormFieldErrors } from './errors/root/useRootFormFieldErrors';
6
7
  export { default as useFormErrorConsoleDebug } from './errors/useFormErrorConsoleDebug';
7
8
  export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
9
+ export { default as useFormErrorsIfAny } from './errors/useFormErrorsIfAny';
8
10
  export { default as useFormErrorsOnChange } from './errors/useFormErrorsOnChange';
9
11
  export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
12
+ export { default as useFormOnSubmitSuccessful } from './submit/useFormOnSubmitSuccessful';
13
+ export { default as useFormOnSubmitSuccessfulAndReset } from './submit/useFormOnSubmitSuccessfulAndReset';
14
+ export { default as useResetFormAfterSubmitSuccessful } from './submit/useResetFormOnSubmitSuccessful';
package/dist/cjs/main.js CHANGED
@@ -3,22 +3,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useFormRequestSubmit = exports.useFormErrorsOnChange = exports.useFormErrorsEffect = exports.useFormErrorConsoleDebug = exports.useFormRootFieldErrors = exports.useFormOrphanErrors = exports.useFormErrorOrphansToConsoleError = exports.FormNonFieldErrorMarkerHtmlAttribute = exports.FormNonFieldErrorMarker = void 0;
6
+ exports.useResetFormAfterSubmitSuccessful = exports.useFormOnSubmitSuccessfulAndReset = exports.useFormOnSubmitSuccessful = exports.useFormRequestSubmit = exports.useFormErrorsOnChange = exports.useFormErrorsIfAny = exports.useFormErrorsEffect = exports.useFormErrorConsoleDebug = exports.useRootFormFieldErrors = exports.useOrphanFormErrorsToConsoleError = exports.useOrphanFormErrors = exports.useFormErrorsHasOnlyOrphans = exports.FormNonFieldErrorMarkerHtmlAttribute = exports.FormNonFieldErrorMarker = void 0;
7
7
  var FormNonFieldErrorMarker_1 = require("./errors/nonfield/FormNonFieldErrorMarker");
8
8
  Object.defineProperty(exports, "FormNonFieldErrorMarker", { enumerable: true, get: function () { return __importDefault(FormNonFieldErrorMarker_1).default; } });
9
9
  var FormNonFieldErrorMarker_utils_1 = require("./errors/nonfield/FormNonFieldErrorMarker.utils");
10
- Object.defineProperty(exports, "FormNonFieldErrorMarkerHtmlAttribute", { enumerable: true, get: function () { return FormNonFieldErrorMarker_utils_1.FormNonFieldErrorMarkerHtmlAttribute; } });
11
- var useFormErrorOrphansToConsoleError_1 = require("./errors/orphans/useFormErrorOrphansToConsoleError");
12
- Object.defineProperty(exports, "useFormErrorOrphansToConsoleError", { enumerable: true, get: function () { return __importDefault(useFormErrorOrphansToConsoleError_1).default; } });
13
- var useFormOrphanErrors_1 = require("./errors/orphans/useFormOrphanErrors");
14
- Object.defineProperty(exports, "useFormOrphanErrors", { enumerable: true, get: function () { return __importDefault(useFormOrphanErrors_1).default; } });
15
- var useFormRootFieldErrors_1 = require("./errors/root/useFormRootFieldErrors");
16
- Object.defineProperty(exports, "useFormRootFieldErrors", { enumerable: true, get: function () { return __importDefault(useFormRootFieldErrors_1).default; } });
10
+ Object.defineProperty(exports, "FormNonFieldErrorMarkerHtmlAttribute", { enumerable: true, get: function () { return __importDefault(FormNonFieldErrorMarker_utils_1).default; } });
11
+ var useFormErrorsHasOnlyOrphans_1 = require("./errors/orphan/useFormErrorsHasOnlyOrphans");
12
+ Object.defineProperty(exports, "useFormErrorsHasOnlyOrphans", { enumerable: true, get: function () { return __importDefault(useFormErrorsHasOnlyOrphans_1).default; } });
13
+ var useOrphanFormErrors_1 = require("./errors/orphan/useOrphanFormErrors");
14
+ Object.defineProperty(exports, "useOrphanFormErrors", { enumerable: true, get: function () { return __importDefault(useOrphanFormErrors_1).default; } });
15
+ var useOrphanFormErrorsToConsoleError_1 = require("./errors/orphan/useOrphanFormErrorsToConsoleError");
16
+ Object.defineProperty(exports, "useOrphanFormErrorsToConsoleError", { enumerable: true, get: function () { return __importDefault(useOrphanFormErrorsToConsoleError_1).default; } });
17
+ var useRootFormFieldErrors_1 = require("./errors/root/useRootFormFieldErrors");
18
+ Object.defineProperty(exports, "useRootFormFieldErrors", { enumerable: true, get: function () { return __importDefault(useRootFormFieldErrors_1).default; } });
17
19
  var useFormErrorConsoleDebug_1 = require("./errors/useFormErrorConsoleDebug");
18
20
  Object.defineProperty(exports, "useFormErrorConsoleDebug", { enumerable: true, get: function () { return __importDefault(useFormErrorConsoleDebug_1).default; } });
19
21
  var useFormErrorsEffect_1 = require("./errors/useFormErrorsEffect");
20
22
  Object.defineProperty(exports, "useFormErrorsEffect", { enumerable: true, get: function () { return __importDefault(useFormErrorsEffect_1).default; } });
23
+ var useFormErrorsIfAny_1 = require("./errors/useFormErrorsIfAny");
24
+ Object.defineProperty(exports, "useFormErrorsIfAny", { enumerable: true, get: function () { return __importDefault(useFormErrorsIfAny_1).default; } });
21
25
  var useFormErrorsOnChange_1 = require("./errors/useFormErrorsOnChange");
22
26
  Object.defineProperty(exports, "useFormErrorsOnChange", { enumerable: true, get: function () { return __importDefault(useFormErrorsOnChange_1).default; } });
23
27
  var useFormRequestSubmit_1 = require("./errors/useFormRequestSubmit");
24
28
  Object.defineProperty(exports, "useFormRequestSubmit", { enumerable: true, get: function () { return __importDefault(useFormRequestSubmit_1).default; } });
29
+ var useFormOnSubmitSuccessful_1 = require("./submit/useFormOnSubmitSuccessful");
30
+ Object.defineProperty(exports, "useFormOnSubmitSuccessful", { enumerable: true, get: function () { return __importDefault(useFormOnSubmitSuccessful_1).default; } });
31
+ var useFormOnSubmitSuccessfulAndReset_1 = require("./submit/useFormOnSubmitSuccessfulAndReset");
32
+ Object.defineProperty(exports, "useFormOnSubmitSuccessfulAndReset", { enumerable: true, get: function () { return __importDefault(useFormOnSubmitSuccessfulAndReset_1).default; } });
33
+ var useResetFormOnSubmitSuccessful_1 = require("./submit/useResetFormOnSubmitSuccessful");
34
+ Object.defineProperty(exports, "useResetFormAfterSubmitSuccessful", { enumerable: true, get: function () { return __importDefault(useResetFormOnSubmitSuccessful_1).default; } });
@@ -0,0 +1,2 @@
1
+ declare const useFormOnSubmitSuccessful: (callback?: () => void) => void;
2
+ export default useFormOnSubmitSuccessful;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const react_hook_form_1 = require("react-hook-form");
8
+ const useFormOnSubmitSuccessful = (callback) => {
9
+ const { isSubmitSuccessful } = (0, react_hook_form_1.useFormState)();
10
+ react_1.default.useEffect(() => {
11
+ if (!callback)
12
+ return;
13
+ if (isSubmitSuccessful)
14
+ callback();
15
+ }, [callback, isSubmitSuccessful]);
16
+ };
17
+ exports.default = useFormOnSubmitSuccessful;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Calls a callback after a successful submit and after a form reset.
3
+ *
4
+ * Using RHF and following its rules, we are not able to get the state `isSubmitSuccessful=true` and `isDirty=false`.
5
+ * So, this has to keep track of previous state.
6
+ *
7
+ * SubmitSuccessfulAndReset = was `isSubmitSuccessful` and now `!isSubmitSuccessful && !isDirty`
8
+ */
9
+ declare const useFormOnSubmitSuccessfulAndReset: (callback?: () => void) => void;
10
+ export default useFormOnSubmitSuccessfulAndReset;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const react_hook_form_1 = require("react-hook-form");
8
+ const useFormOnSubmitSuccessful_1 = __importDefault(require("./useFormOnSubmitSuccessful"));
9
+ /**
10
+ * Calls a callback after a successful submit and after a form reset.
11
+ *
12
+ * Using RHF and following its rules, we are not able to get the state `isSubmitSuccessful=true` and `isDirty=false`.
13
+ * So, this has to keep track of previous state.
14
+ *
15
+ * SubmitSuccessfulAndReset = was `isSubmitSuccessful` and now `!isSubmitSuccessful && !isDirty`
16
+ */
17
+ const useFormOnSubmitSuccessfulAndReset = (callback) => {
18
+ const { isSubmitSuccessful, isDirty } = (0, react_hook_form_1.useFormState)();
19
+ /** Tracks whether form has been successfully submitted. (Ref as it should not trigger re-render.) */
20
+ const wasSubmittedSuccessfully = react_1.default.useRef(false);
21
+ // mark as submitted successfully
22
+ (0, useFormOnSubmitSuccessful_1.default)(() => {
23
+ wasSubmittedSuccessfully.current = true;
24
+ });
25
+ react_1.default.useEffect(() => {
26
+ if (!callback)
27
+ return;
28
+ // if hasn't been submitted successfully (to distinguish from initial load)
29
+ if (!wasSubmittedSuccessfully.current)
30
+ return;
31
+ // after resetting (and on initial load), both state vars are `false`
32
+ if (!isSubmitSuccessful && !isDirty) {
33
+ callback();
34
+ // reset ref
35
+ wasSubmittedSuccessfully.current = false;
36
+ }
37
+ }, [isSubmitSuccessful, isDirty]); // eslint-disable-line react-hooks/exhaustive-deps
38
+ };
39
+ exports.default = useFormOnSubmitSuccessfulAndReset;
@@ -0,0 +1,11 @@
1
+ import type { KeepStateOptions } from 'react-hook-form';
2
+ type Options = {
3
+ enabled?: boolean;
4
+ } & KeepStateOptions;
5
+ /**
6
+ * Handle resetting after successful submit with no server validation errors.
7
+ * See `Rules` section: https://react-hook-form.com/api/useform/reset/
8
+ * Specifically: "- It's recommended to reset inside useEffect after submission."
9
+ */
10
+ declare const useResetFormAfterSubmitSuccessful: (options?: Options) => void;
11
+ export default useResetFormAfterSubmitSuccessful;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_hook_form_1 = require("react-hook-form");
7
+ const useFormOnSubmitSuccessful_1 = __importDefault(require("./useFormOnSubmitSuccessful"));
8
+ /**
9
+ * Handle resetting after successful submit with no server validation errors.
10
+ * See `Rules` section: https://react-hook-form.com/api/useform/reset/
11
+ * Specifically: "- It's recommended to reset inside useEffect after submission."
12
+ */
13
+ const useResetFormAfterSubmitSuccessful = (options) => {
14
+ const { reset } = (0, react_hook_form_1.useFormContext)();
15
+ (0, useFormOnSubmitSuccessful_1.default)(() => {
16
+ if (!(options === null || options === void 0 ? void 0 : options.enabled))
17
+ return;
18
+ reset(undefined, options);
19
+ });
20
+ };
21
+ exports.default = useResetFormAfterSubmitSuccessful;
@@ -1,21 +1,22 @@
1
- import { get as f } from "react-hook-form";
2
- import d from "./flattenFieldErrors.mjs";
3
- const i = (e) => {
4
- const c = d(e), s = Object.keys(c);
1
+ import { get as l } from "react-hook-form";
2
+ import n from "./flattenFieldErrors.mjs";
3
+ const d = (t) => {
4
+ const a = n(t), c = Object.keys(a);
5
5
  return Object.fromEntries(
6
- s.reduce(
7
- (t, o) => {
8
- const r = o.replace(l, "");
9
- if (r === o)
10
- return t;
11
- const n = f(e, r, void 0);
12
- return n && t.push([r, n]), t;
6
+ c.reduce(
7
+ (r, o) => {
8
+ const e = o.replace(f, "");
9
+ if (e === o)
10
+ return r;
11
+ const s = l(t, e, void 0);
12
+ return s && r.push([e, s]), r;
13
13
  },
14
14
  []
15
15
  // entries
16
16
  )
17
17
  );
18
- }, l = /\.type$/;
18
+ }, u = d, f = /\.(?:type|message)\b$/;
19
19
  export {
20
- i as default
20
+ u as default,
21
+ f as regexMaybeFieldErrorLeafNode
21
22
  };
@@ -1,5 +1,5 @@
1
1
  import { jsx as t } from "react/jsx-runtime";
2
- import { FormNonFieldErrorMarkerHtmlAttribute as e } from "./FormNonFieldErrorMarker.utils.mjs";
2
+ import e from "./FormNonFieldErrorMarker.utils.mjs";
3
3
  const a = ({ path: r }) => {
4
4
  const o = {
5
5
  [e]: r
@@ -1,6 +1,6 @@
1
- const o = "data-paragrav-rhf-utils-nonfield-error-marker-path", t = (r) => `[role="alert"][${o}="${r}"]`, a = (r, e) => e.querySelector(t(r));
1
+ const e = "data-paragrav-rhf-utils-nonfield-error-marker-path", a = e, t = (r) => `[role="alert"][${e}="${r}"]`, l = (r, o) => o.querySelector(t(r));
2
2
  export {
3
- o as FormNonFieldErrorMarkerHtmlAttribute,
3
+ a as default,
4
4
  t as getFormNonFieldErrorMarkerQuerySelector,
5
- a as hasNonFieldErrorMarkerInDOM
5
+ l as hasNonFieldErrorMarkerInDOM
6
6
  };
@@ -0,0 +1,10 @@
1
+ import { isEmptyObject as e } from "../../utils.mjs";
2
+ import t from "../useFormFlatFieldErrors.mjs";
3
+ import n from "./useOrphanFormErrors.mjs";
4
+ const m = (o) => {
5
+ const r = n(o), s = t();
6
+ return [r, s].some(e) ? !1 : Object.keys(r).length === Object.keys(s).length;
7
+ }, O = m;
8
+ export {
9
+ O as default
10
+ };
@@ -0,0 +1,14 @@
1
+ import "react";
2
+ import { isEmptyObject as t } from "../../utils.mjs";
3
+ import e from "../useFormErrorsOnChange.mjs";
4
+ import { getOrphanFlatFieldErrors as n } from "./utils.mjs";
5
+ const m = (r) => e((o) => {
6
+ if (t(o))
7
+ return {};
8
+ if (!r.current)
9
+ throw new Error();
10
+ return n(o, r.current);
11
+ }), a = m;
12
+ export {
13
+ a as default
14
+ };
@@ -1,10 +1,10 @@
1
1
  import t from "react";
2
2
  import { useFormContext as n, useFormState as a } from "react-hook-form";
3
- import m from "./useFormOrphanErrors.mjs";
3
+ import m from "./useOrphanFormErrors.mjs";
4
4
  const c = (e) => {
5
5
  const { getValues: o } = n(), { errors: s } = a(), r = m(e);
6
6
  t.useEffect(() => {
7
- Object.keys(r).length && console.error("useFormErrorOrphansToConsoleError", {
7
+ Object.keys(r).length && console.error("useOrphanFormErrorsToConsoleError", {
8
8
  location: window.location,
9
9
  values: o(),
10
10
  orphans: r
@@ -16,5 +16,5 @@ const d = (o, t) => {
16
16
  );
17
17
  };
18
18
  export {
19
- d as getRootFlatFieldErrors
19
+ d as getOrphanFlatFieldErrors
20
20
  };
@@ -1,8 +1,8 @@
1
- import e from "./useFormContextErrorsIfAny.mjs";
1
+ import e from "./useFormErrorsIfAny.mjs";
2
2
  const s = (o) => {
3
3
  const r = e();
4
4
  o && r && console.debug("useFormErrorConsoleDebug", r);
5
- }, u = s;
5
+ }, n = s;
6
6
  export {
7
- u as default
7
+ n as default
8
8
  };
@@ -0,0 +1,9 @@
1
+ import { useFormState as o } from "react-hook-form";
2
+ import { isEmptyObject as s } from "../utils.mjs";
3
+ const t = () => {
4
+ const { errors: r } = o();
5
+ return s(r) ? !1 : r;
6
+ }, f = t;
7
+ export {
8
+ f as default
9
+ };
@@ -0,0 +1,6 @@
1
+ import r from "./getFlatFieldErrors.mjs";
2
+ import o from "./useFormErrorsOnChange.mjs";
3
+ const s = () => o(r);
4
+ export {
5
+ s as default
6
+ };
package/dist/esm/main.mjs CHANGED
@@ -1,20 +1,30 @@
1
1
  import { default as e } from "./errors/nonfield/FormNonFieldErrorMarker.mjs";
2
- import { FormNonFieldErrorMarkerHtmlAttribute as s } from "./errors/nonfield/FormNonFieldErrorMarker.utils.mjs";
3
- import { default as m } from "./errors/orphans/useFormErrorOrphansToConsoleError.mjs";
4
- import { default as u } from "./errors/orphans/useFormOrphanErrors.mjs";
5
- import { default as F } from "./errors/root/useFormRootFieldErrors.mjs";
6
- import { default as p } from "./errors/useFormErrorConsoleDebug.mjs";
7
- import { default as x } from "./errors/useFormErrorsEffect.mjs";
8
- import { default as i } from "./errors/useFormErrorsOnChange.mjs";
9
- import { default as h } from "./errors/useFormRequestSubmit.mjs";
2
+ import { default as t } from "./errors/nonfield/FormNonFieldErrorMarker.utils.mjs";
3
+ import { default as a } from "./errors/orphan/useFormErrorsHasOnlyOrphans.mjs";
4
+ import { default as m } from "./errors/orphan/useOrphanFormErrors.mjs";
5
+ import { default as d } from "./errors/orphan/useOrphanFormErrorsToConsoleError.mjs";
6
+ import { default as F } from "./errors/root/useRootFormFieldErrors.mjs";
7
+ import { default as x } from "./errors/useFormErrorConsoleDebug.mjs";
8
+ import { default as i } from "./errors/useFormErrorsEffect.mjs";
9
+ import { default as O } from "./errors/useFormErrorsIfAny.mjs";
10
+ import { default as b } from "./errors/useFormErrorsOnChange.mjs";
11
+ import { default as A } from "./errors/useFormRequestSubmit.mjs";
12
+ import { default as C } from "./submit/useFormOnSubmitSuccessful.mjs";
13
+ import { default as k } from "./submit/useFormOnSubmitSuccessfulAndReset.mjs";
14
+ import { default as H } from "./submit/useResetFormOnSubmitSuccessful.mjs";
10
15
  export {
11
16
  e as FormNonFieldErrorMarker,
12
- s as FormNonFieldErrorMarkerHtmlAttribute,
13
- p as useFormErrorConsoleDebug,
14
- m as useFormErrorOrphansToConsoleError,
15
- x as useFormErrorsEffect,
16
- i as useFormErrorsOnChange,
17
- u as useFormOrphanErrors,
18
- h as useFormRequestSubmit,
19
- F as useFormRootFieldErrors
17
+ t as FormNonFieldErrorMarkerHtmlAttribute,
18
+ x as useFormErrorConsoleDebug,
19
+ i as useFormErrorsEffect,
20
+ a as useFormErrorsHasOnlyOrphans,
21
+ O as useFormErrorsIfAny,
22
+ b as useFormErrorsOnChange,
23
+ C as useFormOnSubmitSuccessful,
24
+ k as useFormOnSubmitSuccessfulAndReset,
25
+ A as useFormRequestSubmit,
26
+ m as useOrphanFormErrors,
27
+ d as useOrphanFormErrorsToConsoleError,
28
+ H as useResetFormAfterSubmitSuccessful,
29
+ F as useRootFormFieldErrors
20
30
  };
@@ -0,0 +1,11 @@
1
+ import u from "react";
2
+ import { useFormState as e } from "react-hook-form";
3
+ const o = (s) => {
4
+ const { isSubmitSuccessful: t } = e();
5
+ u.useEffect(() => {
6
+ s && t && s();
7
+ }, [s, t]);
8
+ }, r = o;
9
+ export {
10
+ r as default
11
+ };
@@ -0,0 +1,14 @@
1
+ import r from "react";
2
+ import { useFormState as c } from "react-hook-form";
3
+ import f from "./useFormOnSubmitSuccessful.mjs";
4
+ const m = (s) => {
5
+ const { isSubmitSuccessful: t, isDirty: u } = c(), e = r.useRef(!1);
6
+ f(() => {
7
+ e.current = !0;
8
+ }), r.useEffect(() => {
9
+ s && e.current && !t && !u && (s(), e.current = !1);
10
+ }, [t, u]);
11
+ }, S = m;
12
+ export {
13
+ S as default
14
+ };
@@ -0,0 +1,11 @@
1
+ import { useFormContext as r } from "react-hook-form";
2
+ import t from "./useFormOnSubmitSuccessful.mjs";
3
+ const s = (e) => {
4
+ const { reset: u } = r();
5
+ t(() => {
6
+ e != null && e.enabled && u(void 0, e);
7
+ });
8
+ }, f = s;
9
+ export {
10
+ f as default
11
+ };
@@ -15,3 +15,11 @@ export type FlatFieldErrors = Record<string, FieldError>;
15
15
  */
16
16
  declare const getFlatFieldErrors: (errors: FieldErrors) => FlatFieldErrors;
17
17
  export default getFlatFieldErrors;
18
+ /**
19
+ * Regular expression to maybe match the end of a leaf node path.
20
+ *
21
+ * i.e., `name.type`, `address.street.message`
22
+ *
23
+ * iow: dot [?: non-capturing]("type" | "message")[\b word boundary][$ end of string]
24
+ */
25
+ export declare const regexMaybeFieldErrorLeafNode: RegExp;
@@ -4,7 +4,7 @@ type Props = {
4
4
  path: string;
5
5
  };
6
6
  /**
7
- * Empty, hidden marker to indicate a non-field error is presented to user.
7
+ * Empty, hidden marker to indicate a non-field/non-root error is being displayed to user.
8
8
  */
9
9
  declare const FormNonFieldErrorMarker: React.FC<Props>;
10
10
  export default FormNonFieldErrorMarker;
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * HTML data attribute for {@link FormNonFieldErrorMarker}.
3
3
  */
4
- export declare const FormNonFieldErrorMarkerHtmlAttribute = "data-paragrav-rhf-utils-nonfield-error-marker-path";
4
+ declare const FormNonFieldErrorMarkerHtmlAttribute = "data-paragrav-rhf-utils-nonfield-error-marker-path";
5
+ export default FormNonFieldErrorMarkerHtmlAttribute;
5
6
  export declare const getFormNonFieldErrorMarkerQuerySelector: (path: string) => string;
6
7
  export declare const hasNonFieldErrorMarkerInDOM: (path: string, parentElement: Pick<HTMLElement, 'querySelector'>) => Element | null;
@@ -13,6 +13,6 @@ import { FieldError } from 'react-hook-form';
13
13
  * - Orphan (undisplayed), which are typically caused by one of the following cases:
14
14
  * - The input is missing.
15
15
  * - The schema is incorrect.
16
- * - The non-field error is not displayed to user with `FormErrorNonFieldMarker`.
16
+ * - The error is not displayed to user with `FormErrorNonFieldMarker`.
17
17
  */
18
18
  export declare const isNonFieldError: (error: FieldError) => boolean;
@@ -0,0 +1,2 @@
1
+ declare const useFormErrorsHasOnlyOrphans: (formRef: React.RefObject<HTMLFormElement>) => boolean;
2
+ export default useFormErrorsHasOnlyOrphans;
@@ -1,4 +1,4 @@
1
1
  import { default as React } from 'react';
2
2
 
3
- declare const useFormOrphanErrors: (formRef: React.RefObject<HTMLFormElement>) => import('../getFlatFieldErrors').FlatFieldErrors;
4
- export default useFormOrphanErrors;
3
+ declare const useOrphanFormErrors: (formRef: React.RefObject<HTMLFormElement>) => import('../getFlatFieldErrors').FlatFieldErrors;
4
+ export default useOrphanFormErrors;
@@ -0,0 +1,8 @@
1
+ import { default as React } from 'react';
2
+
3
+ /**
4
+ * Outputs orphan errors to `console.error` all errors with `ref: undefined` that do not begin with `root(.*)`.
5
+ * Sentry (or equivalent) should capture.
6
+ */
7
+ declare const useOrphanFormErrorsToConsoleError: (formRef: React.RefObject<HTMLFormElement>) => void;
8
+ export default useOrphanFormErrorsToConsoleError;
@@ -0,0 +1,4 @@
1
+ import { FlatFieldErrors } from '../getFlatFieldErrors';
2
+ import { FieldErrors } from 'react-hook-form';
3
+
4
+ export declare const getOrphanFlatFieldErrors: (errors: FieldErrors, parentElement: Pick<HTMLElement, 'querySelector'>) => FlatFieldErrors;
@@ -2,5 +2,5 @@
2
2
  * Root field errors from form.
3
3
  * These are typically displayed at the bottom of the form.
4
4
  */
5
- declare const useFormRootFieldErrors: () => import('../getFlatFieldErrors').FlatFieldErrors;
6
- export default useFormRootFieldErrors;
5
+ declare const useRootFormFieldErrors: () => import('../getFlatFieldErrors').FlatFieldErrors;
6
+ export default useRootFormFieldErrors;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Returns errors only if there are errors.
3
+ * Allows simple typeguard without `isEmpty` dependency.
4
+ */
5
+ declare const useFormErrorsIfAny: () => false | import('react-hook-form').FieldErrors<import("react-hook-form").FieldValues>;
6
+ export default useFormErrorsIfAny;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Use {@link FieldErrors} as {@link FlatFieldErrors}.
3
+ */
4
+ declare const useFormFlatFieldErrors: () => import('./getFlatFieldErrors').FlatFieldErrors;
5
+ export default useFormFlatFieldErrors;
@@ -1,9 +1,14 @@
1
1
  export { default as FormNonFieldErrorMarker } from './errors/nonfield/FormNonFieldErrorMarker';
2
- export { FormNonFieldErrorMarkerHtmlAttribute } from './errors/nonfield/FormNonFieldErrorMarker.utils';
3
- export { default as useFormErrorOrphansToConsoleError } from './errors/orphans/useFormErrorOrphansToConsoleError';
4
- export { default as useFormOrphanErrors } from './errors/orphans/useFormOrphanErrors';
5
- export { default as useFormRootFieldErrors } from './errors/root/useFormRootFieldErrors';
2
+ export { default as FormNonFieldErrorMarkerHtmlAttribute } from './errors/nonfield/FormNonFieldErrorMarker.utils';
3
+ export { default as useFormErrorsHasOnlyOrphans } from './errors/orphan/useFormErrorsHasOnlyOrphans';
4
+ export { default as useOrphanFormErrors } from './errors/orphan/useOrphanFormErrors';
5
+ export { default as useOrphanFormErrorsToConsoleError } from './errors/orphan/useOrphanFormErrorsToConsoleError';
6
+ export { default as useRootFormFieldErrors } from './errors/root/useRootFormFieldErrors';
6
7
  export { default as useFormErrorConsoleDebug } from './errors/useFormErrorConsoleDebug';
7
8
  export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
9
+ export { default as useFormErrorsIfAny } from './errors/useFormErrorsIfAny';
8
10
  export { default as useFormErrorsOnChange } from './errors/useFormErrorsOnChange';
9
11
  export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
12
+ export { default as useFormOnSubmitSuccessful } from './submit/useFormOnSubmitSuccessful';
13
+ export { default as useFormOnSubmitSuccessfulAndReset } from './submit/useFormOnSubmitSuccessfulAndReset';
14
+ export { default as useResetFormAfterSubmitSuccessful } from './submit/useResetFormOnSubmitSuccessful';
@@ -0,0 +1,2 @@
1
+ declare const useFormOnSubmitSuccessful: (callback?: () => void) => void;
2
+ export default useFormOnSubmitSuccessful;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Calls a callback after a successful submit and after a form reset.
3
+ *
4
+ * Using RHF and following its rules, we are not able to get the state `isSubmitSuccessful=true` and `isDirty=false`.
5
+ * So, this has to keep track of previous state.
6
+ *
7
+ * SubmitSuccessfulAndReset = was `isSubmitSuccessful` and now `!isSubmitSuccessful && !isDirty`
8
+ */
9
+ declare const useFormOnSubmitSuccessfulAndReset: (callback?: () => void) => void;
10
+ export default useFormOnSubmitSuccessfulAndReset;
@@ -0,0 +1,12 @@
1
+ import { KeepStateOptions } from 'react-hook-form';
2
+
3
+ type Options = {
4
+ enabled?: boolean;
5
+ } & KeepStateOptions;
6
+ /**
7
+ * Handle resetting after successful submit with no server validation errors.
8
+ * See `Rules` section: https://react-hook-form.com/api/useform/reset/
9
+ * Specifically: "- It's recommended to reset inside useEffect after submission."
10
+ */
11
+ declare const useResetFormAfterSubmitSuccessful: (options?: Options) => void;
12
+ export default useResetFormAfterSubmitSuccessful;
package/package.json CHANGED
@@ -2,9 +2,10 @@
2
2
  "name": "@paragrav/rhf-utils",
3
3
  "author": "paragrav.dev",
4
4
  "license": "MIT",
5
- "version": "0.0.19",
5
+ "version": "0.0.21",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
+ "sideEffects": false,
8
9
  "files": [
9
10
  "dist"
10
11
  ],
@@ -12,22 +13,81 @@
12
13
  "module": "./dist/esm/main.mjs",
13
14
  "exports": {
14
15
  ".": {
15
- "require": {
16
- "types": "./dist/types/main.d.ts",
17
- "default": "./dist/cjs/main.js"
18
- },
19
- "import": {
20
- "types": "./dist/types/main.d.ts",
21
- "default": "./dist/esm/main.mjs"
22
- }
16
+ "import": "./dist/esm/main.mjs",
17
+ "require": "./dist/cjs/main.js",
18
+ "types": "./dist/types/main.d.ts"
23
19
  },
24
- "./useFormErrorConsoleDebug": {
25
- "types": "./dist/types/errors/useFormErrorConsoleDebug.d.ts",
20
+ "./errors/useFormErrorConsoleDebug": {
21
+ "import": "./dist/esm/errors/useFormErrorConsoleDebug.mjs",
26
22
  "require": "./dist/cjs/errors/useFormErrorConsoleDebug.cjs",
27
- "import": "./dist/esm/errors/useFormErrorConsoleDebug.mjs"
23
+ "types": "./dist/types/errors/useFormErrorConsoleDebug.d.ts"
24
+ },
25
+ "./errors/useFormErrorsEffect": {
26
+ "import": "./dist/esm/errors/useFormErrorsEffect.mjs",
27
+ "require": "./dist/cjs/errors/useFormErrorsEffect.cjs",
28
+ "types": "./dist/types/errors/useFormErrorsEffect.d.ts"
29
+ },
30
+ "./errors/useFormErrorsIfAny": {
31
+ "import": "./dist/esm/errors/useFormErrorsIfAny.mjs",
32
+ "require": "./dist/cjs/errors/useFormErrorsIfAny.cjs",
33
+ "types": "./dist/types/errors/useFormErrorsIfAny.d.ts"
34
+ },
35
+ "./errors/useFormErrorsOnChange": {
36
+ "import": "./dist/esm/errors/useFormErrorsOnChange.mjs",
37
+ "require": "./dist/cjs/errors/useFormErrorsOnChange.cjs",
38
+ "types": "./dist/types/errors/useFormErrorsOnChange.d.ts"
39
+ },
40
+ "./errors/useFormRequestSubmit": {
41
+ "import": "./dist/esm/errors/useFormRequestSubmit.mjs",
42
+ "require": "./dist/cjs/errors/useFormRequestSubmit.cjs",
43
+ "types": "./dist/types/errors/useFormRequestSubmit.d.ts"
44
+ },
45
+ "./errors/FormNonFieldErrorMarker": {
46
+ "import": "./dist/esm/errors/nonfield/FormNonFieldErrorMarker.mjs",
47
+ "require": "./dist/cjs/errors/nonfield/FormNonFieldErrorMarker.cjs",
48
+ "types": "./dist/types/errors/nonfield/FormNonFieldErrorMarker.d.ts"
49
+ },
50
+ "./errors/FormNonFieldErrorMarkerHtmlAttribute": {
51
+ "import": "./dist/esm/errors/nonfield/FormNonFieldErrorMarker.utils.mjs",
52
+ "require": "./dist/cjs/errors/nonfield/FormNonFieldErrorMarker.utils.cjs",
53
+ "types": "./dist/types/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts"
54
+ },
55
+ "./errors/useOrphanFormErrorsToConsoleError": {
56
+ "import": "./dist/esm/errors/orphan/useOrphanFormErrorsToConsoleError.mjs",
57
+ "require": "./dist/cjs/errors/orphan/useOrphanFormErrorsToConsoleError.cjs",
58
+ "types": "./dist/types/errors/orphan/useOrphanFormErrorsToConsoleError.d.ts"
59
+ },
60
+ "./errors/useFormErrorsHasOnlyOrphans": {
61
+ "import": "./dist/esm/errors/orphan/useFormErrorsHasOnlyOrphans.mjs",
62
+ "require": "./dist/cjs/errors/orphan/useFormErrorsHasOnlyOrphans.cjs",
63
+ "types": "./dist/types/errors/orphan/useFormErrorsHasOnlyOrphans.d.ts"
64
+ },
65
+ "./errors/useOrphanFormErrors": {
66
+ "import": "./dist/esm/errors/orphan/useOrphanFormErrors.mjs",
67
+ "require": "./dist/cjs/errors/orphan/useOrphanFormErrors.cjs",
68
+ "types": "./dist/types/errors/orphan/useOrphanFormErrors.d.ts"
69
+ },
70
+ "./errors/useRootFormFieldErrors": {
71
+ "import": "./dist/esm/errors/root/useRootFormFieldErrors.mjs",
72
+ "require": "./dist/cjs/errors/root/useRootFormFieldErrors.cjs",
73
+ "types": "./dist/types/errors/root/useRootFormFieldErrors.d.ts"
74
+ },
75
+ "./submit/useFormOnSubmitSuccessful": {
76
+ "import": "./dist/esm/submit/useFormOnSubmitSuccessful.mjs",
77
+ "require": "./dist/cjs/submit/useFormOnSubmitSuccessful.cjs",
78
+ "types": "./dist/types/submit/useFormOnSubmitSuccessful.d.ts"
79
+ },
80
+ "./submit/useFormOnSubmitSuccessfulAndReset": {
81
+ "import": "./dist/esm/submit/useFormOnSubmitSuccessfulAndReset.mjs",
82
+ "require": "./dist/cjs/submit/useFormOnSubmitSuccessfulAndReset.cjs",
83
+ "types": "./dist/types/submit/useFormOnSubmitSuccessfulAndReset.d.ts"
84
+ },
85
+ "./submit/useResetFormAfterSubmitSuccessful": {
86
+ "import": "./dist/esm/submit/useResetFormAfterSubmitSuccessful.mjs",
87
+ "require": "./dist/cjs/submit/useResetFormAfterSubmitSuccessful.cjs",
88
+ "types": "./dist/types/submit/useResetFormAfterSubmitSuccessful.d.ts"
28
89
  }
29
90
  },
30
- "sideEffects": false,
31
91
  "scripts": {
32
92
  "build": "npm run validate && npm run build:cjs && npm run build:esm",
33
93
  "build:cjs": "tsc -p ./tsconfig.cjs.json",
@@ -1,9 +0,0 @@
1
- import React from 'react';
2
- /**
3
- * Orphans are field errors that do not have a ref.
4
- * (Most commonly `root(.*)`, but these are filtered out. They are handled by `useFormRootFieldErrors`.)
5
- * Outputs to `console.error` all errors with `ref: undefined` that do not begin with `root(.*)`.
6
- * Sentry (or equivalent) should capture.
7
- */
8
- declare const useFormErrorOrphansToConsoleError: (formRef: React.RefObject<HTMLFormElement>) => void;
9
- export default useFormErrorOrphansToConsoleError;
@@ -1,2 +0,0 @@
1
- declare const useFormErrorsHasOnlyOrphan: (formRef: React.RefObject<HTMLFormElement>) => boolean;
2
- export default useFormErrorsHasOnlyOrphan;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const utils_1 = require("@/utils");
7
- const useFormContextErrorsIfAny_1 = __importDefault(require("../useFormContextErrorsIfAny"));
8
- const useFormOrphanErrors_1 = __importDefault(require("./useFormOrphanErrors"));
9
- const useFormErrorsHasOnlyOrphan = (formRef) => {
10
- const orphans = (0, useFormOrphanErrors_1.default)(formRef);
11
- const errors = (0, useFormContextErrorsIfAny_1.default)();
12
- // no errors
13
- if (!errors)
14
- return false;
15
- // no orphans
16
- if ((0, utils_1.isEmptyObject)(orphans))
17
- return false;
18
- // equal length
19
- return Object.keys(orphans).length === Object.keys(errors).length;
20
- };
21
- exports.default = useFormErrorsHasOnlyOrphan;
@@ -1,3 +0,0 @@
1
- import type { FieldErrors } from 'react-hook-form';
2
- import type { FlatFieldErrors } from '../getFlatFieldErrors';
3
- export declare const getRootFlatFieldErrors: (errors: FieldErrors, parentElement: Pick<HTMLElement, 'querySelector'>) => FlatFieldErrors;
@@ -1,6 +0,0 @@
1
- /**
2
- * Returns errors only if there are errors.
3
- * Allows simple typeguard without `isEmpty` dependency.
4
- */
5
- declare const useFormContextErrorsIfAny: () => false | import("react-hook-form").FieldErrors<import("react-hook-form").FieldValues>;
6
- export default useFormContextErrorsIfAny;
@@ -1,14 +0,0 @@
1
- import "react";
2
- import { isEmptyObject as t } from "../../utils.mjs";
3
- import e from "../useFormErrorsOnChange.mjs";
4
- import { getRootFlatFieldErrors as n } from "./utils.mjs";
5
- const s = (o) => e((r) => {
6
- if (t(r))
7
- return {};
8
- if (!o.current)
9
- throw console.error(r), new Error("This should not happen.");
10
- return n(r, o.current);
11
- }), a = s;
12
- export {
13
- a as default
14
- };
@@ -1,9 +0,0 @@
1
- import { useFormState as t } from "react-hook-form";
2
- import { isEmptyObject as o } from "../utils.mjs";
3
- const m = () => {
4
- const { errors: r } = t();
5
- return o(r) ? !1 : r;
6
- };
7
- export {
8
- m as default
9
- };
@@ -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,2 +0,0 @@
1
- declare const useFormErrorsHasOnlyOrphan: (formRef: React.RefObject<HTMLFormElement>) => boolean;
2
- export default useFormErrorsHasOnlyOrphan;
@@ -1,4 +0,0 @@
1
- import { FlatFieldErrors } from '../getFlatFieldErrors';
2
- import { FieldErrors } from 'react-hook-form';
3
-
4
- export declare const getRootFlatFieldErrors: (errors: FieldErrors, parentElement: Pick<HTMLElement, 'querySelector'>) => FlatFieldErrors;
@@ -1,6 +0,0 @@
1
- /**
2
- * Returns errors only if there are errors.
3
- * Allows simple typeguard without `isEmpty` dependency.
4
- */
5
- declare const useFormContextErrorsIfAny: () => false | import('react-hook-form').FieldErrors<import("react-hook-form").FieldValues>;
6
- export default useFormContextErrorsIfAny;