import { zodResolver } from "@hookform/resolvers/zod"; import type { ComponentProps } from "react"; import type { FieldValues, SubmitHandler, UseFormProps, UseFormReturn } from "react-hook-form"; import { FormProvider, useForm, useFormContext } from "react-hook-form"; import type { TypeOf, ZodSchema, ZodType } from "zod"; import cn from "@/helpers/cn"; import { H6 } from "./Typography"; interface UseZodFormProps> extends UseFormProps> { schema: T; } export const useZodForm = >({ schema, ...formConfig }: UseZodFormProps) => { return useForm>({ ...formConfig, resolver: (zodResolver as any)(schema) as any }); }; interface FieldErrorProps { name?: string; } export const FieldError = ({ name }: FieldErrorProps) => { const { formState: { errors } } = useFormContext(); if (!name) { return null; } const error = errors[name]; if (!error) { return null; } return
{error.message as string}
; }; interface FormProps> extends Omit, "onSubmit"> { className?: string; form: UseFormReturn; onSubmit: SubmitHandler; } export const Form = ({ children, className = "", form, onSubmit }: FormProps) => { return (
{children}
); };