The RadioGroup component provides a set of radio buttons that allow users to select a single option from multiple choices. It's designed for forms and settings where only one option can be selected.
import { RadioGroup } from '@dashflowx/core'export default function RadioGroupExample() {const items = [{ id: 'option1', title: 'Option 1', value: 'option1' },{ id: 'option2', title: 'Option 2', value: 'option2' },{ id: 'option3', title: 'Option 3', value: 'option3' }];return (<RadioGroupitems={items}defaultValue="option1"/>)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
items | Array<iRadioGroupItem> | [] | Array of radio button items |
defaultValue | string | - | Default selected value |
className | string | '' | Additional CSS classes |
interface iRadioGroupItem {id: string;title: string;value: string;handleClick?: any;}
import { RadioGroup } from '@dashflowx/core'export default function BasicRadioGroup() {const items = [{ id: 'option1', title: 'Option 1', value: 'option1' },{ id: 'option2', title: 'Option 2', value: 'option2' },{ id: 'option3', title: 'Option 3', value: 'option3' }];return (<RadioGroupitems={items}defaultValue="option1"/>)}
Preview:
import { RadioGroup } from '@dashflowx/core'export default function CustomStyledRadioGroup() {const items = [{ id: 'option1', title: 'Option 1', value: 'option1' },{ id: 'option2', title: 'Option 2', value: 'option2' },{ id: 'option3', title: 'Option 3', value: 'option3' }];return (<RadioGroupitems={items}defaultValue="option1"className="border border-blue-500 rounded-lg p-4"/>)}
Preview:
import { RadioGroup } from '@dashflowx/core'export default function FormRadioGroup() {const items = [{ id: 'male', title: 'Male', value: 'male' },{ id: 'female', title: 'Female', value: 'female' },{ id: 'other', title: 'Other', value: 'other' }];return (<RadioGroupitems={items}defaultValue="male"/>)}
Preview:
import { RadioGroup } from '@dashflowx/core'export default function SettingsRadioGroup() {const items = [{ id: 'light', title: 'Light Theme', value: 'light' },{ id: 'dark', title: 'Dark Theme', value: 'dark' },{ id: 'auto', title: 'Auto', value: 'auto' }];return (<RadioGroupitems={items}defaultValue="auto"/>)}
Preview:
import { RadioGroup } from '@dashflowx/core'export default function LanguageSelection() {const items = [{ id: 'en', title: 'English', value: 'en' },{ id: 'es', title: 'Spanish', value: 'es' },{ id: 'fr', title: 'French', value: 'fr' },{ id: 'de', title: 'German', value: 'de' }];return (<RadioGroupitems={items}defaultValue="en"/>)}
Preview:
const genderSelection = (<RadioGroupitems={[{ id: 'male', title: 'Male', value: 'male' },{ id: 'female', title: 'Female', value: 'female' },{ id: 'other', title: 'Other', value: 'other' }]}defaultValue="male"/>);
const themeSelection = (<RadioGroupitems={[{ id: 'light', title: 'Light', value: 'light' },{ id: 'dark', title: 'Dark', value: 'dark' },{ id: 'auto', title: 'Auto', value: 'auto' }]}defaultValue="auto"/>);
const languageSelection = (<RadioGroupitems={[{ id: 'en', title: 'English', value: 'en' },{ id: 'es', title: 'Spanish', value: 'es' },{ id: 'fr', title: 'French', value: 'fr' }]}defaultValue="en"/>);
const sizeSelection = (<RadioGroupitems={[{ id: 'small', title: 'Small', value: 'small' },{ id: 'medium', title: 'Medium', value: 'medium' },{ id: 'large', title: 'Large', value: 'large' }]}defaultValue="medium"/>);
Use clear and descriptive labels for radio options.
// ✅ Good - Clear labelsconst items = [{ id: 'light', title: 'Light Theme', value: 'light' },{ id: 'dark', title: 'Dark Theme', value: 'dark' }];// ❌ Avoid - Unclear labelsconst items = [{ id: 'opt1', title: 'Option 1', value: 'opt1' },{ id: 'opt2', title: 'Option 2', value: 'opt2' }];
Set logical default values based on context.
// ✅ Good - Logical default<RadioGroup defaultValue="auto" />// ❌ Avoid - Random default<RadioGroup defaultValue="option2" />
Maintain consistent spacing between radio options.
// ✅ Good - Consistent spacing<RadioGroup className="space-y-2" />// ❌ Avoid - Inconsistent spacing<RadioGroup className="space-y-1" />
Ensure all radio options have proper labels.
// ✅ Good - Proper labelsconst items = [{ id: 'option1', title: 'Option 1', value: 'option1' }];// ❌ Avoid - Missing labelsconst items = [{ id: 'option1', title: '', value: 'option1' }];
Use unique IDs for each radio option.
// ✅ Good - Unique IDsconst items = [{ id: 'theme-light', title: 'Light', value: 'light' },{ id: 'theme-dark', title: 'Dark', value: 'dark' }];// ❌ Avoid - Duplicate IDsconst items = [{ id: 'option', title: 'Light', value: 'light' },{ id: 'option', title: 'Dark', value: 'dark' }];
You can customize the radio group appearance using CSS classes:
<RadioGroupitems={items}defaultValue="option1"className="border border-gray-300 rounded-lg p-4"/>
Use different themes for different contexts:
// Light theme<RadioGroup className="bg-white border-gray-200" />// Dark theme<RadioGroup className="bg-gray-900 border-gray-700 text-white" />// Brand theme<RadioGroup className="bg-blue-50 border-blue-200" />
Use different sizes for different emphasis levels:
// Small radio group<RadioGroup className="text-sm" />// Standard radio group<RadioGroup className="text-base" />// Large radio group<RadioGroup className="text-lg" />
Load radio options dynamically:
const [radioOptions, setRadioOptions] = useState([]);useEffect(() => {// Load options based on user permissionsloadRadioOptions().then(setRadioOptions);}, []);
Show/hide radio options based on conditions:
const getRadioOptions = (userRole) => {const baseOptions = [{ id: 'basic', title: 'Basic', value: 'basic' }];if (userRole === 'admin') {baseOptions.push({ id: 'admin', title: 'Admin', value: 'admin' });}return baseOptions;};
Handle radio group state:
const [selectedValue, setSelectedValue] = useState('option1');const handleRadioChange = (value) => {setSelectedValue(value);// Handle value change};
- Checkbox: For multiple selections
- Switch: For toggle options
- Select: For dropdown selections
- Form: For form containers
- Label: For form labels
For the complete API reference and advanced usage patterns, see the RadioGroup API documentation.
