@paragrav/rhf-utils 0.0.56 → 0.0.58

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.
@@ -1,14 +1,21 @@
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
- });
1
+ let s;
2
+ const t = (e) => {
3
+ s = e;
4
+ }, l = (e) => {
5
+ var a;
6
+ return {
7
+ ...s,
8
+ options: {
9
+ ...s == null ? void 0 : s.options,
10
+ ...e,
11
+ classNames: [
12
+ ...((a = s == null ? void 0 : s.options) == null ? void 0 : a.classNames) ?? [],
13
+ ...(e == null ? void 0 : e.classNames) ?? []
14
+ ]
15
+ }
16
+ };
17
+ };
11
18
  export {
12
- e as _getConfig,
13
- n as setConfig
19
+ l as _getConfig,
20
+ t as setConfig
14
21
  };
@@ -1,25 +1,28 @@
1
- import n from "react";
2
- import { useForm as p } from "react-hook-form";
3
- import u from "../submit/useSubmitFormOnChange.mjs";
4
- import { _getConfig as c } from "../config/index.mjs";
5
- const f = ({
1
+ import { zodResolver as f } from "../node_modules/@hookform/resolvers/zod/dist/zod.mjs";
2
+ import i from "react";
3
+ import { useForm as u } from "react-hook-form";
4
+ import c from "../submit/useSubmitFormOnChange.mjs";
5
+ import { _getConfig as d } from "../config/index.mjs";
6
+ const a = ({
7
+ schema: t,
6
8
  rhf: m,
7
9
  form: o,
8
- utils: i
10
+ utils: p
9
11
  }) => {
10
- const t = c(o == null ? void 0 : o.options), s = n.useId(), e = p({
11
- ...t.rhf,
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,
12
15
  ...m
13
- }), r = n.useRef(null);
14
- return u(r, { disabled: !t.options.submitOnChange }), {
16
+ }), r = i.useRef(null);
17
+ return c(r, { disabled: !e.options.submitOnChange }), {
15
18
  id: s,
16
19
  ref: r,
17
- context: e,
20
+ context: n,
18
21
  providerProps: {
19
22
  formId: s,
20
23
  formRef: r,
21
- rhfForm: e,
22
- utils: i
24
+ rhfForm: n,
25
+ utils: p
23
26
  },
24
27
  formProps: {
25
28
  formId: s,
@@ -27,7 +30,7 @@ const f = ({
27
30
  form: o
28
31
  }
29
32
  };
30
- }, P = f;
33
+ }, h = a;
31
34
  export {
32
- P as default
35
+ h as default
33
36
  };
@@ -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,20 +1,36 @@
1
1
  import { FormSubmitError } from '../submit/error/FormSubmitError';
2
- import { UseFormOptions } from '../form/useForm';
3
- import { FieldValues, UseFormProps, UseFormStateReturn } from 'react-hook-form';
2
+ import { UseFormFieldsProps, UseFormOptions } from '../form/useForm';
3
+ import { UseFormProps } from 'react-hook-form';
4
4
 
5
5
  type Config = {
6
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
+ */
7
12
  options?: UseFormOptions;
13
+ /**
14
+ * Component injected at the top of the form.
15
+ *
16
+ * e.g., block navigation on dirty form.
17
+ */
18
+ ChildrenFirst?: React.FC<UseFormFieldsProps>;
19
+ /**
20
+ * Component injected at the bottom of the form.
21
+ *
22
+ * e.g., root errors list.
23
+ */
24
+ ChildrenLast?: React.FC<UseFormFieldsProps>;
8
25
  /**
9
26
  * Handle non-{@link FormSubmitError} errors. Optionally, return {@link FormSubmitError} to merge into context errors.
10
27
  */
11
28
  onSubmitError?: (error: unknown) => FormSubmitError | undefined;
12
- Prompter?: React.FC<UseFormStateReturn<FieldValues>>;
13
29
  };
14
30
  export declare const setConfig: (config: Config) => void;
15
31
  export declare const _getConfig: (options?: UseFormOptions) => {
16
32
  options: {
17
- hideRootErrorsList?: boolean | undefined;
33
+ classNames: string[];
18
34
  submitOnChange?: boolean | undefined;
19
35
  resetValuesOnSubmitError?: boolean | undefined;
20
36
  stopSubmitPropagation?: boolean | undefined;
@@ -26,9 +42,9 @@ export declare const _getConfig: (options?: UseFormOptions) => {
26
42
  reValidateMode: "onBlur" | "onChange" | "onSubmit";
27
43
  defaultValues: {
28
44
  [x: string]: any;
29
- } | ((payload?: unknown) => Promise<FieldValues>);
30
- values: FieldValues;
31
- errors: import('react-hook-form').FieldErrors<FieldValues>;
45
+ } | ((payload?: unknown) => Promise<import('react-hook-form').FieldValues>);
46
+ values: import('react-hook-form').FieldValues;
47
+ errors: import('react-hook-form').FieldErrors<import('react-hook-form').FieldValues>;
32
48
  resetOptions: Partial<{
33
49
  keepDirtyValues: boolean;
34
50
  keepErrors: boolean;
@@ -42,7 +58,7 @@ export declare const _getConfig: (options?: UseFormOptions) => {
42
58
  keepIsValid: boolean;
43
59
  keepSubmitCount: boolean;
44
60
  }> | undefined;
45
- resolver: import('react-hook-form').Resolver<FieldValues, any>;
61
+ resolver: import('react-hook-form').Resolver<import('react-hook-form').FieldValues, any>;
46
62
  context: any;
47
63
  shouldFocusError: boolean;
48
64
  shouldUnregister: boolean;
@@ -51,7 +67,8 @@ export declare const _getConfig: (options?: UseFormOptions) => {
51
67
  criteriaMode: import('react-hook-form').CriteriaMode;
52
68
  delayError: number;
53
69
  }>, "mode" | "reValidateMode" | "resetOptions" | "context" | "shouldFocusError" | "shouldUnregister" | "shouldUseNativeValidation" | "progressive" | "criteriaMode" | "delayError"> | undefined;
70
+ ChildrenFirst?: import('react').FC<UseFormFieldsProps> | undefined;
71
+ ChildrenLast?: import('react').FC<UseFormFieldsProps> | undefined;
54
72
  onSubmitError?: ((error: unknown) => FormSubmitError | undefined) | undefined;
55
- Prompter?: import('react').FC<UseFormStateReturn<FieldValues>> | undefined;
56
73
  };
57
74
  export {};
@@ -15,6 +15,7 @@ export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
15
15
  export { default as useFormErrorsIfAny } from './errors/useFormErrorsIfAny';
16
16
  export { default as useFormErrorsMemo } from './errors/useFormErrorsMemo';
17
17
  export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
18
+ export type { UseFormFieldsProps, UseFormOptions } from './form/useForm';
18
19
  export { default as useForm } from './form/useForm';
19
20
  export { default as FormUtilsContextProvider } from './form/context/utils/FormUtilsContextProvider';
20
21
  export { default as useFormUtilsContext } from './form/context/utils/useFormUtilsContext';
@@ -1,5 +1,6 @@
1
1
  import { UseFormProps } from './useForm';
2
2
  import { z } from 'zod';
3
+ import { FieldValues } from 'react-hook-form';
3
4
 
4
5
  /**
5
6
  * Form providers (with utils) with immediate descendent {@link Form}.
@@ -8,5 +9,5 @@ import { z } from 'zod';
8
9
  * Use {@link useForm} if you need to specific nesting hierarchy.
9
10
  * (e.g., wrapping `<tr>` with provider and nesting `<form>` within `<td>` to appease HTML rules.)
10
11
  */
11
- declare function FormWithProviders<TSchema extends z.SomeZodObject, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ children, ...props }: React.PropsWithChildren<UseFormProps<TSchema, 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;
12
13
  export default FormWithProviders;
@@ -1,10 +1,11 @@
1
1
  import { UseFormProps } from './useForm';
2
2
  import { z } from 'zod';
3
+ import { FieldValues } from 'react-hook-form';
3
4
  import { default as React } from 'react';
4
5
 
5
- export type _FormProps<TSchema extends z.SomeZodObject, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = React.PropsWithChildren<Pick<UseFormProps<TSchema, TFieldValues, TTransformedValues>, 'form'> & {
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'> & {
6
7
  formId: string;
7
8
  formRef: React.RefObject<HTMLFormElement>;
8
9
  }>;
9
- declare function _Form<TSchema extends z.SomeZodObject, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ formId, formRef, form, children, }: _FormProps<TSchema, TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
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;
10
11
  export default _Form;
@@ -1,12 +1,12 @@
1
1
  import { FormUtilsContextProviderProps } from './context/utils/FormUtilsContextProvider';
2
2
  import { z } from 'zod';
3
- import { UseFormReturn } from 'react-hook-form';
3
+ import { FieldValues, UseFormReturn } from 'react-hook-form';
4
4
 
5
- export type _FormProvidersProps<TSchema extends z.SomeZodObject, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = React.PropsWithChildren<{
5
+ export type _FormProvidersProps<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = React.PropsWithChildren<{
6
6
  formId: string;
7
7
  formRef: React.RefObject<HTMLFormElement>;
8
8
  rhfForm: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
9
9
  utils?: FormUtilsContextProviderProps;
10
10
  }>;
11
- declare function _FormProviders<TSchema extends z.SomeZodObject, 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;
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
12
  export default _FormProviders;
@@ -2,33 +2,35 @@ import { FormUtilsContextProviderProps } from './context/utils/FormUtilsContextP
2
2
  import { _FormProvidersProps } from './_FormProviders';
3
3
  import { _FormProps } from './_Form';
4
4
  import { z } from 'zod';
5
- import { SubmitErrorHandler, UseFormProps as RhfUseFormProps, UseFormReturn } from 'react-hook-form';
5
+ import { FieldValues, SubmitErrorHandler, UseFormProps as RhfUseFormProps, UseFormReturn } from 'react-hook-form';
6
6
  import { default as React } from 'react';
7
7
  import { DevtoolUIProps } from '@hookform/devtools/dist/devToolUI';
8
8
 
9
9
  export type UseFormOptions = {
10
- /** */
11
- hideRootErrorsList?: boolean;
10
+ /** Control visibility of certain items via your own classes and styles. */
11
+ classNames?: string[];
12
12
  /** Request submit via listener on form change. */
13
13
  submitOnChange?: boolean;
14
14
  /** Reset form values if submit throw errors. Use case: optimistic UI toggle form that submits on each change */
15
15
  resetValuesOnSubmitError?: boolean;
16
+ /** Stop propagation of submit event. Useful for portals. */
16
17
  stopSubmitPropagation?: boolean;
18
+ /** Control dev tool options. */
17
19
  devTool?: DevtoolUIProps | 'disabled';
18
20
  };
19
- export type UseFormFieldProps<TSchema extends z.SomeZodObject, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = {
21
+ export type UseFormFieldsProps<TFieldValues extends FieldValues = FieldValues, TTransformedValues extends FieldValues = FieldValues> = {
20
22
  formId: string;
21
23
  formRef: React.RefObject<HTMLFormElement>;
22
24
  ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
23
25
  options?: UseFormOptions;
24
26
  };
25
- export type UseFormProps<TSchema extends z.SomeZodObject, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = {
27
+ export type UseFormProps<TSchema extends z.ZodObject<FieldValues>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>> = {
26
28
  schema?: TSchema;
27
29
  /** {@link RhfUseFormProps} props for RHF's {@link useRhfForm}. */
28
30
  rhf?: RhfUseFormProps<TFieldValues>;
29
31
  /** {@link FormProps} props for Form. */
30
32
  form?: {
31
- Fields?: React.FC<UseFormFieldProps<TSchema, TFieldValues, TTransformedValues>>;
33
+ Fields?: React.FC<UseFormFieldsProps<TFieldValues, TTransformedValues>>;
32
34
  onSubmit?: (values: TTransformedValues, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => unknown | Promise<unknown>;
33
35
  /** Submit error handler. (Only for errors not of instance `FormError`, which are handled internally.) */
34
36
  onSubmitError?: (error: unknown, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => void;
@@ -48,7 +50,11 @@ export type UseFormProps<TSchema extends z.SomeZodObject, TFieldValues extends z
48
50
  * see https://caniuse.com/?search=form%20attribute
49
51
  * @returns Form provider and form.
50
52
  */
51
- declare const useForm: <TSchema extends z.SomeZodObject, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ rhf, form, utils, }: UseFormProps<TSchema, TFieldValues, TTransformedValues>) => {
53
+ declare const useForm: <TSchema extends z.ZodObject<FieldValues, z.UnknownKeysParam, z.ZodTypeAny, {
54
+ [x: string]: any;
55
+ }, {
56
+ [x: string]: any;
57
+ }>, TFieldValues extends z.input<TSchema>, TTransformedValues extends z.output<TSchema>>({ schema, rhf, form, utils, }: UseFormProps<TSchema, TFieldValues, TTransformedValues>) => {
52
58
  readonly id: string;
53
59
  readonly ref: React.RefObject<HTMLFormElement>;
54
60
  readonly context: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
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.56",
5
+ "version": "0.0.58",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,