Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 1x 1x 1x 1x 1x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 5x 4x 4x 40x 40x 40x 40x 40x 40x 40x 40x 9x 9x 9x 9x 40x 40x 40x 40x 40x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 3x 3x 3x 115x 115x 115x 40x 40x 40x 40x 40x 8x 8x 6x 6x 6x 6x 6x 6x 6x 8x 8x 2x 2x 2x 2x 2x 2x 8x 8x 40x 40x 40x 40x 1x 1x 1x | import React, { forwardRef } from 'react';
import { RadioGroupProps } from '@/types/ui';
export const RadioGroup = forwardRef<HTMLInputElement, RadioGroupProps>(
(
{
label,
options,
value,
onChange,
disabled = false,
error,
helperText,
state = 'default',
size = 'md',
layout = 'vertical',
required = false,
name,
className = '',
...props
},
ref
) => {
// Derive state from error prop if provided
const effectiveState = error ? 'error' : state;
// Size classes for radio buttons
const sizeClasses = {
sm: 'w-3.5 h-3.5',
md: 'w-4 h-4',
lg: 'w-5 h-5'};
// State-based border colors
const stateClasses = {
default: 'border-gray-3',
success: 'border-green',
error: 'border-red',
warning: 'border-yellow'};
// Label text size
const labelSizeClasses = {
sm: 'text-sm',
md: 'text-base',
lg: 'text-lg'};
// Helper text size
const helperTextSizeClasses = {
sm: 'text-xs',
md: 'text-sm',
lg: 'text-base'};
// Layout classes
const layoutClasses = {
vertical: 'flex flex-col space-y-2',
horizontal: 'flex flex-row flex-wrap gap-4'};
const handleChange = (optionValue: string) => {
if (!disabled && onChange) {
onChange(optionValue);
}
};
const groupId = `radio-group-${name}`;
return (
<fieldset className={className} aria-invalid={effectiveState === 'error'} aria-describedby={error ? `${groupId}-error` : helperText ? `${groupId}-helper` : undefined}>
{/* Legend (main label) */}
{label && (
<legend className={`font-medium text-dark dark:text-gray-100 mb-2 ${labelSizeClasses[size]}`}>
{label}
{required && <span className="text-red ml-1">*</span>}
</legend>
)}
{/* Radio options */}
<div className={layoutClasses[layout]}>
{options.map((option, index) => {
const optionId = `${groupId}-option-${index}`;
const isDisabled = disabled || option.disabled;
const isChecked = value === option.value;
return (
<div key={option.value} className="flex flex-col">
<label
htmlFor={optionId}
className={`flex items-center gap-2 ${
isDisabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer'
}`}
>
<input
ref={index === 0 ? ref : undefined}
type="radio"
id={optionId}
name={name}
value={option.value}
checked={isChecked}
onChange={() => handleChange(option.value)}
disabled={isDisabled}
className={`
${sizeClasses[size]}
${stateClasses[effectiveState]}
text-blue
focus:ring-2
focus:ring-blue
focus:ring-offset-1
dark:focus:ring-offset-gray-900
${isDisabled ? 'cursor-not-allowed bg-gray-2 dark:bg-gray-700' : 'bg-white dark:bg-gray-800 cursor-pointer'}
`}
{...props}
/>
<span className={`text-dark dark:text-gray-100 ${labelSizeClasses[size]} ${isDisabled ? 'opacity-50' : ''}`}>
{option.label}
</span>
</label>
{/* Option-level helper text */}
{option.helperText && (
<p className={`text-dark-5 dark:text-gray-400 ${helperTextSizeClasses[size]} ml-6 mt-0.5`}>
{option.helperText}
</p>
)}
</div>
);
})}
</div>
{/* Group-level helper text or error message */}
{(helperText || error) && (
<div className="mt-2">
{error && (
<p
id={`${groupId}-error`}
className={`text-red ${helperTextSizeClasses[size]}`}
role="alert"
>
{error}
</p>
)}
{helperText && !error && (
<p
id={`${groupId}-helper`}
className={`text-dark-5 dark:text-gray-400 ${helperTextSizeClasses[size]}`}
>
{helperText}
</p>
)}
</div>
)}
</fieldset>
);
}
);
RadioGroup.displayName = 'RadioGroup';
|