@paragrav/rhf-utils 0.0.50 → 0.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/devtool/LazyDevTool.mjs +7 -11
- package/dist/esm/env-conditions/production.mjs +4 -0
- package/dist/esm/errors/isFieldError.mjs +2 -2
- package/dist/esm/errors/orphan/getOrphansFromFlatFieldErrors.mjs +5 -12
- package/dist/esm/errors/orphan/isOrphanFormError.mjs +12 -0
- package/dist/esm/errors/orphan/useOrphanFormErrorsToConsole.mjs +8 -9
- package/dist/esm/errors/outputErrors.mjs +5 -6
- package/dist/esm/exports.mjs +38 -34
- package/dist/esm/form/Form.mjs +32 -40
- package/dist/esm/form/context/FormProvider.mjs +17 -19
- package/dist/esm/form/context/group/useFormGroupChildIsMountedTracker.mjs +5 -14
- package/dist/esm/form/context/group/useFormGroupChildIsSubmittingTracker.mjs +6 -11
- package/dist/esm/form/context/group/useFormGroupChildTracker.mjs +13 -0
- package/dist/esm/form/context/group/useFormOrParentIsBusy.mjs +10 -0
- package/dist/esm/form/context/utils/FormUtilsContextProvider.mjs +10 -10
- package/dist/esm/form/useForm.mjs +14 -16
- package/dist/esm/form/useFormWithProviders.mjs +15 -17
- package/dist/types/src/devtool/LazyDevTool.d.ts +9 -0
- package/dist/types/src/env-conditions/development.d.ts +7 -0
- package/dist/types/src/env-conditions/production.d.ts +1 -0
- package/dist/types/src/errors/consoleErrors.d.ts +13 -0
- package/dist/types/src/errors/context/index.d.ts +10 -0
- package/dist/types/src/errors/context/provider.d.ts +11 -0
- package/dist/types/src/errors/flat/flattenFieldErrors.d.ts +15 -0
- package/dist/types/src/errors/flat/getFlatFieldErrors.d.ts +25 -0
- package/dist/types/src/errors/flat/types.d.ts +13 -0
- package/dist/types/src/errors/flat/useFormFlatFieldErrorsMemo.d.ts +5 -0
- package/dist/types/src/errors/flat/useRefdFieldErrorsMemo.d.ts +9 -0
- package/dist/types/src/errors/isFieldError.d.ts +7 -0
- package/dist/types/src/errors/message/FormErrorMessage.d.ts +8 -0
- package/dist/types/src/errors/message/FormErrorMessageByPath.d.ts +9 -0
- package/dist/types/src/errors/nonfield/FormNonFieldErrorMarker.d.ts +10 -0
- package/dist/types/src/errors/nonfield/FormNonFieldErrorMarker.spec.d.ts +1 -0
- package/dist/types/src/errors/nonfield/FormNonFieldErrorMarker.utils.d.ts +2 -0
- package/dist/types/src/errors/nonfield/FormNonFieldErrorMarkerHtmlAttribute.d.ts +5 -0
- package/dist/types/src/errors/nonfield/isNonFieldError.d.ts +19 -0
- package/dist/types/src/errors/orphan/getOrphansFromFieldErrors.d.ts +5 -0
- package/dist/types/src/errors/orphan/getOrphansFromFlatFieldErrors.d.ts +4 -0
- package/dist/types/src/errors/orphan/isOrphanFormError.d.ts +4 -0
- package/dist/types/src/errors/orphan/useFormHasOrphanAndNoFieldErrors.d.ts +9 -0
- package/dist/types/src/errors/orphan/useOrphanFormErrorsMemo.d.ts +4 -0
- package/dist/types/src/errors/orphan/useOrphanFormErrorsToConsole.d.ts +9 -0
- package/dist/types/src/errors/outputErrors.d.ts +13 -0
- package/dist/types/src/errors/root/FormRootErrors.d.ts +7 -0
- package/dist/types/src/errors/root/FormRootErrorsFromContext.d.ts +3 -0
- package/dist/types/src/errors/root/consts.d.ts +1 -0
- package/dist/types/src/errors/root/getRootsFromFieldErrors.d.ts +5 -0
- package/dist/types/src/errors/root/getRootsFromFlatFieldErrors.d.ts +4 -0
- package/dist/types/src/errors/root/isFormErrorPathRoot.d.ts +6 -0
- package/dist/types/src/errors/root/useRootFormFieldErrorsMemo.d.ts +6 -0
- package/dist/types/src/errors/useFormErrorConsoleDebug.d.ts +8 -0
- package/dist/types/src/errors/useFormErrorsEffect.d.ts +4 -0
- package/dist/types/src/errors/useFormErrorsIfAny.d.ts +6 -0
- package/dist/types/src/errors/useFormErrorsMemo.d.ts +7 -0
- package/dist/types/src/errors/useFormErrorsToConsole.d.ts +13 -0
- package/dist/types/src/errors/useFormRequestSubmit.d.ts +6 -0
- package/dist/types/src/exports.d.ts +39 -0
- package/dist/types/src/form/ErrorBoundary.d.ts +15 -0
- package/dist/types/src/form/Form.d.ts +29 -0
- package/dist/types/src/form/FormBase.d.ts +9 -0
- package/dist/types/src/form/FormWithProviders.d.ts +13 -0
- package/dist/types/src/form/context/FormProvider.d.ts +15 -0
- package/dist/types/src/form/context/group/FormGroupContextProvider.d.ts +5 -0
- package/dist/types/src/form/context/group/index.d.ts +15 -0
- package/dist/types/src/form/context/group/useFormGroupChildIsMountedTracker.d.ts +5 -0
- package/dist/types/src/form/context/group/useFormGroupChildIsSubmittingTracker.d.ts +5 -0
- package/dist/types/src/form/context/group/useFormGroupChildTracker.d.ts +6 -0
- package/dist/types/src/form/context/group/useFormGroupIsAnyBusy.d.ts +2 -0
- package/dist/types/src/form/context/group/useFormGroupIsChildBusy.d.ts +2 -0
- package/dist/types/src/form/context/group/useFormGroupIsParentBusy.d.ts +2 -0
- package/dist/types/src/form/context/group/useFormGroupParentTracker.d.ts +7 -0
- package/dist/types/src/form/context/group/useFormOrParentIsBusy.d.ts +2 -0
- package/dist/types/src/form/context/utils/FormUtilsContextProvider.d.ts +14 -0
- package/dist/types/src/form/context/utils/useFormUtilsContext.d.ts +9 -0
- package/dist/types/src/form/search/FormSearch.d.ts +32 -0
- package/dist/types/src/form/search/FormSearchHiddenInputs.d.ts +13 -0
- package/dist/types/src/form/search/getNewSearchParams.d.ts +2 -0
- package/dist/types/src/form/useForm.d.ts +29 -0
- package/dist/types/src/form/useFormIsBusy.d.ts +2 -0
- package/dist/types/src/form/useFormOrParentIsBusy.d.ts +2 -0
- package/dist/types/src/form/useFormWithProviders.d.ts +31 -0
- package/dist/types/src/form/utils/getSubmitterButtonData.d.ts +11 -0
- package/dist/types/src/isEmptyObject.d.ts +2 -0
- package/dist/types/src/main.d.ts +1 -0
- package/dist/types/src/submit/error/FormSubmitError.d.ts +6 -0
- package/dist/types/src/submit/error/handleFormSubmitError.d.ts +7 -0
- package/dist/types/src/submit/useFormOnSubmitSuccessful.d.ts +2 -0
- package/dist/types/src/submit/useFormOnSubmitSuccessfulAndReset.d.ts +13 -0
- package/dist/types/src/submit/useResetFormOnSubmitError.d.ts +7 -0
- package/dist/types/src/submit/useResetFormOnSubmitSuccessful.d.ts +12 -0
- package/dist/types/src/submit/useSubmitFormOnChange.d.ts +4 -0
- package/dist/types/src/utils/createContext.d.ts +8 -0
- package/dist/types/src/utils/fixedForwardRef.d.ts +7 -0
- package/dist/types/src/utils/useImperativeForwardedRef.d.ts +11 -0
- package/dist/types/src/utils/useThrowAsyncError.d.ts +5 -0
- package/dist/types/src/utils/useToggle.d.ts +4 -0
- package/package.json +17 -3
|
@@ -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 @@
|
|
|
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,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,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;
|
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.
|
|
5
|
+
"version": "0.0.53",
|
|
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",
|
|
@@ -107,10 +113,18 @@
|
|
|
107
113
|
"default": "./dist/esm/form/context/group/useFormGroupParentTracker.mjs",
|
|
108
114
|
"types": "./dist/types/form/context/group/useFormGroupParentTracker.d.ts"
|
|
109
115
|
},
|
|
116
|
+
"./form/context/group/useFormOrParentIsBusy": {
|
|
117
|
+
"default": "./dist/esm/form/context/group/useFormOrParentIsBusy.mjs",
|
|
118
|
+
"types": "./dist/types/form/context/group/useFormOrParentIsBusy.d.ts"
|
|
119
|
+
},
|
|
110
120
|
"./form/context/group/useFormGroupChildIsSubmittingTracker": {
|
|
111
121
|
"default": "./dist/esm/form/context/group/useFormGroupChildIsSubmittingTracker.mjs",
|
|
112
122
|
"types": "./dist/types/form/context/group/useFormGroupChildIsSubmittingTracker.d.ts"
|
|
113
123
|
},
|
|
124
|
+
"./form/context/group/useFormGroupChildTracker": {
|
|
125
|
+
"default": "./dist/esm/form/context/group/useFormGroupChildTracker.mjs",
|
|
126
|
+
"types": "./dist/types/form/context/group/useFormGroupChildTracker.d.ts"
|
|
127
|
+
},
|
|
114
128
|
"./form/context/group/useFormGroupChildIsMountedTracker": {
|
|
115
129
|
"default": "./dist/esm/form/context/group/useFormGroupChildIsMountedTracker.mjs",
|
|
116
130
|
"types": "./dist/types/form/context/group/useFormGroupChildIsMountedTracker.d.ts"
|
|
@@ -159,12 +173,12 @@
|
|
|
159
173
|
"scripts": {
|
|
160
174
|
"build": "npm install && npm run validate && vite build",
|
|
161
175
|
"build:dev": "vite build --config vite.config.dev.ts",
|
|
162
|
-
"dev": "vite",
|
|
176
|
+
"dev": "vite --config vite.config.dev.ts",
|
|
163
177
|
"lint": "eslint --ignore-path ./.gitignore --ext ts,tsx --report-unused-disable-directives --max-warnings 0 .",
|
|
164
178
|
"preinstall": "npx only-allow npm",
|
|
165
179
|
"prepare": "husky",
|
|
166
180
|
"prepublishOnly": "npm run build",
|
|
167
|
-
"preview": "vite preview",
|
|
181
|
+
"preview": "vite preview --config vite.config.dev.ts",
|
|
168
182
|
"test": "vitest",
|
|
169
183
|
"test:ct": "playwright test -c playwright-ct.config.ts",
|
|
170
184
|
"test:ct:ui": "playwright test -c playwright-ct.config.ts --ui",
|