@paragrav/rhf-utils 0.0.53 → 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.
Files changed (38) hide show
  1. package/README.md +1 -0
  2. package/dist/esm/config/index.mjs +14 -0
  3. package/dist/esm/errors/orphan/useFormHasOrphanAndNoFieldErrors.mjs +1 -1
  4. package/dist/esm/errors/orphan/useOrphanFormErrorsMemo.mjs +1 -1
  5. package/dist/esm/errors/useFormErrorsIfAny.mjs +1 -1
  6. package/dist/esm/errors/useFormErrorsToConsole.mjs +1 -1
  7. package/dist/esm/exports.mjs +72 -78
  8. package/dist/esm/form/useForm.mjs +30 -23
  9. package/dist/esm/form/utils/getSubmitterButtonData.mjs +2 -1
  10. package/dist/esm/node_modules/@hookform/resolvers/zod/dist/zod.mjs +49 -0
  11. package/dist/esm/submit/useResetFormOnSubmitError.mjs +11 -11
  12. package/dist/esm/submit/useSubmitFormOnChange.mjs +14 -14
  13. package/dist/types/src/config/index.d.ts +69 -0
  14. package/dist/types/src/exports.d.ts +2 -4
  15. package/dist/types/src/form/FormWithProviders.d.ts +3 -3
  16. package/dist/types/src/form/_Form.d.ts +11 -0
  17. package/dist/types/src/form/_FormBase.d.ts +8 -0
  18. package/dist/types/src/form/_FormProviders.d.ts +12 -0
  19. package/dist/types/src/form/context/utils/FormUtilsContextProvider.d.ts +3 -6
  20. package/dist/types/src/form/useForm.d.ts +43 -11
  21. package/dist/types/src/main.d.ts +7 -1
  22. package/dist/types/src/submit/error/FormSubmitError.d.ts +8 -2
  23. package/dist/types/src/submit/error/setCtxErrorsByFormSubmitError.d.ts +9 -0
  24. package/dist/types/src/submit/useResetFormOnSubmitError.d.ts +3 -1
  25. package/dist/types/src/submit/useSubmitFormOnChange.d.ts +3 -1
  26. package/dist/types/src/utils/isEmptyObject.d.ts +2 -0
  27. package/package.json +5 -17
  28. package/dist/esm/errors/root/FormRootErrors.mjs +0 -14
  29. package/dist/esm/errors/root/FormRootErrorsFromContext.mjs +0 -11
  30. package/dist/esm/form/Form.mjs +0 -63
  31. package/dist/esm/form/FormBase.mjs +0 -10
  32. package/dist/esm/form/FormWithProviders.mjs +0 -10
  33. package/dist/esm/form/context/FormProvider.mjs +0 -25
  34. package/dist/esm/form/useFormWithProviders.mjs +0 -28
  35. package/dist/esm/submit/error/handleFormSubmitError.mjs +0 -12
  36. package/dist/esm/utils/fixedForwardRef.mjs +0 -7
  37. package/dist/esm/utils/useImperativeForwardedRef.mjs +0 -16
  38. /package/dist/esm/{isEmptyObject.mjs → utils/isEmptyObject.mjs} +0 -0
package/README.md ADDED
@@ -0,0 +1 @@
1
+ Readme
@@ -0,0 +1,14 @@
1
+ let o;
2
+ const n = (t) => {
3
+ o = t;
4
+ }, e = (t) => ({
5
+ ...o,
6
+ options: {
7
+ ...o == null ? void 0 : o.options,
8
+ ...t
9
+ }
10
+ });
11
+ export {
12
+ e as _getConfig,
13
+ n as setConfig
14
+ };
@@ -1,4 +1,4 @@
1
- import r from "../../isEmptyObject.mjs";
1
+ import r from "../../utils/isEmptyObject.mjs";
2
2
  import t from "../flat/useRefdFieldErrorsMemo.mjs";
3
3
  import m from "./useOrphanFormErrorsMemo.mjs";
4
4
  const n = (o) => {
@@ -1,5 +1,5 @@
1
1
  import "react";
2
- import t from "../../isEmptyObject.mjs";
2
+ import t from "../../utils/isEmptyObject.mjs";
3
3
  import e from "../useFormErrorsMemo.mjs";
4
4
  import m from "./getOrphansFromFieldErrors.mjs";
5
5
  const s = (r) => e((o) => {
@@ -1,5 +1,5 @@
1
1
  import { useFormState as o } from "react-hook-form";
2
- import s from "../isEmptyObject.mjs";
2
+ import s from "../utils/isEmptyObject.mjs";
3
3
  const t = () => {
4
4
  const { errors: r } = o();
5
5
  return s(r) ? !1 : r;
@@ -1,6 +1,6 @@
1
1
  import a from "react";
2
2
  import { useFormContext as l, useFormState as E } from "react-hook-form";
3
- import c from "../isEmptyObject.mjs";
3
+ import c from "../utils/isEmptyObject.mjs";
4
4
  import d from "./outputErrors.mjs";
5
5
  const h = (e, f, r) => {
6
6
  const { getValues: m } = l(), { errors: u } = E();
@@ -1,80 +1,74 @@
1
- import { default as e } from "./devtool/LazyDevTool.mjs";
2
- import { default as s } from "./errors/context/index.mjs";
3
- import { default as u } from "./errors/context/provider.mjs";
4
- import { default as f } from "./errors/message/FormErrorMessage.mjs";
5
- import { default as d } from "./errors/message/FormErrorMessageByPath.mjs";
6
- import { default as x } from "./errors/nonfield/FormNonFieldErrorMarker.mjs";
7
- import { default as i } from "./errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.mjs";
8
- import { default as E } from "./errors/orphan/useFormHasOrphanAndNoFieldErrors.mjs";
9
- import { default as S } from "./errors/orphan/useOrphanFormErrorsMemo.mjs";
10
- import { default as c } from "./errors/orphan/useOrphanFormErrorsToConsole.mjs";
11
- import { default as P } from "./errors/root/useRootFormFieldErrorsMemo.mjs";
12
- import { default as G } from "./errors/useFormErrorConsoleDebug.mjs";
13
- import { default as O } from "./errors/useFormErrorsEffect.mjs";
14
- import { default as I } from "./errors/useFormErrorsIfAny.mjs";
15
- import { default as k } from "./errors/useFormErrorsMemo.mjs";
16
- import { default as B } from "./errors/useFormRequestSubmit.mjs";
17
- import { default as R } from "./form/Form.mjs";
18
- import { default as N } from "./form/FormWithProviders.mjs";
19
- import { default as U } from "./form/useForm.mjs";
20
- import { default as q } from "./form/useFormWithProviders.mjs";
21
- import { default as L } from "./form/context/FormProvider.mjs";
22
- import { default as w } from "./form/context/utils/FormUtilsContextProvider.mjs";
23
- import { default as K } from "./form/context/utils/useFormUtilsContext.mjs";
24
- import { default as V } from "./form/context/group/FormGroupContextProvider.mjs";
25
- import { default as Y } from "./form/context/group/useFormGroupChildIsMountedTracker.mjs";
26
- import { default as _ } from "./form/context/group/useFormGroupChildIsSubmittingTracker.mjs";
27
- import { default as rr } from "./form/context/group/useFormGroupChildTracker.mjs";
28
- import { default as er } from "./form/context/group/useFormGroupIsAnyBusy.mjs";
29
- import { default as sr } from "./form/context/group/useFormGroupIsChildBusy.mjs";
30
- import { default as ur } from "./form/context/group/useFormGroupIsParentBusy.mjs";
31
- import { default as fr } from "./form/context/group/useFormGroupParentTracker.mjs";
32
- import { default as dr } from "./form/context/group/useFormOrParentIsBusy.mjs";
33
- import { default as xr } from "./form/utils/getSubmitterButtonData.mjs";
34
- import { FormSubmitError as ir } from "./submit/error/FormSubmitError.mjs";
35
- import { default as Er } from "./submit/useFormOnSubmitSuccessful.mjs";
36
- import { default as Sr } from "./submit/useFormOnSubmitSuccessfulAndReset.mjs";
37
- import { default as cr } from "./submit/useResetFormOnSubmitError.mjs";
38
- import { default as Pr } from "./submit/useResetFormOnSubmitSuccessful.mjs";
39
- import { default as Gr } from "./submit/useSubmitFormOnChange.mjs";
1
+ import { setConfig as e } from "./config/index.mjs";
2
+ import { default as s } from "./devtool/LazyDevTool.mjs";
3
+ import { default as a } from "./errors/context/index.mjs";
4
+ import { default as f } from "./errors/context/provider.mjs";
5
+ import { default as d } from "./errors/message/FormErrorMessage.mjs";
6
+ import { default as x } from "./errors/message/FormErrorMessageByPath.mjs";
7
+ import { default as n } from "./errors/nonfield/FormNonFieldErrorMarker.mjs";
8
+ import { default as E } from "./errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.mjs";
9
+ import { default as S } from "./errors/orphan/useFormHasOrphanAndNoFieldErrors.mjs";
10
+ import { default as c } from "./errors/orphan/useOrphanFormErrorsMemo.mjs";
11
+ import { default as y } from "./errors/orphan/useOrphanFormErrorsToConsole.mjs";
12
+ import { default as M } from "./errors/root/useRootFormFieldErrorsMemo.mjs";
13
+ import { default as g } from "./errors/useFormErrorConsoleDebug.mjs";
14
+ import { default as P } from "./errors/useFormErrorsEffect.mjs";
15
+ import { default as A } from "./errors/useFormErrorsIfAny.mjs";
16
+ import { default as T } from "./errors/useFormErrorsMemo.mjs";
17
+ import { default as v } from "./errors/useFormRequestSubmit.mjs";
18
+ import { default as N } from "./form/useForm.mjs";
19
+ import { default as U } from "./form/context/utils/FormUtilsContextProvider.mjs";
20
+ import { default as z } from "./form/context/utils/useFormUtilsContext.mjs";
21
+ import { default as j } from "./form/context/group/FormGroupContextProvider.mjs";
22
+ import { default as J } from "./form/context/group/useFormGroupChildIsMountedTracker.mjs";
23
+ import { default as Q } from "./form/context/group/useFormGroupChildIsSubmittingTracker.mjs";
24
+ import { default as W } from "./form/context/group/useFormGroupChildTracker.mjs";
25
+ import { default as Y } from "./form/context/group/useFormGroupIsAnyBusy.mjs";
26
+ import { default as _ } from "./form/context/group/useFormGroupIsChildBusy.mjs";
27
+ import { default as rr } from "./form/context/group/useFormGroupIsParentBusy.mjs";
28
+ import { default as er } from "./form/context/group/useFormGroupParentTracker.mjs";
29
+ import { default as sr } from "./form/context/group/useFormOrParentIsBusy.mjs";
30
+ import { default as ar } from "./form/utils/getSubmitterButtonData.mjs";
31
+ import { FormSubmitError as fr } from "./submit/error/FormSubmitError.mjs";
32
+ import { default as dr } from "./submit/useFormOnSubmitSuccessful.mjs";
33
+ import { default as xr } from "./submit/useFormOnSubmitSuccessfulAndReset.mjs";
34
+ import { default as nr } from "./submit/useResetFormOnSubmitError.mjs";
35
+ import { default as Er } from "./submit/useResetFormOnSubmitSuccessful.mjs";
36
+ import { default as Sr } from "./submit/useSubmitFormOnChange.mjs";
40
37
  export {
41
- R as Form,
42
- f as FormErrorMessage,
43
- d as FormErrorMessageByPath,
44
- u as FormErrorsFlatContextProvider,
45
- V as FormGroupContextProvider,
46
- x as FormNonFieldErrorMarker,
47
- i as FormNonFieldErrorMarkerHtmlAttribute,
48
- L as FormProvider,
49
- ir as FormSubmitError,
50
- w as FormUtilsContextProvider,
51
- N as FormWithProviders,
52
- e as LazyDevTool,
53
- xr as getSubmitterButtonData,
54
- U as useForm,
55
- G as useFormErrorConsoleDebug,
56
- O as useFormErrorsEffect,
57
- s as useFormErrorsFlatContext,
58
- I as useFormErrorsIfAny,
59
- k as useFormErrorsMemo,
60
- Y as useFormGroupChildIsMountedTracker,
61
- _ as useFormGroupChildIsSubmittingTracker,
62
- rr as useFormGroupChildTracker,
63
- er as useFormGroupIsAnyBusy,
64
- sr as useFormGroupIsChildBusy,
65
- ur as useFormGroupIsParentBusy,
66
- fr as useFormGroupParentTracker,
67
- E as useFormHasOrphanAndNoFieldErrors,
68
- Er as useFormOnSubmitSuccessful,
69
- Sr as useFormOnSubmitSuccessfulAndReset,
70
- dr as useFormOrParentIsBusy,
71
- B as useFormRequestSubmit,
72
- K as useFormUtilsContext,
73
- q as useFormWithProviders,
74
- S as useOrphanFormErrorsMemo,
75
- c as useOrphanFormErrorsToConsole,
76
- Pr as useResetFormAfterSubmitSuccessful,
77
- cr as useResetFormOnSubmitError,
78
- P as useRootFormFieldErrorsMemo,
79
- Gr as useSubmitFormOnChange
38
+ d as FormErrorMessage,
39
+ x as FormErrorMessageByPath,
40
+ f as FormErrorsFlatContextProvider,
41
+ j as FormGroupContextProvider,
42
+ n as FormNonFieldErrorMarker,
43
+ E as FormNonFieldErrorMarkerHtmlAttribute,
44
+ fr as FormSubmitError,
45
+ U as FormUtilsContextProvider,
46
+ s as LazyDevTool,
47
+ ar as getSubmitterButtonData,
48
+ e as setConfig,
49
+ N as useForm,
50
+ g as useFormErrorConsoleDebug,
51
+ P as useFormErrorsEffect,
52
+ a as useFormErrorsFlatContext,
53
+ A as useFormErrorsIfAny,
54
+ T as useFormErrorsMemo,
55
+ J as useFormGroupChildIsMountedTracker,
56
+ Q as useFormGroupChildIsSubmittingTracker,
57
+ W as useFormGroupChildTracker,
58
+ Y as useFormGroupIsAnyBusy,
59
+ _ as useFormGroupIsChildBusy,
60
+ rr as useFormGroupIsParentBusy,
61
+ er as useFormGroupParentTracker,
62
+ S as useFormHasOrphanAndNoFieldErrors,
63
+ dr as useFormOnSubmitSuccessful,
64
+ xr as useFormOnSubmitSuccessfulAndReset,
65
+ sr as useFormOrParentIsBusy,
66
+ v as useFormRequestSubmit,
67
+ z as useFormUtilsContext,
68
+ c as useOrphanFormErrorsMemo,
69
+ y as useOrphanFormErrorsToConsole,
70
+ Er as useResetFormAfterSubmitSuccessful,
71
+ nr as useResetFormOnSubmitError,
72
+ M as useRootFormFieldErrorsMemo,
73
+ Sr as useSubmitFormOnChange
80
74
  };
@@ -1,29 +1,36 @@
1
- import { jsx as n } from "react/jsx-runtime";
1
+ import { zodResolver as f } from "../node_modules/@hookform/resolvers/zod/dist/zod.mjs";
2
2
  import i from "react";
3
3
  import { useForm as u } from "react-hook-form";
4
- import F from "../utils/useImperativeForwardedRef.mjs";
5
- import c from "./Form.mjs";
6
- const f = ({
7
- rhf: e,
8
- form: o
4
+ import c from "../submit/useSubmitFormOnChange.mjs";
5
+ import { _getConfig as d } from "../config/index.mjs";
6
+ const a = ({
7
+ schema: t,
8
+ rhf: m,
9
+ form: o,
10
+ utils: p
9
11
  }) => {
10
- const r = i.useId(), m = u(e), t = F();
11
- return {
12
- id: r,
13
- ref: t,
14
- context: m,
15
- Form: ({ children: s }) => /* @__PURE__ */ n(
16
- c,
17
- {
18
- formId: r,
19
- formRef: t,
20
- onSubmit: o == null ? void 0 : o.onSubmit,
21
- ...o,
22
- children: s
23
- }
24
- )
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,
15
+ ...m
16
+ }), r = i.useRef(null);
17
+ return c(r, { disabled: !e.options.submitOnChange }), {
18
+ id: s,
19
+ ref: r,
20
+ context: n,
21
+ providerProps: {
22
+ formId: s,
23
+ formRef: r,
24
+ rhfForm: n,
25
+ utils: p
26
+ },
27
+ formProps: {
28
+ formId: s,
29
+ formRef: r,
30
+ form: o
31
+ }
25
32
  };
26
- }, b = f;
33
+ }, h = a;
27
34
  export {
28
- b as default
35
+ h as default
29
36
  };
@@ -1,6 +1,7 @@
1
1
  import "react";
2
2
  const e = (t) => {
3
- if (t.nativeEvent instanceof SubmitEvent && (t.nativeEvent.submitter instanceof HTMLButtonElement || t.nativeEvent.submitter instanceof HTMLInputElement) && t.nativeEvent.submitter.name)
3
+ if (t.nativeEvent instanceof SubmitEvent && !(!(t.nativeEvent.submitter instanceof HTMLButtonElement) && // not button
4
+ !(t.nativeEvent.submitter instanceof HTMLInputElement)) && t.nativeEvent.submitter.name)
4
5
  return [
5
6
  t.nativeEvent.submitter.name,
6
7
  t.nativeEvent.submitter.value
@@ -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,18 +1,18 @@
1
- import i from "react";
2
- import { useFormContext as m, useFormState as n } from "react-hook-form";
3
- const f = (t) => {
4
- const { reset: r } = m(), { isSubmitting: e, isSubmitSuccessful: u, isSubmitted: o, submitCount: s } = n();
5
- i.useEffect(() => {
6
- t || e || u || o && s !== 0 && r(void 0);
1
+ import f from "react";
2
+ import { useFormContext as n, useFormState as o } from "react-hook-form";
3
+ const c = (t) => {
4
+ const e = !!(t != null && t.disabled), { reset: r } = n(), { isSubmitting: u, isSubmitSuccessful: s, isSubmitted: i, submitCount: m } = o();
5
+ f.useEffect(() => {
6
+ e || u || s || i && m !== 0 && r();
7
7
  }, [
8
- t,
9
8
  e,
10
9
  u,
11
- o,
10
+ s,
11
+ i,
12
12
  r,
13
- s
13
+ m
14
14
  ]);
15
- }, b = f;
15
+ }, a = c;
16
16
  export {
17
- b as default
17
+ a as default
18
18
  };
@@ -1,21 +1,21 @@
1
- import s from "react";
2
- import u from "../errors/useFormRequestSubmit.mjs";
3
- const m = (t, n) => {
4
- const o = u(t);
5
- s.useEffect(() => {
6
- if (n)
1
+ import m from "react";
2
+ import s from "../errors/useFormRequestSubmit.mjs";
3
+ const a = (t, e) => {
4
+ const u = s(t);
5
+ m.useEffect(() => {
6
+ if (e != null && e.disabled)
7
7
  return;
8
- const e = t.current;
9
- if (!e)
8
+ const r = t.current;
9
+ if (!r)
10
10
  throw new Error();
11
- const r = () => {
12
- o();
11
+ const n = () => {
12
+ u();
13
13
  };
14
- return e.addEventListener("change", r), () => {
15
- e.removeEventListener("change", r);
14
+ return r.addEventListener("change", n), () => {
15
+ r.removeEventListener("change", n);
16
16
  };
17
17
  }, []);
18
- }, a = m;
18
+ }, i = a;
19
19
  export {
20
- a as default
20
+ i as default
21
21
  };
@@ -0,0 +1,69 @@
1
+ import { FormSubmitError } from '../submit/error/FormSubmitError';
2
+ import { UseFormFieldsProps, UseFormOptions } from '../form/useForm';
3
+ import { FieldValues, UseFormProps, UseFormStateReturn } from 'react-hook-form';
4
+
5
+ type Config = {
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
+ */
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>;
19
+ /**
20
+ * Handle non-{@link FormSubmitError} errors. Optionally, return {@link FormSubmitError} to merge into context errors.
21
+ */
22
+ onSubmitError?: (error: unknown) => FormSubmitError | undefined;
23
+ Prompter?: React.FC<UseFormStateReturn<FieldValues>>;
24
+ };
25
+ export declare const setConfig: (config: Config) => void;
26
+ export declare const _getConfig: (options?: UseFormOptions) => {
27
+ options: {
28
+ hideRootErrorsList?: boolean | undefined;
29
+ submitOnChange?: boolean | undefined;
30
+ resetValuesOnSubmitError?: boolean | undefined;
31
+ stopSubmitPropagation?: boolean | undefined;
32
+ devTool?: import('@hookform/devtools/dist/devToolUI').DevtoolUIProps | "disabled" | undefined;
33
+ };
34
+ rhf?: Pick<Partial<{
35
+ mode: "onBlur" | "onChange" | "onSubmit" | "onTouched" | "all";
36
+ disabled: boolean;
37
+ reValidateMode: "onBlur" | "onChange" | "onSubmit";
38
+ defaultValues: {
39
+ [x: string]: any;
40
+ } | ((payload?: unknown) => Promise<FieldValues>);
41
+ values: FieldValues;
42
+ errors: import('react-hook-form').FieldErrors<FieldValues>;
43
+ resetOptions: Partial<{
44
+ keepDirtyValues: boolean;
45
+ keepErrors: boolean;
46
+ keepDirty: boolean;
47
+ keepValues: boolean;
48
+ keepDefaultValues: boolean;
49
+ keepIsSubmitted: boolean;
50
+ keepIsSubmitSuccessful: boolean;
51
+ keepTouched: boolean;
52
+ keepIsValidating: boolean;
53
+ keepIsValid: boolean;
54
+ keepSubmitCount: boolean;
55
+ }> | undefined;
56
+ resolver: import('react-hook-form').Resolver<FieldValues, any>;
57
+ context: any;
58
+ shouldFocusError: boolean;
59
+ shouldUnregister: boolean;
60
+ shouldUseNativeValidation: boolean;
61
+ progressive: boolean;
62
+ criteriaMode: import('react-hook-form').CriteriaMode;
63
+ delayError: number;
64
+ }>, "mode" | "reValidateMode" | "resetOptions" | "context" | "shouldFocusError" | "shouldUnregister" | "shouldUseNativeValidation" | "progressive" | "criteriaMode" | "delayError"> | undefined;
65
+ Children?: import('react').FC<UseFormFieldsProps> | undefined;
66
+ onSubmitError?: ((error: unknown) => FormSubmitError | undefined) | undefined;
67
+ Prompter?: import('react').FC<UseFormStateReturn<FieldValues>> | undefined;
68
+ };
69
+ export {};
@@ -1,3 +1,4 @@
1
+ export { setConfig } from './config';
1
2
  export { default as LazyDevTool } from './devtool/LazyDevTool';
2
3
  export { default as useFormErrorsFlatContext } from './errors/context';
3
4
  export { default as FormErrorsFlatContextProvider } from './errors/context/provider';
@@ -14,11 +15,8 @@ export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
14
15
  export { default as useFormErrorsIfAny } from './errors/useFormErrorsIfAny';
15
16
  export { default as useFormErrorsMemo } from './errors/useFormErrorsMemo';
16
17
  export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
17
- export { default as Form } from './form/Form';
18
- export { default as FormWithProviders } from './form/FormWithProviders';
18
+ export type { UseFormFieldsProps, UseFormOptions } from './form/useForm';
19
19
  export { default as useForm } from './form/useForm';
20
- export { default as useFormWithProviders } from './form/useFormWithProviders';
21
- export { default as FormProvider } from './form/context/FormProvider';
22
20
  export { default as FormUtilsContextProvider } from './form/context/utils/FormUtilsContextProvider';
23
21
  export { default as useFormUtilsContext } from './form/context/utils/useFormUtilsContext';
24
22
  export { default as FormGroupContextProvider } from './form/context/group/FormGroupContextProvider';
@@ -1,13 +1,13 @@
1
1
  import { UseFormProps } from './useForm';
2
+ import { z } from 'zod';
2
3
  import { FieldValues } from 'react-hook-form';
3
4
 
4
- export type FormWithProviders<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = React.PropsWithChildren<UseFormProps<TFieldValues, TTransformedValues>>;
5
5
  /**
6
- * {@link FormProvider} (and utils) with immediate descendent {@link Form}.
6
+ * Form providers (with utils) with immediate descendent {@link Form}.
7
7
  * (Syntactic sugar for {@link useForm}. Use this for most applications.)
8
8
  *
9
9
  * Use {@link useForm} if you need to specific nesting hierarchy.
10
10
  * (e.g., wrapping `<tr>` with provider and nesting `<form>` within `<td>` to appease HTML rules.)
11
11
  */
12
- declare function FormWithProviders<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ children, ...props }: FormWithProviders<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
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;
13
13
  export default FormWithProviders;
@@ -0,0 +1,11 @@
1
+ import { UseFormProps } from './useForm';
2
+ import { z } from 'zod';
3
+ import { FieldValues } from 'react-hook-form';
4
+ import { default as React } from 'react';
5
+
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'> & {
7
+ formId: string;
8
+ formRef: React.RefObject<HTMLFormElement>;
9
+ }>;
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;
11
+ export default _Form;
@@ -0,0 +1,8 @@
1
+ import { default as React } from 'react';
2
+
3
+ export type FormBaseProps = React.PropsWithoutRef<JSX.IntrinsicElements['form']> & {
4
+ formRef: React.RefObject<HTMLFormElement>;
5
+ antiCsrfToken?: string;
6
+ };
7
+ declare const _FormBase: ({ formRef, antiCsrfToken, children, ...props }: FormBaseProps) => import("react/jsx-runtime").JSX.Element;
8
+ export default _FormBase;
@@ -0,0 +1,12 @@
1
+ import { FormUtilsContextProviderProps } from './context/utils/FormUtilsContextProvider';
2
+ import { z } from 'zod';
3
+ import { FieldValues, UseFormReturn } from 'react-hook-form';
4
+
5
+ export type _FormProvidersProps<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = React.PropsWithChildren<{
6
+ formId: string;
7
+ formRef: React.RefObject<HTMLFormElement>;
8
+ rhfForm: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
9
+ utils?: FormUtilsContextProviderProps;
10
+ }>;
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
+ export default _FormProviders;
@@ -1,14 +1,11 @@
1
- import { FormProps } from '../../Form';
2
- import { FieldValues } from 'react-hook-form';
3
-
1
+ /// <reference types="react" />
4
2
  export type FormUtilsContextProviderProps = {
5
3
  /** Callback after successful submit, and form reset. */
6
4
  onSubmitSuccessfulAndReset?: () => void;
7
5
  };
8
- type Props<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = React.PropsWithChildren<{
6
+ type Props = React.PropsWithChildren<{
9
7
  formId: string;
10
8
  formRef: React.RefObject<HTMLFormElement>;
11
- formProps?: Omit<FormProps<TFieldValues, TTransformedValues>, 'formRef' | 'formId'>;
12
9
  } & FormUtilsContextProviderProps>;
13
- declare function FormUtilsContextProvider<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ formRef, onSubmitSuccessfulAndReset, children, }: Props<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
10
+ declare function FormUtilsContextProvider({ formRef, onSubmitSuccessfulAndReset, children, }: Props): import("react/jsx-runtime").JSX.Element;
14
11
  export default FormUtilsContextProvider;
@@ -1,16 +1,45 @@
1
- import { FormProps } from './Form';
2
- import { FieldValues, UseFormProps as RhfUseFormProps } from 'react-hook-form';
1
+ import { FormUtilsContextProviderProps } from './context/utils/FormUtilsContextProvider';
2
+ import { _FormProvidersProps } from './_FormProviders';
3
+ import { _FormProps } from './_Form';
4
+ import { z } from 'zod';
5
+ import { FieldValues, SubmitErrorHandler, UseFormProps as RhfUseFormProps, UseFormReturn } from 'react-hook-form';
3
6
  import { default as React } from 'react';
7
+ import { DevtoolUIProps } from '@hookform/devtools/dist/devToolUI';
4
8
 
5
- export type UseFormProps<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = {
9
+ export type UseFormOptions = {
10
+ hideRootErrorsList?: boolean;
11
+ /** Request submit via listener on form change. */
12
+ submitOnChange?: boolean;
13
+ /** Reset form values if submit throw errors. Use case: optimistic UI toggle form that submits on each change */
14
+ resetValuesOnSubmitError?: boolean;
15
+ stopSubmitPropagation?: boolean;
16
+ devTool?: DevtoolUIProps | 'disabled';
17
+ };
18
+ export type UseFormFieldsProps<TFieldValues extends FieldValues = FieldValues, TTransformedValues extends FieldValues = FieldValues> = {
19
+ formId: string;
20
+ formRef: React.RefObject<HTMLFormElement>;
21
+ ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
22
+ options?: UseFormOptions;
23
+ };
24
+ export type UseFormProps<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = {
25
+ schema?: TSchema;
6
26
  /** {@link RhfUseFormProps} props for RHF's {@link useRhfForm}. */
7
27
  rhf?: RhfUseFormProps<TFieldValues>;
8
28
  /** {@link FormProps} props for Form. */
9
- form?: Omit<FormProps<TFieldValues, TTransformedValues>, 'formId' | 'formRef'>;
29
+ form?: {
30
+ Fields?: React.FC<UseFormFieldsProps<TFieldValues, TTransformedValues>>;
31
+ onSubmit?: (values: TTransformedValues, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => unknown | Promise<unknown>;
32
+ /** Submit error handler. (Only for errors not of instance `FormError`, which are handled internally.) */
33
+ onSubmitError?: (error: unknown, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => void;
34
+ /** Submit handler when form is invalid. */
35
+ onSubmitInvalid?: SubmitErrorHandler<TFieldValues>;
36
+ options?: UseFormOptions;
37
+ props?: React.PropsWithoutRef<JSX.IntrinsicElements['form']>;
38
+ };
39
+ utils?: FormUtilsContextProviderProps;
10
40
  };
11
- export type UseFormReturn = ReturnType<typeof useForm>;
12
41
  /**
13
- * Base Rhf Form hook. Use `Form` in `index.ts` for most cases.
42
+ * Core Rhf Form hook. Use `Form` in `index.ts` for most cases.
14
43
  * Use this if you need to separate form components.
15
44
  * e.g., a form for table row due to `form` being considered invalid HTML around `tr` tags.
16
45
  * With this hook, you can wrap the `tr` tag with `form.Provider` and use `form.Form` inside
@@ -18,12 +47,15 @@ export type UseFormReturn = ReturnType<typeof useForm>;
18
47
  * see https://caniuse.com/?search=form%20attribute
19
48
  * @returns Form provider and form.
20
49
  */
21
- declare const useForm: <TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ rhf, form, }: UseFormProps<TFieldValues, TTransformedValues>) => {
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>) => {
22
55
  readonly id: string;
23
56
  readonly ref: React.RefObject<HTMLFormElement>;
24
- readonly context: import('react-hook-form').UseFormReturn<TFieldValues, unknown, TTransformedValues>;
25
- readonly Form: React.FC<{
26
- children?: React.ReactNode;
27
- }>;
57
+ readonly context: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
58
+ readonly providerProps: _FormProvidersProps<TSchema, TFieldValues, TTransformedValues>;
59
+ readonly formProps: _FormProps<TSchema, TFieldValues, TTransformedValues>;
28
60
  };
29
61
  export default useForm;
@@ -1 +1,7 @@
1
- export {};
1
+ import { default as React } from 'react';
2
+
3
+ type Props = {
4
+ error: boolean;
5
+ };
6
+ declare const ToggleFormSpec: React.FC<Props>;
7
+ export default ToggleFormSpec;
@@ -1,6 +1,12 @@
1
1
  import { FieldPath, FieldValues } from 'react-hook-form';
2
2
 
3
3
  export declare class FormSubmitError<TFieldValues extends FieldValues = FieldValues> extends Error {
4
- errors: Partial<Record<FieldPath<TFieldValues> | `root.${string}`, string>>;
5
- constructor(errors: Partial<Record<FieldPath<TFieldValues> | `root.${string}`, string>>, message?: string);
4
+ errors: Record<FieldPath<TFieldValues> | `root.${string}`, {
5
+ type?: string;
6
+ message: string;
7
+ }>;
8
+ constructor(errors: Record<FieldPath<TFieldValues> | `root.${string}`, {
9
+ type?: string;
10
+ message: string;
11
+ }>, message?: string);
6
12
  }
@@ -0,0 +1,9 @@
1
+ import { FormSubmitError } from './FormSubmitError';
2
+ import { FieldValues, UseFormReturn } from 'react-hook-form';
3
+
4
+ /**
5
+ * Handle thrown {@link FormSubmitError}.
6
+ * Add key/values as name/message to RHF context errors.
7
+ */
8
+ declare const setCtxErrorsByFormSubmitError: <TFieldValues extends FieldValues, TTransformedValues extends FieldValues>(ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>, error: FormSubmitError<any>) => void;
9
+ export default setCtxErrorsByFormSubmitError;
@@ -3,5 +3,7 @@
3
3
  * See `Rules` section: https://react-hook-form.com/api/useform/reset/
4
4
  * Specifically: "- It's recommended to reset inside useEffect after submission."
5
5
  */
6
- declare const useResetFormOnSubmitError: (disabled?: boolean) => void;
6
+ declare const useResetFormOnSubmitError: (options?: {
7
+ disabled?: boolean;
8
+ }) => void;
7
9
  export default useResetFormOnSubmitError;
@@ -1,4 +1,6 @@
1
1
  import { default as React } from 'react';
2
2
 
3
- declare const useSubmitFormOnChange: (formRef: React.RefObject<HTMLFormElement>, disabled?: boolean) => void;
3
+ declare const useSubmitFormOnChange: (formRef: React.RefObject<HTMLFormElement>, options?: {
4
+ disabled?: boolean;
5
+ }) => void;
4
6
  export default useSubmitFormOnChange;
@@ -0,0 +1,2 @@
1
+ declare const isEmptyObject: (obj: Record<string, unknown>) => boolean;
2
+ export default isEmptyObject;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@paragrav/rhf-utils",
3
3
  "author": "paragrav.dev",
4
4
  "license": "MIT",
5
- "version": "0.0.53",
5
+ "version": "0.0.57",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,
@@ -21,6 +21,10 @@
21
21
  "default": "./dist/esm/exports.mjs",
22
22
  "types": "./dist/types/exports.d.ts"
23
23
  },
24
+ "./setConfig": {
25
+ "default": "./dist/esm/setConfig.mjs",
26
+ "types": "./dist/types/setConfig.d.ts"
27
+ },
24
28
  "./LazyDevTool": {
25
29
  "default": "./dist/esm/devtool/LazyDevTool.mjs",
26
30
  "types": "./dist/types/devtool/LazyDevTool.d.ts"
@@ -77,26 +81,10 @@
77
81
  "default": "./dist/esm/errors/root/useRootFormFieldErrors.mjs",
78
82
  "types": "./dist/types/errors/root/useRootFormFieldErrors.d.ts"
79
83
  },
80
- "./form/Form": {
81
- "default": "./dist/esm/form/Form.mjs",
82
- "types": "./dist/types/form/Form.d.ts"
83
- },
84
- "./form/context/FormWithProviders": {
85
- "default": "./dist/esm/form/context/FormWithProviders.mjs",
86
- "types": "./dist/types/form/context/FormWithProviders.d.ts"
87
- },
88
84
  "./form/useForm": {
89
85
  "default": "./dist/esm/form/useForm.mjs",
90
86
  "types": "./dist/types/form/useForm.d.ts"
91
87
  },
92
- "./form/useFormWithProviders": {
93
- "default": "./dist/esm/form/useFormWithProviders.mjs",
94
- "types": "./dist/types/form/useFormWithProviders.d.ts"
95
- },
96
- "./form/context/FormProvider": {
97
- "default": "./dist/esm/form/context/FormProvider.mjs",
98
- "types": "./dist/types/form/context/FormProvider.d.ts"
99
- },
100
88
  "./form/context/FormUtilsContextProvider": {
101
89
  "default": "./dist/esm/form/context/FormUtilsContextProvider.mjs",
102
90
  "types": "./dist/types/form/context/FormUtilsContextProvider.d.ts"
@@ -1,14 +0,0 @@
1
- import { jsx as t } from "react/jsx-runtime";
2
- import i from "../../isEmptyObject.mjs";
3
- const l = ({ errors: r, ...e }) => i(r) ? null : /* @__PURE__ */ t("ul", { ...e, children: Object.values(r).map((o) => /* @__PURE__ */ t(
4
- "li",
5
- {
6
- role: "alert",
7
- "aria-live": "polite",
8
- children: o.message
9
- },
10
- JSON.stringify(o)
11
- )) }), n = l;
12
- export {
13
- n as default
14
- };
@@ -1,11 +0,0 @@
1
- import { jsx as t } from "react/jsx-runtime";
2
- import { useFormErrorsFlatContextMaybe as s } from "../context/index.mjs";
3
- import m from "./FormRootErrors.mjs";
4
- const u = () => {
5
- var o;
6
- const r = (o = s()) == null ? void 0 : o.roots;
7
- return r ? /* @__PURE__ */ t(m, { errors: r }) : null;
8
- };
9
- export {
10
- u as default
11
- };
@@ -1,63 +0,0 @@
1
- import { jsxs as C, jsx as t } from "react/jsx-runtime";
2
- import "react";
3
- import { useFormContext as O } from "react-hook-form";
4
- import v from "../devtool/LazyDevTool.mjs";
5
- import T from "../errors/root/FormRootErrorsFromContext.mjs";
6
- import V from "../submit/error/handleFormSubmitError.mjs";
7
- import j from "../submit/useResetFormOnSubmitError.mjs";
8
- import w from "../submit/useSubmitFormOnChange.mjs";
9
- import E from "../utils/useImperativeForwardedRef.mjs";
10
- import P from "./FormBase.mjs";
11
- function G({
12
- formId: u,
13
- formRef: h,
14
- Fields: f,
15
- //
16
- onSubmit: d,
17
- onSubmitError: l,
18
- onSubmitInvalid: F,
19
- //
20
- options: r,
21
- //
22
- children: x,
23
- ...g
24
- }) {
25
- const m = O(), c = E(h);
26
- w(c, !(r != null && r.submitOnChange)), j(!(r != null && r.resetValuesOnSubmitError));
27
- const R = async (e) => {
28
- r != null && r.stopSubmitPropagation && e.stopPropagation();
29
- try {
30
- await m.handleSubmit(b, F)(e);
31
- } catch (a) {
32
- y(a, e);
33
- }
34
- }, b = (e, a) => d == null ? void 0 : d(
35
- e,
36
- m,
37
- a
38
- ), y = (e, a) => {
39
- V(e, m), l == null || l(e, m, a);
40
- };
41
- return /* @__PURE__ */ C(
42
- P,
43
- {
44
- id: u,
45
- onSubmit: R,
46
- noValidate: !0,
47
- style: {
48
- position: "relative"
49
- },
50
- ...g,
51
- ref: c,
52
- children: [
53
- (r == null ? void 0 : r.devTool) !== "disabled" && /* @__PURE__ */ t(v, { placement: "top-right", ...r == null ? void 0 : r.devTool }),
54
- f && /* @__PURE__ */ t(f, { ctx: m }),
55
- x,
56
- !(r != null && r.hideRootErrors) && /* @__PURE__ */ t(T, {})
57
- ]
58
- }
59
- );
60
- }
61
- export {
62
- G as default
63
- };
@@ -1,10 +0,0 @@
1
- import { jsxs as m, jsx as s } from "react/jsx-runtime";
2
- import "react";
3
- import a from "../utils/fixedForwardRef.mjs";
4
- const f = ({ antiCsrfToken: r, children: o, ...e }, t) => /* @__PURE__ */ m("form", { role: "form", ...e, ref: t, children: [
5
- r && /* @__PURE__ */ s("input", { type: "hidden", name: "anticsrftoken", value: r }),
6
- o
7
- ] }), p = a(f);
8
- export {
9
- p as default
10
- };
@@ -1,10 +0,0 @@
1
- import { jsx as o } from "react/jsx-runtime";
2
- import i from "../errors/context/provider.mjs";
3
- import m from "./useFormWithProviders.mjs";
4
- function s({ children: e, ...t }) {
5
- const r = m(t);
6
- return /* @__PURE__ */ o(r.Provider, { children: /* @__PURE__ */ o(i, { formRef: r.ref, children: /* @__PURE__ */ o(r.Form, { children: e }) }) });
7
- }
8
- export {
9
- s as default
10
- };
@@ -1,25 +0,0 @@
1
- import { jsx as r } from "react/jsx-runtime";
2
- import { FormProvider as f } from "react-hook-form";
3
- import n from "./utils/FormUtilsContextProvider.mjs";
4
- function v({
5
- formId: o,
6
- formRef: m,
7
- rhf: i,
8
- form: t,
9
- utils: e,
10
- children: d
11
- }) {
12
- return /* @__PURE__ */ r(f, { ...i, children: /* @__PURE__ */ r(
13
- n,
14
- {
15
- formId: o,
16
- formRef: m,
17
- formProps: t,
18
- ...e,
19
- children: d
20
- }
21
- ) });
22
- }
23
- export {
24
- v as default
25
- };
@@ -1,28 +0,0 @@
1
- import { jsx as i } from "react/jsx-runtime";
2
- import "react";
3
- import s from "./context/FormProvider.mjs";
4
- import f from "./useForm.mjs";
5
- const d = ({
6
- rhf: t,
7
- form: o,
8
- utils: e
9
- }) => {
10
- const r = f({ rhf: t, form: o });
11
- return {
12
- ...r,
13
- Provider: ({ children: m }) => /* @__PURE__ */ i(
14
- s,
15
- {
16
- formRef: r.ref,
17
- rhf: r.context,
18
- formId: r.id,
19
- form: o,
20
- utils: e,
21
- children: m
22
- }
23
- )
24
- };
25
- }, P = d;
26
- export {
27
- P as default
28
- };
@@ -1,12 +0,0 @@
1
- import { FormSubmitError as m } from "./FormSubmitError.mjs";
2
- const n = (r, o) => {
3
- r instanceof m && Object.entries(r.errors).forEach(([t, e]) => {
4
- o.setError(t, {
5
- type: "FormSubmitError",
6
- message: e
7
- });
8
- });
9
- };
10
- export {
11
- n as default
12
- };
@@ -1,7 +0,0 @@
1
- import e from "react";
2
- function o(r) {
3
- return e.forwardRef(r);
4
- }
5
- export {
6
- o as default
7
- };
@@ -1,16 +0,0 @@
1
- import r from "react";
2
- const a = (t) => {
3
- const e = r.useRef(null);
4
- return r.useImperativeHandle(
5
- t,
6
- () => {
7
- if (!e.current)
8
- throw new Error();
9
- return e.current;
10
- },
11
- []
12
- ), e;
13
- }, o = a;
14
- export {
15
- o as default
16
- };