"use client" import * as React from "react" import * as LabelPrimitive from "@radix-ui/react-label" import { Slot } from "@radix-ui/react-slot" import { Controller, type ControllerProps, type FieldPath, type FieldValues, useFormContext, } from "react-hook-form" import { cn } from "@/lib/utils" const Form = React.forwardRef< HTMLFormElement, React.ComponentProps<"form"> >(({ className, ...props }, ref) => { const methods = useFormContext() return (
{})} {...props} /> ) }) Form.displayName = "Form" // ============================================================ // FormFieldContext - provides field-level context // ============================================================ const FormFieldContext = React.createContext< ControllerProps >({} as never) const FormField = < TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath >({ ...props }: ControllerProps) => { const { name } = props return ( }> ) } // ============================================================ // useFormField - must be used inside a FormField // ============================================================ function useFormField(): ControllerProps { const fieldContext = React.useContext(FormFieldContext) if (!fieldContext) { throw new Error("useFormField must be used within a FormField") } return fieldContext } // ============================================================ // FormItem - wraps a labeled form control // ============================================================ const FormItem = React.forwardRef< HTMLDivElement, React.HTMLAttributes >(({ className, ...props }, ref) => (
)) FormItem.displayName = "FormItem" // ============================================================ // FormLabel // ============================================================ const FormLabel = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) FormLabel.displayName = LabelPrimitive.Root.displayName // ============================================================ // FormControl - Slot bridge // ============================================================ const FormControl = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ ...props }, ref) => ( )) FormControl.displayName = "FormControl" // ============================================================ // FormDescription // ============================================================ const FormDescription = React.forwardRef< HTMLParagraphElement, React.HTMLAttributes >(({ className, ...props }, ref) => (

)) FormDescription.displayName = "FormDescription" // ============================================================ // FormMessage // ============================================================ const FormMessage = React.forwardRef< HTMLParagraphElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => { const { error } = useFormField() const body = error ? String(error.message ?? "") : children if (!body) return null return (

{body}

) }) FormMessage.displayName = "FormMessage" export { Form, FormField, FormItem, FormLabel, FormControl, FormDescription, FormMessage, useFormField, }