{"version":3,"file":"hooks.mjs","names":[],"sources":["../src/hooks.ts"],"sourcesContent":["\"use client\";\n\nimport type { ValidationErrors } from \"next-safe-action\";\nimport type { SingleInputActionFn } from \"next-safe-action/hooks\";\nimport { useAction, useOptimisticAction } from \"next-safe-action/hooks\";\nimport * as React from \"react\";\nimport type { Resolver } from \"react-hook-form\";\nimport { useForm } from \"react-hook-form\";\nimport type { HookProps, UseHookFormActionHookReturn, UseHookFormOptimisticActionHookReturn } from \"./hooks.types\";\nimport type { ErrorMapperProps } from \"./index\";\nimport { mapToHookFormErrors } from \"./index\";\nimport type { InferInputOrDefault, InferOutputOrDefault, StandardSchemaV1 } from \"./standard-schema\";\n\n// ─── Error Mapper Hook ──────────────────────────────────────────────\n\n/**\n * For more advanced use cases where you want full customization of the hooks used, you can\n * use this hook to map a validation errors object to a `FieldErrors` compatible with react-hook-form.\n * You can then pass the returned `hookFormValidationErrors` property to `useForm`'s `errors` prop.\n *\n * @param validationErrors Validation errors object from `next-safe-action`\n * @returns Object of `FieldErrors` compatible with react-hook-form\n */\nexport function useHookFormActionErrorMapper<Schema extends StandardSchemaV1 | undefined>(\n\tvalidationErrors: ValidationErrors<Schema> | undefined,\n\tprops?: ErrorMapperProps\n) {\n\tconst joinBy = props?.joinBy;\n\n\tconst hookFormValidationErrors = React.useMemo(\n\t\t() => mapToHookFormErrors<Schema>(validationErrors, joinBy !== undefined ? { joinBy } : undefined),\n\t\t[validationErrors, joinBy]\n\t);\n\n\treturn {\n\t\thookFormValidationErrors,\n\t};\n}\n\n// ─── Shared Form Integration ────────────────────────────────────────\n\n/**\n * Internal helper that wires up a safe action result with react-hook-form.\n * Extracts the common logic shared by `useHookFormAction` and `useHookFormOptimisticAction`.\n */\nfunction useFormIntegration<Schema extends StandardSchemaV1 | undefined, FormContext>(\n\taction: { result: { validationErrors?: unknown }; executeAsync: (...args: any[]) => any; reset: () => void },\n\thookFormResolver: Resolver<InferInputOrDefault<Schema, any>, FormContext, InferOutputOrDefault<Schema, any>>,\n\tprops?: {\n\t\terrorMapProps?: ErrorMapperProps;\n\t\tformProps?: Record<string, any>;\n\t}\n) {\n\tconst { hookFormValidationErrors } = useHookFormActionErrorMapper<Schema>(\n\t\taction.result.validationErrors as ValidationErrors<Schema> | undefined,\n\t\tprops?.errorMapProps\n\t);\n\n\tconst form = useForm<InferInputOrDefault<Schema, any>, FormContext, InferOutputOrDefault<Schema, any>>({\n\t\t...props?.formProps,\n\t\tresolver: hookFormResolver,\n\t\terrors: hookFormValidationErrors,\n\t});\n\n\tconst { handleSubmit, reset: resetForm } = form;\n\tconst { executeAsync, reset: resetAction } = action;\n\n\tconst handleSubmitWithAction = React.useCallback(\n\t\t(e?: React.BaseSyntheticEvent) => handleSubmit(executeAsync)(e),\n\t\t[handleSubmit, executeAsync]\n\t);\n\n\tconst resetFormAndAction = React.useCallback(() => {\n\t\tresetForm();\n\t\tresetAction();\n\t}, [resetForm, resetAction]);\n\n\treturn { form, handleSubmitWithAction, resetFormAndAction };\n}\n\n// ─── Public Hooks ───────────────────────────────────────────────────\n\n/**\n * This hook is a wrapper around `useAction` and `useForm` that makes it easier to use safe actions\n * with react-hook-form. It also maps validation errors to `FieldErrors` compatible with react-hook-form.\n *\n * @param safeAction The safe action\n * @param hookFormResolver A react-hook-form validation resolver\n * @param props Optional props for both `useAction`, `useForm` hooks and error mapper\n * @returns An object containing `action` and `form` controllers, `handleSubmitWithAction`, and `resetFormAndAction`\n */\nexport function useHookFormAction<\n\tServerError,\n\tSchema extends StandardSchemaV1 | undefined,\n\tShapedErrors,\n\tData,\n\tFormContext = any,\n>(\n\tsafeAction: SingleInputActionFn<ServerError, Schema, ShapedErrors, Data>,\n\thookFormResolver: Resolver<InferInputOrDefault<Schema, any>, FormContext, InferOutputOrDefault<Schema, any>>,\n\tprops?: HookProps<ServerError, Schema, ShapedErrors, Data, FormContext>\n): UseHookFormActionHookReturn<ServerError, Schema, ShapedErrors, Data, FormContext> {\n\tconst action = useAction(safeAction, props?.actionProps);\n\tconst { form, handleSubmitWithAction, resetFormAndAction } = useFormIntegration<Schema, FormContext>(\n\t\taction,\n\t\thookFormResolver,\n\t\tprops\n\t);\n\n\treturn { action, form, handleSubmitWithAction, resetFormAndAction };\n}\n\n/**\n * This hook is a wrapper around `useOptimisticAction` and `useForm` that makes it easier to use safe actions\n * with react-hook-form. It also maps validation errors to `FieldErrors` compatible with react-hook-form.\n *\n * @param safeAction The safe action\n * @param hookFormResolver A react-hook-form validation resolver\n * @param props Required `currentState` and `updateFn` props for the action, and additional optional\n * props for both `useAction`, `useForm` hooks and error mapper\n * @returns An object containing `action` and `form` controllers, `handleSubmitWithAction`, and `resetFormAndAction`\n */\nexport function useHookFormOptimisticAction<\n\tServerError,\n\tSchema extends StandardSchemaV1 | undefined,\n\tShapedErrors,\n\tData,\n\tState,\n\tFormContext = any,\n>(\n\tsafeAction: SingleInputActionFn<ServerError, Schema, ShapedErrors, Data>,\n\thookFormResolver: Resolver<InferInputOrDefault<Schema, any>, FormContext, InferOutputOrDefault<Schema, any>>,\n\tprops: HookProps<ServerError, Schema, ShapedErrors, Data, FormContext> & {\n\t\tactionProps: {\n\t\t\tcurrentState: State;\n\t\t\tupdateFn: (state: State, input: InferInputOrDefault<Schema, void>) => State;\n\t\t};\n\t}\n): UseHookFormOptimisticActionHookReturn<ServerError, Schema, ShapedErrors, Data, State, FormContext> {\n\tconst action = useOptimisticAction(safeAction, props.actionProps);\n\tconst { form, handleSubmitWithAction, resetFormAndAction } = useFormIntegration<Schema, FormContext>(\n\t\taction,\n\t\thookFormResolver,\n\t\tprops\n\t);\n\n\treturn { action, form, handleSubmitWithAction, resetFormAndAction };\n}\n\nexport type * from \"./hooks.types\";\n"],"mappings":";;;;;;;;;;;;;;AAuBA,SAAgB,6BACf,kBACA,OACC;CACD,MAAM,SAAS,OAAO;AAOtB,QAAO,EACN,0BANgC,MAAM,cAChC,oBAA4B,kBAAkB,WAAW,KAAA,IAAY,EAAE,QAAQ,GAAG,KAAA,EAAU,EAClG,CAAC,kBAAkB,OAAO,CAC1B,EAIA;;;;;;AASF,SAAS,mBACR,QACA,kBACA,OAIC;CACD,MAAM,EAAE,6BAA6B,6BACpC,OAAO,OAAO,kBACd,OAAO,cACP;CAED,MAAM,OAAO,QAA0F;EACtG,GAAG,OAAO;EACV,UAAU;EACV,QAAQ;EACR,CAAC;CAEF,MAAM,EAAE,cAAc,OAAO,cAAc;CAC3C,MAAM,EAAE,cAAc,OAAO,gBAAgB;AAY7C,QAAO;EAAE;EAAM,wBAVgB,MAAM,aACnC,MAAiC,aAAa,aAAa,CAAC,EAAE,EAC/D,CAAC,cAAc,aAAa,CAC5B;EAOsC,oBALZ,MAAM,kBAAkB;AAClD,cAAW;AACX,gBAAa;KACX,CAAC,WAAW,YAAY,CAAC;EAE+B;;;;;;;;;;;AAc5D,SAAgB,kBAOf,YACA,kBACA,OACoF;CACpF,MAAM,SAAS,UAAU,YAAY,OAAO,YAAY;CACxD,MAAM,EAAE,MAAM,wBAAwB,uBAAuB,mBAC5D,QACA,kBACA,MACA;AAED,QAAO;EAAE;EAAQ;EAAM;EAAwB;EAAoB;;;;;;;;;;;;AAapE,SAAgB,4BAQf,YACA,kBACA,OAMqG;CACrG,MAAM,SAAS,oBAAoB,YAAY,MAAM,YAAY;CACjE,MAAM,EAAE,MAAM,wBAAwB,uBAAuB,mBAC5D,QACA,kBACA,MACA;AAED,QAAO;EAAE;EAAQ;EAAM;EAAwB;EAAoB"}