Radio Group18
One choice from a short list. The dot springs into the option you pick and wobbles as it lands. For long lists, use a Select instead.
<RadioGroup label="Plan">
<Radio value="free" label="Free" />
<Radio value="pro" label="Pro" />
<Radio value="team" label="Team" />
</RadioGroup>Usage
import { Radio, RadioGroup } from "plonk-ui";
export function Plan() {
return (
<RadioGroup label="Plan" name="plan" defaultValue="free">
<Radio value="free" label="Free" />
<Radio value="pro" label="Pro" />
</RadioGroup>
);
}RadioGroup is a <fieldset> with a legend, and every Radio inside is a
real <input type="radio"> sharing the group's name. The selected value is
submitted with the form. A Radio has to be inside a RadioGroup.
Descriptions
Each option can have a description. Disable a single option with disabled,
or the whole group with disabled on RadioGroup.
import { Radio, RadioGroup } from "plonk-ui";
export default function Descriptions() {
return (
<RadioGroup label="Shipping" name="shipping" defaultValue="standard">
<Radio
value="standard"
label="Standard"
description="3–5 business days. Free."
/>
<Radio value="express" label="Express" description="Next day. $12." />
<Radio
value="pickup"
label="Pick up in store"
description="Out of stock nearby."
disabled
/>
</RadioGroup>
);
}Controlled
Pass value and onValueChange to keep the selection in state. Use null for
"nothing selected": undefined would make the group uncontrolled.
import { useState } from "react";
import { Button, Radio, RadioGroup } from "plonk-ui";
export default function Controlled() {
// null: controlled, nothing selected.
const [size, setSize] = useState<string | null>("md");
return (
<div className="flex flex-col items-start gap-4">
<RadioGroup
label="Card size"
orientation="horizontal"
value={size}
onValueChange={setSize}
>
<Radio value="sm" label="Small" />
<Radio value="md" label="Medium" />
<Radio value="lg" label="Large" />
</RadioGroup>
<div className="flex items-center gap-3">
<Button size="sm" variant="neutral" onPress={() => setSize(null)}>
Clear
</Button>
<span className="font-bold">Selected: {size ?? "nothing"}</span>
</div>
</div>
);
}Errors
error belongs to the group. It shows under the options, marks every option
invalid, and shakes them once. Add required when an answer is mandatory.
Personalities
Sticker springs the dot in and wobbles the circle. Retro pops it in two steps, Comic punches it in fast with a big overshoot, and Arcade springs it in without the wobble.
Accessibility
- A native radio group:
Tabenters the group once, at the selected option, and arrow keys move and select. - The label is the fieldset's
legend, so screen readers announce it when you enter the group. descriptionanderrorare linked to the group witharia-describedby.orientationonly changes the layout; the keys stay the same.
API
RadioGroup
All other <fieldset> props are passed through.
| Prop | Type | Default | Description |
|---|---|---|---|
name | string | — | Field name shared by all options. Generated when not set. |
value | string | null | — | Controlled selected value. Pass null for no selection; undefined makes the group uncontrolled. |
defaultValue | string | — | Selected value on first render, when uncontrolled. |
onValueChange | (value: string) => void | — | Called with the option's value when it's selected. |
required | boolean | false | One option must be selected. Adds an asterisk to the label. |
orientation | "horizontal" | "vertical" | "vertical" | Stacks options or lays them out in a wrapping row. Arrow keys work the same either way. |
label | ReactNode | — | Group label, rendered as the fieldset's legend. |
description | ReactNode | — | Help text under the label, read with the group. |
error | ReactNode | — | Error message for the whole group. Every option is marked invalid and the options shake once. |
disabled | boolean | — | Disables every option in the group. |
Radio
All other <input> props are passed through.
| Prop | Type | Default | Description |
|---|---|---|---|
value* | string | — | This option's value, sent with the form when selected. |
label | ReactNode | — | Visible label, linked to the option. |
description | ReactNode | — | Help text under the option's label. |
disabled | boolean | — | Disables this option only. |