@paragrav/rhf-utils 0.0.143 → 0.0.144
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
CHANGED
|
@@ -69,7 +69,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
69
69
|
// optional form component (defaults to primitive HTML form)
|
|
70
70
|
FormComponent: Form.Root,
|
|
71
71
|
|
|
72
|
-
// optional wrapper to inject your own hooks and components
|
|
72
|
+
// optional wrapper component to inject your own hooks and components
|
|
73
73
|
// around all RhfUtilsZodForm instances
|
|
74
74
|
FormChildrenWrapper: (
|
|
75
75
|
// UseRhfUtilsFormChildrenProps
|
|
@@ -93,7 +93,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
93
93
|
|
|
94
94
|
return (
|
|
95
95
|
<>
|
|
96
|
-
{/* RhfUtilsZodForm.Children outlet (see "Component Hierarchy" section) */}
|
|
96
|
+
{/* RhfUtilsZodForm.Children "outlet" (see "Component Hierarchy" section) */}
|
|
97
97
|
{children}
|
|
98
98
|
|
|
99
99
|
{/* root errors list */}
|
|
@@ -103,7 +103,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
103
103
|
},
|
|
104
104
|
|
|
105
105
|
// non-FormSubmitError thrown in onSubmit
|
|
106
|
-
// use case: handle and transform error data for frontend
|
|
106
|
+
// use case: handle and transform server error data for frontend
|
|
107
107
|
onSubmitErrorUnknown: (
|
|
108
108
|
error, // unknown
|
|
109
109
|
) => {
|
|
@@ -166,7 +166,7 @@ Currently, only `zod` is supported.
|
|
|
166
166
|
// handle error declaratively (i.e., no throw/catch)
|
|
167
167
|
onSubmitError={({
|
|
168
168
|
error, // unknown
|
|
169
|
-
context, // UseRhfUtilsFormOnSubmitErrorContext (id, ref, rhf context, utils options)
|
|
169
|
+
context, // UseRhfUtilsFormOnSubmitErrorContext (id, ref, rhf context, utils options, `FormSubmitFieldErrors` from `onSubmitErrorUnknown`)
|
|
170
170
|
event, // SubmitEvent
|
|
171
171
|
}) => {
|
|
172
172
|
props.onError(error);
|
|
@@ -309,8 +309,9 @@ import '@paragrav/rhf-utils';
|
|
|
309
309
|
declare module '@paragrav/rhf-utils' {
|
|
310
310
|
export interface Register {
|
|
311
311
|
RhfUtilsFormOptions: {
|
|
312
|
-
/**
|
|
312
|
+
/** Custom props for your custom hooks/behaviors. */
|
|
313
313
|
enableMyOptionalFormHook?: boolean;
|
|
314
|
+
configMyOptionalFormHook?: MyCustomFormHookConfig;
|
|
314
315
|
};
|
|
315
316
|
}
|
|
316
317
|
}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as i, Fragment as B, jsxs as Z } from "react/jsx-runtime";
|
|
2
2
|
import a from "react";
|
|
3
|
-
import { c as
|
|
3
|
+
import { c as C } from "./createContext.mjs";
|
|
4
4
|
import { zodResolver as I } from "@hookform/resolvers/zod";
|
|
5
5
|
import { useFormContext as b, Controller as L, get as z, useFormState as x, FormProvider as A, useForm as $ } from "react-hook-form";
|
|
6
6
|
import { i as O, _ as H, u as g } from "./useFlatFieldErrorsContext.mjs";
|
|
@@ -8,7 +8,7 @@ import { flatten as Q } from "flat";
|
|
|
8
8
|
const {
|
|
9
9
|
Provider: G,
|
|
10
10
|
useMaybe: _
|
|
11
|
-
} =
|
|
11
|
+
} = C(), J = a.lazy(
|
|
12
12
|
() => import("@hookform/devtools").then((r) => ({
|
|
13
13
|
default: r.DevTool
|
|
14
14
|
}))
|
|
@@ -51,11 +51,11 @@ function er({
|
|
|
51
51
|
const m = b(), d = _(), c = async (h) => {
|
|
52
52
|
u != null && u.stopSubmitPropagation && h.stopPropagation();
|
|
53
53
|
try {
|
|
54
|
-
await m.handleSubmit(
|
|
54
|
+
await m.handleSubmit(p, s)(h);
|
|
55
55
|
} catch (f) {
|
|
56
|
-
|
|
56
|
+
P(f, h);
|
|
57
57
|
}
|
|
58
|
-
},
|
|
58
|
+
}, p = (h, f) => e == null ? void 0 : e(
|
|
59
59
|
h,
|
|
60
60
|
{
|
|
61
61
|
formId: r,
|
|
@@ -65,33 +65,34 @@ function er({
|
|
|
65
65
|
FormSubmitError: F
|
|
66
66
|
},
|
|
67
67
|
f
|
|
68
|
-
),
|
|
68
|
+
), P = (h, f) => {
|
|
69
69
|
var y;
|
|
70
|
-
const
|
|
70
|
+
const E = h instanceof F ? (
|
|
71
71
|
// consumer can manually throw FormSubmitError (e.g., manual validation)
|
|
72
72
|
h.errors
|
|
73
73
|
) : (
|
|
74
74
|
// if error is not FormSubmitError, consumer can provide global handler
|
|
75
75
|
(y = d == null ? void 0 : d.onSubmitErrorUnknown) == null ? void 0 : y.call(d, h)
|
|
76
76
|
);
|
|
77
|
-
|
|
77
|
+
E && X(m, E), o == null || o(
|
|
78
78
|
h,
|
|
79
79
|
{
|
|
80
80
|
formId: r,
|
|
81
81
|
formRef: t,
|
|
82
82
|
context: m,
|
|
83
|
-
utils: u
|
|
83
|
+
utils: u,
|
|
84
|
+
errors: E
|
|
84
85
|
},
|
|
85
86
|
f
|
|
86
87
|
);
|
|
87
|
-
},
|
|
88
|
+
}, R = {
|
|
88
89
|
formId: r,
|
|
89
90
|
formRef: t,
|
|
90
91
|
context: m,
|
|
91
92
|
utils: u,
|
|
92
93
|
Controller: rr,
|
|
93
94
|
FormSubmitError: F
|
|
94
|
-
}, V =
|
|
95
|
+
}, V = R, N = (d == null ? void 0 : d.FormComponent) ?? "form", q = (d == null ? void 0 : d.FormChildrenWrapper) ?? Y;
|
|
95
96
|
return /* @__PURE__ */ i(
|
|
96
97
|
N,
|
|
97
98
|
{
|
|
@@ -106,18 +107,18 @@ function er({
|
|
|
106
107
|
props: typeof (u == null ? void 0 : u.devTool) == "object" ? u.devTool : void 0
|
|
107
108
|
}
|
|
108
109
|
),
|
|
109
|
-
/* @__PURE__ */ i(n, { ...
|
|
110
|
+
/* @__PURE__ */ i(n, { ...R })
|
|
110
111
|
] })
|
|
111
112
|
}
|
|
112
113
|
);
|
|
113
114
|
}
|
|
114
|
-
const
|
|
115
|
+
const v = (r) => (t) => (
|
|
115
116
|
// re-create object from entries
|
|
116
117
|
Object.fromEntries(
|
|
117
118
|
// break down to entries
|
|
118
119
|
Object.entries(t).filter(r)
|
|
119
120
|
)
|
|
120
|
-
), k = (r) => !!r.ref, tr = ([, r]) => k(r), or =
|
|
121
|
+
), k = (r) => !!r.ref, tr = ([, r]) => k(r), or = v(
|
|
121
122
|
tr
|
|
122
123
|
), W = "data-rhfutils-nonfield-error-marker-path", sr = (r, t) => (
|
|
123
124
|
// list of selector variants
|
|
@@ -144,9 +145,9 @@ const C = (r) => (t) => (
|
|
|
144
145
|
!k(t) && // non-root
|
|
145
146
|
!j(r) && // not marked in DOM
|
|
146
147
|
!ur(r, e)
|
|
147
|
-
), ir = (r) => ([t, e]) => lr(t, e, r), dr = (r, t) =>
|
|
148
|
+
), ir = (r) => ([t, e]) => lr(t, e, r), dr = (r, t) => v(
|
|
148
149
|
ir(t)
|
|
149
|
-
)(r), ar = ([r]) => j(r), mr =
|
|
150
|
+
)(r), ar = ([r]) => j(r), mr = v(
|
|
150
151
|
ar
|
|
151
152
|
), hr = (r) => Q(r), cr = (r) => {
|
|
152
153
|
const t = hr(r), e = Object.keys(t);
|
|
@@ -289,7 +290,7 @@ const C = (r) => (t) => (
|
|
|
289
290
|
n.removeEventListener("change", l);
|
|
290
291
|
};
|
|
291
292
|
}, []);
|
|
292
|
-
}, { Provider: Sr, useRequired: Ur } =
|
|
293
|
+
}, { Provider: Sr, useRequired: Ur } = C(), Zr = Ur, xr = ({
|
|
293
294
|
formId: r,
|
|
294
295
|
formRef: t,
|
|
295
296
|
options: e,
|
|
@@ -438,7 +439,7 @@ const _r = (r, ...t) => (
|
|
|
438
439
|
}, jr = kr(I), {
|
|
439
440
|
Provider: wr,
|
|
440
441
|
useRequired: D
|
|
441
|
-
} =
|
|
442
|
+
} = C(), Ir = ({
|
|
442
443
|
config: r,
|
|
443
444
|
children: t
|
|
444
445
|
}) => {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
import { UseFormReturn } from 'react-hook-form';
|
|
3
3
|
import { FormSubmitError } from '../submit/error/FormSubmitError';
|
|
4
|
+
import { FormSubmitFieldErrors } from '../submit/error/FormSubmitFieldErrors';
|
|
4
5
|
import { RhfUtilsFormOptions } from './RhfUtilsFormOptions';
|
|
5
6
|
import { SafeFieldValues } from './SafeFieldValues';
|
|
6
7
|
/**
|
|
@@ -23,4 +24,5 @@ export type UseRhfUtilsFormOnSubmitErrorContext<TFieldValues extends SafeFieldVa
|
|
|
23
24
|
formRef: React.RefObject<HTMLFormElement>;
|
|
24
25
|
context: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
25
26
|
utils?: RhfUtilsFormOptions;
|
|
27
|
+
errors?: FormSubmitFieldErrors;
|
|
26
28
|
};
|