API: components
RadioGroup
A set of radios of which one is checked, one tab stop moved through with the arrows, with native form support (the APG radio group), in each framework.
A set of choices of which exactly one can be checked (WAI-ARIA APG
radio group pattern): the group is one tab
stop, and the arrow keys move the focus and the check together. It works in a native <form>: it
submits, resets and validates as a set of <input type="radio"> of one name would.
RadioGroup is one component, not a namespace; its radios come from /radio.
Behaviour
- Value: the checked radio's
value(a string), ornullwhile none is checked (the default). Checking a radio reports its value (reasonnone). - Focus: the group's tab stop is its checked radio when it is enabled, else its first
enabled radio; every other radio has
tabindex="-1". Tab enters the group there, and Tab again leaves it. - Arrows: ArrowDown and ArrowRight move the focus to the next enabled radio and check it,
ArrowUp and ArrowLeft to the previous, whatever the
orientation(the APG). Disabled radios are skipped. WithloopFocus(the default) they wrap at the ends; without it, an arrow at an end does nothing. - RTL: ArrowLeft is the next radio and ArrowRight the previous, in the direction of a DirectionProvider, else the group's computed one. ArrowDown and ArrowUp do not change.
- Forms: each radio renders a visually hidden
radioinput right after it, named by the group'sname; the form submitsname=valuefor the checked one, nothing while none is.requiredfails the validation while none is checked, and focuses the group's tab stop. A form reset returns an uncontrolled group todefaultValue; a controlled one reports the default throughonValueChange. - Name: the group is a
radiogroup; name it witharia-labelledby(a heading) oraria-label, or put it in a Field, whose Label names it. - In a Field: the group is the field's control, as a CheckboxGroup. The
Field's Label names it (
aria-labelledby), its Description and Errors describe it (aria-describedby),aria-invalidand the field's state attributes are on the group, andvalidatereceives the value (ornull). Its radios take the field'snameanddisabled. The validated element is the first enabled radio's hidden input (radios of one name share their validity), sorequiredblocks a native submission in a Field too; the focus goes to the checked radio, else the first enabled one.
Anatomy
| part | element | data-primitives-part | role |
|---|---|---|---|
| RadioGroup | div | radio-group-root | the radiogroup; holds the value |
Its radios are Radio.Roots, each with its hidden input.
Usage
import { Radio } from "@fragiola/primitives-react/radio";
import { RadioGroup } from "@fragiola/primitives-react/radio-group";
<RadioGroup aria-labelledby="plan" name="plan" required onValueChange={(value) => console.log(value)}>
<h2 id="plan">Plan</h2>
<label>
<Radio.Root value="free">
<Radio.Indicator />
</Radio.Root>
Free
</label>
<label>
<Radio.Root value="pro">
<Radio.Indicator />
</Radio.Root>
Pro
</label>
</RadioGroup>;In a Field
<Form>
<Field.Root name="contact">
<Field.Label>Contact by</Field.Label>
<RadioGroup required>
<label><Radio.Root value="email"><Radio.Indicator /></Radio.Root> Email</label>
<label><Radio.Root value="phone"><Radio.Indicator /></Radio.Root> Phone</label>
</RadioGroup>
<Field.Description>How we reach you.</Field.Description>
<Field.Error match="valueMissing">Choose how we reach you.</Field.Error>
</Field.Root>
<button type="submit">Save</button>
</Form>The group has no name of its own: the field gives it, and the form submits contact=phone.
A submission with none checked is blocked, shows the error and focuses "Email".
Props
It also takes what the primitive contract gives: render
(Vue: as and the render slot), a class and a style, a ref, and any attribute, which it
forwards. Vue binds them as v-model:value and @value-change; Angular as [(value)] and
(valueChangeDetails).
RadioGroup
| prop | type | default | description |
|---|---|---|---|
value | string | null | the checked radio's value, controlled; null for none | |
defaultValue | string | null | null | the initial value, uncontrolled |
onValueChange | (value: string | null, details: { reason }) => void | every change (reason none); null when a reset returns the group to none | |
disabled | boolean | false | disables every radio |
required | boolean | false | the form's validation fails while no radio is checked |
name | string | every radio's hidden input takes it (a Field's wins) | |
orientation | "horizontal" | "vertical" | aria-orientation and data-orientation; it changes no key | |
loopFocus | boolean | true | the arrows wrap from the last radio to the first and back |
State and part attributes
| part | attributes |
|---|---|
| RadioGroup | role="radiogroup", aria-required="true" when required, aria-disabled="true" and data-disabled when disabled, aria-orientation and data-orientation when given, data-required; in a Field, aria-labelledby (the Field's Label), aria-describedby, aria-invalid and the field's states |
The radios' attributes are on the Radio page.
Keyboard
| key | action |
|---|---|
| Tab | enters the group at the checked radio, else its first enabled one; Tab again leaves it |
| ArrowDown, ArrowRight | the next enabled radio, focused and checked (ArrowLeft in RTL); wraps with loopFocus |
| ArrowUp, ArrowLeft | the previous enabled radio, focused and checked (ArrowRight in RTL); wraps with loopFocus |
| Space | checks the focused radio |
| Enter | nothing (a native radio does not check on Enter) |
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/radio-group | ChangeDetails, ChangeReason, RadioGroup, RadioGroupProps, RadioGroupState, RenderProp |
@fragiola/primitives-vue/radio-group | ChangeDetails, ChangeReason, RadioGroup, RadioGroupEmits, RadioGroupProps, RadioGroupState, RenderSlotProps |
@fragiola/primitives-angular/radio-group | ChangeDetails, ChangeReason, RadioEntry, RadioGroup, RadioGroupOrientation, RadioGroupState, ValueChangeEvent |