Radio group
One choice among a few options, all visible: dots with their words, or whole cards.
Web uniquement — React, depuis @krizaka/ui/radio-group. Bêta : son API peut encore changer à une version mineure.
Quand l’utiliser
- In a form, to choose exactly one of two to five options that should all be seen: a frequency, a quality.
RadioGroup.Cardwhen each option needs more than a line: an amount, a plan, a payment method.
Quand ne pas l’utiliser
Installation
Installer
npm install @krizaka/ui@beta @krizaka/tailwind@beta @krizaka/tokens@beta tailwindcssStyles
@import "tailwindcss";
@import "@krizaka/tailwind";
@import "@krizaka/ui/tailwind.css";Importer
import { RadioGroup } from "@krizaka/ui/radio-group";Exemples
Items
Dots and their words, one disabled.
import { RadioGroup } from "@krizaka/ui/radio-group";
export default function RadioGroupItems() {
return (
<RadioGroup.Root label="Notification frequency" defaultValue="daily">
<RadioGroup.Item value="instant">Instantly</RadioGroup.Item>
<RadioGroup.Item value="daily">Once a day</RadioGroup.Item>
<RadioGroup.Item value="weekly">Once a week</RadioGroup.Item>
<RadioGroup.Item value="never" disabled>
Never
</RadioGroup.Item>
</RadioGroup.Root>
);
}Cards
Whole cards: the chosen one carries the accent.
import { RadioGroup } from "@krizaka/ui/radio-group";
const AMOUNTS = [
["5", "$5", "A coffee"],
["20", "$20", "A dinner"],
["50", "$50", "A night out"],
];
// Whole cards: the chosen one carries the accent border and tint.
export default function RadioGroupCards() {
return (
<RadioGroup.Root label="Amount" defaultValue="20" className="grid w-[30rem] max-w-full grid-cols-3 gap-3">
{AMOUNTS.map(([value, amount, hint]) => (
<RadioGroup.Card key={value} value={value}>
<span className="font-display text-lg font-bold">{amount}</span>
<span className="text-xs text-fg-secondary">{hint}</span>
</RadioGroup.Card>
))}
</RadioGroup.Root>
);
}Horizontal
orientation="horizontal": the items in a row.
import { RadioGroup } from "@krizaka/ui/radio-group";
// `orientation="horizontal"`: left and right arrows, the items in a row.
export default function RadioGroupHorizontal() {
return (
<RadioGroup.Root label="Quality" defaultValue="1080" orientation="horizontal" className="gap-5">
<RadioGroup.Item value="720">720p</RadioGroup.Item>
<RadioGroup.Item value="1080">1080p</RadioGroup.Item>
<RadioGroup.Item value="2160">4K</RadioGroup.Item>
</RadioGroup.Root>
);
}Props
RadioGroupRoot
value/defaultValue + onValueChange, orientation, required, name for a form.
| Prop | Type | Défaut | Description |
|---|---|---|---|
invalid | boolean | — | Marks every radio invalid: aria-invalid (pair it with a Field.Error). |
label | string | — | The group's accessible name — passed translated (or aria-labelledby a visible heading). |
RadioGroupItem
A radio: a dot, and its words when given.
| Prop | Type | Défaut | Description |
|---|---|---|---|
children | ReactNode | — | The words next to the dot, clickable. Otherwise name it with Field.Label htmlFor or aria-label. |
Accessibilité
- Radix RadioGroup:
role="radiogroup"named bylabel,role="radio"witharia-checkedon each option. - Disabled options are skipped by the arrows.
Clavier
| Touches | Action |
|---|---|
| Tab | Moves into the group (one tab stop, on the chosen option) and out of it. |
| Arrow keys | Move to the next or previous option and choose it. |
Bonnes pratiques
- Give the group a
label: the question being answered. - Pre-select the safest or most common option with
defaultValuewhen there is one. - Keep the labels parallel and short.
Composants liés
Générée depuis le code de @krizaka/ui 2.0.0-beta.6 : son meta.ts, ses exemples et ses types.Modifier cette documentation sur GitHub