Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

InputsReact

Radio Group

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…
import * as React from 'react'
import { Checkbox } from '@hamur/react/checkbox'
import { Radio } from '@hamur/react/radio'
import { RadioGroup } from '@hamur/react/radio-group'

const plans = ['free', 'pro', 'team']

export default function RadioGroupExample() {
  const [plan, setPlan] = React.useState('pro')
  const [locked, setLocked] = React.useState(false)
  return (
    <div className="stack">
      <RadioGroup
        className="stack"
        name="plan"
        value={plan}
        readOnly={locked}
        onValueChange={setPlan}
      >
        {plans.map((name) => (
          <label className="row" key={name}>
            <Radio.Root className="radio" value={name}>
              <Radio.Indicator className="radio-indicator" />
            </Radio.Root>
            {name}
          </label>
        ))}
      </RadioGroup>
      <label className="row">
        <Checkbox.Root className="checkbox" onCheckedChange={setLocked}>
          <Checkbox.Indicator className="checkbox-indicator">✓</Checkbox.Indicator>
        </Checkbox.Root>
        Lock the choice
      </label>
      <p className="muted">Plan: {plan}</p>
    </div>
  )
}
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/react

Not 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>.

RadioGroup props
PropTypeDefault
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.booleanfalse
readOnlyWhether the user should be unable to select a different radio button in the group.booleanfalse
requiredWhether the user must choose a value before submitting a form.booleanfalse
inputRefA ref to access the hidden input element.React.Ref<HTMLInputElement>—
RadioGroup data attributes
Data attributeDescription
data-disabledPresent when the radio group is disabled.

Shared props

Every part that renders an element takes these.

Shared props
PropType
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

Keyboard
KeysDoesPart
ArrowDownArrowUpArrowLeftArrowRightMoves to the next or previous radio and selects itRadioGroup
SpaceSelects the focused radioRadioGroup
EnterDoes not select, as with native radiosRadioGroup
TabLeaves the group in one stepRadioGroup

Accessibility

What the parts set
PartSetsSo that
RadioGrouprole="radiogroup"the radios are announced as one set
RadioGrouparia-labelledby to the Field label or the Fieldset legendthe set is read with its question
RadioGrouparia-disabled, aria-readonly, aria-required from disabled, readOnly and requiredthe group's constraints are announced

Focus order

  1. The group is one tab stop: the checked radio, or the first one.
  2. Arrow keys move focus and the selection together.