@paragrav/rhf-utils 0.0.85 → 0.0.87
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/Form.mjs +40 -37
- package/dist/esm/form/_Controller.mjs +9 -0
- package/dist/esm/form/_FormBase.mjs +13 -9
- package/dist/esm/form/useForm.mjs +16 -17
- package/dist/types/client/config.d.ts +1 -0
- package/dist/types/client/index.d.ts +1 -1
- package/dist/types/form/_Controller.d.ts +6 -0
- package/dist/types/form/_FormBase.d.ts +4 -2
- package/dist/types/form/useForm.d.ts +12 -25
- package/package.json +1 -1
package/dist/esm/form/Form.mjs
CHANGED
|
@@ -1,81 +1,84 @@
|
|
|
1
|
-
import { jsxs as
|
|
1
|
+
import { jsxs as S, jsx as C } from "react/jsx-runtime";
|
|
2
2
|
import "react";
|
|
3
|
-
import { useFormContext as
|
|
3
|
+
import { useFormContext as T } from "react-hook-form";
|
|
4
4
|
import v from "../devtool/LazyDevTool.mjs";
|
|
5
5
|
import { FormSubmitError as E } from "../submit/error/FormSubmitError.mjs";
|
|
6
6
|
import V from "../submit/error/setCtxErrorsByFormSubmitError.mjs";
|
|
7
|
-
import w from "./
|
|
8
|
-
|
|
7
|
+
import w from "./_Controller.mjs";
|
|
8
|
+
import z from "./_FormBase.mjs";
|
|
9
|
+
function J({
|
|
9
10
|
formId: B,
|
|
10
|
-
formRef:
|
|
11
|
+
formRef: u,
|
|
11
12
|
//
|
|
12
13
|
onSubmit: s,
|
|
13
14
|
onSubmitError: h,
|
|
14
15
|
onSubmitInvalid: F,
|
|
15
|
-
Fields:
|
|
16
|
+
Fields: x,
|
|
16
17
|
//
|
|
17
18
|
form: d,
|
|
18
|
-
options:
|
|
19
|
-
config:
|
|
19
|
+
options: r,
|
|
20
|
+
config: a,
|
|
20
21
|
//
|
|
21
|
-
children:
|
|
22
|
+
children: _
|
|
22
23
|
}) {
|
|
23
|
-
const l =
|
|
24
|
-
|
|
24
|
+
const l = T(), j = async (t) => {
|
|
25
|
+
r != null && r.stopSubmitPropagation && t.stopPropagation();
|
|
25
26
|
try {
|
|
26
|
-
await l.handleSubmit(
|
|
27
|
+
await l.handleSubmit(N, F)(t);
|
|
27
28
|
} catch (e) {
|
|
28
|
-
|
|
29
|
+
P(e, t);
|
|
29
30
|
}
|
|
30
|
-
},
|
|
31
|
-
|
|
31
|
+
}, N = (t, e) => s == null ? void 0 : s(
|
|
32
|
+
t,
|
|
32
33
|
l,
|
|
33
34
|
e
|
|
34
|
-
),
|
|
35
|
+
), P = (t, e) => {
|
|
35
36
|
var b;
|
|
36
|
-
const m =
|
|
37
|
+
const m = t instanceof E ? (
|
|
37
38
|
// consumer can manually throw FormSubmitError (e.g., manual validation)
|
|
38
|
-
|
|
39
|
+
t
|
|
39
40
|
) : (
|
|
40
41
|
// if error is not FormSubmitError, consumer can provide global handler
|
|
41
|
-
(b =
|
|
42
|
+
(b = a == null ? void 0 : a.onSubmitError) == null ? void 0 : b.call(a, t)
|
|
42
43
|
);
|
|
43
44
|
m && V(l, m), h == null || h(
|
|
44
|
-
|
|
45
|
+
t,
|
|
45
46
|
l,
|
|
46
47
|
e
|
|
47
48
|
);
|
|
48
|
-
},
|
|
49
|
+
}, y = {
|
|
49
50
|
formId: B,
|
|
50
|
-
formRef:
|
|
51
|
+
formRef: u,
|
|
51
52
|
ctx: l,
|
|
52
|
-
options:
|
|
53
|
-
|
|
53
|
+
options: r,
|
|
54
|
+
Controller: w
|
|
55
|
+
}, c = (t) => {
|
|
54
56
|
var m;
|
|
55
|
-
const e = (m =
|
|
56
|
-
return e && /* @__PURE__ */
|
|
57
|
+
const e = (m = a == null ? void 0 : a.Children) == null ? void 0 : m[t];
|
|
58
|
+
return e && /* @__PURE__ */ C(e, { ...y });
|
|
57
59
|
};
|
|
58
|
-
return /* @__PURE__ */
|
|
59
|
-
|
|
60
|
+
return /* @__PURE__ */ S(
|
|
61
|
+
z,
|
|
60
62
|
{
|
|
61
63
|
noValidate: !0,
|
|
62
64
|
style: {
|
|
63
65
|
position: "relative"
|
|
64
66
|
},
|
|
67
|
+
config: a,
|
|
68
|
+
formRef: u,
|
|
65
69
|
...d,
|
|
66
|
-
className: [
|
|
67
|
-
onSubmit:
|
|
68
|
-
formRef: x,
|
|
70
|
+
className: [r == null ? void 0 : r.classNames, d == null ? void 0 : d.className].filter(Boolean).join(" "),
|
|
71
|
+
onSubmit: j,
|
|
69
72
|
children: [
|
|
70
|
-
|
|
71
|
-
(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
c("top"),
|
|
74
|
+
(r == null ? void 0 : r.devTool) !== "disabled" && /* @__PURE__ */ C(v, { placement: "top-right", ...r == null ? void 0 : r.devTool }),
|
|
75
|
+
x && /* @__PURE__ */ C(x, { ...y }),
|
|
76
|
+
_,
|
|
77
|
+
c("bottom")
|
|
75
78
|
]
|
|
76
79
|
}
|
|
77
80
|
);
|
|
78
81
|
}
|
|
79
82
|
export {
|
|
80
|
-
|
|
83
|
+
J as default
|
|
81
84
|
};
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { jsxs as
|
|
1
|
+
import { jsxs as p, jsx as s } from "react/jsx-runtime";
|
|
2
2
|
import "react";
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const l = ({
|
|
4
|
+
config: e,
|
|
5
|
+
formRef: t,
|
|
5
6
|
antiCsrfToken: r,
|
|
6
7
|
children: o,
|
|
7
|
-
...
|
|
8
|
-
}) =>
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
...m
|
|
9
|
+
}) => {
|
|
10
|
+
const n = (e == null ? void 0 : e.FormComponent) ?? "form";
|
|
11
|
+
return /* @__PURE__ */ p(n, { role: "form", ...m, ref: t, children: [
|
|
12
|
+
r && /* @__PURE__ */ s("input", { type: "hidden", name: "anticsrftoken", value: r }),
|
|
13
|
+
o
|
|
14
|
+
] });
|
|
15
|
+
};
|
|
12
16
|
export {
|
|
13
|
-
|
|
17
|
+
l as default
|
|
14
18
|
};
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useForm as
|
|
3
|
-
import { FormSubmitError as
|
|
4
|
-
const
|
|
5
|
-
onSubmit:
|
|
6
|
-
onSubmitError:
|
|
1
|
+
import m from "react";
|
|
2
|
+
import { useForm as F } from "react-hook-form";
|
|
3
|
+
import { FormSubmitError as a } from "../submit/error/FormSubmitError.mjs";
|
|
4
|
+
const I = ({
|
|
5
|
+
onSubmit: t,
|
|
6
|
+
onSubmitError: e,
|
|
7
7
|
onSubmitInvalid: f,
|
|
8
|
-
defaultValues:
|
|
9
|
-
Fields:
|
|
8
|
+
defaultValues: p,
|
|
9
|
+
Fields: n,
|
|
10
10
|
config: u,
|
|
11
11
|
rhf: c,
|
|
12
12
|
form: i,
|
|
13
13
|
options: d
|
|
14
14
|
}) => {
|
|
15
|
-
const r =
|
|
15
|
+
const r = m.useId(), s = F({
|
|
16
16
|
...c,
|
|
17
|
-
defaultValues:
|
|
18
|
-
}), o =
|
|
17
|
+
defaultValues: p
|
|
18
|
+
}), o = m.useRef(null);
|
|
19
19
|
return {
|
|
20
20
|
id: r,
|
|
21
21
|
ref: o,
|
|
@@ -27,19 +27,18 @@ const E = ({
|
|
|
27
27
|
options: d
|
|
28
28
|
},
|
|
29
29
|
formProps: {
|
|
30
|
-
Fields:
|
|
31
|
-
onSubmit:
|
|
32
|
-
onSubmitError:
|
|
30
|
+
Fields: n,
|
|
31
|
+
onSubmit: t,
|
|
32
|
+
onSubmitError: e,
|
|
33
33
|
onSubmitInvalid: f,
|
|
34
34
|
formId: r,
|
|
35
35
|
formRef: o,
|
|
36
36
|
config: u,
|
|
37
37
|
form: i
|
|
38
38
|
},
|
|
39
|
-
|
|
40
|
-
}
|
|
39
|
+
FormSubmitError: a
|
|
41
40
|
};
|
|
42
41
|
};
|
|
43
42
|
export {
|
|
44
|
-
|
|
43
|
+
I as default
|
|
45
44
|
};
|
|
@@ -34,4 +34,5 @@ export type ClientConfig = {
|
|
|
34
34
|
* Handle non-{@link FormSubmitError} errors. Optionally, return {@link FormSubmitError} to merge into context errors.
|
|
35
35
|
*/
|
|
36
36
|
onSubmitError?: (error: unknown) => FormSubmitError | undefined;
|
|
37
|
+
FormComponent?: React.FC<React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>>>;
|
|
37
38
|
};
|
|
@@ -17,7 +17,7 @@ export type CreateRhfUtilsClient<TObjectSchema extends ObjectSchema<TObjectSchem
|
|
|
17
17
|
NonNullable<UseFormPropsForm['onSubmit']>,
|
|
18
18
|
UseFormPropsForm['onSubmitError'],
|
|
19
19
|
UseFormPropsForm['onSubmitInvalid']
|
|
20
|
-
], defaultValues: UseFormPropsForm['defaultValues'], Fields: UseFormPropsForm['Fields'], props
|
|
20
|
+
], defaultValues: UseFormPropsForm['defaultValues'], Fields: UseFormPropsForm['Fields'], props?: UseFormProps<TSchemaInput, TSchemaOutput>) => UseFormReturn<TSchemaInput, TSchemaOutput>;
|
|
21
21
|
FormWithProviders: <TSchema extends TObjectSchema, TSchemaInput extends SafeFieldValues = TSchema[TObjectSchemaInputKey], TSchemaOutput extends SafeFieldValues = TSchema[TObjectSchemaOutputKey]>(props: {
|
|
22
22
|
schema: TSchema;
|
|
23
23
|
} & FormWithProvidersProps<TSchemaInput, TSchemaOutput>) => JSX.Element;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SafeFieldValues } from './SafeFieldValues';
|
|
2
|
+
import { ControllerProps, FieldPath } from 'react-hook-form';
|
|
3
|
+
|
|
4
|
+
export type _ControllerProps<TFieldValues extends SafeFieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<ControllerProps<TFieldValues, TName>, 'control'>;
|
|
5
|
+
declare function _Controller<TFieldValues extends SafeFieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(props: _ControllerProps<TFieldValues, TName>): import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
export default _Controller;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { ClientConfig } from '../client/config';
|
|
1
2
|
import { default as React } from 'react';
|
|
2
3
|
|
|
3
|
-
export type FormBaseProps = React.
|
|
4
|
+
export type FormBaseProps = React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>> & {
|
|
5
|
+
config?: ClientConfig;
|
|
4
6
|
formRef: React.RefObject<HTMLFormElement>;
|
|
5
7
|
antiCsrfToken?: string;
|
|
6
8
|
};
|
|
7
|
-
declare const _FormBase: ({ formRef, antiCsrfToken, children, ...props }: FormBaseProps) => import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
declare const _FormBase: ({ config, formRef, antiCsrfToken, children, ...props }: FormBaseProps) => import("react/jsx-runtime").JSX.Element;
|
|
8
10
|
export default _FormBase;
|
|
@@ -2,6 +2,8 @@ import { UseFormOptions } from './useFormOptions';
|
|
|
2
2
|
import { SafeFieldValues } from './SafeFieldValues';
|
|
3
3
|
import { FormProvidersProps } from './FormProviders';
|
|
4
4
|
import { FormProps } from './Form';
|
|
5
|
+
import { default as _Controller } from './_Controller';
|
|
6
|
+
import { FormSubmitError } from '../submit/error/FormSubmitError';
|
|
5
7
|
import { ClientConfig } from '../client/config';
|
|
6
8
|
import { SubmitErrorHandler, UseFormProps as RhfUseFormProps, UseFormReturn as RhfUseFormReturn } from 'react-hook-form';
|
|
7
9
|
import { default as React } from 'react';
|
|
@@ -11,6 +13,7 @@ export type UseFormFieldsProps<TFieldValues extends SafeFieldValues = SafeFieldV
|
|
|
11
13
|
formRef: React.RefObject<HTMLFormElement>;
|
|
12
14
|
ctx: RhfUseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
13
15
|
options?: UseFormOptions;
|
|
16
|
+
Controller: typeof _Controller<TFieldValues>;
|
|
14
17
|
};
|
|
15
18
|
export type UseFormProps<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = {
|
|
16
19
|
onSubmit?: (values: TTransformedValues, context: RhfUseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => unknown | Promise<unknown>;
|
|
@@ -27,6 +30,14 @@ export type UseFormProps<TFieldValues extends SafeFieldValues, TTransformedValue
|
|
|
27
30
|
form?: React.PropsWithoutRef<JSX.IntrinsicElements['form']>;
|
|
28
31
|
options?: UseFormOptions;
|
|
29
32
|
};
|
|
33
|
+
export type UseFormReturn<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = {
|
|
34
|
+
id: string;
|
|
35
|
+
ref: React.RefObject<HTMLFormElement>;
|
|
36
|
+
context: RhfUseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
37
|
+
providerProps: FormProvidersProps<TFieldValues, TTransformedValues>;
|
|
38
|
+
formProps: FormProps<TFieldValues, TTransformedValues>;
|
|
39
|
+
FormSubmitError: typeof FormSubmitError<TFieldValues>;
|
|
40
|
+
};
|
|
30
41
|
/**
|
|
31
42
|
* Core Rhf Form hook. Use `Form` in `index.ts` for most cases.
|
|
32
43
|
* Use this if you need to separate form components.
|
|
@@ -36,30 +47,6 @@ export type UseFormProps<TFieldValues extends SafeFieldValues, TTransformedValue
|
|
|
36
47
|
* see https://caniuse.com/?search=form%20attribute
|
|
37
48
|
* @returns Form provider and form.
|
|
38
49
|
*/
|
|
39
|
-
declare const useForm: <TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>({ onSubmit, onSubmitError, onSubmitInvalid, defaultValues, Fields, config, rhf, form, options, }: UseFormProps<TFieldValues, TTransformedValues>) =>
|
|
40
|
-
readonly id: string;
|
|
41
|
-
readonly ref: React.RefObject<HTMLFormElement>;
|
|
42
|
-
readonly context: RhfUseFormReturn<TFieldValues, unknown, TTransformedValues>;
|
|
43
|
-
readonly providerProps: FormProvidersProps<TFieldValues, TTransformedValues>;
|
|
44
|
-
readonly formProps: FormProps<TFieldValues, TTransformedValues>;
|
|
45
|
-
readonly SubmitError: {
|
|
46
|
-
new (errors: Record<`root.${string}` | import('react-hook-form').Path<TFieldValues>, {
|
|
47
|
-
type?: string | undefined;
|
|
48
|
-
message: string;
|
|
49
|
-
}>, message?: string | undefined): {
|
|
50
|
-
errors: Record<`root.${string}` | import('react-hook-form').Path<TFieldValues>, {
|
|
51
|
-
type?: string | undefined;
|
|
52
|
-
message: string;
|
|
53
|
-
}>;
|
|
54
|
-
name: string;
|
|
55
|
-
message: string;
|
|
56
|
-
stack?: string | undefined;
|
|
57
|
-
};
|
|
58
|
-
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
59
|
-
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
60
|
-
stackTraceLimit: number;
|
|
61
|
-
};
|
|
62
|
-
};
|
|
50
|
+
declare const useForm: <TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>({ onSubmit, onSubmitError, onSubmitInvalid, defaultValues, Fields, config, rhf, form, options, }: UseFormProps<TFieldValues, TTransformedValues>) => UseFormReturn<TFieldValues, TTransformedValues>;
|
|
63
51
|
export default useForm;
|
|
64
52
|
export type UseForm<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = typeof useForm<TFieldValues, TTransformedValues>;
|
|
65
|
-
export type UseFormReturn<TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues> = ReturnType<UseForm<TFieldValues, TTransformedValues>>;
|