Components

Radio Group

Provides shared state to a series of radio buttons.

Best apple

Usage guidelines#

Anatomy#

Import the component and assemble its parts:

Anatomytsx
import { Radio } from '@egoist/solid-base/radio';
import { RadioGroup } from '@egoist/solid-base/radio-group';

<RadioGroup>
  <Radio.Root>
    <Radio.Indicator />
  </Radio.Root>
</RadioGroup>;

Examples#

Labeling a radio group#

Label the group with aria-labelledby and a sibling label element:

Using aria-labelledby to label a radio grouptsx
<div id="storage-type-label">Storage type</div>
<RadioGroup aria-labelledby="storage-type-label">{/* ... */}</RadioGroup>

An enclosing <label> is the simplest labeling pattern for each radio:

Using an enclosing label to label a radio buttontsx
<label>
  <Radio.Root value="ssd" />
  SSD
</label>

Rendering as a native button#

By default, <Radio.Root> renders a <span> element to support enclosing labels. Prefer rendering each radio as a native button when using sibling labels (for/id).

Sibling label pattern with a native buttontsx
<div id="storage-type">Storage type</div>
<RadioGroup defaultValue="ssd" aria-labelledby="storage-type">
  <div>
    <label for="storage-type-ssd">SSD</label>
    <Radio.Root value="ssd" id="storage-type-ssd" nativeButton render="button">
      <Radio.Indicator />
    </Radio.Root>
  </div>
</RadioGroup>

Native buttons with wrapping labels are supported by using the render callback to avoid invalid HTML, so the hidden input is placed outside the label:

Render callbacktsx
<div id="storage-type">Storage type</div>
<RadioGroup defaultValue="ssd" aria-labelledby="storage-type">
  <Radio.Root
    value="ssd"
    nativeButton
    render={(buttonProps) => (
      <label>
        <button {...buttonProps} />
        SSD
      </label>
    )}
  />
</RadioGroup>

Controlled#

Pass value and update it from onValueChange:

const [storage, setStorage] = createSignal('ssd');

<RadioGroup value={storage()} onValueChange={(value) => setStorage(value)}>
  {/* ... */}
</RadioGroup>;

API reference#

<RadioGroup>

Provides shared state to a series of radio buttons. Renders a <div> element.

PropTypeDefault
idstring—

The id of the radio group element.

disabledbooleanfalse

Whether the component should ignore user interaction.

readOnlybooleanfalse

Whether the user should be unable to select a different radio button in the group.

requiredbooleanfalse

Whether the user must choose a value before submitting a form.

namestring—

Identifies the field when a form is submitted.

formstring—

Identifies the form that owns the radio inputs. Useful when the radio group is rendered outside the form.

valueValue—

The controlled value of the radio item that should be currently selected.

To render an uncontrolled radio group, use the defaultValue prop instead.

defaultValueValue—

The uncontrolled value of the radio button that should be initially selected.

To render a controlled radio group, use the value prop instead.

onValueChange| ((value: Value, eventDetails: RadioGroup.ChangeEventDetails) => void)—

Callback fired when the value changes.

inputRefRefProp<HTMLInputElement | null>—

A ref to access the hidden input element (the checked one, or the first enabled one). Called with null when that input unmounts.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the radio group is disabled.

The radio buttons inside the group are documented on the Radio page:

<Radio.Root>

Represents the radio button itself. Renders a <span> element and a hidden <input> beside.

PropTypeDefault
idstring—

The id of the hidden input element.

When nativeButton is true, the id is applied to the root element.

value*Value—

The unique identifying value of the radio in a group.

disabledboolean—

Whether the component should ignore user interaction.

requiredboolean—

Whether the user must choose a value before submitting a form.

readOnlyboolean—

Whether the user should be unable to select the radio button.

inputRefRefProp<HTMLInputElement>—

A ref to access the hidden input element.

nativeButtonbooleanfalse

Whether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-checked

Present when the radio is checked.

data-unchecked

Present when the radio is not checked.

data-disabled

Present when the radio is disabled.

data-readonly

Present when the radio is readonly.

data-required

Present when the radio is required.

data-valid

Present when the radio is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the radio is in an invalid state (when wrapped in Field.Root).

data-touched

Present when the radio has been touched (when wrapped in Field.Root).

data-dirty

Present when the radio's value has changed (when wrapped in Field.Root).

data-filled

Present when the radio is checked (when wrapped in Field.Root).

data-focused

Present when the radio is focused (when wrapped in Field.Root).

<Radio.Indicator>

Indicates whether the radio button is selected. Renders a <span> element.

PropTypeDefault
keepMountedbooleanfalse

Whether to keep the HTML element in the DOM when the radio button is inactive.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-checked

Present when the radio is checked.

data-unchecked

Present when the radio is not checked.

data-disabled

Present when the radio is disabled.

data-readonly

Present when the radio is readonly.

data-required

Present when the radio is required.

data-valid

Present when the radio is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the radio is in an invalid state (when wrapped in Field.Root).

data-touched

Present when the radio has been touched (when wrapped in Field.Root).

data-dirty

Present when the radio's value has changed (when wrapped in Field.Root).

data-filled

Present when the radio is checked (when wrapped in Field.Root).

data-focused

Present when the radio is focused (when wrapped in Field.Root).