import React, { createContext, useContext } from 'react'; import { useStyles } from '../../core/hooks/useStyles'; import { useTheme } from '../../core/theme/ThemeProvider'; import { Text } from '../Text/Text'; interface RadioGroupContextType { value: string; onChange: (value: string) => void; name: string; } const RadioGroupContext = createContext(null); const useRadioGroup = () => { const context = useContext(RadioGroupContext); if (!context) throw new Error('RadioGroupItem must be used within a RadioGroup.'); return context; }; interface RadioGroupProps { children: React.ReactNode; value: string; onChange: (value: string) => void; name: string; label?: string; className?: string; } export const RadioGroup: React.FC = ({ children, value, onChange, name, label, className }) => { return (
{label && {label}} {children}
); }; interface RadioGroupItemProps { value: string; label: string; className?: string; } export const RadioGroupItem: React.FC = ({ value, label, className }) => { const { value: selectedValue, onChange, name } = useRadioGroup(); const { theme } = useTheme(); const createStyle = useStyles('radio-item'); const isChecked = selectedValue === value; const containerClass = createStyle({ display: 'flex', alignItems: 'center', gap: theme.spacing.sm, cursor: 'pointer', padding: `${theme.spacing.sm} 0`, }); const radioClass = createStyle({ width: '18px', height: '18px', borderRadius: '50%', border: `2px solid ${isChecked ? theme.colors.primary : theme.colors.border}`, display: 'grid', placeContent: 'center', transition: 'all 0.2s', }); const indicatorClass = createStyle({ width: '10px', height: '10px', borderRadius: '50%', backgroundColor: theme.colors.primary, transform: isChecked ? 'scale(1)' : 'scale(0)', transition: 'transform 0.2s', }); const inputClass = createStyle({ position: 'absolute', opacity: 0, width: 0, height: 0, '&:focus-visible + span': { boxShadow: `0 0 0 2px ${theme.colors.background}, 0 0 0 4px ${theme.colors.primary}`, } }); return ( ); };