RadioGroup
Unas pocas opciones a la vista, de las que se elige exactamente una. Elegir otra suelta la primera.
import { RadioGroup, RadioGroupSkeleton } from '@sypnia/ui/react'
Una de pocas
El rol de un nuevo miembro. Las flechas mueven la elección.
The invitation will say «editor». It can be changed later.
Código
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>
);
} En una fila
Las etiquetas cortas pueden ir lado a lado.
Código
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>
);
} Estados
Nada elegido con un error que dice cómo arreglarlo, y desactivado.
Choose how often to back up before turning backups on.
The Free plan backs up every week.
Código
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
La pieza real y su skeleton: la misma caja, así nada se mueve cuando llegan los datos.
Código
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>
);
} Cuándo usarlo
Úsalo cuando
- De dos a cinco opciones que se excluyen, y verlas todas ayuda a decidir: un rol, un formato, una frecuencia.
- Cada opción puede llevar una línea que la explica.
No lo uses cuando
- Las opciones necesitan un título y una frase cada una, como los planes: RadioCards.
- Hay más de cinco o seis: Select mantiene corto el formulario.
- Se pueden elegir varias: CheckboxGroup.
- Cambia la vista de inmediato en vez de guardarse con un formulario: SegmentedControl o Tabs.
Para todos
- El grupo es una parada de Tab: Tab llega a la opción elegida y las flechas mueven la elección. Son radios nativos.
- Se anuncia como
radiogroupcon la pregunta como nombre, y cada opción dice su posición («2 de 3»). - Empieza sin nada elegido cuando ninguna opción es un valor seguro; nunca elijas por la persona algo que cuesta dinero o borra datos.
- Dentro de un Field toma la etiqueta, la pista y el error por sí solo; suelto, dale un
aria-label.
Props
| RadioGroup | Tipo | Por defecto |
|---|---|---|
optionsobligatorio | 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 | Tipo | Por defecto |
|---|---|---|
items | number | 3 |
orientation | "horizontal" | "vertical" |