The RadioGroup component is used for displaying a list of related options, in the form of Radio inputs, where only one choice can be selected.

Importing

In a React app, the component can be imported via:

import { RadioGroup } from '@customerio/pluma-components/react';

In an Ember app, the component is named PlumaRadioGroup.

Usage

A RadioGroup should contain Radio components as children. RadioGroup will forward the name attribute to any Radios nested within, as well as connect onChange handlers to them, so there's no need to define those props on the Radios themselves:

Test radio group
import { Radio, RadioGroup } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState('one');

  return <RadioGroup
    label="Test radio group"
    value={value}
    onChange={(value) => {
      setValue(value);
    }}
  >
    <Radio value="one" label="One" />
    <Radio value="two" label="Two" />
    <Radio value="three" label="Three" />
  </RadioGroup>
}

Or, in Ember:

Loading...

Labels

A RadioGroup must always have an associated label. The component accepts a label string prop, which renders text above the nested radios.

Alternatively, ariaLabelledby may be used to associate the group with a label elsewhere in the DOM. To make sure the association is set up both ways, it's recommended to also set for on the label, pointing to an id on the radio group:

import { Radio, RadioGroup } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState(null);

return <>
    <Box
      as="label"
      id="my_label_id"
      htmlFor="my_group"
    >
      Make a selection
    </Box>
    <RadioGroup
      id="my_input"
      ariaLabelledby="my_label_id"
      value={value}
      onChange={(value) => {
        setValue(value);
      }}
    >
      <Radio value="one" label="One" />
      <Radio value="two" label="Two" />
      <Radio value="three" label="Three" />
    </RadioGroup>
  </>
}

Finally, the ariaLabel prop can be used if no other element is suitable for a label.

Disabled groups

The isDisabled prop on a RadioGroup will take precedence over the isDisabled prop on individual Radio components.

It is not necessary to set isDisabled on the individual Radio components, as the RadioGroup provides that state to the Radios through its context.

Test radio group
import { Radio, RadioGroup } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [isDisabled, setIsDisabled] = useState(false);
  const [value, setValue] = useState('one');
  
  return <>
    <Box as="label" mb="200" display="block">
      <Checkbox
        label="Disable group"
        isChecked={isDisabled}
        onChange={(e) => {
          setIsDisabled(e.target.checked);
        }}
      />
    </Box>

    <RadioGroup
      label="Test radio group"
      isDisabled={isDisabled}
      value={value}
      onChange={(value) => {
        setValue(value);
      }}
    >
      <Radio value="one" label="One" isDisabled={false} />
      <Radio value="two" label="Two" isDisabled={false} />
      <Radio value="three" label="Three" isDisabled={true} />
    </RadioGroup>
  </>
}

API

The aria-label attribute to be applied on the fieldset

The aria-labelledby attribute to be applied on the fieldset

An optional id attribute to assign to the fieldset

Whether the entire group is disabled

The label text to be shown above the radio group

The name shared by radios in this group. If none is provided, one will be generated

On this page