@paragrav/rhf-utils 0.0.113 → 0.0.114
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 +31 -6
- package/dist/esm/errors/trpc/getOnSubmitTrpcClientErrorHandler.mjs +6 -0
- package/dist/esm/errors/trpc/trpcClientErrorToFormSubmitErrorsSchemaTransformer.mjs +34 -0
- package/dist/esm/form/Form.mjs +102 -0
- package/dist/types/client/config/RhfUtilsClientConfig.d.ts +4 -6
- package/dist/types/errors/trpc/getOnSubmitTrpcClientErrorHandler.d.ts +2 -2
- package/dist/types/form/UseRhfUtilsFormChildrenProps.d.ts +1 -1
- package/dist/types/form/UseRhfUtilsFormOnSubmitContext.d.ts +27 -0
- package/dist/types/form/UseRhfUtilsFormProps.d.ts +4 -3
- package/dist/types/form/UseRhfUtilsFormReturn.d.ts +1 -1
- package/package.json +3 -5
- package/dist/esm/src/errors/trpc/getOnSubmitTrpcClientErrorHandler.mjs +0 -7
- package/dist/esm/src/errors/trpc/trpcClientErrorToFormSubmitErrorsSchemaTransformer.mjs +0 -35
- package/dist/esm/src/form/Form.mjs +0 -91
- /package/dist/esm/{src/client → client}/config/useRhfUtilsClientConfigContext.mjs +0 -0
- /package/dist/esm/{src/client → client}/index.mjs +0 -0
- /package/dist/esm/{src/client → client}/zod/context/RhfUtilsClientForZodContextProvider.mjs +0 -0
- /package/dist/esm/{src/client → client}/zod/context/RhfUtilsZodForm.mjs +0 -0
- /package/dist/esm/{src/client → client}/zod/context/useRhfUtilsClientForZodContext.mjs +0 -0
- /package/dist/esm/{src/client → client}/zod/context/useRhfUtilsZodForm.mjs +0 -0
- /package/dist/esm/{src/client → client}/zod/createRhfUtilsClientForZod.mjs +0 -0
- /package/dist/esm/{src/devtool → devtool}/LazyDevTool.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/flat/FlatFieldErrorsList.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/flat/context/FlatFieldErrorsContextProvider.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/flat/context/useFlatFieldErrorsContext.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/flat/filterFlatFieldErrors.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/flat/flattenFieldErrors.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/flat/getFlatFieldErrors.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/flat/useFlatFieldErrorsOutput.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/isFieldErrorRefd.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/message/FormErrorMessage.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/message/FormErrorMessageByPath.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/nonfield/FormNonFieldErrorMarker.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/nonfield/FormNonFieldErrorMarker.utils.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/nonfield/FormNonFieldErrorMarkerHtmlAttribute.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/nonfield/isNonFieldErrorMarkerInDOM.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/orphan/getIsOrphanFormErrorWithParentElement.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/orphan/getOrphansFromFlatFieldErrors.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/orphan/isOrphanFormError.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/output/consoleErrors.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/root/RootErrorsListFromFlatFieldErrorsContext.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/root/consts.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/root/getRootsFromFlatFieldErrors.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/root/isFlatFieldErrorEntryPathRoot.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/root/isFormErrorPathRoot.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/trpc/trpcClientErrorMessageSchema.mjs +0 -0
- /package/dist/esm/{src/errors → errors}/useFormErrorsTransformMemo.mjs +0 -0
- /package/dist/esm/{src/exports.mjs → exports.mjs} +0 -0
- /package/dist/esm/{src/form → form}/FormWithProviders.mjs +0 -0
- /package/dist/esm/{src/form → form}/RhfUtilsFormProviders.mjs +0 -0
- /package/dist/esm/{src/form → form}/_Controller.mjs +0 -0
- /package/dist/esm/{src/form → form}/_FormBase.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/FormGroupContextProvider.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/index.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormGroupChildIsMountedTracker.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormGroupChildIsSubmittingTracker.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormGroupChildTracker.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormGroupIsAnyBusy.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormGroupIsChildBusy.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormGroupIsParentBusy.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormGroupParentTracker.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/group/useFormOrParentIsBusy.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/utils/RhfUtilsContextProvider.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/utils/useRhfUtilsContext.mjs +0 -0
- /package/dist/esm/{src/form → form}/context/utils/useRhfUtilsContextRequestSubmit.mjs +0 -0
- /package/dist/esm/{src/form → form}/useFormIsBusy.mjs +0 -0
- /package/dist/esm/{src/form → form}/useRhfUtilsForm.mjs +0 -0
- /package/dist/esm/{src/form → form}/utils/getSubmitterButtonData.mjs +0 -0
- /package/dist/esm/{src/form → form}/utils/useFormRequestSubmit.mjs +0 -0
- /package/dist/esm/{src/submit → submit}/error/FormSubmitError.mjs +0 -0
- /package/dist/esm/{src/submit → submit}/error/setCtxErrorsByFormSubmitErrors.mjs +0 -0
- /package/dist/esm/{src/submit → submit}/useFormOnSubmitSuccessful.mjs +0 -0
- /package/dist/esm/{src/submit → submit}/useFormOnSubmitSuccessfulAndReset.mjs +0 -0
- /package/dist/esm/{src/submit → submit}/useResetFormOnSubmitError.mjs +0 -0
- /package/dist/esm/{src/submit → submit}/useResetFormOnSubmitSuccessful.mjs +0 -0
- /package/dist/esm/{src/submit → submit}/useSubmitFormOnChange.mjs +0 -0
- /package/dist/esm/{src/utils → utils}/createContext.mjs +0 -0
- /package/dist/esm/{src/utils → utils}/isEmptyObject.mjs +0 -0
package/README.md
CHANGED
|
@@ -34,12 +34,11 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
34
34
|
FormComponent: Form.Root,
|
|
35
35
|
|
|
36
36
|
// transform backend error
|
|
37
|
-
|
|
38
|
-
onSubmitError: (
|
|
37
|
+
onUnknownSubmitError: (
|
|
39
38
|
error, // unknown
|
|
40
39
|
) => {
|
|
41
40
|
// if other error, re-throw to handle in ErrorBoundary
|
|
42
|
-
if (!(error instanceof
|
|
41
|
+
if (!(error instanceof MyServerError)) throw error;
|
|
43
42
|
|
|
44
43
|
// return to be merged into RHF context errors
|
|
45
44
|
return transformServerErrorToFormSubmitErrors(error);
|
|
@@ -75,7 +74,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
75
74
|
output: {
|
|
76
75
|
// callback to determine when to output form context errors to console ("debug" or "error")
|
|
77
76
|
console: ({ isDevelopment, hasOrphans }) =>
|
|
78
|
-
(hasOrphans && { type: 'error' }) || // report orphan errors
|
|
77
|
+
(hasOrphans && { type: 'error' }) || // report all orphan errors
|
|
79
78
|
(isDevelopment && { type: 'debug' }), // facilitate debugging on dev
|
|
80
79
|
|
|
81
80
|
// callback to determine when to throw an error based on context
|
|
@@ -106,14 +105,18 @@ Currently, only `zod` is supported.
|
|
|
106
105
|
email: '',
|
|
107
106
|
}}
|
|
108
107
|
// submit handler
|
|
109
|
-
onSubmit={async (
|
|
108
|
+
onSubmit={async (
|
|
109
|
+
data, // schema output
|
|
110
|
+
context, // UseRhfUtilsFormOnSubmitContext (form id, form ref, context (rhf), utils options, strongly-typed FormSubmitError class)
|
|
111
|
+
event, // SubmitEvent
|
|
112
|
+
) => {
|
|
110
113
|
await authLoginService(data);
|
|
111
114
|
onSuccess();
|
|
112
115
|
}}
|
|
113
116
|
// handle error declaratively (i.e., no throw/catch)
|
|
114
117
|
onSubmitError={({
|
|
115
118
|
error, // unknown
|
|
116
|
-
context, // rhf
|
|
119
|
+
context, // UseRhfUtilsFormOnSubmitErrorContext (form id, form ref, context (rhf), utils options)
|
|
117
120
|
event, // SubmitEvent
|
|
118
121
|
}) => {
|
|
119
122
|
onError(error);
|
|
@@ -155,6 +158,28 @@ const Children: RhfUtilsUseFormChildrenZodFC<typeof schema> = ({ ... }) => { };
|
|
|
155
158
|
function Children({...}: RhfUtilsUseFormChildrenZodProps<typeof schema>) { }
|
|
156
159
|
```
|
|
157
160
|
|
|
161
|
+
## `FormSubmitError`
|
|
162
|
+
|
|
163
|
+
This is a derived `Error` class you can use to throw a structured error in your submit handler. It is strongly-typed, so it only allows field names in schema and `root`/`root.{string}` keys.
|
|
164
|
+
|
|
165
|
+
It uses `FormSubmitErrors` shape, which is a flat (dot notation) equivalent of RHF's `FieldErrors`.
|
|
166
|
+
|
|
167
|
+
Example:
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
<RhfUtilsZodForm
|
|
171
|
+
onSubmit={({ FormSubmitError }) => {
|
|
172
|
+
if (isProblem())
|
|
173
|
+
throw new FormSubmitError({
|
|
174
|
+
root: { message: 'There was a problem with the form.' },
|
|
175
|
+
'street.address': { message: 'Street address invalid.' },
|
|
176
|
+
});
|
|
177
|
+
}}
|
|
178
|
+
/>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Any non-`FormSubmitError`s thrown from submit handler can be transformed by `RhfUtilsClientConfig`'s `onUnknownSubmitError` callback. This takes an `unknown` error and can return a `FormSubmitErrors` object, which is merged in RHF's context errors. (Most common use case will be transforming backend errors to frontend shape. You can use `getOnSubmitTrpcClientErrorHandler` HOF for TRPC backends.)
|
|
182
|
+
|
|
158
183
|
## `RhfUtilsFormOptions`
|
|
159
184
|
|
|
160
185
|
These options can be set globally and/or per form.
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { jsxs as T, jsx as l } from "react/jsx-runtime";
|
|
2
|
+
import "react";
|
|
3
|
+
import { useFormContext as _ } from "react-hook-form";
|
|
4
|
+
import D from "../client/config/useRhfUtilsClientConfigContext.mjs";
|
|
5
|
+
import N from "../devtool/LazyDevTool.mjs";
|
|
6
|
+
import { FormSubmitError as C } from "../submit/error/FormSubmitError.mjs";
|
|
7
|
+
import P from "../submit/error/setCtxErrorsByFormSubmitErrors.mjs";
|
|
8
|
+
import S from "./_Controller.mjs";
|
|
9
|
+
import w from "./_FormBase.mjs";
|
|
10
|
+
function A({
|
|
11
|
+
formId: s,
|
|
12
|
+
formRef: p,
|
|
13
|
+
//
|
|
14
|
+
onSubmit: c,
|
|
15
|
+
onSubmitError: d,
|
|
16
|
+
onSubmitInvalid: v,
|
|
17
|
+
Children: x,
|
|
18
|
+
//
|
|
19
|
+
form: m,
|
|
20
|
+
utils: o,
|
|
21
|
+
//
|
|
22
|
+
children: b
|
|
23
|
+
}) {
|
|
24
|
+
const n = _(), t = D(), F = async (e) => {
|
|
25
|
+
o != null && o.stopSubmitPropagation && e.stopPropagation();
|
|
26
|
+
try {
|
|
27
|
+
await n.handleSubmit(j, v)(e);
|
|
28
|
+
} catch (r) {
|
|
29
|
+
B(r, e);
|
|
30
|
+
}
|
|
31
|
+
}, j = (e, r) => c == null ? void 0 : c(
|
|
32
|
+
e,
|
|
33
|
+
{
|
|
34
|
+
formId: s,
|
|
35
|
+
formRef: p,
|
|
36
|
+
context: n,
|
|
37
|
+
utils: o,
|
|
38
|
+
FormSubmitError: C
|
|
39
|
+
},
|
|
40
|
+
r
|
|
41
|
+
), B = (e, r) => {
|
|
42
|
+
var y;
|
|
43
|
+
const a = e instanceof C ? (
|
|
44
|
+
// consumer can manually throw FormSubmitError (e.g., manual validation)
|
|
45
|
+
e.errors
|
|
46
|
+
) : (
|
|
47
|
+
// if error is not FormSubmitError, consumer can provide global handler
|
|
48
|
+
(y = t == null ? void 0 : t.onUnknownSubmitError) == null ? void 0 : y.call(t, e)
|
|
49
|
+
);
|
|
50
|
+
a && P(n, a), d == null || d(
|
|
51
|
+
e,
|
|
52
|
+
{
|
|
53
|
+
formId: s,
|
|
54
|
+
formRef: p,
|
|
55
|
+
context: n,
|
|
56
|
+
utils: o
|
|
57
|
+
},
|
|
58
|
+
r
|
|
59
|
+
);
|
|
60
|
+
}, h = {
|
|
61
|
+
formId: s,
|
|
62
|
+
formRef: p,
|
|
63
|
+
context: n,
|
|
64
|
+
utils: o,
|
|
65
|
+
Controller: S,
|
|
66
|
+
FormSubmitError: C
|
|
67
|
+
}, i = (e) => {
|
|
68
|
+
var a;
|
|
69
|
+
const r = (a = t == null ? void 0 : t.Children) == null ? void 0 : a[e];
|
|
70
|
+
return r && /* @__PURE__ */ l(r, { ...h });
|
|
71
|
+
};
|
|
72
|
+
return /* @__PURE__ */ T(
|
|
73
|
+
w,
|
|
74
|
+
{
|
|
75
|
+
noValidate: !0,
|
|
76
|
+
style: {
|
|
77
|
+
position: "relative"
|
|
78
|
+
},
|
|
79
|
+
config: t,
|
|
80
|
+
formRef: p,
|
|
81
|
+
...m,
|
|
82
|
+
className: [o == null ? void 0 : o.classNames, m == null ? void 0 : m.className].filter(Boolean).join(" "),
|
|
83
|
+
onSubmit: F,
|
|
84
|
+
children: [
|
|
85
|
+
i("top"),
|
|
86
|
+
(o == null ? void 0 : o.devTool) && /* @__PURE__ */ l(
|
|
87
|
+
N,
|
|
88
|
+
{
|
|
89
|
+
isDevelopment: t == null ? void 0 : t.isDevelopment,
|
|
90
|
+
props: typeof o.devTool == "object" ? o.devTool : void 0
|
|
91
|
+
}
|
|
92
|
+
),
|
|
93
|
+
x && /* @__PURE__ */ l(x, { ...h }),
|
|
94
|
+
b,
|
|
95
|
+
i("bottom")
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
export {
|
|
101
|
+
A as default
|
|
102
|
+
};
|
|
@@ -39,18 +39,16 @@ export type RhfUtilsClientConfig = {
|
|
|
39
39
|
*/
|
|
40
40
|
FormComponent?: React.FC<React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>>>;
|
|
41
41
|
/**
|
|
42
|
-
* Global submit error handler.
|
|
43
|
-
*
|
|
44
|
-
* Called when/if submit handler throws an error other than {@link FormSubmitError}.
|
|
42
|
+
* Global submit error handler when/if submit handler throws an error other than {@link FormSubmitError}.
|
|
45
43
|
*
|
|
46
44
|
* Example use case:
|
|
47
45
|
* - transform API errors to {@link FormSubmitErrors}.
|
|
48
46
|
*
|
|
49
47
|
* @returns
|
|
50
|
-
* - {@link FormSubmitErrors}: merge into context errors.
|
|
51
|
-
* - `undefined`: do nothing.
|
|
48
|
+
* - if {@link FormSubmitErrors}: merge into context errors.
|
|
49
|
+
* - if `undefined`: do nothing.
|
|
52
50
|
*/
|
|
53
|
-
|
|
51
|
+
onUnknownSubmitError?: (error: unknown) => FormSubmitErrors | undefined;
|
|
54
52
|
/**
|
|
55
53
|
* Components injected by slot across all forms.
|
|
56
54
|
* (Per-form injection can be done via `Children` prop.)
|
|
@@ -2,9 +2,9 @@ import { RhfUtilsClientConfig } from '../../client/config/RhfUtilsClientConfig';
|
|
|
2
2
|
import { FormSubmitErrors } from '../../submit/error/FormSubmitErrors';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* HOF that returns submit error handler for {@link RhfUtilsClientConfig.
|
|
5
|
+
* HOF that returns submit error handler for {@link RhfUtilsClientConfig.onUnknownSubmitError}.
|
|
6
6
|
*
|
|
7
7
|
* @param onError Fallback for handling non-TRPC errors.
|
|
8
8
|
* @returns FormSubmitError
|
|
9
9
|
*/
|
|
10
|
-
export declare const getOnSubmitTrpcClientErrorHandler: (onNotTrpcClientError: NonNullable<RhfUtilsClientConfig['
|
|
10
|
+
export declare const getOnSubmitTrpcClientErrorHandler: (onNotTrpcClientError: NonNullable<RhfUtilsClientConfig['onUnknownSubmitError']>) => (error: unknown) => FormSubmitErrors | undefined;
|
|
@@ -8,7 +8,7 @@ import { SafeFieldValues } from './SafeFieldValues';
|
|
|
8
8
|
/**
|
|
9
9
|
* Component props for `Children`.
|
|
10
10
|
*
|
|
11
|
-
* Consider keeping shape in line with `UseRhfUtilsFormReturn`.
|
|
11
|
+
* Consider keeping shape in line with `UseRhfUtilsFormOnSubmitProps` and `UseRhfUtilsFormReturn`.
|
|
12
12
|
*/
|
|
13
13
|
export type UseRhfUtilsFormChildrenProps<TFieldValues extends SafeFieldValues = SafeFieldValues, TTransformedValues extends SafeFieldValues = SafeFieldValues> = {
|
|
14
14
|
formId: string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { UseFormReturn } from 'react-hook-form';
|
|
3
|
+
import { FormSubmitError } from '../submit/error/FormSubmitError';
|
|
4
|
+
import { RhfUtilsFormOptions } from './RhfUtilsFormOptions';
|
|
5
|
+
import { SafeFieldValues } from './SafeFieldValues';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Props for onSubmit other than `data` and `event`.
|
|
9
|
+
*
|
|
10
|
+
* Consider keeping shape in line with `UseRhfUtilsFormChildrenProps` and `UseRhfUtilsFormReturn`.
|
|
11
|
+
*/
|
|
12
|
+
export type UseRhfUtilsFormOnSubmitContext<TFieldValues extends SafeFieldValues = SafeFieldValues, TTransformedValues extends SafeFieldValues = SafeFieldValues> = {
|
|
13
|
+
formId: string;
|
|
14
|
+
formRef: React.RefObject<HTMLFormElement>;
|
|
15
|
+
context: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
16
|
+
utils?: RhfUtilsFormOptions;
|
|
17
|
+
FormSubmitError: typeof FormSubmitError<TFieldValues>;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Props for onSubmitError other than `error` and `event`.
|
|
21
|
+
*/
|
|
22
|
+
export type UseRhfUtilsFormOnSubmitErrorContext<TFieldValues extends SafeFieldValues = SafeFieldValues, TTransformedValues extends SafeFieldValues = SafeFieldValues> = {
|
|
23
|
+
formId: string;
|
|
24
|
+
formRef: React.RefObject<HTMLFormElement>;
|
|
25
|
+
context: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
26
|
+
utils?: RhfUtilsFormOptions;
|
|
27
|
+
};
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
|
-
import { SubmitErrorHandler, UseFormProps
|
|
2
|
+
import { SubmitErrorHandler, UseFormProps } from 'react-hook-form';
|
|
3
3
|
import { RhfUtilsFormOptions } from './RhfUtilsFormOptions';
|
|
4
4
|
import { SafeFieldValues } from './SafeFieldValues';
|
|
5
5
|
import { UseRhfUtilsFormChildrenProps } from './UseRhfUtilsFormChildrenProps';
|
|
6
|
+
import { UseRhfUtilsFormOnSubmitContext, UseRhfUtilsFormOnSubmitErrorContext } from './UseRhfUtilsFormOnSubmitContext';
|
|
6
7
|
|
|
7
8
|
export type UseRhfUtilsFormProps<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = {
|
|
8
|
-
onSubmit?: (values: TTransformedValues, context:
|
|
9
|
+
onSubmit?: (values: TTransformedValues, context: UseRhfUtilsFormOnSubmitContext<TFieldValues, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => unknown | Promise<unknown>;
|
|
9
10
|
/** Submit error handler. (Only for errors not of instance `FormError`, which are handled internally.) */
|
|
10
|
-
onSubmitError?: (error: unknown, context:
|
|
11
|
+
onSubmitError?: (error: unknown, context: UseRhfUtilsFormOnSubmitErrorContext<TFieldValues, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => void;
|
|
11
12
|
/** Submit handler when form is invalid. */
|
|
12
13
|
onSubmitInvalid?: SubmitErrorHandler<TFieldValues>;
|
|
13
14
|
defaultValues?: UseFormProps<TFieldValues>['defaultValues'];
|
|
@@ -8,7 +8,7 @@ import { SafeFieldValues } from './SafeFieldValues';
|
|
|
8
8
|
/**
|
|
9
9
|
* Return type for `useRhfUtilsForm`.
|
|
10
10
|
*
|
|
11
|
-
* Consider keeping shape in line with `UseRhfUtilsFormChildrenProps`.
|
|
11
|
+
* Consider keeping shape in line with `UseRhfUtilsFormChildrenProps` and `UseRhfUtilsFormOnSubmitProps`.
|
|
12
12
|
*/
|
|
13
13
|
export type UseRhfUtilsFormReturn<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = {
|
|
14
14
|
id: string;
|
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.114",
|
|
6
6
|
"description": "Integration utilities for react-hook-form.",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"sideEffects": false,
|
|
@@ -22,16 +22,14 @@
|
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@hookform/devtools": "4",
|
|
24
24
|
"@hookform/resolvers": "3",
|
|
25
|
+
"@trpc/client": "next",
|
|
26
|
+
"@trpc/server": "next",
|
|
25
27
|
"flat": "6",
|
|
26
28
|
"react": "18",
|
|
27
29
|
"react-dom": "18",
|
|
28
30
|
"react-hook-form": "7",
|
|
29
31
|
"zod": "3"
|
|
30
32
|
},
|
|
31
|
-
"optionalDependencies": {
|
|
32
|
-
"@trpc/client": "next",
|
|
33
|
-
"@trpc/server": "next"
|
|
34
|
-
},
|
|
35
33
|
"repository": {
|
|
36
34
|
"type": "git",
|
|
37
35
|
"url": "git+https://github.com/paragrav/rhf-utils.git"
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { TRPCClientError as o } from "../../../node_modules/.pnpm/@trpc_client@11.0.0-rc.390_@trpc_server@11.0.0-rc.390/node_modules/@trpc/client/dist/TRPCClientError.mjs";
|
|
2
|
-
import "../../../node_modules/.pnpm/@trpc_server@11.0.0-rc.390/node_modules/@trpc/server/dist/unstable-core-do-not-import/rootConfig.mjs";
|
|
3
|
-
import { trpcClientErrorToFormSubmitErrorsSchemaTransformer as n } from "./trpcClientErrorToFormSubmitErrorsSchemaTransformer.mjs";
|
|
4
|
-
const p = (t) => (r) => r instanceof o ? n.parse(r) : t(r);
|
|
5
|
-
export {
|
|
6
|
-
p as getOnSubmitTrpcClientErrorHandler
|
|
7
|
-
};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { TRPCClientError as e } from "../../../node_modules/.pnpm/@trpc_client@11.0.0-rc.390_@trpc_server@11.0.0-rc.390/node_modules/@trpc/client/dist/TRPCClientError.mjs";
|
|
2
|
-
import "../../../node_modules/.pnpm/@trpc_server@11.0.0-rc.390/node_modules/@trpc/server/dist/unstable-core-do-not-import/rootConfig.mjs";
|
|
3
|
-
import { z as o } from "zod";
|
|
4
|
-
import { trpcClientErrorMessageSchema as m } from "./trpcClientErrorMessageSchema.mjs";
|
|
5
|
-
const p = o.custom(
|
|
6
|
-
(r) => r instanceof e
|
|
7
|
-
// check instance type
|
|
8
|
-
).transform((r) => r.message).transform((r) => {
|
|
9
|
-
try {
|
|
10
|
-
return JSON.parse(r);
|
|
11
|
-
} catch {
|
|
12
|
-
return [
|
|
13
|
-
{
|
|
14
|
-
validation: "root",
|
|
15
|
-
code: "server",
|
|
16
|
-
message: r,
|
|
17
|
-
path: ["root"]
|
|
18
|
-
}
|
|
19
|
-
];
|
|
20
|
-
}
|
|
21
|
-
}).pipe(o.array(m)).transform(
|
|
22
|
-
(r) => r.map(
|
|
23
|
-
(t) => [
|
|
24
|
-
t.path.join("."),
|
|
25
|
-
// flatten path (e.g., "street.name")
|
|
26
|
-
{
|
|
27
|
-
type: t.code,
|
|
28
|
-
message: t.message
|
|
29
|
-
}
|
|
30
|
-
]
|
|
31
|
-
)
|
|
32
|
-
).transform((r) => Object.fromEntries(r));
|
|
33
|
-
export {
|
|
34
|
-
p as trpcClientErrorToFormSubmitErrorsSchemaTransformer
|
|
35
|
-
};
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { jsxs as B, jsx as d } from "react/jsx-runtime";
|
|
2
|
-
import "react";
|
|
3
|
-
import { useFormContext as T } from "react-hook-form";
|
|
4
|
-
import _ from "../client/config/useRhfUtilsClientConfigContext.mjs";
|
|
5
|
-
import D from "../devtool/LazyDevTool.mjs";
|
|
6
|
-
import { FormSubmitError as x } from "../submit/error/FormSubmitError.mjs";
|
|
7
|
-
import N from "../submit/error/setCtxErrorsByFormSubmitErrors.mjs";
|
|
8
|
-
import P from "./_Controller.mjs";
|
|
9
|
-
import S from "./_FormBase.mjs";
|
|
10
|
-
function A({
|
|
11
|
-
formId: y,
|
|
12
|
-
formRef: c,
|
|
13
|
-
//
|
|
14
|
-
onSubmit: p,
|
|
15
|
-
onSubmitError: s,
|
|
16
|
-
onSubmitInvalid: f,
|
|
17
|
-
Children: l,
|
|
18
|
-
//
|
|
19
|
-
form: m,
|
|
20
|
-
utils: o,
|
|
21
|
-
//
|
|
22
|
-
children: v
|
|
23
|
-
}) {
|
|
24
|
-
const a = T(), t = _(), b = async (e) => {
|
|
25
|
-
o != null && o.stopSubmitPropagation && e.stopPropagation();
|
|
26
|
-
try {
|
|
27
|
-
await a.handleSubmit(F, f)(e);
|
|
28
|
-
} catch (r) {
|
|
29
|
-
j(r, e);
|
|
30
|
-
}
|
|
31
|
-
}, F = (e, r) => p == null ? void 0 : p(
|
|
32
|
-
e,
|
|
33
|
-
a,
|
|
34
|
-
r
|
|
35
|
-
), j = (e, r) => {
|
|
36
|
-
var h;
|
|
37
|
-
const n = e instanceof x ? (
|
|
38
|
-
// consumer can manually throw FormSubmitError (e.g., manual validation)
|
|
39
|
-
e.errors
|
|
40
|
-
) : (
|
|
41
|
-
// if error is not FormSubmitError, consumer can provide global handler
|
|
42
|
-
(h = t == null ? void 0 : t.onSubmitError) == null ? void 0 : h.call(t, e)
|
|
43
|
-
);
|
|
44
|
-
n && N(a, n), s == null || s(
|
|
45
|
-
e,
|
|
46
|
-
a,
|
|
47
|
-
r
|
|
48
|
-
);
|
|
49
|
-
}, i = {
|
|
50
|
-
formId: y,
|
|
51
|
-
formRef: c,
|
|
52
|
-
context: a,
|
|
53
|
-
utils: o,
|
|
54
|
-
Controller: P,
|
|
55
|
-
FormSubmitError: x
|
|
56
|
-
}, C = (e) => {
|
|
57
|
-
var n;
|
|
58
|
-
const r = (n = t == null ? void 0 : t.Children) == null ? void 0 : n[e];
|
|
59
|
-
return r && /* @__PURE__ */ d(r, { ...i });
|
|
60
|
-
};
|
|
61
|
-
return /* @__PURE__ */ B(
|
|
62
|
-
S,
|
|
63
|
-
{
|
|
64
|
-
noValidate: !0,
|
|
65
|
-
style: {
|
|
66
|
-
position: "relative"
|
|
67
|
-
},
|
|
68
|
-
config: t,
|
|
69
|
-
formRef: c,
|
|
70
|
-
...m,
|
|
71
|
-
className: [o == null ? void 0 : o.classNames, m == null ? void 0 : m.className].filter(Boolean).join(" "),
|
|
72
|
-
onSubmit: b,
|
|
73
|
-
children: [
|
|
74
|
-
C("top"),
|
|
75
|
-
(o == null ? void 0 : o.devTool) && /* @__PURE__ */ d(
|
|
76
|
-
D,
|
|
77
|
-
{
|
|
78
|
-
isDevelopment: t == null ? void 0 : t.isDevelopment,
|
|
79
|
-
props: typeof o.devTool == "object" ? o.devTool : void 0
|
|
80
|
-
}
|
|
81
|
-
),
|
|
82
|
-
l && /* @__PURE__ */ d(l, { ...i }),
|
|
83
|
-
v,
|
|
84
|
-
C("bottom")
|
|
85
|
-
]
|
|
86
|
-
}
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
export {
|
|
90
|
-
A as default
|
|
91
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|