import { createContext, forwardRef, useContext, useId, type InputHTMLAttributes, } from 'react' import { cn } from '@/lib/utils' interface RadioGroupContextValue { name: string value?: string disabled?: boolean hasError?: boolean onChange?: (value: string) => void } const RadioGroupContext = createContext(null) export interface RadioGroupProps { label?: string description?: string error?: string value?: string defaultValue?: string disabled?: boolean orientation?: 'vertical' | 'horizontal' name?: string onChange?: (value: string) => void children: React.ReactNode className?: string } export const RadioGroup = forwardRef( ( { label, description, error, value, defaultValue, disabled, orientation = 'vertical', name: nameProp, onChange, children, className, }, ref, ) => { const autoId = useId() const name = nameProp ?? autoId const descriptionId = `${name}-description` const errorId = `${name}-error` const hasError = !!error return (
{(label || description) && (
{label && ( {label} )} {description && (

{description}

)}
)}
{children}
{hasError && (

{error}

)}
) }, ) RadioGroup.displayName = 'RadioGroup' export interface RadioProps extends Omit, 'type' | 'size'> { label?: string description?: string value: string } export const Radio = forwardRef( ({ label, description, value, disabled: disabledProp, className, id: idProp, ...props }, ref) => { const autoId = useId() const id = idProp ?? autoId const descriptionId = `${id}-description` const group = useContext(RadioGroupContext) const name = group?.name const isChecked = group?.value != null ? group.value === value : undefined const disabled = disabledProp ?? group?.disabled const hasError = group?.hasError const handleChange = () => { group?.onChange?.(value) } return (
{(label || description) && (
{label && ( )} {description && (

{description}

)}
)}
) }, ) Radio.displayName = 'Radio'