"use client"; import * as React from "react"; import { cn } from "../../lib/utils"; interface RadioGroupContextType { value: string; onValueChange: (value: string) => void; name?: string; orientation?: "horizontal" | "vertical"; } const RadioGroupContext = React.createContext< RadioGroupContextType | undefined >(undefined); interface RadioGroupProps extends React.HTMLAttributes { value?: string; defaultValue?: string; onValueChange?: (value: string) => void; name?: string; disabled?: boolean; orientation?: "horizontal" | "vertical"; } const RadioGroup = React.forwardRef( ( { className, value, defaultValue, onValueChange, name, orientation = "vertical", ...props }, ref ) => { const [selectedValue, setSelectedValue] = React.useState( value || defaultValue || "" ); React.useEffect(() => { if (value !== undefined) { setSelectedValue(value); } }, [value]); const handleValueChange = React.useCallback( (newValue: string) => { if (value === undefined) { setSelectedValue(newValue); } onValueChange?.(newValue); }, [onValueChange, value] ); return (
); } ); RadioGroup.displayName = "RadioGroup"; interface RadioGroupItemProps extends Omit, "value"> { value: string; customSize?: "sm" | "md" | "lg"; } const RadioGroupItem = React.forwardRef( ({ className, value, customSize = "md", ...props }, ref) => { const context = React.useContext(RadioGroupContext); if (!context) { throw new Error("RadioGroupItem must be used within a RadioGroup"); } const { value: selectedValue, onValueChange, name } = context; const checked = selectedValue === value; const [focused, setFocused] = React.useState(false); const radioRef = React.useRef(null); // Size mappings for custom sizes const sizeMap = { sm: { outer: "h-3.5 w-3.5", inner: "h-1.5 w-1.5", icon: "h-2 w-2" }, md: { outer: "h-4 w-4", inner: "h-2 w-2", icon: "h-2.5 w-2.5" }, lg: { outer: "h-5 w-5", inner: "h-2.5 w-2.5", icon: "h-3 w-3" }, }; const itemSize = sizeMap[customSize]; const handleChange = () => { onValueChange(value); }; const handleFocus = () => setFocused(true); const handleBlur = () => setFocused(false); return (
radioRef.current?.click()} >
{props.children && ( )}
); } ); RadioGroupItem.displayName = "RadioGroupItem"; export { RadioGroup, RadioGroupItem };