UNPKG

@next-safe-action/adapter-react-hook-form

Version:

This adapter offers a way to seamlessly integrate next-safe-action with react-hook-form.

128 lines (124 loc) 5.59 kB
import * as react_hook_form from "react-hook-form"; import { UseFormProps, UseFormReturn, Resolver } from "react-hook-form"; import { SafeActionFn, ValidationErrors } from "next-safe-action"; import { HookCallbacks, UseActionHookReturn, UseOptimisticActionHookReturn, HookSafeActionFn, } from "next-safe-action/hooks"; import { S as StandardSchemaV1, E as ErrorMapperProps, I as InferInputOrDefault, a as InferOutputOrDefault, } from "./standard-schema-DVTKKT6T.mjs"; /** * Optional props for `useHookFormAction` and `useHookFormOptimisticAction`. */ type HookProps<ServerError, S extends StandardSchemaV1 | undefined, CVE, Data, FormContext = any> = { errorMapProps?: ErrorMapperProps; actionProps?: HookCallbacks<ServerError, S, CVE, Data>; formProps?: Omit<UseFormProps<InferInputOrDefault<S, any>, FormContext, InferOutputOrDefault<S, any>>, "resolver">; }; /** * Type of the return object of the `useHookFormAction` hook. */ type UseHookFormActionHookReturn<ServerError, S extends StandardSchemaV1 | undefined, CVE, Data, FormContext = any> = { action: UseActionHookReturn<ServerError, S, CVE, Data>; form: UseFormReturn<InferInputOrDefault<S, any>, FormContext, InferOutputOrDefault<S, any>>; handleSubmitWithAction: (e?: React.BaseSyntheticEvent) => Promise<void>; resetFormAndAction: () => void; }; /** * Type of the return object of the `useHookFormOptimisticAction` hook. */ type UseHookFormOptimisticActionHookReturn< ServerError, S extends StandardSchemaV1 | undefined, CVE, Data, State, FormContext = any, > = Omit<UseHookFormActionHookReturn<ServerError, S, CVE, Data, FormContext>, "action"> & { action: UseOptimisticActionHookReturn<ServerError, S, CVE, 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 StandardSchemaV1 | undefined, any, infer CVE, infer Data> ? UseHookFormActionHookReturn<ServerError, S, CVE, 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 StandardSchemaV1 | undefined, any, infer CVE, infer Data> ? UseHookFormOptimisticActionHookReturn<ServerError, S, CVE, 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 StandardSchemaV1 | undefined>( validationErrors: ValidationErrors<S> | undefined, props?: ErrorMapperProps ): { hookFormValidationErrors: react_hook_form.FieldErrors<InferOutputOrDefault<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 StandardSchemaV1 | undefined, CVE, Data, FormContext = any>( safeAction: HookSafeActionFn<ServerError, S, CVE, Data>, hookFormResolver: Resolver<InferInputOrDefault<S, any>, FormContext, InferOutputOrDefault<S, any>>, props?: HookProps<ServerError, S, CVE, Data, FormContext> ): UseHookFormActionHookReturn<ServerError, S, CVE, 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 StandardSchemaV1 | undefined, CVE, Data, State, FormContext = any, >( safeAction: HookSafeActionFn<ServerError, S, CVE, Data>, hookFormResolver: Resolver<InferInputOrDefault<S, any>, FormContext, InferOutputOrDefault<S, any>>, props: HookProps<ServerError, S, CVE, Data, FormContext> & { actionProps: { currentState: State; updateFn: (state: State, input: InferInputOrDefault<S, void>) => State; }; } ): UseHookFormOptimisticActionHookReturn<ServerError, S, CVE, Data, State, FormContext>; export { type HookProps, type InferUseHookFormActionHookReturn, type InferUseHookFormOptimisticActionHookReturn, type UseHookFormActionHookReturn, type UseHookFormOptimisticActionHookReturn, useHookFormAction, useHookFormActionErrorMapper, useHookFormOptimisticAction, };