Components / Core
RadioGroup
A set of radios: pick exactly one. Arrow keys move between them (native). ref points at the radio that takes focus (the checked one, else the first enabled one), so form libraries can focus the group, e.g. react-hook-form's field.ref.
import { RadioGroup } from "@stylhero/ui";
Example
Code
import { useState } from "react";
import { RadioGroup } from "@stylhero/ui";
function RadioGroupExample() {
const [size, setSize] = useState("m");
return (
<RadioGroup legend="Size" row value={size} onValueChange={setSize}
options={[{ value: "s", label: "Small" }, { value: "m", label: "Medium" }, { value: "l", label: "Large" }, { value: "xl", label: "XL", disabled: true }]} />
);
}
Props
| Prop | Type | Default | Description |
|---|---|---|---|
legend required |
ReactNode |
- | The group's question, e.g. "Notify me by". |
options required |
Array<ChoiceOption | string> |
- | |
defaultValue |
string |
- | |
error |
ReactNode |
- | |
hint |
ReactNode |
- | |
name |
string |
- | |
onValueChange |
(value: string) => void |
- | |
row |
boolean |
- | Lay the options out side by side. |
value |
string |
- |
Also takes every <fieldset> attribute (id, style, data-*, aria-*, event handlers…), passed to that element. Takes a ref.
Types
type ChoiceOption = { value: string; label: ReactNode; hint?: ReactNode; disabled?: boolean };
Keyboard
| Key | Does |
|---|---|
| Tab | Moves into the group, to the checked option |
| Arrow keys | Move to and select the next option |
Accessibility
- Native radios in a
<fieldset>.refpoints at the checked option, else the first enabled one.
Generated from src/components/FieldSet.tsx. Something wrong or missing? Open an issue.