RadioGroup
A few options in sight, of which exactly one is chosen. Choosing another one lets go of the first.
import { RadioGroup, RadioGroupSkeleton } from '@sypnia/ui/react'
One of a few
The role of a new member. Arrow keys move the choice.
The invitation will say «editor». It can be changed later.
Code
import { useState } from 'react';
import { Field, RadioGroup, type Option } from '@sypnia/ui/react';
const roles: Option[] = [
{ value: 'owner', label: 'Owner', description: 'Everything, including billing and deleting the project.' },
{ value: 'editor', label: 'Editor', description: 'Changes data and tables, not the team.' },
{ value: 'viewer', label: 'Viewer', description: 'Reads data, and nothing else.' },
];
export default function Basic() {
const [role, setRole] = useState('editor');
return (
<Field label="Role" hint={`The invitation will say «${role}». It can be changed later.`}>
<RadioGroup options={roles} value={role} onChange={setRole} />
</Field>
);
} In a row
Short labels can sit side by side.
Code
import { Field, RadioGroup } from '@sypnia/ui/react';
const formats = [{ value: 'csv', label: 'CSV' }, { value: 'json', label: 'JSON' }, { value: 'sql', label: 'SQL' }, { value: 'parquet', label: 'Parquet' }];
export default function Horizontal() {
return (
<Field label="Export as">
<RadioGroup orientation="horizontal" options={formats} defaultValue="csv" />
</Field>
);
} States
Nothing chosen with an error that says how to fix it, and disabled.
Choose how often to back up before turning backups on.
The Free plan backs up every week.
Code
import { Field, FormGrid, RadioGroup } from '@sypnia/ui/react';
const frequency = [{ value: 'hourly', label: 'Every hour' }, { value: 'daily', label: 'Every day' }, { value: 'weekly', label: 'Every week' }];
export default function States() {
return (
<FormGrid>
<Field label="Back up" error="Choose how often to back up before turning backups on.">
<RadioGroup options={frequency} />
</Field>
<Field label="Back up" hint="The Free plan backs up every week.">
<RadioGroup disabled options={frequency} defaultValue="weekly" />
</Field>
</FormGrid>
);
} Skeleton
The real piece and its skeleton: the same box, so nothing moves when the data arrives.
Code
import type { CSSProperties } from 'react';
import { RadioGroup, RadioGroupSkeleton } from '@sypnia/ui/react';
// Options that come from the server. The skeleton has the same box as the real one, so nothing moves when the data arrives.
export default function Skeleton() {
return (
<div className="sy-grid" style={{ '--min': '16rem' } as CSSProperties}>
<RadioGroup aria-label="Region" options={[{ value: 'us', label: 'United States' }, { value: 'eu', label: 'Europe' }, { value: 'sa', label: 'South America' }]} defaultValue="sa" />
<RadioGroupSkeleton items={3} />
</div>
);
} When to use it
Use it when
- Two to five options that exclude each other, and seeing all of them helps to decide: a role, a format, a frequency.
- Each option can carry a line that explains it.
Do not use it when
- Options need a title and a sentence each, like plans: RadioCards.
- There are more than five or six: Select keeps the form short.
- Several can be chosen: CheckboxGroup.
- It changes the view at once instead of being saved with a form: SegmentedControl or Tabs.
For everyone
- The group is one tab stop: Tab reaches the chosen option, and the arrows move the choice. These are native radios.
- It is announced as a
radiogroupwith the question as its name, and each option says its position («2 of 3»). - Start with nothing chosen when no option is a safe default; never choose for the person something that costs money or deletes data.
- Inside a Field it takes the label, the hint and the error by itself; alone, give it an
aria-label.
Props
| RadioGroup | Type | Default |
|---|---|---|
optionsrequired | Option[] | |
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
className | string | |
defaultValue | string | |
disabled | boolean | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
nameThe name of the value when it travels in a form. | string | |
onChange | ((value: string) => void) | |
orientationvertical: one under another (the default, easier to scan). horizontal: in a row that wraps, for short labels. | "horizontal" | "vertical" | |
value | string | null |
| RadioGroupSkeleton | Type | Default |
|---|---|---|
items | number | 3 |
orientation | "horizontal" | "vertical" |