@paragrav/rhf-utils 0.0.17 → 0.0.18
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.
- package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.d.ts +10 -0
- package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.js +15 -0
- package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.spec.d.ts +1 -0
- package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.spec.js +15 -0
- package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +5 -0
- package/dist/cjs/errors/nonfield/FormNonFieldErrorMarker.utils.js +9 -0
- package/dist/cjs/errors/nonfield/utils.d.ts +9 -0
- package/dist/cjs/errors/nonfield/utils.js +78 -0
- package/dist/cjs/errors/orphans/useFormOrphanErrors.d.ts +0 -5
- package/dist/cjs/errors/orphans/useFormOrphanErrors.js +0 -5
- package/dist/cjs/errors/orphans/utils.d.ts +14 -2
- package/dist/cjs/errors/orphans/utils.js +22 -15
- package/dist/cjs/errors/root/useFormRootFieldErrors.d.ts +6 -0
- package/dist/cjs/errors/root/useFormRootFieldErrors.js +13 -0
- package/dist/cjs/errors/root/utils.d.ts +7 -0
- package/dist/cjs/errors/root/utils.js +13 -0
- package/dist/cjs/main.d.ts +3 -2
- package/dist/cjs/main.js +6 -5
- package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.mjs +19 -0
- package/dist/esm/errors/nonfield/FormNonFieldErrorMarker.utils.mjs +5 -0
- package/dist/esm/errors/nonfield/utils.mjs +14 -0
- package/dist/esm/errors/orphans/utils.mjs +13 -14
- package/dist/esm/errors/{roots → root}/utils.mjs +1 -1
- package/dist/esm/main.mjs +12 -11
- package/dist/types/errors/nonfield/FormNonFieldErrorMarker.d.ts +10 -0
- package/dist/types/errors/nonfield/FormNonFieldErrorMarker.spec.d.ts +1 -0
- package/dist/types/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +5 -0
- package/dist/types/errors/nonfield/utils.d.ts +10 -0
- package/dist/types/errors/orphans/useFormOrphanErrors.d.ts +0 -5
- package/dist/types/errors/orphans/utils.d.ts +14 -2
- package/dist/types/errors/root/useFormRootFieldErrors.d.ts +6 -0
- package/dist/types/errors/root/utils.d.ts +8 -0
- package/dist/types/main.d.ts +3 -2
- package/package.json +1 -1
- package/dist/esm/errors/orphans/FormErrorNonFieldMarker.mjs +0 -19
- package/dist/esm/errors/utils.mjs +0 -14
- /package/dist/esm/errors/{roots → root}/useFormRootFieldErrors.mjs +0 -0
|
@@ -0,0 +1,10 @@
|
|
|
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 FormNonFieldErrorMarker: React.FC<Props>;
|
|
10
|
+
export default FormNonFieldErrorMarker;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
4
|
+
const FormNonFieldErrorMarker_utils_1 = require("./FormNonFieldErrorMarker.utils");
|
|
5
|
+
/**
|
|
6
|
+
* Empty, hidden marker to indicate a non-field error is presented to user.
|
|
7
|
+
*/
|
|
8
|
+
const FormNonFieldErrorMarker = ({ path }) => {
|
|
9
|
+
const attribute = {
|
|
10
|
+
[FormNonFieldErrorMarker_utils_1.FormNonFieldErrorMarkerHtmlAttribute]: path,
|
|
11
|
+
};
|
|
12
|
+
return ((0, jsx_runtime_1.jsx)("div", { role: "alert", "aria-hidden": "true" // hide from screen readers
|
|
13
|
+
, style: { display: 'none' }, ...attribute }));
|
|
14
|
+
};
|
|
15
|
+
exports.default = FormNonFieldErrorMarker;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
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 jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
|
+
const experimental_ct_react_1 = require("@playwright/experimental-ct-react");
|
|
8
|
+
const FormNonFieldErrorMarker_1 = __importDefault(require("./FormNonFieldErrorMarker"));
|
|
9
|
+
const FormNonFieldErrorMarker_utils_1 = require("./FormNonFieldErrorMarker.utils");
|
|
10
|
+
const path = 'test';
|
|
11
|
+
const querySelector = (0, FormNonFieldErrorMarker_utils_1.getFormNonFieldErrorMarkerQuerySelector)(path);
|
|
12
|
+
(0, experimental_ct_react_1.test)('FormNonFieldErrorMarker can be located via query', async ({ page, mount, }) => {
|
|
13
|
+
await mount((0, jsx_runtime_1.jsx)(FormNonFieldErrorMarker_1.default, { path: path }));
|
|
14
|
+
await (0, experimental_ct_react_1.expect)(page.locator(querySelector)).toBeAttached();
|
|
15
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML data attribute for {@link FormNonFieldErrorMarker}.
|
|
3
|
+
*/
|
|
4
|
+
export declare const FormNonFieldErrorMarkerHtmlAttribute = "data-paragrav-rhf-utils-nonfield-error-marker-path";
|
|
5
|
+
export declare const getFormNonFieldErrorMarkerQuerySelector: (path: string) => string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFormNonFieldErrorMarkerQuerySelector = exports.FormNonFieldErrorMarkerHtmlAttribute = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* HTML data attribute for {@link FormNonFieldErrorMarker}.
|
|
6
|
+
*/
|
|
7
|
+
exports.FormNonFieldErrorMarkerHtmlAttribute = 'data-paragrav-rhf-utils-nonfield-error-marker-path';
|
|
8
|
+
const getFormNonFieldErrorMarkerQuerySelector = (path) => `[role="alert"][${exports.FormNonFieldErrorMarkerHtmlAttribute}="${path}"]`;
|
|
9
|
+
exports.getFormNonFieldErrorMarkerQuerySelector = getFormNonFieldErrorMarkerQuerySelector;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FieldError, FieldErrors } from 'react-hook-form';
|
|
2
|
+
/**
|
|
3
|
+
* @returns A flattened {@link FieldErrors} record. (e.g. `{ "address.street": FieldError }`)
|
|
4
|
+
*/
|
|
5
|
+
export declare const getNonFieldErrorRecordByFilter: (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 getNonFieldErrorListByFilter: (errors: FieldErrors, where?: ((path: string, flattenedErrors: Record<string, unknown>) => boolean) | undefined) => FieldError[];
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getNonFieldErrorListByFilter = exports.getNonFieldErrorRecordByFilter = 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. (e.g. `{ "address.street": FieldError }`)
|
|
8
|
+
*/
|
|
9
|
+
const getNonFieldErrorRecordByFilter = (errors, where) => {
|
|
10
|
+
const keys = getNonFieldErrorPathsByFilter(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.getNonFieldErrorRecordByFilter = getNonFieldErrorRecordByFilter;
|
|
18
|
+
/**
|
|
19
|
+
* @returns An array of field errors. (Not a record.)
|
|
20
|
+
*/
|
|
21
|
+
const getNonFieldErrorListByFilter = (errors, where) => {
|
|
22
|
+
const keys = getNonFieldErrorPathsByFilter(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.getNonFieldErrorListByFilter = getNonFieldErrorListByFilter;
|
|
29
|
+
/**
|
|
30
|
+
* Retrieve non-field error paths from {@link FieldErrors} object.
|
|
31
|
+
*
|
|
32
|
+
* Non-field errors are those without an association to an input via {@link FieldError.ref}.
|
|
33
|
+
*
|
|
34
|
+
* These fall into one of three categories:
|
|
35
|
+
* - Root errors, which are displayed to user at bottom/top of form.
|
|
36
|
+
* - Non-field errors (e.g., array min/max validation), which are typically displayed manually to user near relevant fields.
|
|
37
|
+
* - Orphan error occurs when a non-field error has not been displayed to user.
|
|
38
|
+
* - This is typically caused by one of the following cases:
|
|
39
|
+
* - 1. The input is missing.
|
|
40
|
+
* - 3. The schema is incorrect.
|
|
41
|
+
* - 2. The non-field error is not displayed to user with `FormErrorNonFieldMarker`.
|
|
42
|
+
*
|
|
43
|
+
* RHF uses deeply-nested object for errors, which reflects the structure of the form.
|
|
44
|
+
* And the type is quite complex. All this complicates the process of finding errors.
|
|
45
|
+
* This function automatically looks only for keys that end with `.ref`.
|
|
46
|
+
* (This makes sure we only look at the leaf nodes of the object tree.)
|
|
47
|
+
* This function also automatically removes the `.ref` suffix from the keys.
|
|
48
|
+
*
|
|
49
|
+
* @param where Filter callback.
|
|
50
|
+
* @param map Transformer callback.
|
|
51
|
+
* @returns Tuple of field errors and keys.
|
|
52
|
+
*/
|
|
53
|
+
const getNonFieldErrorPathsByFilter = (errors, where) => {
|
|
54
|
+
/**
|
|
55
|
+
* e.g.:
|
|
56
|
+
* ```js
|
|
57
|
+
* { root: { ref: undefined, message: 'Form is invalid.' }, email: { ref: Object, message: 'Invalid.' } }
|
|
58
|
+
* ```
|
|
59
|
+
* flattens to:
|
|
60
|
+
* ```js
|
|
61
|
+
* { 'root.ref': undefined, 'root.message': 'Form is invalid.', 'email.ref': Object, 'email.message': 'Invalid.' }
|
|
62
|
+
* ```
|
|
63
|
+
* returns (if no additional filter/map specified)
|
|
64
|
+
* ```js
|
|
65
|
+
* [ 'root', 'email' ]
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
const flattenedErrors = (0, flat_1.flatten)(errors);
|
|
69
|
+
const flattenedErrorObjectKeys = Object.keys(flattenedErrors);
|
|
70
|
+
// look for leaf node, which would have ref prop (e.g., `orphan_field.ref`)
|
|
71
|
+
const flattenedErrorObjectKeysEndingWithDotRef = flattenedErrorObjectKeys.filter((key) => key.match(regexEndsWithDotRef));
|
|
72
|
+
// remove `.ref` suffix to get flat field path (e.g., `name`, `address.street`)
|
|
73
|
+
const paths = flattenedErrorObjectKeysEndingWithDotRef.map((flattenedKey) => flattenedKey.replace(regexEndsWithDotRef, ''));
|
|
74
|
+
// filter via callback
|
|
75
|
+
const flattenedKeysFiltered = paths.filter((path) => where ? where(path, flattenedErrors) : true);
|
|
76
|
+
return flattenedKeysFiltered;
|
|
77
|
+
};
|
|
78
|
+
const regexEndsWithDotRef = /\.ref$/;
|
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
/**
|
|
3
|
-
* Root field errors from form.
|
|
4
|
-
* These are typically displayed at the bottom of the form.
|
|
5
|
-
* (Root errors have to processed this way due to (potentially) recursive types.)
|
|
6
|
-
*/
|
|
7
2
|
declare const useFormOrphanErrors: (formRef: React.RefObject<HTMLFormElement>) => Record<string, import("react-hook-form").FieldError>;
|
|
8
3
|
export default useFormOrphanErrors;
|
|
@@ -5,11 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const useFormErrorsOnChange_1 = __importDefault(require("../useFormErrorsOnChange"));
|
|
7
7
|
const utils_1 = require("./utils");
|
|
8
|
-
/**
|
|
9
|
-
* Root field errors from form.
|
|
10
|
-
* These are typically displayed at the bottom of the form.
|
|
11
|
-
* (Root errors have to processed this way due to (potentially) recursive types.)
|
|
12
|
-
*/
|
|
13
8
|
const useFormOrphanErrors = (formRef) => (0, useFormErrorsOnChange_1.default)((errors) => {
|
|
14
9
|
if (JSON.stringify(errors) === '{}')
|
|
15
10
|
return {};
|
|
@@ -1,3 +1,15 @@
|
|
|
1
1
|
import type { FieldErrors } from 'react-hook-form';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Get flat record of orphan errors -- i.e., non-field, non-root and not displayed to user using {@link FormNonFieldErrorMarker}.
|
|
4
|
+
* @param fieldErrors
|
|
5
|
+
* @param formElement Ref to form element for scoped `querySelector`.
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare const getOrphanFieldErrorsWithMarkerCheck: (fieldErrors: FieldErrors, formElement: HTMLFormElement) => Record<string, import("react-hook-form").FieldError>;
|
|
9
|
+
/**
|
|
10
|
+
* Get non-field errors that are not root errors.
|
|
11
|
+
*
|
|
12
|
+
* NOTE: This is an intermediary function used by {@link getOrphanFieldErrorsWithMarkerCheck}.
|
|
13
|
+
* It is exported for sake of testing.
|
|
14
|
+
*/
|
|
15
|
+
export declare const getNonFieldNonRootErrors: (errors: FieldErrors) => Record<string, import("react-hook-form").FieldError>;
|
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const getOrphanFieldErrorsWithMarkerCheck = (
|
|
14
|
-
const
|
|
15
|
-
return (0,
|
|
3
|
+
exports.getNonFieldNonRootErrors = exports.getOrphanFieldErrorsWithMarkerCheck = void 0;
|
|
4
|
+
const FormNonFieldErrorMarker_utils_1 = require("../nonfield/FormNonFieldErrorMarker.utils");
|
|
5
|
+
const utils_1 = require("../nonfield/utils");
|
|
6
|
+
const utils_2 = require("../root/utils");
|
|
7
|
+
/**
|
|
8
|
+
* Get flat record of orphan errors -- i.e., non-field, non-root and not displayed to user using {@link FormNonFieldErrorMarker}.
|
|
9
|
+
* @param fieldErrors
|
|
10
|
+
* @param formElement Ref to form element for scoped `querySelector`.
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
const getOrphanFieldErrorsWithMarkerCheck = (fieldErrors, formElement) => {
|
|
14
|
+
const nonFieldNonRootErrors = (0, exports.getNonFieldNonRootErrors)(fieldErrors);
|
|
15
|
+
return (0, utils_1.getNonFieldErrorRecordByFilter)(nonFieldNonRootErrors, (path) =>
|
|
16
16
|
// if doesn't have alert, then it's an orphan
|
|
17
|
-
|
|
17
|
+
!formElement.querySelector((0, FormNonFieldErrorMarker_utils_1.getFormNonFieldErrorMarkerQuerySelector)(path)));
|
|
18
18
|
};
|
|
19
19
|
exports.getOrphanFieldErrorsWithMarkerCheck = getOrphanFieldErrorsWithMarkerCheck;
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Get non-field errors that are not root errors.
|
|
22
|
+
*
|
|
23
|
+
* NOTE: This is an intermediary function used by {@link getOrphanFieldErrorsWithMarkerCheck}.
|
|
24
|
+
* It is exported for sake of testing.
|
|
25
|
+
*/
|
|
26
|
+
const getNonFieldNonRootErrors = (errors) => (0, utils_1.getNonFieldErrorRecordByFilter)(errors, (path) => !(0, utils_2.isFormErrorPathRoot)(path));
|
|
27
|
+
exports.getNonFieldNonRootErrors = getNonFieldNonRootErrors;
|
|
@@ -0,0 +1,13 @@
|
|
|
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 useFormErrorsOnChange_1 = __importDefault(require("../useFormErrorsOnChange"));
|
|
7
|
+
const utils_1 = require("./utils");
|
|
8
|
+
/**
|
|
9
|
+
* Root field errors from form.
|
|
10
|
+
* These are typically displayed at the bottom of the form.
|
|
11
|
+
*/
|
|
12
|
+
const useFormRootFieldErrors = () => (0, useFormErrorsOnChange_1.default)(utils_1.getRootFieldErrorList);
|
|
13
|
+
exports.default = useFormRootFieldErrors;
|
|
@@ -0,0 +1,7 @@
|
|
|
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[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRootFieldErrorList = exports.isFormErrorPathRoot = void 0;
|
|
4
|
+
const utils_1 = require("../nonfield/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.getNonFieldErrorListByFilter)(errors, exports.isFormErrorPathRoot);
|
|
13
|
+
exports.getRootFieldErrorList = getRootFieldErrorList;
|
package/dist/cjs/main.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export { default as
|
|
1
|
+
export { default as FormNonFieldErrorMarker } from './errors/nonfield/FormNonFieldErrorMarker';
|
|
2
|
+
export { FormNonFieldErrorMarkerHtmlAttribute } from './errors/nonfield/FormNonFieldErrorMarker.utils';
|
|
2
3
|
export { default as useFormErrorOrphansToConsoleError } from './errors/orphans/useFormErrorOrphansToConsoleError';
|
|
3
4
|
export { default as useFormOrphanErrors } from './errors/orphans/useFormOrphanErrors';
|
|
4
|
-
export { default as useFormRootFieldErrors } from './errors/
|
|
5
|
+
export { default as useFormRootFieldErrors } from './errors/root/useFormRootFieldErrors';
|
|
5
6
|
export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
|
|
6
7
|
export { default as useFormErrorsOnChange } from './errors/useFormErrorsOnChange';
|
|
7
8
|
export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
|
package/dist/cjs/main.js
CHANGED
|
@@ -3,15 +3,16 @@ 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.useFormRootFieldErrors = exports.useFormOrphanErrors = exports.useFormErrorOrphansToConsoleError = exports.
|
|
7
|
-
var
|
|
8
|
-
Object.defineProperty(exports, "
|
|
9
|
-
|
|
6
|
+
exports.useFormRequestSubmit = exports.useFormErrorsOnChange = exports.useFormErrorsEffect = exports.useFormRootFieldErrors = exports.useFormOrphanErrors = exports.useFormErrorOrphansToConsoleError = exports.FormNonFieldErrorMarkerHtmlAttribute = exports.FormNonFieldErrorMarker = void 0;
|
|
7
|
+
var FormNonFieldErrorMarker_1 = require("./errors/nonfield/FormNonFieldErrorMarker");
|
|
8
|
+
Object.defineProperty(exports, "FormNonFieldErrorMarker", { enumerable: true, get: function () { return __importDefault(FormNonFieldErrorMarker_1).default; } });
|
|
9
|
+
var FormNonFieldErrorMarker_utils_1 = require("./errors/nonfield/FormNonFieldErrorMarker.utils");
|
|
10
|
+
Object.defineProperty(exports, "FormNonFieldErrorMarkerHtmlAttribute", { enumerable: true, get: function () { return FormNonFieldErrorMarker_utils_1.FormNonFieldErrorMarkerHtmlAttribute; } });
|
|
10
11
|
var useFormErrorOrphansToConsoleError_1 = require("./errors/orphans/useFormErrorOrphansToConsoleError");
|
|
11
12
|
Object.defineProperty(exports, "useFormErrorOrphansToConsoleError", { enumerable: true, get: function () { return __importDefault(useFormErrorOrphansToConsoleError_1).default; } });
|
|
12
13
|
var useFormOrphanErrors_1 = require("./errors/orphans/useFormOrphanErrors");
|
|
13
14
|
Object.defineProperty(exports, "useFormOrphanErrors", { enumerable: true, get: function () { return __importDefault(useFormOrphanErrors_1).default; } });
|
|
14
|
-
var useFormRootFieldErrors_1 = require("./errors/
|
|
15
|
+
var useFormRootFieldErrors_1 = require("./errors/root/useFormRootFieldErrors");
|
|
15
16
|
Object.defineProperty(exports, "useFormRootFieldErrors", { enumerable: true, get: function () { return __importDefault(useFormRootFieldErrors_1).default; } });
|
|
16
17
|
var useFormErrorsEffect_1 = require("./errors/useFormErrorsEffect");
|
|
17
18
|
Object.defineProperty(exports, "useFormErrorsEffect", { enumerable: true, get: function () { return __importDefault(useFormErrorsEffect_1).default; } });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx as t } from "react/jsx-runtime";
|
|
2
|
+
import { FormNonFieldErrorMarkerHtmlAttribute as e } from "./FormNonFieldErrorMarker.utils.mjs";
|
|
3
|
+
const i = ({ path: r }) => {
|
|
4
|
+
const o = {
|
|
5
|
+
[e]: r
|
|
6
|
+
};
|
|
7
|
+
return /* @__PURE__ */ t(
|
|
8
|
+
"div",
|
|
9
|
+
{
|
|
10
|
+
role: "alert",
|
|
11
|
+
"aria-hidden": "true",
|
|
12
|
+
style: { display: "none" },
|
|
13
|
+
...o
|
|
14
|
+
}
|
|
15
|
+
);
|
|
16
|
+
}, l = i;
|
|
17
|
+
export {
|
|
18
|
+
l as default
|
|
19
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { flatten as f } from "../../node_modules/flat/index.mjs";
|
|
2
|
+
import { get as l } from "react-hook-form";
|
|
3
|
+
const F = (e, t) => i(e, t).reduce((r, s) => (r[s] = l(e, s), r), {}), g = (e, t) => i(e, t).map((r) => l(e, r)), i = (e, t) => {
|
|
4
|
+
const n = f(e);
|
|
5
|
+
return Object.keys(n).filter((o) => o.match(d)).map(
|
|
6
|
+
(o) => o.replace(d, "")
|
|
7
|
+
).filter(
|
|
8
|
+
(o) => t ? t(o, n) : !0
|
|
9
|
+
);
|
|
10
|
+
}, d = /\.ref$/;
|
|
11
|
+
export {
|
|
12
|
+
g as getNonFieldErrorListByFilter,
|
|
13
|
+
F as getNonFieldErrorRecordByFilter
|
|
14
|
+
};
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
`[role="alert"][${m}="${i}"]`
|
|
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))
|
|
12
11
|
)
|
|
13
12
|
);
|
|
14
|
-
};
|
|
13
|
+
}, m = (r) => e(r, (o) => !l(o));
|
|
15
14
|
export {
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
m as getNonFieldNonRootErrors,
|
|
16
|
+
d as getOrphanFieldErrorsWithMarkerCheck
|
|
18
17
|
};
|
package/dist/esm/main.mjs
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import { default as e
|
|
2
|
-
import {
|
|
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";
|
|
3
4
|
import { default as u } from "./errors/orphans/useFormOrphanErrors.mjs";
|
|
4
|
-
import { default as
|
|
5
|
-
import { default as
|
|
6
|
-
import { default as
|
|
5
|
+
import { default as F } from "./errors/root/useFormRootFieldErrors.mjs";
|
|
6
|
+
import { default as p } from "./errors/useFormErrorsEffect.mjs";
|
|
7
|
+
import { default as x } from "./errors/useFormErrorsOnChange.mjs";
|
|
7
8
|
import { default as i } from "./errors/useFormRequestSubmit.mjs";
|
|
8
9
|
export {
|
|
9
|
-
e as
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
e as FormNonFieldErrorMarker,
|
|
11
|
+
s as FormNonFieldErrorMarkerHtmlAttribute,
|
|
12
|
+
m as useFormErrorOrphansToConsoleError,
|
|
13
|
+
p as useFormErrorsEffect,
|
|
14
|
+
x as useFormErrorsOnChange,
|
|
14
15
|
u as useFormOrphanErrors,
|
|
15
16
|
i as useFormRequestSubmit,
|
|
16
|
-
|
|
17
|
+
F as useFormRootFieldErrors
|
|
17
18
|
};
|
|
@@ -0,0 +1,10 @@
|
|
|
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 FormNonFieldErrorMarker: React.FC<Props>;
|
|
10
|
+
export default FormNonFieldErrorMarker;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML data attribute for {@link FormNonFieldErrorMarker}.
|
|
3
|
+
*/
|
|
4
|
+
export declare const FormNonFieldErrorMarkerHtmlAttribute = "data-paragrav-rhf-utils-nonfield-error-marker-path";
|
|
5
|
+
export declare const getFormNonFieldErrorMarkerQuerySelector: (path: string) => string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FieldError, FieldErrors } from 'react-hook-form';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @returns A flattened {@link FieldErrors} record. (e.g. `{ "address.street": FieldError }`)
|
|
5
|
+
*/
|
|
6
|
+
export declare const getNonFieldErrorRecordByFilter: (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 getNonFieldErrorListByFilter: (errors: FieldErrors, where?: ((path: string, flattenedErrors: Record<string, unknown>) => boolean) | undefined) => FieldError[];
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Root field errors from form.
|
|
5
|
-
* These are typically displayed at the bottom of the form.
|
|
6
|
-
* (Root errors have to processed this way due to (potentially) recursive types.)
|
|
7
|
-
*/
|
|
8
3
|
declare const useFormOrphanErrors: (formRef: React.RefObject<HTMLFormElement>) => Record<string, import('react-hook-form').FieldError>;
|
|
9
4
|
export default useFormOrphanErrors;
|
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import { FieldErrors } from 'react-hook-form';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
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>;
|
|
@@ -0,0 +1,8 @@
|
|
|
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[];
|
package/dist/types/main.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export { default as
|
|
1
|
+
export { default as FormNonFieldErrorMarker } from './errors/nonfield/FormNonFieldErrorMarker';
|
|
2
|
+
export { FormNonFieldErrorMarkerHtmlAttribute } from './errors/nonfield/FormNonFieldErrorMarker.utils';
|
|
2
3
|
export { default as useFormErrorOrphansToConsoleError } from './errors/orphans/useFormErrorOrphansToConsoleError';
|
|
3
4
|
export { default as useFormOrphanErrors } from './errors/orphans/useFormOrphanErrors';
|
|
4
|
-
export { default as useFormRootFieldErrors } from './errors/
|
|
5
|
+
export { default as useFormRootFieldErrors } from './errors/root/useFormRootFieldErrors';
|
|
5
6
|
export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
|
|
6
7
|
export { default as useFormErrorsOnChange } from './errors/useFormErrorsOnChange';
|
|
7
8
|
export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
|
package/package.json
CHANGED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { jsx as e } from "react/jsx-runtime";
|
|
2
|
-
const o = ({ path: r }) => {
|
|
3
|
-
const t = {
|
|
4
|
-
[a]: r
|
|
5
|
-
};
|
|
6
|
-
return /* @__PURE__ */ e(
|
|
7
|
-
"div",
|
|
8
|
-
{
|
|
9
|
-
role: "alert",
|
|
10
|
-
"aria-hidden": "true",
|
|
11
|
-
style: { display: "none" },
|
|
12
|
-
...t
|
|
13
|
-
}
|
|
14
|
-
);
|
|
15
|
-
}, n = o, a = "data-paragrav-rhf-utils-marker-path";
|
|
16
|
-
export {
|
|
17
|
-
a as FormErrorNonFieldMarkerHtmlAttribute,
|
|
18
|
-
n as default
|
|
19
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { flatten as f } from "../node_modules/flat/index.mjs";
|
|
2
|
-
import { get as d } from "react-hook-form";
|
|
3
|
-
const p = (e, t) => i(e, t).reduce((r, s) => (r[s] = d(e, s), r), {}), F = (e, t) => i(e, t).map((r) => d(e, r)), i = (e, t) => {
|
|
4
|
-
const o = f(e);
|
|
5
|
-
return Object.keys(o).filter((n) => n.match(l)).map(
|
|
6
|
-
(n) => n.replace(l, "")
|
|
7
|
-
).filter(
|
|
8
|
-
(n) => t ? t(n, o) : !0
|
|
9
|
-
);
|
|
10
|
-
}, l = /\.ref$/;
|
|
11
|
-
export {
|
|
12
|
-
F as getFieldErrorListByFilter,
|
|
13
|
-
p as getFieldErrorsByFilter
|
|
14
|
-
};
|
|
File without changes
|