import React, { useEffect, useState } from 'react'; import { FiCircle } from 'react-icons/fi'; import { HiCheckCircle } from 'react-icons/hi'; import ErrorMessage from './ErrorMessage'; interface Option { icon?: React.ReactElement; label: string; description?: string; value: string; } export interface RadioGroupProps { errors?: string[]; value?: string; options: Option[]; allowDeselect?: boolean; className?: string; onChange?: (selected: string) => void; } const RadioGroup: React.FC = (props) => { const [selected, setSelected] = useState(props.value); const isSelected = (value: string) => value === selected; useEffect(() => { setSelected(props.value); }, [props.value]); useEffect(() => { props.onChange && props.onChange(selected ?? ''); }, [selected]); return (
{props.options.map((item, index) => (
{ if (isSelected(item.value)) { if (props.allowDeselect) { setSelected(undefined); } } else { setSelected(item.value); } }} >
{item.icon && (
{item.icon}
)}
{item.description && (

{item.description}

)}
{!isSelected(item.value) && } {isSelected(item.value) && }
))}
); }; export default RadioGroup;