@paragrav/rhf-utils 0.0.124 → 0.0.126
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 +14 -6
- package/dist/esm/client/index.mjs +29 -35
- package/dist/esm/client/utils.mjs +24 -0
- package/dist/esm/devtool/LazyDevTool.mjs +1 -2
- package/dist/esm/errors/flat/context/useFlatFieldErrorsContextOutput.mjs +1 -2
- package/dist/esm/errors/flat/getFlatFieldErrors.mjs +1 -2
- package/dist/esm/form/context/utils/RhfUtilsContextProvider.mjs +19 -19
- package/dist/esm/form/utils/useFormRequestSubmit.mjs +1 -2
- package/dist/esm/submit/useResetFormOnSubmitted.mjs +12 -13
- package/dist/esm/submit/useSubmitFormOnChange.mjs +2 -4
- package/dist/esm/utils/createContext.mjs +1 -2
- package/dist/types/client/utils.d.ts +47 -0
- package/dist/types/form/RhfUtilsFormOptions.d.ts +1 -1
- package/dist/types/submit/useResetFormOnSubmitted.d.ts +7 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
38
38
|
},
|
|
39
39
|
},
|
|
40
40
|
|
|
41
|
-
// optional (defaults to primitive HTML form)
|
|
41
|
+
// optional form component (defaults to primitive HTML form)
|
|
42
42
|
FormComponent: Form.Root,
|
|
43
43
|
|
|
44
44
|
// optional wrapper to inject your own hooks and components
|
|
@@ -49,7 +49,7 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
49
49
|
formId, // unique id string
|
|
50
50
|
formRef, // ref
|
|
51
51
|
context, // rhf UseFormReturn
|
|
52
|
-
options, // RhfUtilsFormOptions
|
|
52
|
+
options, // RhfUtilsFormOptions (with any custom props)
|
|
53
53
|
Controller, // strictly-typed controller
|
|
54
54
|
FormSubmitError, // strictly-typed error class
|
|
55
55
|
children, // RhfUtilsZodForm's Children instance
|
|
@@ -57,7 +57,9 @@ export const rhfUtilsClientConfig: RhfUtilsClientConfig = {
|
|
|
57
57
|
) => {
|
|
58
58
|
// your own hooks/behaviors
|
|
59
59
|
// (using custom option prop -- see "Extend RhfUtilsFormOptions" section)
|
|
60
|
-
useMyFormNavigationPrompt(
|
|
60
|
+
useMyFormNavigationPrompt({
|
|
61
|
+
enabled: !!options?.enableMyFormNavigationPrompt,
|
|
62
|
+
});
|
|
61
63
|
|
|
62
64
|
return (
|
|
63
65
|
<>
|
|
@@ -165,13 +167,16 @@ Currently, only `zod` is supported.
|
|
|
165
167
|
<button type="submit">Login</button>
|
|
166
168
|
</>
|
|
167
169
|
)}
|
|
168
|
-
// form-specific options/overrides
|
|
170
|
+
// form-specific options/overrides (merged with defaults)
|
|
169
171
|
rhf={{
|
|
170
172
|
mode: 'onBlur',
|
|
171
173
|
}}
|
|
172
174
|
utils={{
|
|
173
175
|
submitOnChange: true,
|
|
174
|
-
|
|
176
|
+
resetOnSubmitted: {
|
|
177
|
+
onSuccess: { values: 'current' },
|
|
178
|
+
onError: { values: 'defaults' },
|
|
179
|
+
},
|
|
175
180
|
// custom option props
|
|
176
181
|
// (see "Extend RhfUtilsFormOptions" section)
|
|
177
182
|
enableMyFormNavigationPrompt: true,
|
|
@@ -242,7 +247,10 @@ type RhfUtilsFormOptions = {
|
|
|
242
247
|
submitOnChange?: boolean;
|
|
243
248
|
|
|
244
249
|
/** Reset form after submitted. */
|
|
245
|
-
|
|
250
|
+
resetOnSubmitted?: {
|
|
251
|
+
onSuccess?: { values: 'defaults' | 'current' };
|
|
252
|
+
onError?: { values: 'defaults' };
|
|
253
|
+
};
|
|
246
254
|
|
|
247
255
|
/** Control dev tool options. (Lazy-loaded when truthy value supplied.) */
|
|
248
256
|
devTool?: true | Pick<DevtoolUIProps, 'placement' | 'styles'>;
|
|
@@ -1,52 +1,46 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { jsx as n } from "react/jsx-runtime";
|
|
2
|
+
import m from "../form/FormWithProviders.mjs";
|
|
3
|
+
import l from "../form/useRhfUtilsForm.mjs";
|
|
4
|
+
import { mergeRhfUtilsClientConfigDefaultsWithUseRhfUtilsFormProps as F } from "./utils.mjs";
|
|
5
|
+
const p = (r) => (o) => {
|
|
6
|
+
const i = f(r, o);
|
|
6
7
|
return {
|
|
7
|
-
useForm: (
|
|
8
|
-
const
|
|
8
|
+
useForm: (e, t) => {
|
|
9
|
+
const s = i(e, t);
|
|
9
10
|
return {
|
|
10
|
-
...
|
|
11
|
-
FormWithProviders: () => /* @__PURE__ */
|
|
11
|
+
...l(s),
|
|
12
|
+
FormWithProviders: () => /* @__PURE__ */ n(m, { ...s })
|
|
12
13
|
};
|
|
13
14
|
},
|
|
14
|
-
FormWithProviders:
|
|
15
|
+
FormWithProviders: u(r, o)
|
|
15
16
|
};
|
|
16
|
-
},
|
|
17
|
-
var o, i, f, l, n, h;
|
|
17
|
+
}, f = (r, o) => function(e, t) {
|
|
18
18
|
return {
|
|
19
19
|
...t,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
form: {
|
|
31
|
-
...(f = r.defaults) == null ? void 0 : f.form,
|
|
32
|
-
...t.form,
|
|
33
|
-
className: [(n = (l = r.defaults) == null ? void 0 : l.form) == null ? void 0 : n.className, (h = t.form) == null ? void 0 : h.className].filter(Boolean).join(" ")
|
|
34
|
-
}
|
|
20
|
+
...F(
|
|
21
|
+
o.defaults,
|
|
22
|
+
t,
|
|
23
|
+
{
|
|
24
|
+
rhf: {
|
|
25
|
+
resolver: r(e)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
)
|
|
35
29
|
};
|
|
36
|
-
},
|
|
37
|
-
schema:
|
|
30
|
+
}, u = (r, o) => function({
|
|
31
|
+
schema: e,
|
|
38
32
|
children: t,
|
|
39
|
-
...
|
|
33
|
+
...s
|
|
40
34
|
}) {
|
|
41
|
-
const
|
|
42
|
-
return /* @__PURE__ */
|
|
43
|
-
|
|
35
|
+
const h = f(r, o);
|
|
36
|
+
return /* @__PURE__ */ n(
|
|
37
|
+
m,
|
|
44
38
|
{
|
|
45
|
-
...
|
|
39
|
+
...h(e, s),
|
|
46
40
|
children: t
|
|
47
41
|
}
|
|
48
42
|
);
|
|
49
43
|
};
|
|
50
44
|
export {
|
|
51
|
-
|
|
45
|
+
p as createRhfUtilsClient
|
|
52
46
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const i = (l, ...t) => t.reduce(
|
|
2
|
+
(s, e) => {
|
|
3
|
+
var f, o;
|
|
4
|
+
return {
|
|
5
|
+
rhf: {
|
|
6
|
+
...s == null ? void 0 : s.rhf,
|
|
7
|
+
...e.rhf
|
|
8
|
+
},
|
|
9
|
+
utils: {
|
|
10
|
+
...s == null ? void 0 : s.utils,
|
|
11
|
+
...e.utils
|
|
12
|
+
},
|
|
13
|
+
form: {
|
|
14
|
+
...s == null ? void 0 : s.form,
|
|
15
|
+
...e.form,
|
|
16
|
+
className: [(f = s == null ? void 0 : s.form) == null ? void 0 : f.className, (o = e.form) == null ? void 0 : o.className].filter(Boolean).join(" ")
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
l
|
|
21
|
+
);
|
|
22
|
+
export {
|
|
23
|
+
i as mergeRhfUtilsClientConfigDefaultsWithUseRhfUtilsFormProps
|
|
24
|
+
};
|
|
@@ -6,8 +6,7 @@ const a = e.lazy(
|
|
|
6
6
|
default: o.DevTool
|
|
7
7
|
}))
|
|
8
8
|
), u = ({ isDevelopment: o, props: r }) => {
|
|
9
|
-
if (!o)
|
|
10
|
-
return null;
|
|
9
|
+
if (!o) return null;
|
|
11
10
|
const { control: l } = n();
|
|
12
11
|
return /* @__PURE__ */ t(e.Suspense, { fallback: null, children: /* @__PURE__ */ t(a, { control: l, ...r }) });
|
|
13
12
|
};
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import { _RhfUtilsContextProvider as
|
|
8
|
-
const
|
|
9
|
-
formId:
|
|
10
|
-
formRef:
|
|
1
|
+
import { jsx as f } from "react/jsx-runtime";
|
|
2
|
+
import u from "../../../client/config/useRhfUtilsClientConfigContext.mjs";
|
|
3
|
+
import d from "../../../errors/flat/context/useFlatFieldErrorsContextOutput.mjs";
|
|
4
|
+
import i from "../../../submit/useFormOnSubmitSuccessfulAndReset.mjs";
|
|
5
|
+
import x from "../../../submit/useResetFormOnSubmitted.mjs";
|
|
6
|
+
import O from "../../../submit/useSubmitFormOnChange.mjs";
|
|
7
|
+
import { _RhfUtilsContextProvider as h } from "./useRhfUtilsContext.mjs";
|
|
8
|
+
const P = ({
|
|
9
|
+
formId: l,
|
|
10
|
+
formRef: m,
|
|
11
11
|
onSubmitSuccessfulAndReset: t,
|
|
12
12
|
options: r,
|
|
13
|
-
children:
|
|
13
|
+
children: C
|
|
14
14
|
}) => {
|
|
15
15
|
var a;
|
|
16
|
-
const
|
|
17
|
-
return
|
|
16
|
+
const e = u();
|
|
17
|
+
return d((a = e == null ? void 0 : e.fieldErrors) == null ? void 0 : a.output), O(m, {
|
|
18
18
|
disabled: !(r != null && r.submitOnChange)
|
|
19
|
-
}),
|
|
20
|
-
(r == null ? void 0 : r.
|
|
21
|
-
onSuccess:
|
|
19
|
+
}), x(
|
|
20
|
+
(r == null ? void 0 : r.resetOnSubmitted) ?? (t && {
|
|
21
|
+
onSuccess: { values: "defaults" }
|
|
22
22
|
})
|
|
23
|
-
),
|
|
23
|
+
), i(() => {
|
|
24
24
|
t == null || t();
|
|
25
|
-
}), /* @__PURE__ */
|
|
25
|
+
}), /* @__PURE__ */ f(h, { value: { formId: l, formRef: m, options: r }, children: C });
|
|
26
26
|
};
|
|
27
27
|
export {
|
|
28
|
-
|
|
28
|
+
P as default
|
|
29
29
|
};
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { useFormContext as
|
|
2
|
-
import
|
|
3
|
-
const
|
|
4
|
-
const { reset:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const t = e[
|
|
9
|
-
if (!t)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
r(o, {
|
|
1
|
+
import { useFormContext as n } from "react-hook-form";
|
|
2
|
+
import c from "./useFormOnSubmitted.mjs";
|
|
3
|
+
const a = (e) => {
|
|
4
|
+
const { reset: u, getValues: s } = n();
|
|
5
|
+
c((o) => {
|
|
6
|
+
var r;
|
|
7
|
+
if (!e) return;
|
|
8
|
+
const t = (r = e[o ? "onSuccess" : "onError"]) == null ? void 0 : r.values;
|
|
9
|
+
if (!t) return;
|
|
10
|
+
const m = t === "current" ? s() : void 0;
|
|
11
|
+
u(m, {
|
|
13
12
|
// keep submit stuff for tracking relevant state (e.g., `useFormOnSubmitSuccessful`)
|
|
14
13
|
keepIsSubmitSuccessful: !0,
|
|
15
14
|
keepIsSubmitted: !0,
|
|
@@ -19,5 +18,5 @@ const S = (e) => {
|
|
|
19
18
|
});
|
|
20
19
|
};
|
|
21
20
|
export {
|
|
22
|
-
|
|
21
|
+
a as default
|
|
23
22
|
};
|
|
@@ -3,11 +3,9 @@ import m from "../form/utils/useFormRequestSubmit.mjs";
|
|
|
3
3
|
const d = (t, e) => {
|
|
4
4
|
const u = !(e != null && e.disabled), a = m(t);
|
|
5
5
|
c.useEffect(() => {
|
|
6
|
-
if (!u)
|
|
7
|
-
return;
|
|
6
|
+
if (!u) return;
|
|
8
7
|
const r = t.current;
|
|
9
|
-
if (!r)
|
|
10
|
-
throw new Error();
|
|
8
|
+
if (!r) throw new Error();
|
|
11
9
|
const n = () => {
|
|
12
10
|
a();
|
|
13
11
|
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { SafeFieldValues } from '../form/SafeFieldValues';
|
|
2
|
+
import { UseRhfUtilsFormProps } from '../form/UseRhfUtilsFormProps';
|
|
3
|
+
import { RhfUtilsClientConfig } from './config/RhfUtilsClientConfig';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Merge {@link RhfUtilsClientConfig.defaults} with array of {@link UseRhfUtilsFormProps}'s overrides.
|
|
7
|
+
*/
|
|
8
|
+
export declare const mergeRhfUtilsClientConfigDefaultsWithUseRhfUtilsFormProps: <TFieldValues extends SafeFieldValues, TTransformedValues extends SafeFieldValues>(defaults: RhfUtilsClientConfig['defaults'], ...props: Pick<UseRhfUtilsFormProps<TFieldValues, TTransformedValues>, 'rhf' | 'utils' | 'form'>[]) => {
|
|
9
|
+
rhf?: Pick<Partial<{
|
|
10
|
+
mode: "onChange" | "onBlur" | "onSubmit" | "onTouched" | "all";
|
|
11
|
+
disabled: boolean;
|
|
12
|
+
reValidateMode: "onChange" | "onBlur" | "onSubmit";
|
|
13
|
+
defaultValues: ((payload?: unknown) => Promise<import('react-hook-form').FieldValues>) | {
|
|
14
|
+
[x: string]: any;
|
|
15
|
+
};
|
|
16
|
+
values: import('react-hook-form').FieldValues;
|
|
17
|
+
errors: import('react-hook-form').FieldErrors<import('react-hook-form').FieldValues>;
|
|
18
|
+
resetOptions: Partial<{
|
|
19
|
+
keepDirtyValues: boolean;
|
|
20
|
+
keepErrors: boolean;
|
|
21
|
+
keepDirty: boolean;
|
|
22
|
+
keepValues: boolean;
|
|
23
|
+
keepDefaultValues: boolean;
|
|
24
|
+
keepIsSubmitted: boolean;
|
|
25
|
+
keepIsSubmitSuccessful: boolean;
|
|
26
|
+
keepTouched: boolean;
|
|
27
|
+
keepIsValidating: boolean;
|
|
28
|
+
keepIsValid: boolean;
|
|
29
|
+
keepSubmitCount: boolean;
|
|
30
|
+
}> | undefined;
|
|
31
|
+
resolver: import('react-hook-form').Resolver<import('react-hook-form').FieldValues, any>;
|
|
32
|
+
context: any;
|
|
33
|
+
shouldFocusError: boolean;
|
|
34
|
+
shouldUnregister: boolean;
|
|
35
|
+
shouldUseNativeValidation: boolean;
|
|
36
|
+
progressive: boolean;
|
|
37
|
+
criteriaMode: import('react-hook-form').CriteriaMode;
|
|
38
|
+
delayError: number;
|
|
39
|
+
}>, "shouldUnregister" | "mode" | "reValidateMode" | "resetOptions" | "context" | "shouldFocusError" | "shouldUseNativeValidation" | "progressive" | "criteriaMode" | "delayError"> | undefined;
|
|
40
|
+
utils?: {
|
|
41
|
+
stopSubmitPropagation?: boolean | undefined;
|
|
42
|
+
submitOnChange?: boolean | undefined;
|
|
43
|
+
resetOnSubmitted?: import('../submit/useResetFormOnSubmitted').UseResetFormOnSubmittedOptions;
|
|
44
|
+
devTool?: true | Pick<import('@hookform/devtools/dist/devToolUI').DevtoolUIProps, "placement" | "styles"> | undefined;
|
|
45
|
+
} | undefined;
|
|
46
|
+
form?: Omit<Omit<import('react').DetailedHTMLProps<import('react').FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, "ref">, "onSubmit"> | undefined;
|
|
47
|
+
} | undefined;
|
|
@@ -13,7 +13,7 @@ export type RhfUtilsFormOptions = {
|
|
|
13
13
|
/** Request submit via listener on form change. */
|
|
14
14
|
submitOnChange?: boolean;
|
|
15
15
|
/** Reset form after submit. */
|
|
16
|
-
|
|
16
|
+
resetOnSubmitted?: UseResetFormOnSubmittedOptions;
|
|
17
17
|
/**
|
|
18
18
|
* Control dev tool options.
|
|
19
19
|
* (Lazy-loaded when truthy value supplied.
|
|
@@ -5,14 +5,18 @@
|
|
|
5
5
|
export type UseResetFormOnSubmittedOptions = undefined | {
|
|
6
6
|
/**
|
|
7
7
|
* Reset values on submit successful.
|
|
8
|
-
* - `
|
|
8
|
+
* - `'defaults'`: reset to default values
|
|
9
9
|
* - `'current'`: reset to current values
|
|
10
10
|
*/
|
|
11
|
-
onSuccess?:
|
|
11
|
+
onSuccess?: {
|
|
12
|
+
values: 'defaults' | 'current';
|
|
13
|
+
};
|
|
12
14
|
/**
|
|
13
15
|
* Reset values on submit error. (reset to default values)
|
|
14
16
|
*/
|
|
15
|
-
onError?:
|
|
17
|
+
onError?: {
|
|
18
|
+
values: 'defaults';
|
|
19
|
+
};
|
|
16
20
|
};
|
|
17
21
|
/**
|
|
18
22
|
* Handle resetting after successful submit with no server validation errors.
|