@paragrav/rhf-utils 0.0.45 → 0.0.48
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/esm/errors/context/provider.mjs +7 -7
- package/dist/esm/errors/flat/getFlatFieldErrors.mjs +11 -11
- package/dist/esm/errors/isFieldError.mjs +2 -2
- package/dist/esm/errors/orphan/getOrphansFromFieldErrors.mjs +9 -0
- package/dist/esm/errors/orphan/getOrphansFromFlatFieldErrors.mjs +16 -0
- package/dist/esm/errors/orphan/useOrphanFormErrorsMemo.mjs +4 -4
- package/dist/esm/errors/root/getRootsFromFieldErrors.mjs +9 -0
- package/dist/esm/errors/root/getRootsFromFlatFieldErrors.mjs +7 -0
- package/dist/esm/errors/root/useRootFormFieldErrorsMemo.mjs +1 -1
- package/dist/esm/form/Form.mjs +49 -44
- package/dist/esm/main.mjs +14 -12
- package/dist/esm/submit/useSubmitFormOnChange.mjs +21 -0
- package/dist/types/errors/orphan/getOrphansFromFieldErrors.d.ts +5 -0
- package/dist/types/errors/orphan/getOrphansFromFlatFieldErrors.d.ts +4 -0
- package/dist/types/errors/root/getRootsFromFieldErrors.d.ts +5 -0
- package/dist/types/errors/root/getRootsFromFlatFieldErrors.d.ts +4 -0
- package/dist/types/form/Form.d.ts +8 -1
- package/dist/types/main.d.ts +1 -0
- package/dist/types/submit/useSubmitFormOnChange.d.ts +4 -0
- package/package.json +5 -1
- package/dist/esm/errors/nonfield/isNonFieldError.mjs +0 -5
- package/dist/esm/errors/orphan/getOrphanFlatFieldErrors.mjs +0 -20
- package/dist/esm/errors/root/getRootFlatFieldErrors.mjs +0 -11
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import
|
|
3
|
-
import a from "../orphan/
|
|
4
|
-
import i from "../root/
|
|
1
|
+
import { jsx as n } from "react/jsx-runtime";
|
|
2
|
+
import l from "../flat/getFlatFieldErrors.mjs";
|
|
3
|
+
import a from "../orphan/getOrphansFromFlatFieldErrors.mjs";
|
|
4
|
+
import i from "../root/getRootsFromFlatFieldErrors.mjs";
|
|
5
5
|
import p from "../useFormErrorsMemo.mjs";
|
|
6
6
|
import { _FormErrorsFlatContextProvider as c } from "./index.mjs";
|
|
7
7
|
const d = ({
|
|
@@ -9,10 +9,10 @@ const d = ({
|
|
|
9
9
|
children: t
|
|
10
10
|
}) => {
|
|
11
11
|
const e = p((s) => {
|
|
12
|
-
const r =
|
|
13
|
-
return { all: r, roots: m, orphans:
|
|
12
|
+
const r = l(s), m = i(r), F = a(r, o.current);
|
|
13
|
+
return { all: r, roots: m, orphans: F };
|
|
14
14
|
});
|
|
15
|
-
return /* @__PURE__ */
|
|
15
|
+
return /* @__PURE__ */ n(c, { value: e, children: t });
|
|
16
16
|
}, C = d;
|
|
17
17
|
export {
|
|
18
18
|
C as default
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { get as
|
|
2
|
-
import
|
|
1
|
+
import { get as f } from "react-hook-form";
|
|
2
|
+
import n from "./flattenFieldErrors.mjs";
|
|
3
3
|
const p = (t) => {
|
|
4
|
-
const a =
|
|
4
|
+
const a = n(t), c = Object.keys(a);
|
|
5
5
|
return Object.fromEntries(
|
|
6
6
|
c.reduce(
|
|
7
|
-
(
|
|
8
|
-
const
|
|
9
|
-
if (
|
|
10
|
-
return
|
|
11
|
-
const
|
|
12
|
-
return
|
|
7
|
+
(e, o) => {
|
|
8
|
+
const r = o.replace(d, "");
|
|
9
|
+
if (r === o)
|
|
10
|
+
return e;
|
|
11
|
+
const s = f(t, r, void 0);
|
|
12
|
+
return s && e.push([r, s]), e;
|
|
13
13
|
},
|
|
14
14
|
[]
|
|
15
15
|
// entries
|
|
16
16
|
)
|
|
17
17
|
);
|
|
18
|
-
},
|
|
18
|
+
}, d = /\.(?:type|message)\b$/;
|
|
19
19
|
export {
|
|
20
20
|
p as default,
|
|
21
|
-
|
|
21
|
+
d as regexMaybeFieldErrorLeafNode
|
|
22
22
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import i from "../isFieldError.mjs";
|
|
2
|
+
import { hasNonFieldErrorMarkerInDOM as m } from "../nonfield/FormNonFieldErrorMarker.utils.mjs";
|
|
3
|
+
import s from "../root/isFormErrorPathRoot.mjs";
|
|
4
|
+
const n = (o, t) => Object.fromEntries(
|
|
5
|
+
Object.entries(o).filter(
|
|
6
|
+
([r, e]) => (
|
|
7
|
+
// not root path
|
|
8
|
+
!s(r) && // non-field
|
|
9
|
+
!i(e) && // does not have marker
|
|
10
|
+
!m(r, t)
|
|
11
|
+
)
|
|
12
|
+
)
|
|
13
|
+
);
|
|
14
|
+
export {
|
|
15
|
+
n as default
|
|
16
|
+
};
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import "react";
|
|
2
2
|
import t from "../../isEmptyObject.mjs";
|
|
3
3
|
import e from "../useFormErrorsMemo.mjs";
|
|
4
|
-
import m from "./
|
|
5
|
-
const
|
|
4
|
+
import m from "./getOrphansFromFieldErrors.mjs";
|
|
5
|
+
const s = (r) => e((o) => {
|
|
6
6
|
if (t(o))
|
|
7
7
|
return {};
|
|
8
8
|
if (!r.current)
|
|
9
9
|
throw new Error();
|
|
10
10
|
return m(o, r.current);
|
|
11
|
-
}),
|
|
11
|
+
}), E = s;
|
|
12
12
|
export {
|
|
13
|
-
|
|
13
|
+
E as default
|
|
14
14
|
};
|
package/dist/esm/form/Form.mjs
CHANGED
|
@@ -1,82 +1,87 @@
|
|
|
1
|
-
import { jsxs as
|
|
1
|
+
import { jsxs as R, jsx as l } from "react/jsx-runtime";
|
|
2
2
|
import "react";
|
|
3
|
-
import { useFormContext as
|
|
4
|
-
import
|
|
5
|
-
import { useFormErrorsFlatContextMaybe as
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
import { useFormContext as i } from "react-hook-form";
|
|
4
|
+
import j from "../devtool/LazyDevTool.mjs";
|
|
5
|
+
import { useFormErrorsFlatContextMaybe as T } from "../errors/context/index.mjs";
|
|
6
|
+
import V from "../errors/root/FormRootErrors.mjs";
|
|
7
|
+
import w from "../submit/useSubmitFormOnChange.mjs";
|
|
8
|
+
import P from "../utils/useImperativeForwardedRef.mjs";
|
|
9
|
+
import z from "./FormBase.mjs";
|
|
10
|
+
import { FormError as B } from "./FormError.mjs";
|
|
11
|
+
function K({
|
|
12
|
+
isDev: F,
|
|
13
|
+
formId: y,
|
|
14
|
+
formRef: E,
|
|
15
|
+
Fields: c,
|
|
14
16
|
//
|
|
15
17
|
onSubmit: a,
|
|
16
|
-
onSubmitError:
|
|
17
|
-
onSubmitInvalid:
|
|
18
|
+
onSubmitError: t,
|
|
19
|
+
onSubmitInvalid: x,
|
|
18
20
|
//
|
|
19
21
|
options: r,
|
|
20
22
|
//
|
|
21
|
-
children:
|
|
22
|
-
...
|
|
23
|
+
children: g,
|
|
24
|
+
...s
|
|
23
25
|
}) {
|
|
24
|
-
var
|
|
25
|
-
const e =
|
|
26
|
-
|
|
26
|
+
var h;
|
|
27
|
+
const e = i(), d = P(E);
|
|
28
|
+
w(d, !!(r != null && r.submitOnChange));
|
|
29
|
+
const u = (h = T()) == null ? void 0 : h.roots, b = (m) => (r != null && r.stopSubmitPropagation && m.stopPropagation(), e.handleSubmit(
|
|
30
|
+
v,
|
|
27
31
|
// `as` required due to conditional generic
|
|
28
|
-
|
|
29
|
-
)(
|
|
32
|
+
x
|
|
33
|
+
)(m)), v = async (m, o) => {
|
|
30
34
|
try {
|
|
31
35
|
await (a == null ? void 0 : a(
|
|
32
|
-
|
|
36
|
+
m,
|
|
33
37
|
e,
|
|
34
|
-
|
|
38
|
+
o
|
|
35
39
|
));
|
|
36
|
-
} catch (
|
|
37
|
-
if (r != null && r.disableEmptyRootError || e.setError("root", {}),
|
|
38
|
-
Object.entries(
|
|
39
|
-
e.setError(
|
|
40
|
+
} catch (f) {
|
|
41
|
+
if (r != null && r.disableEmptyRootError || e.setError("root", {}), r != null && r.resetValuesOnSubmitError && e.reset(void 0), f instanceof B) {
|
|
42
|
+
Object.entries(f.errors).forEach(([C, O]) => {
|
|
43
|
+
e.setError(C, {
|
|
40
44
|
type: "FormError",
|
|
41
|
-
message:
|
|
45
|
+
message: O
|
|
42
46
|
});
|
|
43
47
|
});
|
|
44
48
|
return;
|
|
45
49
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
t == null || t(
|
|
51
|
+
f,
|
|
52
|
+
m,
|
|
49
53
|
e,
|
|
50
|
-
|
|
54
|
+
o
|
|
51
55
|
);
|
|
52
56
|
}
|
|
53
57
|
};
|
|
54
|
-
return /* @__PURE__ */
|
|
55
|
-
|
|
58
|
+
return /* @__PURE__ */ R(
|
|
59
|
+
z,
|
|
56
60
|
{
|
|
57
|
-
id:
|
|
58
|
-
onSubmit:
|
|
61
|
+
id: y,
|
|
62
|
+
onSubmit: b,
|
|
59
63
|
noValidate: !0,
|
|
60
|
-
...
|
|
61
|
-
ref:
|
|
64
|
+
...s,
|
|
65
|
+
ref: d,
|
|
62
66
|
style: {
|
|
63
67
|
position: "relative"
|
|
64
68
|
},
|
|
65
69
|
children: [
|
|
66
|
-
(r == null ? void 0 : r.devTool) !== "disabled" && /* @__PURE__ */
|
|
67
|
-
|
|
70
|
+
(r == null ? void 0 : r.devTool) !== "disabled" && /* @__PURE__ */ l(
|
|
71
|
+
j,
|
|
68
72
|
{
|
|
69
|
-
isDev:
|
|
73
|
+
isDev: F,
|
|
70
74
|
placement: "top-right",
|
|
71
75
|
...r == null ? void 0 : r.devTool
|
|
72
76
|
}
|
|
73
77
|
),
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
c && /* @__PURE__ */ l(c, { ctx: e }),
|
|
79
|
+
g,
|
|
80
|
+
u && /* @__PURE__ */ l(V, { errors: u })
|
|
76
81
|
]
|
|
77
82
|
}
|
|
78
83
|
);
|
|
79
84
|
}
|
|
80
85
|
export {
|
|
81
|
-
|
|
86
|
+
K as default
|
|
82
87
|
};
|
package/dist/esm/main.mjs
CHANGED
|
@@ -6,14 +6,14 @@ import { default as d } from "./errors/message/FormErrorMessageByPath.mjs";
|
|
|
6
6
|
import { default as x } from "./errors/nonfield/FormNonFieldErrorMarker.mjs";
|
|
7
7
|
import { default as i } from "./errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.mjs";
|
|
8
8
|
import { default as E } from "./errors/orphan/useFormHasOrphanAndNoFieldErrors.mjs";
|
|
9
|
-
import { default as
|
|
10
|
-
import { default as
|
|
11
|
-
import { default as
|
|
9
|
+
import { default as c } from "./errors/orphan/useOrphanFormErrorsMemo.mjs";
|
|
10
|
+
import { default as S } from "./errors/orphan/useOrphanFormErrorsToConsole.mjs";
|
|
11
|
+
import { default as P } from "./errors/root/useRootFormFieldErrorsMemo.mjs";
|
|
12
12
|
import { default as v } from "./errors/useFormErrorConsoleDebug.mjs";
|
|
13
13
|
import { default as G } from "./errors/useFormErrorsEffect.mjs";
|
|
14
|
-
import { default as
|
|
15
|
-
import { default as
|
|
16
|
-
import { default as
|
|
14
|
+
import { default as A } from "./errors/useFormErrorsIfAny.mjs";
|
|
15
|
+
import { default as O } from "./errors/useFormErrorsMemo.mjs";
|
|
16
|
+
import { default as B } from "./errors/useFormRequestSubmit.mjs";
|
|
17
17
|
import { default as R } from "./form/Form.mjs";
|
|
18
18
|
import { FormError as N } from "./form/FormError.mjs";
|
|
19
19
|
import { default as U } from "./form/FormWithProvider.mjs";
|
|
@@ -33,6 +33,7 @@ import { default as dr } from "./form/utils/getSubmitterButtonData.mjs";
|
|
|
33
33
|
import { default as xr } from "./submit/useFormOnSubmitSuccessful.mjs";
|
|
34
34
|
import { default as ir } from "./submit/useFormOnSubmitSuccessfulAndReset.mjs";
|
|
35
35
|
import { default as Er } from "./submit/useResetFormOnSubmitSuccessful.mjs";
|
|
36
|
+
import { default as cr } from "./submit/useSubmitFormOnChange.mjs";
|
|
36
37
|
export {
|
|
37
38
|
R as Form,
|
|
38
39
|
N as FormError,
|
|
@@ -51,8 +52,8 @@ export {
|
|
|
51
52
|
v as useFormErrorConsoleDebug,
|
|
52
53
|
G as useFormErrorsEffect,
|
|
53
54
|
s as useFormErrorsFlatContext,
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
A as useFormErrorsIfAny,
|
|
56
|
+
O as useFormErrorsMemo,
|
|
56
57
|
_ as useFormGroupChildIsMountedTracker,
|
|
57
58
|
rr as useFormGroupChildIsSubmittingTracker,
|
|
58
59
|
er as useFormGroupIsAnyBusy,
|
|
@@ -62,11 +63,12 @@ export {
|
|
|
62
63
|
E as useFormHasOrphanAndNoFieldErrors,
|
|
63
64
|
xr as useFormOnSubmitSuccessful,
|
|
64
65
|
ir as useFormOnSubmitSuccessfulAndReset,
|
|
65
|
-
|
|
66
|
+
B as useFormRequestSubmit,
|
|
66
67
|
V as useFormUtilsContext,
|
|
67
68
|
L as useFormWithProvider,
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
c as useOrphanFormErrorsMemo,
|
|
70
|
+
S as useOrphanFormErrorsToConsole,
|
|
70
71
|
Er as useResetFormAfterSubmitSuccessful,
|
|
71
|
-
|
|
72
|
+
P as useRootFormFieldErrorsMemo,
|
|
73
|
+
cr as useSubmitFormOnChange
|
|
72
74
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import s from "react";
|
|
2
|
+
import u from "../errors/useFormRequestSubmit.mjs";
|
|
3
|
+
const m = (t, n) => {
|
|
4
|
+
const o = u(t);
|
|
5
|
+
s.useEffect(() => {
|
|
6
|
+
if (!n)
|
|
7
|
+
return;
|
|
8
|
+
const e = t.current;
|
|
9
|
+
if (!e)
|
|
10
|
+
throw new Error();
|
|
11
|
+
const r = () => {
|
|
12
|
+
o();
|
|
13
|
+
};
|
|
14
|
+
return e.addEventListener("change", r), () => {
|
|
15
|
+
e.removeEventListener("change", r);
|
|
16
|
+
};
|
|
17
|
+
}, []);
|
|
18
|
+
}, a = m;
|
|
19
|
+
export {
|
|
20
|
+
a as default
|
|
21
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { FlatFieldErrors } from '../flat/types';
|
|
2
|
+
import { FieldErrors } from 'react-hook-form';
|
|
3
|
+
|
|
4
|
+
declare const getOrphansFromFieldErrors: (errors: FieldErrors, parentElement: Pick<HTMLElement, 'querySelector'>) => FlatFieldErrors;
|
|
5
|
+
export default getOrphansFromFieldErrors;
|
|
@@ -7,17 +7,24 @@ export type FormProps<TFieldValues extends FieldValues, TTransformedValues exten
|
|
|
7
7
|
isDev: boolean;
|
|
8
8
|
formId?: string;
|
|
9
9
|
formRef?: React.RefObject<HTMLFormElement>;
|
|
10
|
+
Fields?: React.FC<{
|
|
11
|
+
ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
12
|
+
}>;
|
|
10
13
|
onSubmit?: (values: TTransformedValues, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => unknown | Promise<unknown>;
|
|
11
14
|
/** Submit error handler. (Only for errors not of instance `FormError`, which are handled internally.) */
|
|
12
15
|
onSubmitError?: (error: unknown, values: TTransformedValues, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => void;
|
|
13
16
|
/** Submit handler when form is invalid. */
|
|
14
17
|
onSubmitInvalid?: SubmitErrorHandler<TFieldValues>;
|
|
15
18
|
options?: {
|
|
19
|
+
/** Request submit via listener on form change. */
|
|
20
|
+
submitOnChange?: boolean;
|
|
21
|
+
/** Reset form values if submit throw errors. Use case: optimistic UI toggle form that submits on each change */
|
|
22
|
+
resetValuesOnSubmitError?: boolean;
|
|
16
23
|
stopSubmitPropagation?: boolean;
|
|
17
24
|
disableEmptyRootError?: boolean;
|
|
18
25
|
devTool?: DevtoolUIProps | 'disabled';
|
|
19
26
|
};
|
|
20
27
|
className?: string;
|
|
21
28
|
}>;
|
|
22
|
-
declare function Form<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ isDev, formId, formRef, onSubmit, onSubmitError, onSubmitInvalid, options, children, ...props }: FormProps<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
|
|
29
|
+
declare function Form<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ isDev, formId, formRef, Fields, onSubmit, onSubmitError, onSubmitInvalid, options, children, ...props }: FormProps<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
|
|
23
30
|
export default Form;
|
package/dist/types/main.d.ts
CHANGED
|
@@ -33,3 +33,4 @@ export { default as getSubmitterButtonData } from './form/utils/getSubmitterButt
|
|
|
33
33
|
export { default as useFormOnSubmitSuccessful } from './submit/useFormOnSubmitSuccessful';
|
|
34
34
|
export { default as useFormOnSubmitSuccessfulAndReset } from './submit/useFormOnSubmitSuccessfulAndReset';
|
|
35
35
|
export { default as useResetFormAfterSubmitSuccessful } from './submit/useResetFormOnSubmitSuccessful';
|
|
36
|
+
export { default as useSubmitFormOnChange } from './submit/useSubmitFormOnChange';
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@paragrav/rhf-utils",
|
|
3
3
|
"author": "paragrav.dev",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.48",
|
|
6
6
|
"description": "Integration utilities for react-hook-form.",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"sideEffects": false,
|
|
@@ -146,6 +146,10 @@
|
|
|
146
146
|
"./submit/useResetFormAfterSubmitSuccessful": {
|
|
147
147
|
"default": "./dist/esm/submit/useResetFormAfterSubmitSuccessful.mjs",
|
|
148
148
|
"types": "./dist/types/submit/useResetFormAfterSubmitSuccessful.d.ts"
|
|
149
|
+
},
|
|
150
|
+
"./submit/useSubmitFormOnChange": {
|
|
151
|
+
"default": "./dist/esm/submit/useSubmitFormOnChange.mjs",
|
|
152
|
+
"types": "./dist/types/submit/useSubmitFormOnChange.d.ts"
|
|
149
153
|
}
|
|
150
154
|
},
|
|
151
155
|
"scripts": {
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import s from "../flat/getFlatFieldErrors.mjs";
|
|
2
|
-
import { hasNonFieldErrorMarkerInDOM as l } from "../nonfield/FormNonFieldErrorMarker.utils.mjs";
|
|
3
|
-
import m from "../nonfield/isNonFieldError.mjs";
|
|
4
|
-
import n from "../root/isFormErrorPathRoot.mjs";
|
|
5
|
-
const d = (o, t) => {
|
|
6
|
-
const e = s(o);
|
|
7
|
-
return Object.fromEntries(
|
|
8
|
-
Object.entries(e).filter(
|
|
9
|
-
([r, i]) => (
|
|
10
|
-
// not root path
|
|
11
|
-
!n(r) && // non-field
|
|
12
|
-
m(i) && // does not have marker
|
|
13
|
-
!l(r, t)
|
|
14
|
-
)
|
|
15
|
-
)
|
|
16
|
-
);
|
|
17
|
-
};
|
|
18
|
-
export {
|
|
19
|
-
d as default
|
|
20
|
-
};
|