@paragrav/rhf-utils 0.0.114 → 0.0.115
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
|
@@ -33,8 +33,9 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
33
33
|
// optional (defaults to primitive HTML form)
|
|
34
34
|
FormComponent: Form.Root,
|
|
35
35
|
|
|
36
|
-
//
|
|
37
|
-
|
|
36
|
+
// non-FormSubmitError throw in onSubmit
|
|
37
|
+
// use case: transform backend error
|
|
38
|
+
onSubmitUnknownError: (
|
|
38
39
|
error, // unknown
|
|
39
40
|
) => {
|
|
40
41
|
// if other error, re-throw to handle in ErrorBoundary
|
|
@@ -96,7 +97,7 @@ And add the context provider to your stack:
|
|
|
96
97
|
|
|
97
98
|
Currently, only `zod` is supported.
|
|
98
99
|
|
|
99
|
-
##
|
|
100
|
+
## Form
|
|
100
101
|
|
|
101
102
|
```tsx
|
|
102
103
|
<RhfUtilsZodForm
|
|
@@ -174,11 +175,15 @@ Example:
|
|
|
174
175
|
root: { message: 'There was a problem with the form.' },
|
|
175
176
|
'street.address': { message: 'Street address invalid.' },
|
|
176
177
|
});
|
|
178
|
+
|
|
179
|
+
await post('/backend');
|
|
177
180
|
}}
|
|
178
181
|
/>
|
|
179
182
|
```
|
|
180
183
|
|
|
181
|
-
Any non-`FormSubmitError`s thrown from submit handler can be transformed by `RhfUtilsClientConfig`'s `
|
|
184
|
+
Any non-`FormSubmitError`s thrown from submit handler (e.g., fetch/axios error) can be transformed by `RhfUtilsClientConfig`'s `onSubmitUnknownError` callback. This takes an `unknown` error and can return a `FormSubmitErrors` object, which is merged in RHF's context errors.
|
|
185
|
+
|
|
186
|
+
Most common use case will be transforming backend errors to frontend shape. (You can use `getOnSubmitTrpcClientErrorHandler` HOF for TRPC backends.)
|
|
182
187
|
|
|
183
188
|
## `RhfUtilsFormOptions`
|
|
184
189
|
|
package/dist/esm/form/Form.mjs
CHANGED
|
@@ -45,7 +45,7 @@ function A({
|
|
|
45
45
|
e.errors
|
|
46
46
|
) : (
|
|
47
47
|
// if error is not FormSubmitError, consumer can provide global handler
|
|
48
|
-
(y = t == null ? void 0 : t.
|
|
48
|
+
(y = t == null ? void 0 : t.onSubmitUnknownError) == null ? void 0 : y.call(t, e)
|
|
49
49
|
);
|
|
50
50
|
a && P(n, a), d == null || d(
|
|
51
51
|
e,
|
|
@@ -48,7 +48,7 @@ export type RhfUtilsClientConfig = {
|
|
|
48
48
|
* - if {@link FormSubmitErrors}: merge into context errors.
|
|
49
49
|
* - if `undefined`: do nothing.
|
|
50
50
|
*/
|
|
51
|
-
|
|
51
|
+
onSubmitUnknownError?: (error: unknown) => FormSubmitErrors | undefined;
|
|
52
52
|
/**
|
|
53
53
|
* Components injected by slot across all forms.
|
|
54
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.onSubmitUnknownError}.
|
|
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['onSubmitUnknownError']>) => (error: unknown) => FormSubmitErrors | undefined;
|