@paragrav/rhf-utils 0.0.102 → 0.0.103

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
@@ -10,11 +10,17 @@ yarn add @paragrav/rhf-utils # yarn
10
10
 
11
11
  ## Config
12
12
 
13
- To configure, create a file like `src/integrations/paragrav-rhf-utils/config.tsx` with desired config:
13
+ To configure, create a file like `src/integrations/paragrav-rhf-utils/config.tsx` with desired config. This is global configuration across all forms.
14
+
15
+ ```tsx
16
+ export const config: RhfUtilsClientConfig = {
17
+ isDevelopment: import.meta.env.DEV,
14
18
 
15
- ```ts
16
- const config: ClientConfig = {
17
19
  defaultOptions: {
20
+ rhf: {
21
+ mode: 'onBlur',
22
+ },
23
+
18
24
  form: {
19
25
  devTool: {
20
26
  placement: 'top-right',
@@ -24,25 +30,29 @@ const config: ClientConfig = {
24
30
 
25
31
  FormComponent: Form.Root,
26
32
 
33
+ // transform backend error
27
34
  onSubmitError: (error) => {
35
+ // if other error, re-throw to handle
28
36
  if (!(error instanceof TRPCClientError)) throw error;
29
37
 
38
+ // transform
39
+ const errors = transformToFormSubmitError(error);
30
40
 
31
- const errors = trcpClientErrorToRhfErrorTransformer.parse(error);
32
-
41
+ // return to be merged in RHF context errors
33
42
  return new FormSubmitError(errors);
34
43
  },
35
44
 
36
45
  Children: {
37
- top: function Top({ options }) {
38
- // navigation blocker
46
+ top: ({ options }) => {
47
+ // navigation blocker (using custom option)
39
48
  useFormRouterBlocker(!!options?.prompter);
40
49
 
41
50
  return null;
42
51
  },
52
+
43
53
  bottom: () => (
44
54
  <>
45
- // global error list
55
+ {/* global error list */}
46
56
  <RootErrorsListFromFlatFieldErrorsContext />
47
57
  </>
48
58
  ),
@@ -52,7 +62,7 @@ const config: ClientConfig = {
52
62
 
53
63
  ## Provider
54
64
 
55
- And add our context provider to your stack:
65
+ And add the context provider to your stack:
56
66
 
57
67
  ```tsx
58
68
  <RhfUtilsClientForZodContextProvider config={config}>
@@ -62,4 +72,52 @@ And add our context provider to your stack:
62
72
 
63
73
  ## Usage
64
74
 
65
- Coming soon.
75
+ ```tsx
76
+ <RhfUtilsZodFormWithProviders
77
+ schema={authLoginFormSchema}
78
+ onSubmit={async (data) => {
79
+ await authLoginService(data, { from });
80
+ onSuccess?.();
81
+ }}
82
+ onSubmitError={onError}
83
+ defaultValues={{
84
+ email: '',
85
+ }}
86
+ Fields={({ Controller }) => (
87
+ <>
88
+ <Controller
89
+ name="email" // strongly-typed field name
90
+ render={(props) => (
91
+ <InputControlledRender
92
+ controller={props}
93
+ autoFocus
94
+ label="Email"
95
+ autoComplete="email"
96
+ />
97
+ )}
98
+ />
99
+
100
+ <Button type="submit">Login</Button>
101
+ </>
102
+ )}
103
+ />
104
+ ```
105
+
106
+ `Fields` is a component which is rendered as a child of the form. It receives `UseFormFieldsProps` as props, including type-safe `Controller`.
107
+
108
+ ## Extend Options
109
+
110
+ Extend `UseFormOptions` with custom options, which get passed to `Fields` component in `options` prop.
111
+
112
+ ```tsx
113
+ import '@paragrav/rhf-utils';
114
+
115
+ declare module '@paragrav/rhf-utils' {
116
+ export interface Register {
117
+ UseFormOptions: {
118
+ /** Enable user prompt to confirm navigating away from dirty form. */
119
+ prompter?: boolean;
120
+ };
121
+ }
122
+ }
123
+ ```
@@ -2,7 +2,7 @@ import { jsx as i } from "react/jsx-runtime";
2
2
  import e from "./useRhfUtilsClientForZodContext.mjs";
3
3
  function f({
4
4
  schema: o,
5
- form: t
5
+ ...t
6
6
  }) {
7
7
  const r = e();
8
8
  return /* @__PURE__ */ i(r.FormWithProviders, { schema: o, ...t });
@@ -28,23 +28,47 @@ export type RhfUtilsClientConfig = {
28
28
  form?: UseFormOptions;
29
29
  };
30
30
  /**
31
- * Components injected by slot across all forms.
32
- * (Per-form injection can be done via Fields prop.)
31
+ * Supply your own `<form>` component.
33
32
  *
34
- * top: e.g., block navigation on dirty form.
35
- * bottom: e.g., root errors list.
33
+ * By default, a primitive `<form>` element is used.
36
34
  */
37
- Children?: Partial<Record<FormChildrenSlots, React.FC<UseFormFieldsProps>>>;
35
+ FormComponent?: React.FC<React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>>>;
38
36
  /**
39
37
  * Handle non-{@link FormSubmitError} errors. Optionally, return {@link FormSubmitError} to merge into context errors.
38
+ *
39
+ * Example use case:
40
+ * - transform API errors to {@link FormSubmitError}.
40
41
  */
41
42
  onSubmitError?: (error: unknown) => FormSubmitError | undefined;
42
- FormComponent?: React.FC<React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>>>;
43
+ /**
44
+ * Components injected by slot across all forms.
45
+ * (Per-form injection can be done via Fields prop.)
46
+ *
47
+ * - `top`: e.g., block navigation on dirty form.
48
+ * - `bottom`: e.g., root errors list.
49
+ */
50
+ Children?: Partial<Record<FormChildrenSlots, React.FC<UseFormFieldsProps>>>;
43
51
  errors?: {
52
+ /**
53
+ * Configure how/if errors should be outputted.
54
+ */
44
55
  output?: {
56
+ /**
57
+ * Configure how/if errors should be outputted to console.
58
+ *
59
+ * Example use cases:
60
+ * - console.debug errors in development environment.
61
+ * - console.error certain errors in production environment.
62
+ */
45
63
  console?: {
46
64
  getConfig?: (context: RhfUtilsErrorsOutputConfigContext) => RhfUtilsErrorsOutputConsoleConfig | null;
47
65
  };
66
+ /**
67
+ * Configure how/if errors should be thrown.
68
+ *
69
+ * Example use case:
70
+ * - bring attention to certain errors in development environment.
71
+ */
48
72
  throw?: {
49
73
  getConfig?: (context: RhfUtilsErrorsOutputConfigContext) => true | string | false;
50
74
  };
@@ -0,0 +1,4 @@
1
+ import { UseFormFieldsProps } from '../../form/useForm';
2
+ import { ZodTypeFieldValues } from './createRhfUtilsClientForZod';
3
+
4
+ export type UseFormFieldsZodProps<TSchema extends ZodTypeFieldValues> = UseFormFieldsProps<TSchema['_input'], TSchema['_output']>;
@@ -3,7 +3,6 @@ import { ZodTypeFieldValues } from '../createRhfUtilsClientForZod';
3
3
 
4
4
  type Props<TSchema extends ZodTypeFieldValues> = {
5
5
  schema: TSchema;
6
- form: Omit<UseFormProps<TSchema['_input'], TSchema['_output']>, 'config'>;
7
- };
8
- declare function RhfUtilsZodFormWithProviders<TSchema extends ZodTypeFieldValues>({ schema, form, }: Props<TSchema>): import("react/jsx-runtime").JSX.Element;
6
+ } & Omit<UseFormProps<TSchema['_input'], TSchema['_output']>, 'config'>;
7
+ declare function RhfUtilsZodFormWithProviders<TSchema extends ZodTypeFieldValues>({ schema, ...props }: Props<TSchema>): import("react/jsx-runtime").JSX.Element;
9
8
  export default RhfUtilsZodFormWithProviders;
@@ -0,0 +1,14 @@
1
+ import { FieldErrors } from 'react-hook-form';
2
+ import { SafeFieldValues } from '../form/SafeFieldValues';
3
+
4
+ /**
5
+ * Output form values and errors to console.
6
+ */
7
+ declare const consoleErrors: (message: string, form: {
8
+ values: SafeFieldValues;
9
+ errors: FieldErrors;
10
+ }, options?: {
11
+ other?: Record<string, unknown>;
12
+ isError?: boolean;
13
+ }) => void;
14
+ export default consoleErrors;
@@ -0,0 +1,9 @@
1
+ import { default as React } from 'react';
2
+
3
+ /**
4
+ * Logs out orphan errors to console.error.
5
+ *
6
+ * In development, it will also throw an error to bring attention to dev.
7
+ */
8
+ declare const useOrphanFormErrorsToConsole: (formRef: React.RefObject<HTMLFormElement>, enabled?: boolean) => void;
9
+ export default useOrphanFormErrorsToConsole;
@@ -0,0 +1,14 @@
1
+ import { FieldErrors } from 'react-hook-form';
2
+ import { SafeFieldValues } from '../form/SafeFieldValues';
3
+
4
+ /**
5
+ * Output form values and errors to console and throw in dev.
6
+ */
7
+ declare const outputErrors: (message: string, form: {
8
+ values: SafeFieldValues;
9
+ errors: FieldErrors;
10
+ }, options?: {
11
+ other?: Record<string, unknown>;
12
+ isError?: boolean;
13
+ }) => void;
14
+ export default outputErrors;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Output form errors to console.debug() for easier debugging for certain condition.
3
+ * (e.g., in dev/staging environments or certain users)
4
+ *
5
+ * `console.debug()` is unlikely to trigger any automated console capture reporting.
6
+ */
7
+ declare const useFormErrorConsoleDebug: (enabled?: boolean) => void;
8
+ export default useFormErrorConsoleDebug;
@@ -0,0 +1,13 @@
1
+ import { FlatFieldErrors } from './flat/types';
2
+
3
+ /**
4
+ * Logs out orphan errors to console.error.
5
+ *
6
+ * In development, it will also throw an error to bring attention to dev.
7
+ */
8
+ declare const useFormErrorsToConsole: (message: string, flatErrors: FlatFieldErrors, options?: {
9
+ disabled?: boolean;
10
+ isError?: boolean;
11
+ data?: Record<string, unknown>;
12
+ }) => void;
13
+ export default useFormErrorsToConsole;
@@ -4,6 +4,7 @@ export { default as RhfUtilsClientForZodContextProvider } from './client/zod/con
4
4
  export { default as RhfUtilsZodFormWithProviders } from './client/zod/context/RhfUtilsZodFormWithProviders';
5
5
  export { default as useRhfUtilsClientForZodContext } from './client/zod/context/useRhfUtilsClientForZodContext';
6
6
  export { default as useRhfUtilsZodForm } from './client/zod/context/useRhfUtilsZodForm';
7
+ export type { UseFormFieldsZodProps } from './client/zod/UseFormFieldsZodProps';
7
8
  export { default as LazyDevTool } from './devtool/LazyDevTool';
8
9
  export { default as useFlatFieldErrorsContext } from './errors/flat/context/useFlatFieldErrorsContext';
9
10
  export { default as FlatFieldErrorsList } from './errors/flat/FlatFieldErrorsList';
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * Use this to extend internal types.
3
+ *
3
4
  * For example, `UseFormOptions`.
4
5
  */
5
6
  export interface Register {
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.102",
5
+ "version": "0.0.103",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,