Radio buttons allow the user to select one option from a set. Use RadioGroup for a complete set with validation support.
Basic RadioGroup
Radio group with label and options
await import('/components/data-input/radio.js');
const { tags, $ } = Lightview;
const { div, RadioGroup } = tags;
// 1. Basic radio group
const basic = RadioGroup({
label: 'Favorite Color',
name: 'color',
options: ['Red', 'Blue', 'Green', 'Yellow'],
color: 'primary'
});
// 2. With descriptions
const withDesc = RadioGroup({
label: 'Subscription Plan',
name: 'plan',
options: [
{ value: 'free', label: 'Free', description: 'Basic features, limited usage' },
{ value: 'pro', label: 'Pro', description: '$9/month, all features' },
{ value: 'enterprise', label: 'Enterprise', description: 'Custom pricing, priority support' }
],
color: 'secondary'
});
// 3. Horizontal layout
const horizontal = RadioGroup({
label: 'Size',
name: 'size',
options: ['S', 'M', 'L', 'XL'],
horizontal: true,
color: 'accent'
});
// Insert all examples
$('#example').content(
div({ style: 'display: flex; flex-direction: column; gap: 1.5rem; max-width: 28rem;' }, basic, withDesc, horizontal)
);
Reactive Example
Two-way binding with signals and validation
await import('/components/data-input/radio.js');
const { signal, tags, $ } = Lightview;
const { div, p, span, RadioGroup } = tags;
// Signal for selected value
const selectedPriority = signal('');
// Priority descriptions
const priorityInfo = {
low: { emoji: '🟢', text: 'Can wait, no rush' },
medium: { emoji: '🟡', text: 'Should be addressed soon' },
high: { emoji: '🟠', text: 'Needs attention today' },
critical: { emoji: '🔴', text: 'Drop everything and fix now!' }
};
// Reactive radio group
const reactiveDemo = div({ style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;' },
RadioGroup({
label: 'Issue Priority',
name: 'priority',
value: selectedPriority,
options: [
{ value: 'low', label: 'Low' },
{ value: 'medium', label: 'Medium' },
{ value: 'high', label: 'High' },
{ value: 'critical', label: 'Critical' }
],
required: true,
color: 'primary'
}),
p({ style: 'font-size: 1.125rem;' },
() => {
const priority = selectedPriority.value;
if (!priority) return span({ style: 'opacity: 0.5;' }, 'Select a priority level');
const info = priorityInfo[priority];
return span({}, info.emoji, ' ', info.text);
}
)
);
$('#example').content(reactiveDemo);