InputsReact
Provides shared state to a series of radio buttons: one value, one name for the form, and arrow-key movement between them.
Rendered by React 19.3.1
Loading React…201Tests passing in React · 0 failing
9.0 KiBSize
Not measured yetMemory
Not measured yetSpeed
productdevbook and 17 upstream authors12 commits · last
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<RadioGroup />Examples
The demo at the top is the one example this component has so far.
API reference
9 props across 1 parts, in React syntax. className, style, render are listed once, under Shared props.
RadioGroupRadioGroup
Provides shared state to a series of radio buttons. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
nameIdentifies the field when a form is submitted. | string | — |
defaultValueThe uncontrolled value of the radio button that should be initially selected. | TValue | — |
valueThe controlled value of the radio item that should be currently selected. | TValue | — |
onValueChangeCallback fired when the value changes. | ((value: TValue, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element | undefined; }) => void) | — |
formIdentifies the form that owns the radio inputs. | string | — |
disabledWhether the component should ignore user interaction. | boolean | false |
readOnlyWhether the user should be unable to select a different radio button in the group. | boolean | false |
requiredWhether the user must choose a value before submitting a form. | boolean | false |
inputRefA ref to access the hidden input element. | React.Ref<HTMLInputElement> | — |
| Data attribute | Description |
|---|---|
data-disabled | Present when the radio group is disabled. |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
| Keys | Does | Part |
|---|---|---|
| ArrowDownArrowUpArrowLeftArrowRight | Moves to the next or previous radio and selects it | RadioGroup |
| Space | Selects the focused radio | RadioGroup |
| Enter | Does not select, as with native radios | RadioGroup |
| Tab | Leaves the group in one step | RadioGroup |
Accessibility
| Part | Sets | So that |
|---|---|---|
| RadioGroup | role="radiogroup" | the radios are announced as one set |
| RadioGroup | aria-labelledby to the Field label or the Fieldset legend | the set is read with its question |
| RadioGroup | aria-disabled, aria-readonly, aria-required from disabled, readOnly and required | the group's constraints are announced |
Focus order
- The group is one tab stop: the checked radio, or the first one.
- Arrow keys move focus and the selection together.