@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 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
- onSubmitError: (error) => {
35
- // if other error, re-throw to handle
36
- if (!(error instanceof TRPCClientError)) throw error;
37
-
38
- // transform
39
- const errors = transformToFormSubmitError(error);
40
-
41
- // return to be merged in RHF context errors
42
- return new FormSubmitError(errors);
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: ({ options }) => {
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
- <RhfUtilsZodFormWithProviders
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
- Fields={({ Controller }) => (
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={(props) => (
91
- <InputControlledRender
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
- <Button type="submit">Login</Button>
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,10 @@
1
+ import { z as r } from "zod";
2
+ const e = r.object({
3
+ validation: r.string(),
4
+ code: r.string(),
5
+ message: r.string(),
6
+ path: r.array(r.string())
7
+ });
8
+ export {
9
+ e as trpcClientErrorMessageSchema
10
+ };
@@ -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
+ };
@@ -1,66 +1,70 @@
1
1
  import { default as e } from "./client/zod/context/RhfUtilsClientForZodContextProvider.mjs";
2
- import { default as s } from "./client/zod/context/RhfUtilsZodFormWithProviders.mjs";
3
- import { default as f } from "./client/zod/context/useRhfUtilsClientForZodContext.mjs";
4
- import { default as m } from "./client/zod/context/useRhfUtilsZodForm.mjs";
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 C } from "./errors/message/FormErrorMessageByPath.mjs";
10
- import { default as G } from "./errors/nonfield/FormNonFieldErrorMarker.mjs";
11
- import { default as P } from "./errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.mjs";
12
- import { default as I } from "./errors/orphan/useFormHasOnlyOrphanFieldErrorsMemo.mjs";
13
- import { default as B } from "./errors/orphan/useOrphanFormErrorsMemo.mjs";
14
- import { default as b } from "./errors/root/RootErrorsListFromFlatFieldErrorsContext.mjs";
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 { default as T } from "./errors/useFormErrorsEffect.mjs";
17
- import { default as g } from "./errors/useFormErrorsIfAny.mjs";
18
- import { default as A } from "./form/FormWithProviders.mjs";
19
- import { default as D } from "./form/useForm.mjs";
20
- import { default as N } from "./form/context/utils/useFormUtilsContext.mjs";
21
- import { default as z } from "./form/context/group/FormGroupContextProvider.mjs";
22
- import { default as q } from "./form/context/group/useFormGroupChildIsMountedTracker.mjs";
23
- import { default as J } from "./form/context/group/useFormGroupChildIsSubmittingTracker.mjs";
24
- import { default as Q } from "./form/context/group/useFormGroupChildTracker.mjs";
25
- import { default as X } from "./form/context/group/useFormGroupIsAnyBusy.mjs";
26
- import { default as _ } from "./form/context/group/useFormGroupIsChildBusy.mjs";
27
- import { default as rr } from "./form/context/group/useFormGroupIsParentBusy.mjs";
28
- import { default as er } from "./form/context/group/useFormGroupParentTracker.mjs";
29
- import { default as sr } from "./form/context/group/useFormOrParentIsBusy.mjs";
30
- import { default as fr } from "./form/utils/getSubmitterButtonData.mjs";
31
- import { FormSubmitError as mr } from "./submit/error/FormSubmitError.mjs";
32
- import { default as dr } from "./submit/useFormOnSubmitSuccessful.mjs";
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
- C as FormErrorMessageByPath,
37
- z as FormGroupContextProvider,
38
- G as FormNonFieldErrorMarker,
39
- P as FormNonFieldErrorMarkerHtmlAttribute,
40
- mr as FormSubmitError,
41
- A as FormWithProviders,
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
- s as RhfUtilsZodFormWithProviders,
45
- b as RootErrorsListFromFlatFieldErrorsContext,
46
- fr as getSubmitterButtonData,
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
- D as useForm,
49
- T as useFormErrorsEffect,
50
- g as useFormErrorsIfAny,
51
- q as useFormGroupChildIsMountedTracker,
52
- J as useFormGroupChildIsSubmittingTracker,
53
- Q as useFormGroupChildTracker,
54
- X as useFormGroupIsAnyBusy,
55
- _ as useFormGroupIsChildBusy,
56
- rr as useFormGroupIsParentBusy,
57
- er as useFormGroupParentTracker,
58
- I as useFormHasOnlyOrphanFieldErrorsMemo,
59
- dr as useFormOnSubmitSuccessful,
60
- sr as useFormOrParentIsBusy,
61
- N as useFormUtilsContext,
62
- B as useOrphanFormErrorsMemo,
63
- f as useRhfUtilsClientForZodContext,
64
- m as useRhfUtilsZodForm,
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
  };
@@ -1,61 +1,61 @@
1
- import { jsxs as F, jsx as o } from "react/jsx-runtime";
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/setCtxErrorsByFormSubmitError.mjs";
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: d,
15
- onSubmitError: c,
14
+ onSubmit: o,
15
+ onSubmitError: d,
16
16
  onSubmitInvalid: y,
17
17
  Fields: h,
18
18
  //
19
19
  form: s,
20
- options: t,
20
+ options: e,
21
21
  //
22
22
  children: f
23
23
  }) {
24
- const l = _(), e = j(), b = async (r) => {
25
- t != null && t.stopSubmitPropagation && r.stopPropagation();
24
+ const l = _(), r = j(), b = async (t) => {
25
+ e != null && e.stopSubmitPropagation && t.stopPropagation();
26
26
  try {
27
- await l.handleSubmit(v, y)(r);
27
+ await l.handleSubmit(v, y)(t);
28
28
  } catch (a) {
29
- B(a, r);
29
+ B(a, t);
30
30
  }
31
- }, v = (r, a) => d == null ? void 0 : d(
32
- r,
31
+ }, v = (t, a) => o == null ? void 0 : o(
32
+ t,
33
33
  l,
34
34
  a
35
- ), B = (r, a) => {
35
+ ), B = (t, a) => {
36
36
  var n;
37
- const m = r instanceof N ? (
37
+ const m = t instanceof N ? (
38
38
  // consumer can manually throw FormSubmitError (e.g., manual validation)
39
- r
39
+ t.errors
40
40
  ) : (
41
41
  // if error is not FormSubmitError, consumer can provide global handler
42
- (n = e == null ? void 0 : e.onSubmitError) == null ? void 0 : n.call(e, r)
42
+ (n = r == null ? void 0 : r.onSubmitError) == null ? void 0 : n.call(r, t)
43
43
  );
44
- m && P(l, m), c == null || c(
45
- r,
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
- ctx: l,
53
- options: t,
52
+ context: l,
53
+ options: e,
54
54
  Controller: S
55
- }, i = (r) => {
55
+ }, i = (t) => {
56
56
  var m;
57
- const a = (m = e == null ? void 0 : e.Children) == null ? void 0 : m[r];
58
- return a && /* @__PURE__ */ o(a, { ...u });
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: e,
67
+ config: r,
68
68
  formRef: C,
69
69
  ...s,
70
- className: [t == null ? void 0 : t.classNames, s == null ? void 0 : s.className].filter(Boolean).join(" "),
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
- (t == null ? void 0 : t.devTool) !== "disabled" && /* @__PURE__ */ o(
74
+ (e == null ? void 0 : e.devTool) !== "disabled" && /* @__PURE__ */ c(
75
75
  D,
76
76
  {
77
77
  placement: "top-right",
78
- isDevelopment: e == null ? void 0 : e.isDevelopment,
79
- ...t == null ? void 0 : t.devTool
78
+ isDevelopment: r == null ? void 0 : r.isDevelopment,
79
+ ...e == null ? void 0 : e.devTool
80
80
  }
81
81
  ),
82
- h && /* @__PURE__ */ o(h, { ...u }),
82
+ h && /* @__PURE__ */ c(h, { ...u }),
83
83
  f,
84
84
  i("bottom")
85
85
  ]
@@ -1,5 +1,5 @@
1
1
  const s = (r, t) => {
2
- Object.entries(t.errors).forEach(([o, e]) => {
2
+ Object.entries(t).forEach(([o, e]) => {
3
3
  r.setError(o, {
4
4
  type: "FormSubmitError",
5
5
  // default
@@ -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 { FormSubmitError } from '../../submit/error/FormSubmitError';
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
- * Handle non-{@link FormSubmitError} errors. Optionally, return {@link FormSubmitError} to merge into context errors.
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 FormSubmitError}.
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) => FormSubmitError | undefined;
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>>;
@@ -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
- ctx: RhfUseFormReturn<TFieldValues, unknown, TTransformedValues>;
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: Record<FieldPath<TFieldValues> | `root.${string}`, {
6
- type?: string;
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.103",
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
- };