@paragrav/rhf-utils 0.0.50 → 0.0.51

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 (86) hide show
  1. package/dist/esm/devtool/LazyDevTool.mjs +7 -11
  2. package/dist/esm/env-conditions/production.mjs +4 -0
  3. package/dist/esm/errors/orphan/useOrphanFormErrorsToConsole.mjs +8 -9
  4. package/dist/esm/errors/outputErrors.mjs +5 -6
  5. package/dist/esm/form/Form.mjs +32 -40
  6. package/dist/esm/form/context/FormProvider.mjs +17 -19
  7. package/dist/esm/form/context/utils/FormUtilsContextProvider.mjs +10 -10
  8. package/dist/esm/form/useForm.mjs +14 -16
  9. package/dist/esm/form/useFormWithProviders.mjs +15 -17
  10. package/dist/types/src/devtool/LazyDevTool.d.ts +9 -0
  11. package/dist/types/src/env-conditions/development.d.ts +7 -0
  12. package/dist/types/src/env-conditions/production.d.ts +1 -0
  13. package/dist/types/src/errors/consoleErrors.d.ts +13 -0
  14. package/dist/types/src/errors/context/index.d.ts +10 -0
  15. package/dist/types/src/errors/context/provider.d.ts +11 -0
  16. package/dist/types/src/errors/flat/flattenFieldErrors.d.ts +15 -0
  17. package/dist/types/src/errors/flat/getFlatFieldErrors.d.ts +25 -0
  18. package/dist/types/src/errors/flat/types.d.ts +13 -0
  19. package/dist/types/src/errors/flat/useFormFlatFieldErrorsMemo.d.ts +5 -0
  20. package/dist/types/src/errors/flat/useRefdFieldErrorsMemo.d.ts +9 -0
  21. package/dist/types/src/errors/isFieldError.d.ts +7 -0
  22. package/dist/types/src/errors/message/FormErrorMessage.d.ts +8 -0
  23. package/dist/types/src/errors/message/FormErrorMessageByPath.d.ts +9 -0
  24. package/dist/types/src/errors/nonfield/FormNonFieldErrorMarker.d.ts +10 -0
  25. package/dist/types/src/errors/nonfield/FormNonFieldErrorMarker.spec.d.ts +1 -0
  26. package/dist/types/src/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +2 -0
  27. package/dist/types/src/errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.d.ts +5 -0
  28. package/dist/types/src/errors/nonfield/isNonFieldError.d.ts +19 -0
  29. package/dist/types/src/errors/orphan/getOrphansFromFieldErrors.d.ts +5 -0
  30. package/dist/types/src/errors/orphan/getOrphansFromFlatFieldErrors.d.ts +4 -0
  31. package/dist/types/src/errors/orphan/useFormHasOrphanAndNoFieldErrors.d.ts +9 -0
  32. package/dist/types/src/errors/orphan/useOrphanFormErrorsMemo.d.ts +4 -0
  33. package/dist/types/src/errors/orphan/useOrphanFormErrorsToConsole.d.ts +9 -0
  34. package/dist/types/src/errors/outputErrors.d.ts +13 -0
  35. package/dist/types/src/errors/root/FormRootErrors.d.ts +7 -0
  36. package/dist/types/src/errors/root/FormRootErrorsFromContext.d.ts +3 -0
  37. package/dist/types/src/errors/root/consts.d.ts +1 -0
  38. package/dist/types/src/errors/root/getRootsFromFieldErrors.d.ts +5 -0
  39. package/dist/types/src/errors/root/getRootsFromFlatFieldErrors.d.ts +4 -0
  40. package/dist/types/src/errors/root/isFormErrorPathRoot.d.ts +6 -0
  41. package/dist/types/src/errors/root/useRootFormFieldErrorsMemo.d.ts +6 -0
  42. package/dist/types/src/errors/useFormErrorConsoleDebug.d.ts +8 -0
  43. package/dist/types/src/errors/useFormErrorsEffect.d.ts +4 -0
  44. package/dist/types/src/errors/useFormErrorsIfAny.d.ts +6 -0
  45. package/dist/types/src/errors/useFormErrorsMemo.d.ts +7 -0
  46. package/dist/types/src/errors/useFormErrorsToConsole.d.ts +13 -0
  47. package/dist/types/src/errors/useFormRequestSubmit.d.ts +6 -0
  48. package/dist/types/src/exports.d.ts +37 -0
  49. package/dist/types/src/form/ErrorBoundary.d.ts +15 -0
  50. package/dist/types/src/form/Form.d.ts +29 -0
  51. package/dist/types/src/form/FormBase.d.ts +9 -0
  52. package/dist/types/src/form/FormWithProviders.d.ts +13 -0
  53. package/dist/types/src/form/context/FormProvider.d.ts +15 -0
  54. package/dist/types/src/form/context/group/FormGroupContextProvider.d.ts +5 -0
  55. package/dist/types/src/form/context/group/index.d.ts +15 -0
  56. package/dist/types/src/form/context/group/useFormGroupChildIsMountedTracker.d.ts +2 -0
  57. package/dist/types/src/form/context/group/useFormGroupChildIsSubmittingTracker.d.ts +2 -0
  58. package/dist/types/src/form/context/group/useFormGroupIsAnyBusy.d.ts +2 -0
  59. package/dist/types/src/form/context/group/useFormGroupIsChildBusy.d.ts +2 -0
  60. package/dist/types/src/form/context/group/useFormGroupIsParentBusy.d.ts +2 -0
  61. package/dist/types/src/form/context/group/useFormGroupParentTracker.d.ts +7 -0
  62. package/dist/types/src/form/context/utils/FormUtilsContextProvider.d.ts +14 -0
  63. package/dist/types/src/form/context/utils/useFormUtilsContext.d.ts +9 -0
  64. package/dist/types/src/form/search/FormSearch.d.ts +32 -0
  65. package/dist/types/src/form/search/FormSearchHiddenInputs.d.ts +13 -0
  66. package/dist/types/src/form/search/getNewSearchParams.d.ts +2 -0
  67. package/dist/types/src/form/useForm.d.ts +29 -0
  68. package/dist/types/src/form/useFormIsBusy.d.ts +2 -0
  69. package/dist/types/src/form/useFormOrParentIsBusy.d.ts +2 -0
  70. package/dist/types/src/form/useFormWithProviders.d.ts +31 -0
  71. package/dist/types/src/form/utils/getSubmitterButtonData.d.ts +11 -0
  72. package/dist/types/src/isEmptyObject.d.ts +2 -0
  73. package/dist/types/src/main.d.ts +1 -0
  74. package/dist/types/src/submit/error/FormSubmitError.d.ts +6 -0
  75. package/dist/types/src/submit/error/handleFormSubmitError.d.ts +7 -0
  76. package/dist/types/src/submit/useFormOnSubmitSuccessful.d.ts +2 -0
  77. package/dist/types/src/submit/useFormOnSubmitSuccessfulAndReset.d.ts +13 -0
  78. package/dist/types/src/submit/useResetFormOnSubmitError.d.ts +7 -0
  79. package/dist/types/src/submit/useResetFormOnSubmitSuccessful.d.ts +12 -0
  80. package/dist/types/src/submit/useSubmitFormOnChange.d.ts +4 -0
  81. package/dist/types/src/utils/createContext.d.ts +8 -0
  82. package/dist/types/src/utils/fixedForwardRef.d.ts +7 -0
  83. package/dist/types/src/utils/useImperativeForwardedRef.d.ts +11 -0
  84. package/dist/types/src/utils/useThrowAsyncError.d.ts +5 -0
  85. package/dist/types/src/utils/useToggle.d.ts +4 -0
  86. package/package.json +9 -3
@@ -0,0 +1,4 @@
1
+ import { FlatFieldErrors } from '../flat/types';
2
+
3
+ declare const getRootsFromFlatFieldErrors: (errors: FlatFieldErrors) => FlatFieldErrors;
4
+ export default getRootsFromFlatFieldErrors;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Determine if an error path is root. (e.g., `root` or `root.nested`)
3
+ * @param path Flat path. (e.g., 'root.nested')
4
+ */
5
+ declare const isFormErrorPathRoot: (path: string) => boolean;
6
+ export default isFormErrorPathRoot;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Root field errors from form.
3
+ * These are typically displayed at the bottom of the form.
4
+ */
5
+ declare const useRootFormFieldErrorsMemo: () => import('../flat/types').FlatFieldErrors;
6
+ export default useRootFormFieldErrorsMemo;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Output form errors to console.debug() for easier debugging for certain condition.
3
+ * (e.g., in dev/staging environments or certain users)
4
+ *
5
+ * `console.debug()` is unlikely to trigger any automated console capture reporting.
6
+ */
7
+ declare const useFormErrorConsoleDebug: (enabled?: boolean) => void;
8
+ export default useFormErrorConsoleDebug;
@@ -0,0 +1,4 @@
1
+ import { FieldErrors } from 'react-hook-form';
2
+
3
+ declare const useFormErrorsEffect: <TReturn>(callback: (errors: FieldErrors) => TReturn) => void;
4
+ export default useFormErrorsEffect;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Returns errors only if there are errors.
3
+ * Allows simple typeguard without `isEmpty` dependency.
4
+ */
5
+ declare const useFormErrorsIfAny: () => false | import('react-hook-form').FieldErrors<import('react-hook-form').FieldValues>;
6
+ export default useFormErrorsIfAny;
@@ -0,0 +1,7 @@
1
+ import { FieldErrors } from 'react-hook-form';
2
+
3
+ /**
4
+ * Create memoized value from form errors.
5
+ */
6
+ declare const useFormErrorsMemo: <TReturn>(callback: (errors: FieldErrors) => TReturn) => TReturn;
7
+ export default useFormErrorsMemo;
@@ -0,0 +1,13 @@
1
+ import { FlatFieldErrors } from './flat/types';
2
+
3
+ /**
4
+ * Logs out orphan errors to console.error.
5
+ *
6
+ * In development, it will also throw an error to bring attention to dev.
7
+ */
8
+ declare const useFormErrorsToConsole: (message: string, flatErrors: FlatFieldErrors, options?: {
9
+ disabled?: boolean;
10
+ isError?: boolean;
11
+ data?: Record<string, unknown>;
12
+ }) => void;
13
+ export default useFormErrorsToConsole;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Get callback to trigger submit manually on form.
3
+ * @link https://developer.mozilla.org/docs/Web/API/HTMLFormElement/requestSubmit
4
+ */
5
+ declare const useFormRequestSubmit: (ref: React.RefObject<HTMLFormElement>) => HTMLFormElement['requestSubmit'];
6
+ export default useFormRequestSubmit;
@@ -0,0 +1,37 @@
1
+ export { default as LazyDevTool } from './devtool/LazyDevTool';
2
+ export { default as useFormErrorsFlatContext } from './errors/context';
3
+ export { default as FormErrorsFlatContextProvider } from './errors/context/provider';
4
+ export { default as FormErrorMessage } from './errors/message/FormErrorMessage';
5
+ export { default as FormErrorMessageByPath } from './errors/message/FormErrorMessageByPath';
6
+ export { default as FormNonFieldErrorMarker } from './errors/nonfield/FormNonFieldErrorMarker';
7
+ export { default as FormNonFieldErrorMarkerHtmlAttribute } from './errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute';
8
+ export { default as useFormHasOrphanAndNoFieldErrors } from './errors/orphan/useFormHasOrphanAndNoFieldErrors';
9
+ export { default as useOrphanFormErrorsMemo } from './errors/orphan/useOrphanFormErrorsMemo';
10
+ export { default as useOrphanFormErrorsToConsole } from './errors/orphan/useOrphanFormErrorsToConsole';
11
+ export { default as useRootFormFieldErrorsMemo } from './errors/root/useRootFormFieldErrorsMemo';
12
+ export { default as useFormErrorConsoleDebug } from './errors/useFormErrorConsoleDebug';
13
+ export { default as useFormErrorsEffect } from './errors/useFormErrorsEffect';
14
+ export { default as useFormErrorsIfAny } from './errors/useFormErrorsIfAny';
15
+ export { default as useFormErrorsMemo } from './errors/useFormErrorsMemo';
16
+ export { default as useFormRequestSubmit } from './errors/useFormRequestSubmit';
17
+ export { default as Form } from './form/Form';
18
+ export { default as FormWithProviders } from './form/FormWithProviders';
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
+ export { default as FormUtilsContextProvider } from './form/context/utils/FormUtilsContextProvider';
23
+ export { default as useFormUtilsContext } from './form/context/utils/useFormUtilsContext';
24
+ export { default as FormGroupContextProvider } from './form/context/group/FormGroupContextProvider';
25
+ export { default as useFormGroupChildIsMountedTracker } from './form/context/group/useFormGroupChildIsMountedTracker';
26
+ export { default as useFormGroupChildIsSubmittingTracker } from './form/context/group/useFormGroupChildIsSubmittingTracker';
27
+ export { default as useFormGroupIsAnyBusy } from './form/context/group/useFormGroupIsAnyBusy';
28
+ export { default as useFormGroupIsChildBusy } from './form/context/group/useFormGroupIsChildBusy';
29
+ export { default as useFormGroupIsParentBusy } from './form/context/group/useFormGroupIsParentBusy';
30
+ export { default as useFormGroupParentTracker } from './form/context/group/useFormGroupParentTracker';
31
+ export { default as getSubmitterButtonData } from './form/utils/getSubmitterButtonData';
32
+ export { FormSubmitError } from './submit/error/FormSubmitError';
33
+ export { default as useFormOnSubmitSuccessful } from './submit/useFormOnSubmitSuccessful';
34
+ export { default as useFormOnSubmitSuccessfulAndReset } from './submit/useFormOnSubmitSuccessfulAndReset';
35
+ export { default as useResetFormOnSubmitError } from './submit/useResetFormOnSubmitError';
36
+ export { default as useResetFormAfterSubmitSuccessful } from './submit/useResetFormOnSubmitSuccessful';
37
+ export { default as useSubmitFormOnChange } from './submit/useSubmitFormOnChange';
@@ -0,0 +1,15 @@
1
+ import { default as React } from 'react';
2
+
3
+ type Props = React.PropsWithChildren;
4
+ type State = {
5
+ error: null | Error;
6
+ };
7
+ export declare class ErrorBoundary extends React.Component<Props, State> {
8
+ state: State;
9
+ static getDerivedStateFromError(error: Error): {
10
+ error: Error;
11
+ };
12
+ componentDidCatch(error: unknown, errorInfo: React.ErrorInfo): void;
13
+ render(): string | number | boolean | Iterable<React.ReactNode> | import("react/jsx-runtime").JSX.Element | null | undefined;
14
+ }
15
+ export {};
@@ -0,0 +1,29 @@
1
+ import { FormBaseProps } from './FormBase';
2
+ import { FieldValues, SubmitErrorHandler, UseFormReturn } from 'react-hook-form';
3
+ import { default as React } from 'react';
4
+ import { DevtoolUIProps } from '@hookform/devtools/dist/devToolUI';
5
+
6
+ export type FormProps<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = React.PropsWithChildren<Pick<FormBaseProps, 'onChange'> & {
7
+ formId?: string;
8
+ formRef?: React.RefObject<HTMLFormElement>;
9
+ Fields?: React.FC<{
10
+ ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
11
+ }>;
12
+ onSubmit?: (values: TTransformedValues, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => unknown | Promise<unknown>;
13
+ /** Submit error handler. (Only for errors not of instance `FormError`, which are handled internally.) */
14
+ onSubmitError?: (error: unknown, context: UseFormReturn<TFieldValues, unknown, TTransformedValues>, event: React.BaseSyntheticEvent<SubmitEvent>) => void;
15
+ /** Submit handler when form is invalid. */
16
+ onSubmitInvalid?: SubmitErrorHandler<TFieldValues>;
17
+ options?: {
18
+ hideRootErrors?: boolean;
19
+ /** Request submit via listener on form change. */
20
+ submitOnChange?: boolean;
21
+ /** Reset form values if submit throw errors. Use case: optimistic UI toggle form that submits on each change */
22
+ resetValuesOnSubmitError?: boolean;
23
+ stopSubmitPropagation?: boolean;
24
+ devTool?: DevtoolUIProps | 'disabled';
25
+ };
26
+ className?: string;
27
+ }>;
28
+ declare function Form<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ formId, formRef, Fields, onSubmit, onSubmitError, onSubmitInvalid, options, children, ...props }: FormProps<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
29
+ export default Form;
@@ -0,0 +1,9 @@
1
+ import { default as React } from 'react';
2
+
3
+ export type FormBaseProps = React.PropsWithoutRef<JSX.IntrinsicElements['form']> & {
4
+ antiCsrfToken?: string;
5
+ };
6
+ declare const _default: (props: Omit<React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, "ref"> & {
7
+ antiCsrfToken?: string | undefined;
8
+ } & React.RefAttributes<HTMLFormElement>) => React.ReactNode;
9
+ export default _default;
@@ -0,0 +1,13 @@
1
+ import { UseFormProps } from './useForm';
2
+ import { FieldValues } from 'react-hook-form';
3
+
4
+ export type FormWithProviders<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = React.PropsWithChildren<UseFormProps<TFieldValues, TTransformedValues>>;
5
+ /**
6
+ * {@link FormProvider} (and utils) with immediate descendent {@link Form}.
7
+ * (Syntactic sugar for {@link useForm}. Use this for most applications.)
8
+ *
9
+ * Use {@link useForm} if you need to specific nesting hierarchy.
10
+ * (e.g., wrapping `<tr>` with provider and nesting `<form>` within `<td>` to appease HTML rules.)
11
+ */
12
+ declare function FormWithProviders<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ children, ...props }: FormWithProviders<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
13
+ export default FormWithProviders;
@@ -0,0 +1,15 @@
1
+ import { FormUtilsContextProviderProps } from './utils/FormUtilsContextProvider';
2
+ import { FormProps } from '../Form';
3
+ import { FieldValues, UseFormReturn } from 'react-hook-form';
4
+
5
+ type Props<TFieldValues extends FieldValues, // input values
6
+ TTransformedValues extends FieldValues> = React.PropsWithChildren<{
7
+ formId: string;
8
+ formRef: React.RefObject<HTMLFormElement>;
9
+ rhf: UseFormReturn<TFieldValues, unknown, TTransformedValues>;
10
+ } & {
11
+ form?: Omit<FormProps<TFieldValues, TTransformedValues>, 'formRef' | 'formId'>;
12
+ utils?: FormUtilsContextProviderProps;
13
+ }>;
14
+ declare function FormProvider<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ formId, formRef, rhf, form, utils, children, }: Props<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
15
+ export default FormProvider;
@@ -0,0 +1,5 @@
1
+ import { default as React } from 'react';
2
+
3
+ type Props = React.PropsWithChildren;
4
+ declare const FormGroupContextProvider: React.FC<Props>;
5
+ export default FormGroupContextProvider;
@@ -0,0 +1,15 @@
1
+ /// <reference types="react" />
2
+ export type FormGroupContext = {
3
+ /** Dependent forms. */
4
+ children: {
5
+ busyIds: string[];
6
+ busyCount: number;
7
+ setBusyId: (id: string, isBusy: boolean) => void;
8
+ };
9
+ /** Main form. */
10
+ parent: {
11
+ isBusy: boolean;
12
+ setIsBusy: (isBusy: boolean) => void;
13
+ };
14
+ };
15
+ export declare const _useFormGroupContextMaybe: () => FormGroupContext | undefined, _useFormGroupContext: () => FormGroupContext, _FormGroupContextProvider: import('react').Provider<FormGroupContext | undefined>;
@@ -0,0 +1,2 @@
1
+ declare const useFormGroupChildIsMountedTracker: () => void;
2
+ export default useFormGroupChildIsMountedTracker;
@@ -0,0 +1,2 @@
1
+ declare const useFormGroupChildIsSubmittingTracker: () => void;
2
+ export default useFormGroupChildIsSubmittingTracker;
@@ -0,0 +1,2 @@
1
+ declare const useFormGroupIsAnyBusy: () => boolean;
2
+ export default useFormGroupIsAnyBusy;
@@ -0,0 +1,2 @@
1
+ declare const useFormGroupIsChildBusy: () => boolean;
2
+ export default useFormGroupIsChildBusy;
@@ -0,0 +1,2 @@
1
+ declare const useFormGroupIsParentBusy: () => boolean;
2
+ export default useFormGroupIsParentBusy;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Connect specific form state as parent context.
3
+ *
4
+ * Requires usage of provider.
5
+ */
6
+ declare const useFormGroupParentTracker: () => void;
7
+ export default useFormGroupParentTracker;
@@ -0,0 +1,14 @@
1
+ import { FormProps } from '../../Form';
2
+ import { FieldValues } from 'react-hook-form';
3
+
4
+ export type FormUtilsContextProviderProps = {
5
+ /** Callback after successful submit, and form reset. */
6
+ onSubmitSuccessfulAndReset?: () => void;
7
+ };
8
+ type Props<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = React.PropsWithChildren<{
9
+ formId: string;
10
+ formRef: React.RefObject<HTMLFormElement>;
11
+ formProps?: Omit<FormProps<TFieldValues, TTransformedValues>, 'formRef' | 'formId'>;
12
+ } & FormUtilsContextProviderProps>;
13
+ declare function FormUtilsContextProvider<TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ formRef, onSubmitSuccessfulAndReset, children, }: Props<TFieldValues, TTransformedValues>): import("react/jsx-runtime").JSX.Element;
14
+ export default FormUtilsContextProvider;
@@ -0,0 +1,9 @@
1
+ /// <reference types="react" />
2
+ export type FormUtilsContext = {
3
+ formRef: React.RefObject<HTMLFormElement>;
4
+ /** Callback to trigger submit manually -- e.g., on change. */
5
+ requestSubmit: HTMLFormElement['requestSubmit'];
6
+ };
7
+ declare const useFormUtilsContext: () => FormUtilsContext, _FormUtilsContextProvider: import('react').Provider<FormUtilsContext | undefined>;
8
+ export default useFormUtilsContext;
9
+ export { _FormUtilsContextProvider };
@@ -0,0 +1,32 @@
1
+ import { FormBaseProps } from '../FormBase';
2
+ import { z } from 'zod';
3
+ import { default as React } from 'react';
4
+
5
+ declare const _default: <TSchema extends z.ZodTypeAny, TSchemaOutput extends z.output<TSchema> = z.output<TSchema>>(props: Omit<FormBaseProps, "children" | "onSubmit"> & {
6
+ /**
7
+ * Search params schema.
8
+ *
9
+ * Make sure all properties are marked as `optional().catch(undefiend)` to avoid errors from parsing.
10
+ */
11
+ schema: TSchema;
12
+ /** Current search params. */
13
+ search: NoInfer<TSchemaOutput>;
14
+ Fields: React.FC<{
15
+ search: TSchemaOutput;
16
+ isSubmitting: boolean;
17
+ }>;
18
+ /**
19
+ * Callback to handle form submission.
20
+ * @param search New search params.
21
+ * @param data Sanitized form data.
22
+ * @param event Form submit event.
23
+ */
24
+ onSubmit?: ((search: TSchemaOutput, data: TSchemaOutput, event: React.FormEvent<HTMLFormElement>) => unknown) | undefined;
25
+ /**
26
+ * Error handler.
27
+ *
28
+ * Obviates need for consumer `try/catch`.
29
+ */
30
+ onSubmitError?: ((error: unknown, search: TSchemaOutput, data: TSchemaOutput, event: React.FormEvent<HTMLFormElement>) => void) | undefined;
31
+ } & React.RefAttributes<HTMLFormElement>) => React.ReactNode;
32
+ export default _default;
@@ -0,0 +1,13 @@
1
+ import { default as React } from 'react';
2
+ import { Stringifiable } from 'query-string';
3
+
4
+ /**
5
+ * Generate hidden inputs from the current search query string.
6
+ * This allows form to not require JS.
7
+ */
8
+ declare const FormSearchHiddenInputs: React.FC<{
9
+ search: Record<string, Stringifiable>;
10
+ /** Keys to ignore. i.e., they have inputs. */
11
+ include: string[];
12
+ }>;
13
+ export default FormSearchHiddenInputs;
@@ -0,0 +1,2 @@
1
+ declare const getNewSearchParams: <TSearchParams extends Record<string, unknown>>(current: TSearchParams | undefined, update: TSearchParams) => TSearchParams;
2
+ export default getNewSearchParams;
@@ -0,0 +1,29 @@
1
+ import { FormProps } from './Form';
2
+ import { FieldValues, UseFormProps as RhfUseFormProps } from 'react-hook-form';
3
+ import { default as React } from 'react';
4
+
5
+ export type UseFormProps<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = {
6
+ /** {@link RhfUseFormProps} props for RHF's {@link useRhfForm}. */
7
+ rhf?: RhfUseFormProps<TFieldValues>;
8
+ /** {@link FormProps} props for Form. */
9
+ form?: Omit<FormProps<TFieldValues, TTransformedValues>, 'formId' | 'formRef'>;
10
+ };
11
+ export type UseFormReturn = ReturnType<typeof useForm>;
12
+ /**
13
+ * Base Rhf Form hook. Use `Form` in `index.ts` for most cases.
14
+ * Use this if you need to separate form components.
15
+ * e.g., a form for table row due to `form` being considered invalid HTML around `tr` tags.
16
+ * With this hook, you can wrap the `tr` tag with `form.Provider` and use `form.Form` inside
17
+ * one of the cells. Other components can be outside form while referring to form by its id via `form` attribute.
18
+ * see https://caniuse.com/?search=form%20attribute
19
+ * @returns Form provider and form.
20
+ */
21
+ declare const useForm: <TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ rhf, form, }: UseFormProps<TFieldValues, TTransformedValues>) => {
22
+ readonly id: string;
23
+ 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
+ }>;
28
+ };
29
+ export default useForm;
@@ -0,0 +1,2 @@
1
+ declare const useFormIsBusy: () => boolean;
2
+ export default useFormIsBusy;
@@ -0,0 +1,2 @@
1
+ declare const useFormOrParentIsBusy: () => boolean;
2
+ export default useFormOrParentIsBusy;
@@ -0,0 +1,31 @@
1
+ import { UseFormProps } from './useForm';
2
+ import { FormUtilsContextProviderProps } from './context/utils/FormUtilsContextProvider';
3
+ import { FieldValues } from 'react-hook-form';
4
+ import { default as React } from 'react';
5
+
6
+ export type UseFormWithProvidersProps<TFieldValues extends FieldValues, TTransformedValues extends FieldValues> = UseFormProps<TFieldValues, TTransformedValues> & {
7
+ /** {@link FormUtilsContextProviderProps} props for `FormUtilsContextProvider`. */
8
+ utils?: FormUtilsContextProviderProps;
9
+ };
10
+ export type UseFormWithProvidersReturn = ReturnType<typeof useFormWithProviders>;
11
+ /**
12
+ * Base Rhf Form hook. Use `Form` in `index.ts` for most cases.
13
+ * Use this if you need to separate form components.
14
+ * e.g., a form for table row due to `form` being considered invalid HTML around `tr` tags.
15
+ * With this hook, you can wrap the `tr` tag with `form.Provider` and use `form.Form` inside
16
+ * one of the cells. Other components can be outside form while referring to form by its id via `form` attribute.
17
+ * see https://caniuse.com/?search=form%20attribute
18
+ * @returns Form provider and form.
19
+ */
20
+ declare const useFormWithProviders: <TFieldValues extends FieldValues, TTransformedValues extends FieldValues>({ rhf, form, utils, }: UseFormWithProvidersProps<TFieldValues, TTransformedValues>) => {
21
+ readonly Provider: React.FC<{
22
+ children?: React.ReactNode;
23
+ }>;
24
+ readonly id: string;
25
+ readonly ref: React.RefObject<HTMLFormElement>;
26
+ readonly context: import('react-hook-form').UseFormReturn<TFieldValues, unknown, TTransformedValues>;
27
+ readonly Form: React.FC<{
28
+ children?: React.ReactNode;
29
+ }>;
30
+ };
31
+ export default useFormWithProviders;
@@ -0,0 +1,11 @@
1
+ import { default as React } from 'react';
2
+
3
+ /**
4
+ * Get [name, value] of button/input that submitted form.
5
+ * Useful when determining user intent
6
+ */
7
+ declare const getSubmitterButtonData: (event: React.BaseSyntheticEvent) => [
8
+ name: string,
9
+ value: string
10
+ ] | undefined;
11
+ export default getSubmitterButtonData;
@@ -0,0 +1,2 @@
1
+ declare const isEmptyObject: (obj: Record<string, unknown>) => boolean;
2
+ export default isEmptyObject;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { FieldPath, FieldValues } from 'react-hook-form';
2
+
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);
6
+ }
@@ -0,0 +1,7 @@
1
+ import { FieldValues, UseFormReturn } from 'react-hook-form';
2
+
3
+ /**
4
+ * Handle form submit error if {@link FormSubmitError}. Add items to RHF form errors.
5
+ */
6
+ declare const handleFormSubmitError: <TFieldValues extends FieldValues, TTransformedValues extends FieldValues>(error: unknown, ctx: UseFormReturn<TFieldValues, unknown, TTransformedValues>) => void;
7
+ export default handleFormSubmitError;
@@ -0,0 +1,2 @@
1
+ declare const useFormOnSubmitSuccessful: (callback?: () => void) => void;
2
+ export default useFormOnSubmitSuccessful;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Calls a callback after a successful submit and after a form reset.
3
+ *
4
+ * Use this when your forms use a isDirty prompter for user action that would lose unsaved data.
5
+ * You must reset form before you can navigate away from page.
6
+ *
7
+ * Using RHF and following its rules, we are not able to get the state `isSubmitSuccessful=true` and `isDirty=false`.
8
+ * So, this has to keep track of previous state.
9
+ *
10
+ * SubmitSuccessfulAndReset = was `isSubmitSuccessful` and now `!isSubmitSuccessful && !isDirty`
11
+ */
12
+ declare const useFormOnSubmitSuccessfulAndReset: (callback?: () => void) => void;
13
+ export default useFormOnSubmitSuccessfulAndReset;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Handle resetting after successful submit with no server validation errors.
3
+ * See `Rules` section: https://react-hook-form.com/api/useform/reset/
4
+ * Specifically: "- It's recommended to reset inside useEffect after submission."
5
+ */
6
+ declare const useResetFormOnSubmitError: (disabled?: boolean) => void;
7
+ export default useResetFormOnSubmitError;
@@ -0,0 +1,12 @@
1
+ import { KeepStateOptions } from 'react-hook-form';
2
+
3
+ type Options = {
4
+ enabled?: boolean;
5
+ } & KeepStateOptions;
6
+ /**
7
+ * Handle resetting after successful submit with no server validation errors.
8
+ * See `Rules` section: https://react-hook-form.com/api/useform/reset/
9
+ * Specifically: "- It's recommended to reset inside useEffect after submission."
10
+ */
11
+ declare const useResetFormAfterSubmitSuccessful: ({ enabled, ...options }: Options) => void;
12
+ export default useResetFormAfterSubmitSuccessful;
@@ -0,0 +1,4 @@
1
+ import { default as React } from 'react';
2
+
3
+ declare const useSubmitFormOnChange: (formRef: React.RefObject<HTMLFormElement>, disabled?: boolean) => void;
4
+ export default useSubmitFormOnChange;
@@ -0,0 +1,8 @@
1
+ import { default as React } from 'react';
2
+
3
+ /**
4
+ * Create a strongly-typed context that throws error if no provider is found.
5
+ * @returns A tuple with a function to get the context and the context provider.
6
+ */
7
+ declare function createContext<T = unknown>(): readonly [() => T | undefined, () => T & ({} | null), React.Provider<T | undefined>];
8
+ export default createContext;
@@ -0,0 +1,7 @@
1
+ import { default as React } from 'react';
2
+
3
+ /**
4
+ * @see https://www.totaltypescript.com/forwardref-with-generic-components
5
+ */
6
+ declare function fixedForwardRef<T, P = object>(render: (props: P, ref: React.Ref<T>) => React.ReactNode): (props: P & React.RefAttributes<T>) => React.ReactNode;
7
+ export default fixedForwardRef;
@@ -0,0 +1,11 @@
1
+ import { default as React } from 'react';
2
+
3
+ /**
4
+ * Create a ref and point forwarded ref to it.
5
+ *
6
+ * Use case: forwarded refs are optional, but sometimes you need the ref.
7
+ *
8
+ * @param forwardedRef Forwarded `ref` prop.
9
+ */
10
+ declare const useImperativeForwardedRef: <TRef>(forwardedRef?: React.ForwardedRef<TRef>) => React.RefObject<TRef>;
11
+ export default useImperativeForwardedRef;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * React cannot handle async errors in event handlers or useEffects.
3
+ */
4
+ declare const useThrowAsyncError: () => (error: unknown) => void;
5
+ export default useThrowAsyncError;
@@ -0,0 +1,4 @@
1
+ import { default as React } from 'react';
2
+
3
+ declare const useToggle: (defaultValue?: boolean) => [boolean, React.DispatchWithoutAction];
4
+ export default useToggle;
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.50",
5
+ "version": "0.0.51",
6
6
  "description": "Integration utilities for react-hook-form.",
7
7
  "type": "module",
8
8
  "sideEffects": false,
@@ -10,6 +10,12 @@
10
10
  "dist"
11
11
  ],
12
12
  "module": "./dist/esm/exports.mjs",
13
+ "imports": {
14
+ "#is-development": {
15
+ "development": "./src/env-conditions/development.ts",
16
+ "default": "./src/env-conditions/production.ts"
17
+ }
18
+ },
13
19
  "exports": {
14
20
  ".": {
15
21
  "default": "./dist/esm/exports.mjs",
@@ -159,12 +165,12 @@
159
165
  "scripts": {
160
166
  "build": "npm install && npm run validate && vite build",
161
167
  "build:dev": "vite build --config vite.config.dev.ts",
162
- "dev": "vite",
168
+ "dev": "vite --config vite.config.dev.ts",
163
169
  "lint": "eslint --ignore-path ./.gitignore --ext ts,tsx --report-unused-disable-directives --max-warnings 0 .",
164
170
  "preinstall": "npx only-allow npm",
165
171
  "prepare": "husky",
166
172
  "prepublishOnly": "npm run build",
167
- "preview": "vite preview",
173
+ "preview": "vite preview --config vite.config.dev.ts",
168
174
  "test": "vitest",
169
175
  "test:ct": "playwright test -c playwright-ct.config.ts",
170
176
  "test:ct:ui": "playwright test -c playwright-ct.config.ts --ui",