Light Dark System

Radio Group

<p-radio-group> | PRadioGroup
Since 1.0 stable

Radio groups are used to group multiple radios or radio buttons so they function as a single form control.

Option 1 Option 2 Option 3
<p-radio-group label="Select an option" name="a" value="1">
  <p-radio value="1">Option 1</p-radio>
  <p-radio value="2">Option 2</p-radio>
  <p-radio value="3">Option 3</p-radio>
</p-radio-group>
import PRadio from 'pure-uikit/dist/react/radio';
import PRadioGroup from 'pure-uikit/dist/react/radio-group';

const App = () => (
  <PRadioGroup label="Select an option" name="a" value="1">
    <PRadio value="1">Option 1</PRadio>
    <PRadio value="2">Option 2</PRadio>
    <PRadio value="3">Option 3</PRadio>
  </PRadioGroup>
);

Examples

Help Text

Add descriptive help text to a radio group with the help-text attribute. For help texts that contain HTML, use the help-text slot instead.

Option 1 Option 2 Option 3
<p-radio-group label="Select an option" help-text="Choose the most appropriate option." name="a" value="1">
  <p-radio value="1">Option 1</p-radio>
  <p-radio value="2">Option 2</p-radio>
  <p-radio value="3">Option 3</p-radio>
</p-radio-group>
import PRadio from 'pure-uikit/dist/react/radio';
import PRadioGroup from 'pure-uikit/dist/react/radio-group';

const App = () => (
  <PRadioGroup label="Select an option" help-text="Choose the most appropriate option." name="a" value="1">
    <PRadio value="1">Option 1</PRadio>
    <PRadio value="2">Option 2</PRadio>
    <PRadio value="3">Option 3</PRadio>
  </PRadioGroup>
);

Radio Buttons

Radio buttons offer an alternate way to display radio controls. In this case, an internal button group is used to group the buttons into a single, cohesive control.

Option 1 Option 2 Option 3
<p-radio-group label="Select an option" help-text="Select an option that makes you proud." name="a" value="1">
  <p-radio-button value="1">Option 1</p-radio-button>
  <p-radio-button value="2">Option 2</p-radio-button>
  <p-radio-button value="3">Option 3</p-radio-button>
</p-radio-group>
import PRadioButton from 'pure-uikit/dist/react/radio-button';
import PRadioGroup from 'pure-uikit/dist/react/radio-group';

const App = () => (
  <PRadioGroup label="Select an option" name="a" value="1">
    <PRadioButton value="1">Option 1</PRadioButton>
    <PRadioButton value="2">Option 2</PRadioButton>
    <PRadioButton value="3">Option 3</PRadioButton>
  </PRadioGroup>
);

Disabling Options

Radios and radio buttons can be disabled by adding the disabled attribute to the respective options inside the radio group.

Option 1 Option 2 Option 3
<p-radio-group label="Select an option" name="a" value="1">
  <p-radio value="1">Option 1</p-radio>
  <p-radio value="2" disabled>Option 2</p-radio>
  <p-radio value="3">Option 3</p-radio>
</p-radio-group>
import PRadio from 'pure-uikit/dist/react/radio';
import PRadioGroup from 'pure-uikit/dist/react/radio-group';

const App = () => (
  <PRadioGroup label="Select an option" name="a" value="1">
    <PRadio value="1">Option 1</PRadio>
    <PRadio value="2" disabled>
      Option 2
    </PRadio>
    <PRadio value="3">Option 3</PRadio>
  </PRadioGroup>
);

Sizing Options

The size of Radios and Radio Buttons will be determined by the Radio Group’s size attribute.

<p-radio-group label="Select an option" size="medium" value="medium" class="radio-group-size">
  <p-radio value="small">Small</p-radio>
  <p-radio value="medium">Medium</p-radio>
  <p-radio value="large">Large</p-radio>
</p-radio-group>

<script>
  const radioGroup = document.querySelector(".radio-group-size");

  radioGroup.addEventListener("p-change", () => {
    radioGroup.size = radioGroup.value;
  });
</script>
import { useState } from "react";
import PRadio from "pure-uikit/dist/react/radio";
import PRadioGroup from "pure-uikit/dist/react/radio-group";

const App = () => {
  const [size, setSize] = useState("medium");

  return (
    <>
      <PRadioGroup
        label="Select an option"
        size={size}
        value={size}
        class="radio-group-size"
        onPChange={event => setSize(event.target.value)}
      >
        <PRadio value="small">Small</PRadio>
        <PRadio value="medium">Medium</PRadio>
        <PRadio value="large">Large</PRadio>
      </PRadioGroup>
    </>
  );
};

Validation

Setting the required attribute to make selecting an option mandatory. If a value has not been selected, it will prevent the form from submitting and display an error message.

Option 1 Option 2 Option 3
Submit
<form class="validation">
  <p-radio-group label="Select an option" name="a" required>
    <p-radio value="1">Option 1</p-radio>
    <p-radio value="2">Option 2</p-radio>
    <p-radio value="3">Option 3</p-radio>
  </p-radio-group>
  <br />
  <p-button type="submit" variant="primary">Submit</p-button>
</form>

<script>
  const form = document.querySelector('.validation');

  // Handle form submit
  form.addEventListener('submit', event => {
    event.preventDefault();
    alert('All fields are valid!');
  });
</script>
import PButton from 'pure-uikit/dist/react/button';
import PIcon from 'pure-uikit/dist/react/icon';
import PRadio from 'pure-uikit/dist/react/radio';
import PRadioGroup from 'pure-uikit/dist/react/radio-group';
const App = () => {
  function handleSubmit(event) {
    event.preventDefault();
    alert('All fields are valid!');
  }

  return (
    <form class="custom-validity" onSubmit={handleSubmit}>
      <PRadioGroup label="Select an option" name="a" required onPChange={handleChange}>
        <PRadio value="1">
          Option 1
        </PRadio>
        <PRadiovalue="2">
          Option 2
        </PRadio>
        <PRadio value="3">
          Option 3
        </PRadio>
      </PRadioGroup>
      <br />
      <PButton type="submit" variant="primary">
        Submit
      </PButton>
    </form>
  );
};

Custom Validity

Use the setCustomValidity() method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string.

Not me Me neither Choose me
Submit
<form class="custom-validity">
  <p-radio-group label="Select an option" name="a" value="1">
    <p-radio value="1">Not me</p-radio>
    <p-radio value="2">Me neither</p-radio>
    <p-radio value="3">Choose me</p-radio>
  </p-radio-group>
  <br />
  <p-button type="submit" variant="primary">Submit</p-button>
</form>

<script>
  const form = document.querySelector('.custom-validity');
  const radioGroup = form.querySelector('p-radio-group');
  const errorMessage = 'You must choose the last option';

  // Set initial validity as soon as the element is defined
  customElements.whenDefined('p-radio').then(() => {
    radioGroup.setCustomValidity(errorMessage);
  });

  // Update validity when a selection is made
  form.addEventListener('p-change', () => {
    const isValid = radioGroup.value === '3';
    radioGroup.setCustomValidity(isValid ? '' : errorMessage);
  });

  // Handle form submit
  form.addEventListener('submit', event => {
    event.preventDefault();
    alert('All fields are valid!');
  });
</script>
import { useEffect, useRef } from 'react';
import PButton from 'pure-uikit/dist/react/button';
import PIcon from 'pure-uikit/dist/react/icon';
import PRadio from 'pure-uikit/dist/react/radio';
import PRadioGroup from 'pure-uikit/dist/react/radio-group';
const App = () => {
  const radioGroup = useRef(null);
  const errorMessage = 'You must choose this option';

  function handleChange() {
    radioGroup.current.setCustomValidity(radioGroup.current.value === '3' ? '' : errorMessage);
  }

  function handleSubmit(event) {
    event.preventDefault();
    alert('All fields are valid!');
  }

  useEffect(() => {
    radio.current.setCustomValidity(errorMessage);
  }, []);

  return (
    <form class="custom-validity" onSubmit={handleSubmit}>
      <PRadioGroup ref={radioGroup} label="Select an option" name="a" value="1" onPChange={handleChange}>
        <PRadio value="1">Not me</PRadio>
        <PRadio value="2">Me neither</PRadio>
        <PRadio value="3">Choose me</PRadio>
      </PRadioGroup>
      <br />
      <PButton type="submit" variant="primary">
        Submit
      </PButton>
    </form>
  );
};

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/radio-group/radio-group.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/radio-group/radio-group.js';

To import this component using a bundler:

import 'pure-uikit/dist/components/radio-group/radio-group.js';

To import this component as a React component:

import PRadioGroup from 'pure-uikit/dist/react/radio-group';

Slots

Name Description
(default) The default slot where <p-radio> or <p-radio-button> elements are placed.
label The radio group’s label. Required for proper accessibility. Alternatively, you can use the label attribute.
help-text Text that describes how to use the radio group. Alternatively, you can use the help-text attribute.

Learn more about using slots.

Properties

Name Description Reflects Type Default
label The radio group’s label. Required for proper accessibility. If you need to display HTML, use the label slot instead. string ""
helpText
help-text
The radio groups’s help text. If you need to display HTML, use the help-text slot instead. string ""
name The name of the radio group, submitted as a name/value pair with form data. string "option"
value The current value of the radio group, submitted as a name/value pair with form data. string ""
size The radio group’s size. This size will be applied to all child radios and radio buttons. "small" | "medium" | "large" "medium"
form By default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work. string ""
required Ensures a child radio is checked before allowing the containing form to submit. boolean false
validity Gets the validity state object - -
validationMessage Gets the validation message - -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
p-change onPChange Emitted when the radio group’s selected value changes. -
p-input onPInput Emitted when the radio group receives user input. -
p-invalid onPInvalid Emitted when the form control has been checked for validity and its constraints aren’t satisfied. -

Learn more about events.

Methods

Name Description Arguments
checkValidity() Checks for validity but does not show a validation message. Returns true when valid and false when invalid. -
getForm() Gets the associated form, if one exists. -
reportValidity() Checks for validity and shows the browser’s validation message if the control is invalid. -
setCustomValidity() Sets a custom validation message. Pass an empty string to restore validity. message:

Learn more about methods.

Parts

Name Description
form-control The form control that wraps the label, input, and help text.
form-control-label The label’s wrapper.
form-control-input The input’s wrapper.
form-control-help-text The help text’s wrapper.
button-group The button group that wraps radio buttons.
button-group__base The button group’s base part.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <p-button-group>