@paragrav/rhf-utils 0.0.103 → 0.0.105
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/README.md +41 -21
- package/dist/esm/client/zod/RhfUtilsZodForm.mjs +12 -0
- package/dist/esm/errors/trpc/getOnSubmitTrpcClientErrorHandler.mjs +6 -0
- package/dist/esm/errors/trpc/trpcClientErrorMessageSchema.mjs +10 -0
- package/dist/esm/errors/trpc/trpcClientErrorToFormSubmitErrorsSchemaTransformer.mjs +34 -0
- package/dist/esm/exports.mjs +56 -52
- package/dist/esm/form/Form.mjs +28 -28
- package/dist/esm/submit/error/{setCtxErrorsByFormSubmitError.mjs → setCtxErrorsByFormSubmitErrors.mjs} +1 -1
- package/dist/types/client/config/RhfUtilsClientConfig.d.ts +10 -4
- package/dist/types/client/zod/RhfUtilsZodForm.d.ts +8 -0
- package/dist/types/errors/trpc/getOnSubmitTrpcClientErrorHandler.d.ts +10 -0
- package/dist/types/errors/trpc/trpcClientErrorMessageSchema.d.ts +23 -0
- package/dist/types/errors/trpc/trpcClientErrorToFormSubmitErrorsSchemaTransformer.d.ts +27 -0
- package/dist/types/exports.d.ts +3 -1
- package/dist/types/form/useForm.d.ts +1 -1
- package/dist/types/submit/error/FormSubmitError.d.ts +3 -9
- package/dist/types/submit/error/FormSubmitErrors.d.ts +15 -0
- package/dist/types/submit/error/setCtxErrorsByFormSubmitErrors.d.ts +10 -0
- package/package.json +3 -1
- package/dist/esm/client/zod/context/RhfUtilsZodFormWithProviders.mjs +0 -12
package/README.md
CHANGED
|
@@ -17,10 +17,12 @@ export const config: RhfUtilsClientConfig = {
|
|
|
17
17
|
isDevelopment: import.meta.env.DEV,
|
|
18
18
|
|
|
19
19
|
defaultOptions: {
|
|
20
|
+
// UseFormProps
|
|
20
21
|
rhf: {
|
|
21
22
|
mode: 'onBlur',
|
|
22
23
|
},
|
|
23
24
|
|
|
25
|
+
// UseFormOptions
|
|
24
26
|
form: {
|
|
25
27
|
devTool: {
|
|
26
28
|
placement: 'top-right',
|
|
@@ -31,19 +33,21 @@ export const config: RhfUtilsClientConfig = {
|
|
|
31
33
|
FormComponent: Form.Root,
|
|
32
34
|
|
|
33
35
|
// transform backend error
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// return to be merged
|
|
42
|
-
return
|
|
36
|
+
// use `getOnSubmitTrpcClientErrorHandler` for TRPC backends
|
|
37
|
+
onSubmitError: (
|
|
38
|
+
error, // unknown
|
|
39
|
+
) => {
|
|
40
|
+
// if other error, re-throw to handle in ErrorBoundary
|
|
41
|
+
if (!(error instanceof ServerError)) throw error;
|
|
42
|
+
|
|
43
|
+
// return to be merged into RHF context errors
|
|
44
|
+
return transformServerErrorToFormSubmitErrors(error);
|
|
43
45
|
},
|
|
44
46
|
|
|
45
47
|
Children: {
|
|
46
|
-
top: (
|
|
48
|
+
top: (
|
|
49
|
+
{ options }, // UseFormFieldsProps
|
|
50
|
+
) => {
|
|
47
51
|
// navigation blocker (using custom option)
|
|
48
52
|
useFormRouterBlocker(!!options?.prompter);
|
|
49
53
|
|
|
@@ -73,31 +77,47 @@ And add the context provider to your stack:
|
|
|
73
77
|
## Usage
|
|
74
78
|
|
|
75
79
|
```tsx
|
|
76
|
-
<
|
|
80
|
+
<RhfUtilsZodForm
|
|
77
81
|
schema={authLoginFormSchema}
|
|
78
|
-
onSubmit={async (data) => {
|
|
79
|
-
await authLoginService(data, { from });
|
|
80
|
-
onSuccess?.();
|
|
81
|
-
}}
|
|
82
|
-
onSubmitError={onError}
|
|
83
82
|
defaultValues={{
|
|
84
83
|
email: '',
|
|
85
84
|
}}
|
|
86
|
-
|
|
85
|
+
// submit handler
|
|
86
|
+
onSubmit={async (data) => {
|
|
87
|
+
await authLoginService(data);
|
|
88
|
+
onSuccess();
|
|
89
|
+
}}
|
|
90
|
+
// handle error declaratively (i.e., no throw/catch)
|
|
91
|
+
onSubmitError={({
|
|
92
|
+
error, // unknown
|
|
93
|
+
context, // rhf UseFormReturn
|
|
94
|
+
event, // SubmitEvent
|
|
95
|
+
}) => {
|
|
96
|
+
onError(error);
|
|
97
|
+
}}
|
|
98
|
+
// fields
|
|
99
|
+
Fields={({
|
|
100
|
+
formId, // unique id string
|
|
101
|
+
formRef, // ref
|
|
102
|
+
context, // rhf UseFormReturn
|
|
103
|
+
options, // UseFormOptions
|
|
104
|
+
Controller, // strongly-typed controlled
|
|
105
|
+
}) => (
|
|
87
106
|
<>
|
|
88
107
|
<Controller
|
|
89
108
|
name="email" // strongly-typed field name
|
|
90
|
-
render={(
|
|
91
|
-
<
|
|
92
|
-
controller={props}
|
|
109
|
+
render={({ field, formState: { isSubmitting } }) => (
|
|
110
|
+
<input
|
|
93
111
|
autoFocus
|
|
94
112
|
label="Email"
|
|
95
113
|
autoComplete="email"
|
|
114
|
+
disabled={isSubmitting}
|
|
115
|
+
{...field}
|
|
96
116
|
/>
|
|
97
117
|
)}
|
|
98
118
|
/>
|
|
99
119
|
|
|
100
|
-
<
|
|
120
|
+
<button type="submit">Login</button>
|
|
101
121
|
</>
|
|
102
122
|
)}
|
|
103
123
|
/>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as e } from "react/jsx-runtime";
|
|
2
|
+
import i from "./context/useRhfUtilsClientForZodContext.mjs";
|
|
3
|
+
function f({
|
|
4
|
+
schema: o,
|
|
5
|
+
...t
|
|
6
|
+
}) {
|
|
7
|
+
const r = i();
|
|
8
|
+
return /* @__PURE__ */ e(r.FormWithProviders, { schema: o, ...t });
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
f as default
|
|
12
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TRPCClientError as o } from "@trpc/client";
|
|
2
|
+
import { trpcClientErrorToFormSubmitErrorsSchemaTransformer as n } from "./trpcClientErrorToFormSubmitErrorsSchemaTransformer.mjs";
|
|
3
|
+
const m = (t) => (r) => r instanceof o ? n.parse(r) : t(r);
|
|
4
|
+
export {
|
|
5
|
+
m as getOnSubmitTrpcClientErrorHandler
|
|
6
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { TRPCClientError as o } from "@trpc/client";
|
|
2
|
+
import { z as e } from "zod";
|
|
3
|
+
import { trpcClientErrorMessageSchema as a } from "./trpcClientErrorMessageSchema.mjs";
|
|
4
|
+
const i = e.custom(
|
|
5
|
+
(r) => r instanceof o
|
|
6
|
+
// check instance type
|
|
7
|
+
).transform((r) => r.message).transform((r) => {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(r);
|
|
10
|
+
} catch {
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
validation: "root",
|
|
14
|
+
code: "server",
|
|
15
|
+
message: r,
|
|
16
|
+
path: ["root"]
|
|
17
|
+
}
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
}).pipe(e.array(a)).transform(
|
|
21
|
+
(r) => r.map(
|
|
22
|
+
(t) => [
|
|
23
|
+
t.path.join("."),
|
|
24
|
+
// flatten path (e.g., "street.name")
|
|
25
|
+
{
|
|
26
|
+
type: t.code,
|
|
27
|
+
message: t.message
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
)
|
|
31
|
+
).transform((r) => Object.fromEntries(r));
|
|
32
|
+
export {
|
|
33
|
+
i as trpcClientErrorToFormSubmitErrorsSchemaTransformer
|
|
34
|
+
};
|
package/dist/esm/exports.mjs
CHANGED
|
@@ -1,66 +1,70 @@
|
|
|
1
1
|
import { default as e } from "./client/zod/context/RhfUtilsClientForZodContextProvider.mjs";
|
|
2
|
-
import { default as
|
|
3
|
-
import { default as f } from "./client/zod/context/
|
|
4
|
-
import { default as
|
|
2
|
+
import { default as a } from "./client/zod/context/useRhfUtilsClientForZodContext.mjs";
|
|
3
|
+
import { default as f } from "./client/zod/context/useRhfUtilsZodForm.mjs";
|
|
4
|
+
import { default as u } from "./client/zod/RhfUtilsZodForm.mjs";
|
|
5
5
|
import { default as d } from "./devtool/LazyDevTool.mjs";
|
|
6
6
|
import { default as x } from "./errors/flat/context/useFlatFieldErrorsContext.mjs";
|
|
7
7
|
import { default as i } from "./errors/flat/FlatFieldErrorsList.mjs";
|
|
8
8
|
import { default as E } from "./errors/message/FormErrorMessage.mjs";
|
|
9
|
-
import { default as
|
|
10
|
-
import { default as
|
|
11
|
-
import { default as
|
|
12
|
-
import { default as
|
|
13
|
-
import { default as
|
|
14
|
-
import { default as
|
|
9
|
+
import { default as h } from "./errors/message/FormErrorMessageByPath.mjs";
|
|
10
|
+
import { default as y } from "./errors/nonfield/FormNonFieldErrorMarker.mjs";
|
|
11
|
+
import { default as M } from "./errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.mjs";
|
|
12
|
+
import { default as T } from "./errors/orphan/useFormHasOnlyOrphanFieldErrorsMemo.mjs";
|
|
13
|
+
import { default as I } from "./errors/orphan/useOrphanFormErrorsMemo.mjs";
|
|
14
|
+
import { default as k } from "./errors/root/RootErrorsListFromFlatFieldErrorsContext.mjs";
|
|
15
15
|
import { default as O } from "./errors/root/useRootFormFieldErrorsMemo.mjs";
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import { default as A } from "./
|
|
19
|
-
import { default as
|
|
20
|
-
import { default as N } from "./form/
|
|
21
|
-
import { default as
|
|
22
|
-
import { default as q } from "./form/context/
|
|
23
|
-
import { default as J } from "./form/context/group/
|
|
24
|
-
import { default as Q } from "./form/context/group/
|
|
25
|
-
import { default as X } from "./form/context/group/
|
|
26
|
-
import { default as _ } from "./form/context/group/
|
|
27
|
-
import { default as rr } from "./form/context/group/
|
|
28
|
-
import { default as er } from "./form/context/group/
|
|
29
|
-
import { default as
|
|
30
|
-
import { default as fr } from "./form/
|
|
31
|
-
import {
|
|
32
|
-
import { default as dr } from "./
|
|
16
|
+
import { getOnSubmitTrpcClientErrorHandler as g } from "./errors/trpc/getOnSubmitTrpcClientErrorHandler.mjs";
|
|
17
|
+
import { trpcClientErrorToFormSubmitErrorsSchemaTransformer as v } from "./errors/trpc/trpcClientErrorToFormSubmitErrorsSchemaTransformer.mjs";
|
|
18
|
+
import { default as A } from "./errors/useFormErrorsEffect.mjs";
|
|
19
|
+
import { default as L } from "./errors/useFormErrorsIfAny.mjs";
|
|
20
|
+
import { default as N } from "./form/FormWithProviders.mjs";
|
|
21
|
+
import { default as W } from "./form/useForm.mjs";
|
|
22
|
+
import { default as q } from "./form/context/utils/useFormUtilsContext.mjs";
|
|
23
|
+
import { default as J } from "./form/context/group/FormGroupContextProvider.mjs";
|
|
24
|
+
import { default as Q } from "./form/context/group/useFormGroupChildIsMountedTracker.mjs";
|
|
25
|
+
import { default as X } from "./form/context/group/useFormGroupChildIsSubmittingTracker.mjs";
|
|
26
|
+
import { default as _ } from "./form/context/group/useFormGroupChildTracker.mjs";
|
|
27
|
+
import { default as rr } from "./form/context/group/useFormGroupIsAnyBusy.mjs";
|
|
28
|
+
import { default as er } from "./form/context/group/useFormGroupIsChildBusy.mjs";
|
|
29
|
+
import { default as ar } from "./form/context/group/useFormGroupIsParentBusy.mjs";
|
|
30
|
+
import { default as fr } from "./form/context/group/useFormGroupParentTracker.mjs";
|
|
31
|
+
import { default as ur } from "./form/context/group/useFormOrParentIsBusy.mjs";
|
|
32
|
+
import { default as dr } from "./form/utils/getSubmitterButtonData.mjs";
|
|
33
|
+
import { FormSubmitError as xr } from "./submit/error/FormSubmitError.mjs";
|
|
34
|
+
import { default as ir } from "./submit/useFormOnSubmitSuccessful.mjs";
|
|
33
35
|
export {
|
|
34
36
|
i as FlatFieldErrorsList,
|
|
35
37
|
E as FormErrorMessage,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
h as FormErrorMessageByPath,
|
|
39
|
+
J as FormGroupContextProvider,
|
|
40
|
+
y as FormNonFieldErrorMarker,
|
|
41
|
+
M as FormNonFieldErrorMarkerHtmlAttribute,
|
|
42
|
+
xr as FormSubmitError,
|
|
43
|
+
N as FormWithProviders,
|
|
42
44
|
d as LazyDevTool,
|
|
43
45
|
e as RhfUtilsClientForZodContextProvider,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
u as RhfUtilsZodForm,
|
|
47
|
+
k as RootErrorsListFromFlatFieldErrorsContext,
|
|
48
|
+
g as getOnSubmitTrpcClientErrorHandler,
|
|
49
|
+
dr as getSubmitterButtonData,
|
|
50
|
+
v as trpcClientErrorToFormSubmitErrorsSchemaTransformer,
|
|
47
51
|
x as useFlatFieldErrorsContext,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
52
|
+
W as useForm,
|
|
53
|
+
A as useFormErrorsEffect,
|
|
54
|
+
L as useFormErrorsIfAny,
|
|
55
|
+
Q as useFormGroupChildIsMountedTracker,
|
|
56
|
+
X as useFormGroupChildIsSubmittingTracker,
|
|
57
|
+
_ as useFormGroupChildTracker,
|
|
58
|
+
rr as useFormGroupIsAnyBusy,
|
|
59
|
+
er as useFormGroupIsChildBusy,
|
|
60
|
+
ar as useFormGroupIsParentBusy,
|
|
61
|
+
fr as useFormGroupParentTracker,
|
|
62
|
+
T as useFormHasOnlyOrphanFieldErrorsMemo,
|
|
63
|
+
ir as useFormOnSubmitSuccessful,
|
|
64
|
+
ur as useFormOrParentIsBusy,
|
|
65
|
+
q as useFormUtilsContext,
|
|
66
|
+
I as useOrphanFormErrorsMemo,
|
|
67
|
+
a as useRhfUtilsClientForZodContext,
|
|
68
|
+
f as useRhfUtilsZodForm,
|
|
65
69
|
O as useRootFormFieldErrorsMemo
|
|
66
70
|
};
|
package/dist/esm/form/Form.mjs
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { jsxs as F, jsx as
|
|
1
|
+
import { jsxs as F, jsx as c } from "react/jsx-runtime";
|
|
2
2
|
import "react";
|
|
3
3
|
import { useFormContext as _ } from "react-hook-form";
|
|
4
4
|
import j from "../client/config/useRhfUtilsClientConfigContext.mjs";
|
|
5
5
|
import D from "../devtool/LazyDevTool.mjs";
|
|
6
6
|
import { FormSubmitError as N } from "../submit/error/FormSubmitError.mjs";
|
|
7
|
-
import P from "../submit/error/
|
|
7
|
+
import P from "../submit/error/setCtxErrorsByFormSubmitErrors.mjs";
|
|
8
8
|
import S from "./_Controller.mjs";
|
|
9
9
|
import T from "./_FormBase.mjs";
|
|
10
10
|
function q({
|
|
11
11
|
formId: x,
|
|
12
12
|
formRef: C,
|
|
13
13
|
//
|
|
14
|
-
onSubmit:
|
|
15
|
-
onSubmitError:
|
|
14
|
+
onSubmit: o,
|
|
15
|
+
onSubmitError: d,
|
|
16
16
|
onSubmitInvalid: y,
|
|
17
17
|
Fields: h,
|
|
18
18
|
//
|
|
19
19
|
form: s,
|
|
20
|
-
options:
|
|
20
|
+
options: e,
|
|
21
21
|
//
|
|
22
22
|
children: f
|
|
23
23
|
}) {
|
|
24
|
-
const l = _(),
|
|
25
|
-
|
|
24
|
+
const l = _(), r = j(), b = async (t) => {
|
|
25
|
+
e != null && e.stopSubmitPropagation && t.stopPropagation();
|
|
26
26
|
try {
|
|
27
|
-
await l.handleSubmit(v, y)(
|
|
27
|
+
await l.handleSubmit(v, y)(t);
|
|
28
28
|
} catch (a) {
|
|
29
|
-
B(a,
|
|
29
|
+
B(a, t);
|
|
30
30
|
}
|
|
31
|
-
}, v = (
|
|
32
|
-
|
|
31
|
+
}, v = (t, a) => o == null ? void 0 : o(
|
|
32
|
+
t,
|
|
33
33
|
l,
|
|
34
34
|
a
|
|
35
|
-
), B = (
|
|
35
|
+
), B = (t, a) => {
|
|
36
36
|
var n;
|
|
37
|
-
const m =
|
|
37
|
+
const m = t instanceof N ? (
|
|
38
38
|
// consumer can manually throw FormSubmitError (e.g., manual validation)
|
|
39
|
-
|
|
39
|
+
t.errors
|
|
40
40
|
) : (
|
|
41
41
|
// if error is not FormSubmitError, consumer can provide global handler
|
|
42
|
-
(n =
|
|
42
|
+
(n = r == null ? void 0 : r.onSubmitError) == null ? void 0 : n.call(r, t)
|
|
43
43
|
);
|
|
44
|
-
m && P(l, m),
|
|
45
|
-
|
|
44
|
+
m && P(l, m), d == null || d(
|
|
45
|
+
t,
|
|
46
46
|
l,
|
|
47
47
|
a
|
|
48
48
|
);
|
|
49
49
|
}, u = {
|
|
50
50
|
formId: x,
|
|
51
51
|
formRef: C,
|
|
52
|
-
|
|
53
|
-
options:
|
|
52
|
+
context: l,
|
|
53
|
+
options: e,
|
|
54
54
|
Controller: S
|
|
55
|
-
}, i = (
|
|
55
|
+
}, i = (t) => {
|
|
56
56
|
var m;
|
|
57
|
-
const a = (m =
|
|
58
|
-
return a && /* @__PURE__ */
|
|
57
|
+
const a = (m = r == null ? void 0 : r.Children) == null ? void 0 : m[t];
|
|
58
|
+
return a && /* @__PURE__ */ c(a, { ...u });
|
|
59
59
|
};
|
|
60
60
|
return /* @__PURE__ */ F(
|
|
61
61
|
T,
|
|
@@ -64,22 +64,22 @@ function q({
|
|
|
64
64
|
style: {
|
|
65
65
|
position: "relative"
|
|
66
66
|
},
|
|
67
|
-
config:
|
|
67
|
+
config: r,
|
|
68
68
|
formRef: C,
|
|
69
69
|
...s,
|
|
70
|
-
className: [
|
|
70
|
+
className: [e == null ? void 0 : e.classNames, s == null ? void 0 : s.className].filter(Boolean).join(" "),
|
|
71
71
|
onSubmit: b,
|
|
72
72
|
children: [
|
|
73
73
|
i("top"),
|
|
74
|
-
(
|
|
74
|
+
(e == null ? void 0 : e.devTool) !== "disabled" && /* @__PURE__ */ c(
|
|
75
75
|
D,
|
|
76
76
|
{
|
|
77
77
|
placement: "top-right",
|
|
78
|
-
isDevelopment:
|
|
79
|
-
...
|
|
78
|
+
isDevelopment: r == null ? void 0 : r.isDevelopment,
|
|
79
|
+
...e == null ? void 0 : e.devTool
|
|
80
80
|
}
|
|
81
81
|
),
|
|
82
|
-
h && /* @__PURE__ */
|
|
82
|
+
h && /* @__PURE__ */ c(h, { ...u }),
|
|
83
83
|
f,
|
|
84
84
|
i("bottom")
|
|
85
85
|
]
|
|
@@ -3,7 +3,7 @@ import { RhfUtilsErrorsOutputConfigContext, RhfUtilsErrorsOutputConsoleConfig }
|
|
|
3
3
|
import { FormChildrenSlots } from '../../form/Form';
|
|
4
4
|
import { UseFormFieldsProps } from '../../form/useForm';
|
|
5
5
|
import { UseFormOptions } from '../../form/useFormOptions';
|
|
6
|
-
import {
|
|
6
|
+
import { FormSubmitErrors } from '../../submit/error/FormSubmitErrors';
|
|
7
7
|
|
|
8
8
|
export type RhfUtilsClientConfig = {
|
|
9
9
|
/**
|
|
@@ -34,12 +34,18 @@ export type RhfUtilsClientConfig = {
|
|
|
34
34
|
*/
|
|
35
35
|
FormComponent?: React.FC<React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>>>;
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Global submit error handler.
|
|
38
|
+
*
|
|
39
|
+
* Called when/if submit handler throws an error other than {@link FormSubmitError}.
|
|
38
40
|
*
|
|
39
41
|
* Example use case:
|
|
40
|
-
* - transform API errors to {@link
|
|
42
|
+
* - transform API errors to {@link FormSubmitErrors}.
|
|
43
|
+
*
|
|
44
|
+
* @returns
|
|
45
|
+
* - {@link FormSubmitErrors}: merge into context errors.
|
|
46
|
+
* - `undefined`: do nothing.
|
|
41
47
|
*/
|
|
42
|
-
onSubmitError?: (error: unknown) =>
|
|
48
|
+
onSubmitError?: (error: unknown) => FormSubmitErrors | undefined;
|
|
43
49
|
/**
|
|
44
50
|
* Components injected by slot across all forms.
|
|
45
51
|
* (Per-form injection can be done via Fields prop.)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { UseFormProps } from '../../form/useForm';
|
|
2
|
+
import { ZodTypeFieldValues } from './createRhfUtilsClientForZod';
|
|
3
|
+
|
|
4
|
+
type Props<TSchema extends ZodTypeFieldValues> = {
|
|
5
|
+
schema: TSchema;
|
|
6
|
+
} & Omit<UseFormProps<TSchema['_input'], TSchema['_output']>, 'config'>;
|
|
7
|
+
declare function RhfUtilsZodForm<TSchema extends ZodTypeFieldValues>({ schema, ...props }: Props<TSchema>): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export default RhfUtilsZodForm;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RhfUtilsClientConfig } from '../../client/config/RhfUtilsClientConfig';
|
|
2
|
+
import { FormSubmitErrors } from '../../submit/error/FormSubmitErrors';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* HOF that returns submit error handler for {@link RhfUtilsClientConfig.onSubmitError}.
|
|
6
|
+
*
|
|
7
|
+
* @param onError Fallback for handling non-TRPC errors.
|
|
8
|
+
* @returns FormSubmitError
|
|
9
|
+
*/
|
|
10
|
+
export declare const getOnSubmitTrpcClientErrorHandler: (onNotTrpcClientError: NonNullable<RhfUtilsClientConfig['onSubmitError']>) => (error: unknown) => FormSubmitErrors | undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Schema for {@link TRPCClientError} message.
|
|
5
|
+
*
|
|
6
|
+
* (Sent from backend as JSON string.)
|
|
7
|
+
*/
|
|
8
|
+
export declare const trpcClientErrorMessageSchema: z.ZodObject<{
|
|
9
|
+
validation: z.ZodString;
|
|
10
|
+
code: z.ZodString;
|
|
11
|
+
message: z.ZodString;
|
|
12
|
+
path: z.ZodArray<z.ZodString, "many">;
|
|
13
|
+
}, "strip", z.ZodTypeAny, {
|
|
14
|
+
message: string;
|
|
15
|
+
code: string;
|
|
16
|
+
path: string[];
|
|
17
|
+
validation: string;
|
|
18
|
+
}, {
|
|
19
|
+
message: string;
|
|
20
|
+
code: string;
|
|
21
|
+
path: string[];
|
|
22
|
+
validation: string;
|
|
23
|
+
}>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TRPCClientErrorLike } from '@trpc/client';
|
|
2
|
+
import { AnyRouter } from '@trpc/server';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { FormSubmitErrors } from '../../submit/error/FormSubmitErrors';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Schema that transforms {@link TRPCClientError} to RHF {@link FormSubmitErrors}.
|
|
8
|
+
*/
|
|
9
|
+
export declare const trpcClientErrorToFormSubmitErrorsSchemaTransformer: z.ZodEffects<z.ZodEffects<z.ZodPipeline<z.ZodEffects<z.ZodEffects<z.ZodType<TRPCClientErrorLike<AnyRouter>, z.ZodTypeDef, TRPCClientErrorLike<AnyRouter>>, string, TRPCClientErrorLike<AnyRouter>>, unknown, TRPCClientErrorLike<AnyRouter>>, z.ZodArray<z.ZodObject<{
|
|
10
|
+
validation: z.ZodString;
|
|
11
|
+
code: z.ZodString;
|
|
12
|
+
message: z.ZodString;
|
|
13
|
+
path: z.ZodArray<z.ZodString, "many">;
|
|
14
|
+
}, "strip", z.ZodTypeAny, {
|
|
15
|
+
message: string;
|
|
16
|
+
code: string;
|
|
17
|
+
path: string[];
|
|
18
|
+
validation: string;
|
|
19
|
+
}, {
|
|
20
|
+
message: string;
|
|
21
|
+
code: string;
|
|
22
|
+
path: string[];
|
|
23
|
+
validation: string;
|
|
24
|
+
}>, "many">>, (readonly [string, {
|
|
25
|
+
type: string;
|
|
26
|
+
message: string;
|
|
27
|
+
}])[], TRPCClientErrorLike<AnyRouter>>, FormSubmitErrors, TRPCClientErrorLike<AnyRouter>>;
|
package/dist/types/exports.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export type { Register } from './register';
|
|
2
2
|
export type { RhfUtilsClientConfig } from './client/config/RhfUtilsClientConfig';
|
|
3
3
|
export { default as RhfUtilsClientForZodContextProvider } from './client/zod/context/RhfUtilsClientForZodContextProvider';
|
|
4
|
-
export { default as RhfUtilsZodFormWithProviders } from './client/zod/context/RhfUtilsZodFormWithProviders';
|
|
5
4
|
export { default as useRhfUtilsClientForZodContext } from './client/zod/context/useRhfUtilsClientForZodContext';
|
|
6
5
|
export { default as useRhfUtilsZodForm } from './client/zod/context/useRhfUtilsZodForm';
|
|
6
|
+
export { default as RhfUtilsZodForm } from './client/zod/RhfUtilsZodForm';
|
|
7
7
|
export type { UseFormFieldsZodProps } from './client/zod/UseFormFieldsZodProps';
|
|
8
8
|
export { default as LazyDevTool } from './devtool/LazyDevTool';
|
|
9
9
|
export { default as useFlatFieldErrorsContext } from './errors/flat/context/useFlatFieldErrorsContext';
|
|
@@ -16,6 +16,8 @@ export { default as useFormHasOnlyOrphanFieldErrorsMemo } from './errors/orphan/
|
|
|
16
16
|
export { default as useOrphanFormErrorsMemo } from './errors/orphan/useOrphanFormErrorsMemo';
|
|
17
17
|
export { default as RootErrorsListFromFlatFieldErrorsContext } from './errors/root/RootErrorsListFromFlatFieldErrorsContext';
|
|
18
18
|
export { default as useRootFormFieldErrorsMemo } from './errors/root/useRootFormFieldErrorsMemo';
|
|
19
|
+
export { getOnSubmitTrpcClientErrorHandler } from './errors/trpc/getOnSubmitTrpcClientErrorHandler';
|
|
20
|
+
export { trpcClientErrorToFormSubmitErrorsSchemaTransformer } from './errors/trpc/trpcClientErrorToFormSubmitErrorsSchemaTransformer';
|
|
19
21
|
export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
|
|
20
22
|
export { default as useFormErrorsIfAny } from './errors/useFormErrorsIfAny';
|
|
21
23
|
export { default as FormWithProviders } from './form/FormWithProviders';
|
|
@@ -10,7 +10,7 @@ import { UseFormOptions } from './useFormOptions';
|
|
|
10
10
|
export type UseFormFieldsProps<TFieldValues extends SafeFieldValues = SafeFieldValues, TTransformedValues extends SafeFieldValues = SafeFieldValues> = {
|
|
11
11
|
formId: string;
|
|
12
12
|
formRef: React.RefObject<HTMLFormElement>;
|
|
13
|
-
|
|
13
|
+
context: RhfUseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
14
14
|
options?: UseFormOptions;
|
|
15
15
|
Controller: typeof _Controller<TFieldValues>;
|
|
16
16
|
};
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import { FieldPath } from 'react-hook-form';
|
|
2
1
|
import { SafeFieldValues } from '../../form/SafeFieldValues';
|
|
2
|
+
import { FormSubmitErrors } from './FormSubmitErrors';
|
|
3
3
|
|
|
4
4
|
export declare class FormSubmitError<TFieldValues extends SafeFieldValues = SafeFieldValues> extends Error {
|
|
5
|
-
errors:
|
|
6
|
-
|
|
7
|
-
message: string;
|
|
8
|
-
}>;
|
|
9
|
-
constructor(errors: Record<FieldPath<TFieldValues> | `root.${string}`, {
|
|
10
|
-
type?: string;
|
|
11
|
-
message: string;
|
|
12
|
-
}>, message?: string);
|
|
5
|
+
errors: FormSubmitErrors<TFieldValues>;
|
|
6
|
+
constructor(errors: FormSubmitErrors<TFieldValues>, message?: string);
|
|
13
7
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FieldPath } from 'react-hook-form';
|
|
2
|
+
import { SafeFieldValues } from '../../form/SafeFieldValues';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Field errors structure based on RHF's `FieldErrors`.
|
|
6
|
+
*
|
|
7
|
+
* - Uses {@link SafeFieldValues} instead of more permissive `FieldValues`.
|
|
8
|
+
* - `root.<string>` keys allowed.
|
|
9
|
+
* - `root` is reserved for special use cases.
|
|
10
|
+
* - simplified `ErrorOption` value.
|
|
11
|
+
*/
|
|
12
|
+
export type FormSubmitErrors<TFieldValues extends SafeFieldValues = SafeFieldValues> = Record<FieldPath<TFieldValues> | `root.${string}`, {
|
|
13
|
+
type?: string;
|
|
14
|
+
message: string;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { UseFormReturn } from 'react-hook-form';
|
|
2
|
+
import { SafeFieldValues } from '../../form/SafeFieldValues';
|
|
3
|
+
import { FormSubmitErrors } from './FormSubmitErrors';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handle thrown {@link FormSubmitError}.
|
|
7
|
+
* Add key/values as name/message to RHF context errors.
|
|
8
|
+
*/
|
|
9
|
+
declare const setCtxErrorsByFormSubmitErrors: <TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>(ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>, errors: FormSubmitErrors) => void;
|
|
10
|
+
export default setCtxErrorsByFormSubmitErrors;
|
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.105",
|
|
6
6
|
"description": "Integration utilities for react-hook-form.",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"sideEffects": false,
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@hookform/devtools": "4",
|
|
24
24
|
"@hookform/resolvers": "3",
|
|
25
|
+
"@trpc/client": "^10.45.2",
|
|
26
|
+
"@trpc/server": "^10.45.2",
|
|
25
27
|
"flat": "6",
|
|
26
28
|
"react": "18",
|
|
27
29
|
"react-dom": "18",
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { jsx as i } from "react/jsx-runtime";
|
|
2
|
-
import e from "./useRhfUtilsClientForZodContext.mjs";
|
|
3
|
-
function f({
|
|
4
|
-
schema: o,
|
|
5
|
-
...t
|
|
6
|
-
}) {
|
|
7
|
-
const r = e();
|
|
8
|
-
return /* @__PURE__ */ i(r.FormWithProviders, { schema: o, ...t });
|
|
9
|
-
}
|
|
10
|
-
export {
|
|
11
|
-
f as default
|
|
12
|
-
};
|