@rpcbase/form 0.54.0 → 0.56.0
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/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -1
- package/dist/navigationGuard.d.ts +20 -0
- package/dist/navigationGuard.d.ts.map +1 -0
- package/dist/useForm.d.ts +7 -0
- package/dist/useForm.d.ts.map +1 -0
- package/dist/useFormNavigationGuard.d.ts +5 -0
- package/dist/useFormNavigationGuard.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export * from 'react-hook-form';
|
|
2
|
+
export { useForm, type UseFormPropsWithNavigationGuard } from './useForm';
|
|
3
|
+
export { DEFAULT_FORM_NAVIGATION_GUARD_MESSAGE, useFormNavigationGuard, } from './useFormNavigationGuard';
|
|
4
|
+
export { normalizeFormNavigationGuardOptions, shouldBlockFormNavigation, type FormNavigationGuardOptions, type NormalizedFormNavigationGuardOptions, } from './navigationGuard';
|
|
2
5
|
export * from './zodResolver';
|
|
3
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,OAAO,EAAE,OAAO,EAAE,KAAK,+BAA+B,EAAE,MAAM,WAAW,CAAA;AACzE,OAAO,EACL,qCAAqC,EACrC,sBAAsB,GACvB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EACL,mCAAmC,EACnC,yBAAyB,EACzB,KAAK,0BAA0B,EAC/B,KAAK,oCAAoC,GAC1C,MAAM,mBAAmB,CAAA;AAC1B,cAAc,eAAe,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
|
-
import { get, set, appendErrors } from "react-hook-form";
|
|
1
|
+
import { useFormState, useForm as useForm$1, get, set, appendErrors } from "react-hook-form";
|
|
2
2
|
export * from "react-hook-form";
|
|
3
|
+
import { useId, useMemo, useCallback } from "react";
|
|
4
|
+
import { useRegisterNavigationGuard } from "@rpcbase/router";
|
|
3
5
|
import * as n$1 from "zod/v4/core";
|
|
6
|
+
const normalizeFormNavigationGuardOptions = (input) => {
|
|
7
|
+
if (input === false) {
|
|
8
|
+
return { enabled: false, blockOnSearch: false };
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
enabled: input?.enabled ?? true,
|
|
12
|
+
message: input?.message,
|
|
13
|
+
blockOnSearch: input?.blockOnSearch ?? false,
|
|
14
|
+
priority: input?.priority
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
const shouldBlockFormNavigation = ({
|
|
18
|
+
args,
|
|
19
|
+
shouldBlockUnload,
|
|
20
|
+
blockOnSearch
|
|
21
|
+
}) => {
|
|
22
|
+
if (!shouldBlockUnload) return false;
|
|
23
|
+
if (args.currentLocation.pathname !== args.nextLocation.pathname) return true;
|
|
24
|
+
if (blockOnSearch && args.currentLocation.search !== args.nextLocation.search) return true;
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
27
|
+
const DEFAULT_FORM_NAVIGATION_GUARD_MESSAGE = "You have unsaved changes. Leave without saving?";
|
|
28
|
+
const useFormNavigationGuard = (form, opts) => {
|
|
29
|
+
const { isDirty, isSubmitting } = useFormState({ control: form.control });
|
|
30
|
+
const normalized = normalizeFormNavigationGuardOptions(opts);
|
|
31
|
+
const reactId = useId();
|
|
32
|
+
const id = useMemo(() => `rpcbase:form:${reactId}`, [reactId]);
|
|
33
|
+
const shouldBlockUnload = normalized.enabled && isDirty && !isSubmitting;
|
|
34
|
+
const shouldBlockNavigation = useCallback(
|
|
35
|
+
(args) => shouldBlockFormNavigation({
|
|
36
|
+
args,
|
|
37
|
+
shouldBlockUnload,
|
|
38
|
+
blockOnSearch: normalized.blockOnSearch
|
|
39
|
+
}),
|
|
40
|
+
[normalized.blockOnSearch, shouldBlockUnload]
|
|
41
|
+
);
|
|
42
|
+
useRegisterNavigationGuard({
|
|
43
|
+
id,
|
|
44
|
+
enabled: normalized.enabled,
|
|
45
|
+
priority: normalized.priority,
|
|
46
|
+
message: normalized.message ?? DEFAULT_FORM_NAVIGATION_GUARD_MESSAGE,
|
|
47
|
+
blockOnSearch: normalized.blockOnSearch,
|
|
48
|
+
shouldBlockNavigation,
|
|
49
|
+
shouldBlockUnload
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const useForm = (props) => {
|
|
53
|
+
const { navigationGuard, ...rhfProps } = props ?? {};
|
|
54
|
+
const form = useForm$1(
|
|
55
|
+
rhfProps
|
|
56
|
+
);
|
|
57
|
+
const normalized = normalizeFormNavigationGuardOptions(navigationGuard);
|
|
58
|
+
useFormNavigationGuard(form, normalized.enabled ? navigationGuard : { enabled: false });
|
|
59
|
+
return form;
|
|
60
|
+
};
|
|
4
61
|
const r = (t2, r2, o2) => {
|
|
5
62
|
if (t2 && "reportValidity" in t2) {
|
|
6
63
|
const s2 = get(o2, r2);
|
|
@@ -115,5 +172,10 @@ function a(o$1, a2, u) {
|
|
|
115
172
|
throw new Error("Invalid input: not a Zod schema");
|
|
116
173
|
}
|
|
117
174
|
export {
|
|
175
|
+
DEFAULT_FORM_NAVIGATION_GUARD_MESSAGE,
|
|
176
|
+
normalizeFormNavigationGuardOptions,
|
|
177
|
+
shouldBlockFormNavigation,
|
|
178
|
+
useForm,
|
|
179
|
+
useFormNavigationGuard,
|
|
118
180
|
a as zodResolver
|
|
119
181
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BlockerFunction } from '../../router/src';
|
|
2
|
+
export type FormNavigationGuardOptions = false | {
|
|
3
|
+
enabled?: boolean;
|
|
4
|
+
message?: string;
|
|
5
|
+
blockOnSearch?: boolean;
|
|
6
|
+
priority?: number;
|
|
7
|
+
};
|
|
8
|
+
export type NormalizedFormNavigationGuardOptions = {
|
|
9
|
+
enabled: boolean;
|
|
10
|
+
message?: string;
|
|
11
|
+
blockOnSearch: boolean;
|
|
12
|
+
priority?: number;
|
|
13
|
+
};
|
|
14
|
+
export declare const normalizeFormNavigationGuardOptions: (input: FormNavigationGuardOptions | undefined) => NormalizedFormNavigationGuardOptions;
|
|
15
|
+
export declare const shouldBlockFormNavigation: ({ args, shouldBlockUnload, blockOnSearch, }: {
|
|
16
|
+
args: Parameters<BlockerFunction>[0];
|
|
17
|
+
shouldBlockUnload: boolean;
|
|
18
|
+
blockOnSearch: boolean;
|
|
19
|
+
}) => boolean;
|
|
20
|
+
//# sourceMappingURL=navigationGuard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigationGuard.d.ts","sourceRoot":"","sources":["../src/navigationGuard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAGtD,MAAM,MAAM,0BAA0B,GAClC,KAAK,GACL;IACA,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAEH,MAAM,MAAM,oCAAoC,GAAG;IACjD,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,aAAa,EAAE,OAAO,CAAA;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,mCAAmC,GAC9C,OAAO,0BAA0B,GAAG,SAAS,KAC5C,oCAWF,CAAA;AAED,eAAO,MAAM,yBAAyB,GAAI,6CAIvC;IACD,IAAI,EAAE,UAAU,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAA;IACpC,iBAAiB,EAAE,OAAO,CAAA;IAC1B,aAAa,EAAE,OAAO,CAAA;CACvB,KAAG,OAKH,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FieldValues, UseFormProps, UseFormReturn } from 'react-hook-form';
|
|
2
|
+
import { FormNavigationGuardOptions } from './navigationGuard';
|
|
3
|
+
export type UseFormPropsWithNavigationGuard<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues = TFieldValues> = UseFormProps<TFieldValues, TContext, TTransformedValues> & {
|
|
4
|
+
navigationGuard?: FormNavigationGuardOptions;
|
|
5
|
+
};
|
|
6
|
+
export declare const useForm: <TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues = TFieldValues>(props?: UseFormPropsWithNavigationGuard<TFieldValues, TContext, TTransformedValues>) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
|
|
7
|
+
//# sourceMappingURL=useForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../src/useForm.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAG/E,OAAO,EAEL,KAAK,0BAA0B,EAChC,MAAM,mBAAmB,CAAA;AAG1B,MAAM,MAAM,+BAA+B,CACzC,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,IAC/B,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,GAAG;IAC7D,eAAe,CAAC,EAAE,0BAA0B,CAAA;CAC7C,CAAA;AAED,eAAO,MAAM,OAAO,GAClB,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,QAAQ,+BAA+B,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,KAClF,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAW1D,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { FieldValues, UseFormReturn } from 'react-hook-form';
|
|
2
|
+
import { FormNavigationGuardOptions } from './navigationGuard';
|
|
3
|
+
export declare const DEFAULT_FORM_NAVIGATION_GUARD_MESSAGE = "You have unsaved changes. Leave without saving?";
|
|
4
|
+
export declare const useFormNavigationGuard: <TFieldValues extends FieldValues, TContext = any, TTransformedValues = TFieldValues>(form: UseFormReturn<TFieldValues, TContext, TTransformedValues>, opts?: FormNavigationGuardOptions) => void;
|
|
5
|
+
//# sourceMappingURL=useFormNavigationGuard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormNavigationGuard.d.ts","sourceRoot":"","sources":["../src/useFormNavigationGuard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAIjE,OAAO,EAGL,KAAK,0BAA0B,EAChC,MAAM,mBAAmB,CAAA;AAG1B,eAAO,MAAM,qCAAqC,oDACC,CAAA;AAEnD,eAAO,MAAM,sBAAsB,GACjC,YAAY,SAAS,WAAW,EAChC,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,MAAM,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EAC/D,OAAO,0BAA0B,KAChC,IA6BF,CAAA"}
|