@paragrav/rhf-utils 0.0.56 → 0.0.57
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/dist/esm/form/useForm.mjs +18 -15
- package/dist/esm/node_modules/@hookform/resolvers/zod/dist/zod.mjs +49 -0
- package/dist/types/src/config/index.d.ts +13 -1
- package/dist/types/src/exports.d.ts +1 -0
- package/dist/types/src/form/FormWithProviders.d.ts +2 -1
- package/dist/types/src/form/_Form.d.ts +3 -2
- package/dist/types/src/form/_FormProviders.d.ts +3 -3
- package/dist/types/src/form/useForm.d.ts +9 -6
- package/package.json +1 -1
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import u from "
|
|
4
|
-
import
|
|
5
|
-
|
|
1
|
+
import { zodResolver as f } from "../node_modules/@hookform/resolvers/zod/dist/zod.mjs";
|
|
2
|
+
import i from "react";
|
|
3
|
+
import { useForm as u } from "react-hook-form";
|
|
4
|
+
import c from "../submit/useSubmitFormOnChange.mjs";
|
|
5
|
+
import { _getConfig as d } from "../config/index.mjs";
|
|
6
|
+
const a = ({
|
|
7
|
+
schema: t,
|
|
6
8
|
rhf: m,
|
|
7
9
|
form: o,
|
|
8
|
-
utils:
|
|
10
|
+
utils: p
|
|
9
11
|
}) => {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
+
const e = d(o == null ? void 0 : o.options), s = i.useId(), n = u({
|
|
13
|
+
resolver: t ? f(t) : void 0,
|
|
14
|
+
...e.rhf,
|
|
12
15
|
...m
|
|
13
|
-
}), r =
|
|
14
|
-
return
|
|
16
|
+
}), r = i.useRef(null);
|
|
17
|
+
return c(r, { disabled: !e.options.submitOnChange }), {
|
|
15
18
|
id: s,
|
|
16
19
|
ref: r,
|
|
17
|
-
context:
|
|
20
|
+
context: n,
|
|
18
21
|
providerProps: {
|
|
19
22
|
formId: s,
|
|
20
23
|
formRef: r,
|
|
21
|
-
rhfForm:
|
|
22
|
-
utils:
|
|
24
|
+
rhfForm: n,
|
|
25
|
+
utils: p
|
|
23
26
|
},
|
|
24
27
|
formProps: {
|
|
25
28
|
formId: s,
|
|
@@ -27,7 +30,7 @@ const f = ({
|
|
|
27
30
|
form: o
|
|
28
31
|
}
|
|
29
32
|
};
|
|
30
|
-
},
|
|
33
|
+
}, h = a;
|
|
31
34
|
export {
|
|
32
|
-
|
|
35
|
+
h as default
|
|
33
36
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { appendErrors as v } from "react-hook-form";
|
|
2
|
+
import { validateFieldsNatively as d, toNestErrors as h } from "@hookform/resolvers";
|
|
3
|
+
var m = function(i, u) {
|
|
4
|
+
for (var o = {}; i.length; ) {
|
|
5
|
+
var r = i[0], c = r.code, n = r.message, e = r.path.join(".");
|
|
6
|
+
if (!o[e])
|
|
7
|
+
if ("unionErrors" in r) {
|
|
8
|
+
var s = r.unionErrors[0].errors[0];
|
|
9
|
+
o[e] = { message: s.message, type: s.code };
|
|
10
|
+
} else
|
|
11
|
+
o[e] = { message: n, type: c };
|
|
12
|
+
if ("unionErrors" in r && r.unionErrors.forEach(function(f) {
|
|
13
|
+
return f.errors.forEach(function(l) {
|
|
14
|
+
return i.push(l);
|
|
15
|
+
});
|
|
16
|
+
}), u) {
|
|
17
|
+
var t = o[e].types, a = t && t[r.code];
|
|
18
|
+
o[e] = v(e, u, o, c, a ? [].concat(a, r.message) : r.message);
|
|
19
|
+
}
|
|
20
|
+
i.shift();
|
|
21
|
+
}
|
|
22
|
+
return o;
|
|
23
|
+
}, E = function(i, u, o) {
|
|
24
|
+
return o === void 0 && (o = {}), function(r, c, n) {
|
|
25
|
+
try {
|
|
26
|
+
return Promise.resolve(function(e, s) {
|
|
27
|
+
try {
|
|
28
|
+
var t = Promise.resolve(i[o.mode === "sync" ? "parse" : "parseAsync"](r, u)).then(function(a) {
|
|
29
|
+
return n.shouldUseNativeValidation && d({}, n), { errors: {}, values: o.raw ? r : a };
|
|
30
|
+
});
|
|
31
|
+
} catch (a) {
|
|
32
|
+
return s(a);
|
|
33
|
+
}
|
|
34
|
+
return t && t.then ? t.then(void 0, s) : t;
|
|
35
|
+
}(0, function(e) {
|
|
36
|
+
if (function(s) {
|
|
37
|
+
return s.errors != null;
|
|
38
|
+
}(e))
|
|
39
|
+
return { values: {}, errors: h(m(e.errors, !n.shouldUseNativeValidation && n.criteriaMode === "all"), n) };
|
|
40
|
+
throw e;
|
|
41
|
+
}));
|
|
42
|
+
} catch (e) {
|
|
43
|
+
return Promise.reject(e);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
export {
|
|
48
|
+
E as zodResolver
|
|
49
|
+
};
|
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
import { FormSubmitError } from '../submit/error/FormSubmitError';
|
|
2
|
-
import { UseFormOptions } from '../form/useForm';
|
|
2
|
+
import { UseFormFieldsProps, UseFormOptions } from '../form/useForm';
|
|
3
3
|
import { FieldValues, UseFormProps, UseFormStateReturn } from 'react-hook-form';
|
|
4
4
|
|
|
5
5
|
type Config = {
|
|
6
6
|
rhf?: Pick<UseFormProps, 'mode' | 'reValidateMode' | 'resetOptions' | 'context' | 'shouldFocusError' | 'shouldUnregister' | 'shouldUseNativeValidation' | 'progressive' | 'criteriaMode' | 'delayError'>;
|
|
7
|
+
/**
|
|
8
|
+
* Default global options settings.
|
|
9
|
+
*
|
|
10
|
+
* Extend this type to inject custom options/functionality via `Children` component.
|
|
11
|
+
*/
|
|
7
12
|
options?: UseFormOptions;
|
|
13
|
+
/**
|
|
14
|
+
* Component injected at the top of the form.
|
|
15
|
+
*
|
|
16
|
+
* e.g., block navigation on dirty form.
|
|
17
|
+
*/
|
|
18
|
+
Children?: React.FC<UseFormFieldsProps>;
|
|
8
19
|
/**
|
|
9
20
|
* Handle non-{@link FormSubmitError} errors. Optionally, return {@link FormSubmitError} to merge into context errors.
|
|
10
21
|
*/
|
|
@@ -51,6 +62,7 @@ export declare const _getConfig: (options?: UseFormOptions) => {
|
|
|
51
62
|
criteriaMode: import('react-hook-form').CriteriaMode;
|
|
52
63
|
delayError: number;
|
|
53
64
|
}>, "mode" | "reValidateMode" | "resetOptions" | "context" | "shouldFocusError" | "shouldUnregister" | "shouldUseNativeValidation" | "progressive" | "criteriaMode" | "delayError"> | undefined;
|
|
65
|
+
Children?: import('react').FC<UseFormFieldsProps> | undefined;
|
|
54
66
|
onSubmitError?: ((error: unknown) => FormSubmitError | undefined) | undefined;
|
|
55
67
|
Prompter?: import('react').FC<UseFormStateReturn<FieldValues>> | undefined;
|
|
56
68
|
};
|
|
@@ -15,6 +15,7 @@ export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
|
|
|
15
15
|
export { default as useFormErrorsIfAny } from './errors/useFormErrorsIfAny';
|
|
16
16
|
export { default as useFormErrorsMemo } from './errors/useFormErrorsMemo';
|
|
17
17
|
export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
|
|
18
|
+
export type { UseFormFieldsProps, UseFormOptions } from './form/useForm';
|
|
18
19
|
export { default as useForm } from './form/useForm';
|
|
19
20
|
export { default as FormUtilsContextProvider } from './form/context/utils/FormUtilsContextProvider';
|
|
20
21
|
export { default as useFormUtilsContext } from './form/context/utils/useFormUtilsContext';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { UseFormProps } from './useForm';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
+
import { FieldValues } from 'react-hook-form';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Form providers (with utils) with immediate descendent {@link Form}.
|
|
@@ -8,5 +9,5 @@ import { z } from 'zod';
|
|
|
8
9
|
* Use {@link useForm} if you need to specific nesting hierarchy.
|
|
9
10
|
* (e.g., wrapping `<tr>` with provider and nesting `<form>` within `<td>` to appease HTML rules.)
|
|
10
11
|
*/
|
|
11
|
-
declare function FormWithProviders<TSchema extends z.
|
|
12
|
+
declare function FormWithProviders<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ children, ...props }: React.PropsWithChildren<UseFormProps<TSchema, TFieldValues, TTransformedValues>>): import("react/jsx-runtime").JSX.Element;
|
|
12
13
|
export default FormWithProviders;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { UseFormProps } from './useForm';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
+
import { FieldValues } from 'react-hook-form';
|
|
3
4
|
import { default as React } from 'react';
|
|
4
5
|
|
|
5
|
-
export type _FormProps<TSchema extends z.
|
|
6
|
+
export type _FormProps<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = React.PropsWithChildren<Pick<UseFormProps<TSchema, TFieldValues, TTransformedValues>, 'form'> & {
|
|
6
7
|
formId: string;
|
|
7
8
|
formRef: React.RefObject<HTMLFormElement>;
|
|
8
9
|
}>;
|
|
9
|
-
declare function _Form<TSchema extends z.
|
|
10
|
+
declare function _Form<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ formId, formRef, form, children, }: _FormProps<TSchema, TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
|
|
10
11
|
export default _Form;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { FormUtilsContextProviderProps } from './context/utils/FormUtilsContextProvider';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import { UseFormReturn } from 'react-hook-form';
|
|
3
|
+
import { FieldValues, UseFormReturn } from 'react-hook-form';
|
|
4
4
|
|
|
5
|
-
export type _FormProvidersProps<TSchema extends z.
|
|
5
|
+
export type _FormProvidersProps<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = React.PropsWithChildren<{
|
|
6
6
|
formId: string;
|
|
7
7
|
formRef: React.RefObject<HTMLFormElement>;
|
|
8
8
|
rhfForm: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
9
9
|
utils?: FormUtilsContextProviderProps;
|
|
10
10
|
}>;
|
|
11
|
-
declare function _FormProviders<TSchema extends z.
|
|
11
|
+
declare function _FormProviders<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ formId, formRef, rhfForm, utils, children, }: _FormProvidersProps<TSchema, TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
|
|
12
12
|
export default _FormProviders;
|
|
@@ -2,12 +2,11 @@ import { FormUtilsContextProviderProps } from './context/utils/FormUtilsContextP
|
|
|
2
2
|
import { _FormProvidersProps } from './_FormProviders';
|
|
3
3
|
import { _FormProps } from './_Form';
|
|
4
4
|
import { z } from 'zod';
|
|
5
|
-
import { SubmitErrorHandler, UseFormProps as RhfUseFormProps, UseFormReturn } from 'react-hook-form';
|
|
5
|
+
import { FieldValues, SubmitErrorHandler, UseFormProps as RhfUseFormProps, UseFormReturn } from 'react-hook-form';
|
|
6
6
|
import { default as React } from 'react';
|
|
7
7
|
import { DevtoolUIProps } from '@hookform/devtools/dist/devToolUI';
|
|
8
8
|
|
|
9
9
|
export type UseFormOptions = {
|
|
10
|
-
/** */
|
|
11
10
|
hideRootErrorsList?: boolean;
|
|
12
11
|
/** Request submit via listener on form change. */
|
|
13
12
|
submitOnChange?: boolean;
|
|
@@ -16,19 +15,19 @@ export type UseFormOptions = {
|
|
|
16
15
|
stopSubmitPropagation?: boolean;
|
|
17
16
|
devTool?: DevtoolUIProps | 'disabled';
|
|
18
17
|
};
|
|
19
|
-
export type
|
|
18
|
+
export type UseFormFieldsProps<TFieldValues extends FieldValues = FieldValues, TTransformedValues extends FieldValues = FieldValues> = {
|
|
20
19
|
formId: string;
|
|
21
20
|
formRef: React.RefObject<HTMLFormElement>;
|
|
22
21
|
ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
23
22
|
options?: UseFormOptions;
|
|
24
23
|
};
|
|
25
|
-
export type UseFormProps<TSchema extends z.
|
|
24
|
+
export type UseFormProps<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = {
|
|
26
25
|
schema?: TSchema;
|
|
27
26
|
/** {@link RhfUseFormProps} props for RHF's {@link useRhfForm}. */
|
|
28
27
|
rhf?: RhfUseFormProps<TFieldValues>;
|
|
29
28
|
/** {@link FormProps} props for Form. */
|
|
30
29
|
form?: {
|
|
31
|
-
Fields?: React.FC<
|
|
30
|
+
Fields?: React.FC<UseFormFieldsProps<TFieldValues, TTransformedValues>>;
|
|
32
31
|
onSubmit?: (values: TTransformedValues, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => unknown | Promise<unknown>;
|
|
33
32
|
/** Submit error handler. (Only for errors not of instance `FormError`, which are handled internally.) */
|
|
34
33
|
onSubmitError?: (error: unknown, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => void;
|
|
@@ -48,7 +47,11 @@ export type UseFormProps<TSchema extends z.SomeZodObject, TFieldValues extends z
|
|
|
48
47
|
* see https://caniuse.com/?search=form%20attribute
|
|
49
48
|
* @returns Form provider and form.
|
|
50
49
|
*/
|
|
51
|
-
declare const useForm: <TSchema extends z.
|
|
50
|
+
declare const useForm: <TSchema extends z.ZodObject<FieldValues, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
51
|
+
[x: string]: any;
|
|
52
|
+
}, {
|
|
53
|
+
[x: string]: any;
|
|
54
|
+
}>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ schema, rhf, form, utils, }: UseFormProps<TSchema, TFieldValues, TTransformedValues>) => {
|
|
52
55
|
readonly id: string;
|
|
53
56
|
readonly ref: React.RefObject<HTMLFormElement>;
|
|
54
57
|
readonly context: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
|