'use client'; import { Slot } from '@radix-ui/react-slot'; import { type ComponentPropsWithoutRef, type ComponentRef, type HTMLAttributes, createContext, forwardRef, useContext, useId, } from 'react'; import { Controller, type ControllerProps, type FieldPath, type FieldValues, FormProvider, useFormContext, } from 'react-hook-form'; import { cn } from '@/utils/tw-merge'; import { Label } from './Label'; import type * as LabelPrimitive from '@radix-ui/react-label'; const Form = FormProvider; type FormFieldContextValue< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, > = { name: TName; }; const FormFieldContext = createContext( {} as FormFieldContextValue, ); const FormField = < TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, >({ ...props }: ControllerProps) => ( // eslint-disable-next-line react/jsx-no-constructed-context-values ); type FormItemContextValue = { id: string; }; const FormItemContext = createContext( {} as FormItemContextValue, ); const FormItem = forwardRef>( ({ className, ...props }, ref) => { const id = useId(); return ( // eslint-disable-next-line react/jsx-no-constructed-context-values
); }, ); FormItem.displayName = 'FormItem'; const useFormField = () => { const fieldContext = useContext(FormFieldContext); const itemContext = useContext(FormItemContext); const { getFieldState, formState } = useFormContext(); const fieldState = getFieldState(fieldContext.name, formState); if (!fieldContext) { throw new Error('useFormField should be used within '); } const { id } = itemContext; return { id, name: fieldContext.name, formItemId: `${id}-form-item`, formDescriptionId: `${id}-form-item-description`, formMessageId: `${id}-form-item-message`, ...fieldState, }; }; const FormLabel = forwardRef< ComponentRef, ComponentPropsWithoutRef >(({ className, ...props }, ref) => { const { error, formItemId } = useFormField(); return (