"use client" import React, { type ComponentPropsWithoutRef, ReactElement, type ReactNode, isValidElement } from "react" import { Info } from "lucide-react" import { Label } from "./ui/label" import { Button } from "./ui/button" import { cn } from "../_lib/utils" interface FormFieldProps extends ComponentPropsWithoutRef<"div"> { label: string link?: boolean linkText?: string onClick?: () => void input: ReactElement error?: string description?: string required?: boolean size?: "sm" | "md" | "lg" labelClassName?: string inputWrapperClassName?: string hideLabel?: boolean id?: string } export function FormField({ label, link, linkText, onClick, input, error, description, required = false, size = "md", labelClassName, inputWrapperClassName, hideLabel = false, id, className, ...props }: FormFieldProps) { // Generate an ID for connecting label with input if not provided const fieldId = id || label.toLowerCase().replace(/\s+/g, "-") // Size-based spacing const sizeClasses = { sm: "space-y-1", md: "space-y-2", lg: "space-y-3", } // Safely clone the input element if it's a valid React element const inputElement = isValidElement(input) ? React.cloneElement(input as ReactElement, { id: fieldId, "aria-invalid": error ? "true" : "false", "aria-describedby": error ? `${fieldId}-error` : description ? `${fieldId}-description` : undefined, }) : input return (
{!hideLabel && ( )} {link && ( )}
{inputElement}
{description && !error && (

{description}

)} {error && ( )}
) }