'use client'; import React, { forwardRef, useState } from 'react'; export interface RadioButtonProps extends React.InputHTMLAttributes { label?: string; className?: string; } export const RadioButton = forwardRef( ({ label, className = '', onChange, ...props }, ref) => { // Keep track of focus state for styling const [isFocused, setIsFocused] = useState(false); // Handle click on the custom radio button const handleClick = () => { if (props.disabled) return; // Only trigger onChange if the radio button is not already checked if (!props.checked) { // Create a synthetic event to pass to the onChange handler const syntheticEvent = { target: { name: props.name, checked: true, type: 'radio', value: props.value } } as React.ChangeEvent; // Call the onChange handler with the synthetic event if (onChange) { onChange(syntheticEvent); } } }; return (
setIsFocused(true)} onBlur={() => setIsFocused(false)} onChange={onChange} {...props} />
{props.checked && (
)}
{label && ( )}
); } ); RadioButton.displayName = 'RadioButton';