import clsx from "clsx"; import { ChangeEvent, FC, HTMLAttributes, useRef, useState } from "react"; import styles from "./styles.module.css"; export interface RadioOption { label: string; value: string | number; description?: string; disabled?: boolean; } export interface RadioGroupProps extends Omit, "onChange"> { name: string; initialValue?: string | number; title: string; options: RadioOption[]; description?: string; onChange?: (e: ChangeEvent) => void; onChangeConfirm?: ( e: ChangeEvent, newValue: string | number, oldValue: string | number, accept: () => void, reject: () => void ) => void; disabled?: boolean; themed?: boolean; } export const RadioGroup: FC = ({ className, name, initialValue, title, options, description, disabled, onChange, onChangeConfirm, themed, ...props }) => { const radioGroupRef = useRef(null!); const [selectedValue, setSelectedValue] = useState( initialValue || options[0]?.value ); const handleChange = (e: ChangeEvent) => { const newValue = typeof selectedValue === "number" ? Number(e.target.value) : e.target.value; if (onChangeConfirm) { onChangeConfirm( e, newValue, selectedValue, () => { if (onChange) onChange(e); setSelectedValue(newValue); }, () => { // noop } ); } else { if (onChange) onChange(e); setSelectedValue(newValue); } }; return (
{title} {description &&

{description}

}
{options?.map((option) => { const id = `${name}_${option.value}`; const checked = String(selectedValue) === String(option.value); return (
{option.description && (

{option.description}

)}
); })}
); };