@paragrav/rhf-utils 0.0.124 → 0.0.125

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 CHANGED
@@ -171,7 +171,10 @@ Currently, only `zod` is supported.
171
171
  }}
172
172
  utils={{
173
173
  submitOnChange: true,
174
- resetValuesOnSubmitError: true,
174
+ resetValuesOnSubmitted: {
175
+ onSuccess: 'current',
176
+ onError: 'defaults',
177
+ },
175
178
  // custom option props
176
179
  // (see "Extend RhfUtilsFormOptions" section)
177
180
  enableMyFormNavigationPrompt: true,
@@ -242,7 +245,10 @@ type RhfUtilsFormOptions = {
242
245
  submitOnChange?: boolean;
243
246
 
244
247
  /** Reset form after submitted. */
245
- resetFormOnSubmitted?: UseResetFormOnSubmittedOptions;
248
+ resetFormOnSubmitted?: {
249
+ onSuccess?: 'defaults' | 'current';
250
+ onError?: 'defaults';
251
+ };
246
252
 
247
253
  /** Control dev tool options. (Lazy-loaded when truthy value supplied.) */
248
254
  devTool?: true | Pick<DevtoolUIProps, 'placement' | 'styles'>;
@@ -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
  };
@@ -6,8 +6,7 @@ const C = (r) => {
6
6
  const l = p(), t = F();
7
7
  u.useEffect(() => {
8
8
  var o, m;
9
- if (!t.hasErrors)
10
- return;
9
+ if (!t.hasErrors) return;
11
10
  const e = (o = r == null ? void 0 : r.console) == null ? void 0 : o.call(r, t);
12
11
  e && E(
13
12
  e.message ?? a,
@@ -9,8 +9,7 @@ const u = (t) => {
9
9
  d,
10
10
  ""
11
11
  );
12
- if (r === o)
13
- return e;
12
+ if (r === o) return e;
14
13
  const f = c(t, r, void 0);
15
14
  return f && e.push([r, f]), e;
16
15
  },
@@ -1,29 +1,29 @@
1
- import { jsx as F } from "react/jsx-runtime";
2
- import O from "../../../client/config/useRhfUtilsClientConfigContext.mjs";
3
- import f from "../../../errors/flat/context/useFlatFieldErrorsContextOutput.mjs";
4
- import h from "../../../submit/useFormOnSubmitSuccessfulAndReset.mjs";
5
- import i from "../../../submit/useResetFormOnSubmitted.mjs";
6
- import l from "../../../submit/useSubmitFormOnChange.mjs";
7
- import { _RhfUtilsContextProvider as d } from "./useRhfUtilsContext.mjs";
8
- const b = ({
1
+ import { jsx as l } from "react/jsx-runtime";
2
+ import d from "../../../client/config/useRhfUtilsClientConfigContext.mjs";
3
+ import i from "../../../errors/flat/context/useFlatFieldErrorsContextOutput.mjs";
4
+ import u from "../../../submit/useFormOnSubmitSuccessfulAndReset.mjs";
5
+ import x from "../../../submit/useResetFormOnSubmitted.mjs";
6
+ import F from "../../../submit/useSubmitFormOnChange.mjs";
7
+ import { _RhfUtilsContextProvider as O } from "./useRhfUtilsContext.mjs";
8
+ const P = ({
9
9
  formId: C,
10
- formRef: e,
10
+ formRef: m,
11
11
  onSubmitSuccessfulAndReset: t,
12
12
  options: r,
13
- children: x
13
+ children: f
14
14
  }) => {
15
15
  var a;
16
- const m = O();
17
- return f((a = m == null ? void 0 : m.fieldErrors) == null ? void 0 : a.output), l(e, {
16
+ const e = d();
17
+ return i((a = e == null ? void 0 : e.fieldErrors) == null ? void 0 : a.output), F(m, {
18
18
  disabled: !(r != null && r.submitOnChange)
19
- }), i(
19
+ }), x(
20
20
  (r == null ? void 0 : r.resetFormOnSubmitted) ?? (t && {
21
- onSuccess: !!t
21
+ onSuccess: "defaults"
22
22
  })
23
- ), h(() => {
23
+ ), u(() => {
24
24
  t == null || t();
25
- }), /* @__PURE__ */ F(d, { value: { formId: C, formRef: e, options: r }, children: x });
25
+ }), /* @__PURE__ */ l(O, { value: { formId: C, formRef: m, options: r }, children: f });
26
26
  };
27
27
  export {
28
- b as default
28
+ P as default
29
29
  };
@@ -1,6 +1,5 @@
1
1
  const u = (t) => (r) => {
2
- if (!t.current)
3
- throw new Error();
2
+ if (!t.current) throw new Error();
4
3
  e(t.current, r);
5
4
  }, e = (t, r) => {
6
5
  t.requestSubmit(r);
@@ -3,11 +3,9 @@ import n from "./useFormOnSubmitted.mjs";
3
3
  const S = (e) => {
4
4
  const { reset: r, getValues: u } = m();
5
5
  n((s) => {
6
- if (!e)
7
- return;
6
+ if (!e) return;
8
7
  const t = e[s ? "onSuccess" : "onError"];
9
- if (!t)
10
- return;
8
+ if (!t) return;
11
9
  const o = t === "current" ? u() : void 0;
12
10
  r(o, {
13
11
  // keep submit stuff for tracking relevant state (e.g., `useFormOnSubmitSuccessful`)
@@ -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
  };
@@ -2,8 +2,7 @@ import r from "react";
2
2
  function i() {
3
3
  const e = r.createContext(void 0), t = () => r.useContext(e), n = () => {
4
4
  const o = t();
5
- if (o === void 0)
6
- throw new Error();
5
+ if (o === void 0) throw new Error();
7
6
  return o;
8
7
  };
9
8
  return {
@@ -5,14 +5,14 @@
5
5
  export type UseResetFormOnSubmittedOptions = undefined | {
6
6
  /**
7
7
  * Reset values on submit successful.
8
- * - `true`: reset to default values
8
+ * - `'defaults'`: reset to default values
9
9
  * - `'current'`: reset to current values
10
10
  */
11
- onSuccess?: true | 'current';
11
+ onSuccess?: 'defaults' | 'current';
12
12
  /**
13
13
  * Reset values on submit error. (reset to default values)
14
14
  */
15
- onError?: true;
15
+ onError?: 'defaults';
16
16
  };
17
17
  /**
18
18
  * Handle resetting after successful submit with no server validation errors.
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.124",
5
+ "version": "0.0.125",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,