@next-safe-action/adapter-react-hook-form
Version:
This adapter offers a way to seamlessly integrate next-safe-action with react-hook-form.
170 lines (166 loc) • 6.01 kB
text/typescript
import * as react_hook_form from "react-hook-form";
import { UseFormProps, UseFormReturn, Resolver } from "react-hook-form";
import * as zod from "zod";
import { SafeActionFn, ValidationErrors } from "next-safe-action";
import { Schema, Infer, InferIn } from "next-safe-action/adapters/types";
import {
HookBaseUtils,
HookCallbacks,
UseActionHookReturn,
UseOptimisticActionHookReturn,
HookSafeActionFn,
} from "next-safe-action/hooks";
import { E as ErrorMapperProps } from "./index.types-B_yD8Cza.mjs";
/**
* Optional props for `useHookFormAction` and `useHookFormOptimisticAction`.
*/
type HookProps<
ServerError,
S extends Schema | undefined,
BAS extends readonly Schema[],
CVE,
CBAVE,
Data,
FormContext = any,
> = {
errorMapProps?: ErrorMapperProps;
actionProps?: HookBaseUtils<S> & HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>;
formProps?: Omit<UseFormProps<S extends Schema ? Infer<S> : any, FormContext>, "resolver">;
};
/**
* Type of the return object of the `useHookFormAction` hook.
*/
type UseHookFormActionHookReturn<
ServerError,
S extends Schema | undefined,
BAS extends readonly Schema[],
CVE,
CBAVE,
Data,
FormContext = any,
> = {
action: UseActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data>;
form: UseFormReturn<S extends Schema ? Infer<S> : any, FormContext>;
handleSubmitWithAction: (e?: React.BaseSyntheticEvent) => Promise<void>;
resetFormAndAction: () => void;
};
/**
* Type of the return object of the `useHookFormOptimisticAction` hook.
*/
type UseHookFormOptimisticActionHookReturn<
ServerError,
S extends Schema | undefined,
BAS extends readonly Schema[],
CVE,
CBAVE,
Data,
State,
FormContext = any,
> = Omit<UseHookFormActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, FormContext>, "action"> & {
action: UseOptimisticActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, State>;
};
/**
* Infer the type of the return object of the `useHookFormAction` hook.
*/
type InferUseHookFormActionHookReturn<T extends Function, FormContext = any> =
T extends SafeActionFn<
infer ServerError,
infer S extends Schema | undefined,
infer BAS extends readonly Schema[],
infer CVE,
infer CBAVE,
infer Data
>
? UseHookFormActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, FormContext>
: never;
/**
* Infer the type of the return object of the `useHookFormOptimisticAction` hook.
*/
type InferUseHookFormOptimisticActionHookReturn<T extends Function, State, FormContext = any> =
T extends SafeActionFn<
infer ServerError,
infer S extends Schema | undefined,
infer BAS extends readonly Schema[],
infer CVE,
infer CBAVE,
infer Data
>
? UseHookFormOptimisticActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, State, FormContext>
: never;
/**
* For more advanced use cases where you want full customization of the hooks used, you can
* use this hook to map a validation errors object to a `FieldErrors` compatible with react-hook-form.
* You can then pass the returned `hookFormValidationErrors` property to `useForm`'s `errors` prop.
*
* @param validationErrors Validation errors object from `next-safe-action`
* @returns Object of `FieldErrors` compatible with react-hook-form
*/
declare function useHookFormActionErrorMapper<S extends Schema | undefined>(
validationErrors: ValidationErrors<S> | undefined,
props?: ErrorMapperProps
): {
hookFormValidationErrors:
| react_hook_form.FieldErrors<S extends zod.ZodType<any, zod.ZodTypeDef, any> ? Infer<S> : any>
| undefined;
};
/**
* This hook is a wrapper around `useAction` and `useForm` that makes it easier to use safe actions
* with react-hook-form. It also maps validation errors to `FieldErrors` compatible with react-hook-form.
*
* @param safeAction The safe action
* @param hookFormResolver A react-hook-form validation resolver
* @param props Optional props for both `useAction`, `useForm` hooks and error mapper
* @returns An object containing `action` and `form` controllers, `handleActionSubmit`, and `resetFormAndAction`
*/
declare function useHookFormAction<
ServerError,
S extends Schema | undefined,
BAS extends readonly Schema[],
CVE,
CBAVE,
Data,
FormContext = any,
>(
safeAction: HookSafeActionFn<ServerError, S, BAS, CVE, CBAVE, Data>,
hookFormResolver: Resolver<S extends Schema ? Infer<S> : any, FormContext>,
props?: HookProps<ServerError, S, BAS, CVE, CBAVE, Data, FormContext>
): UseHookFormActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, FormContext>;
/**
* This hook is a wrapper around `useOptimisticAction` and `useForm` that makes it easier to use safe actions
* with react-hook-form. It also maps validation errors to `FieldErrors` compatible with react-hook-form.
*
* @param safeAction The safe action
* @param hookFormResolver A react-hook-form validation resolver
* @param props Required `currentState` and `updateFn` props for the action, and additional optional
* props for both `useAction`, `useForm` hooks and error mapper
* @returns An object containing `action` and `form` controllers, `handleActionSubmit`, and `resetFormAndAction`
*/
declare function useHookFormOptimisticAction<
ServerError,
S extends Schema | undefined,
BAS extends readonly Schema[],
CVE,
CBAVE,
Data,
State,
FormContext = any,
>(
safeAction: HookSafeActionFn<ServerError, S, BAS, CVE, CBAVE, Data>,
hookFormResolver: Resolver<S extends Schema ? Infer<S> : any, FormContext>,
props: HookProps<ServerError, S, BAS, CVE, CBAVE, Data, FormContext> & {
actionProps: {
currentState: State;
updateFn: (state: State, input: S extends Schema ? InferIn<S> : undefined) => State;
};
}
): UseHookFormOptimisticActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, State, FormContext>;
export {
type HookProps,
type InferUseHookFormActionHookReturn,
type InferUseHookFormOptimisticActionHookReturn,
type UseHookFormActionHookReturn,
type UseHookFormOptimisticActionHookReturn,
useHookFormAction,
useHookFormActionErrorMapper,
useHookFormOptimisticAction,
};