Skip to main content
Light Dark System

Radio Group

<sc-radio-group> | ScRadioGroup
Since 2.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
<sc-radio-group label="Select an option" name="a" value="1">
  <sc-radio value="1">Option 1</sc-radio>
  <sc-radio value="2">Option 2</sc-radio>
  <sc-radio value="3">Option 3</sc-radio>
</sc-radio-group>
import ScRadio from '@styledc-dev/styledc/dist/react/radio';
import ScRadioGroup from '@styledc-dev/styledc/dist/react/radio-group';

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

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
<sc-radio-group label="Select an option" help-text="Choose the most appropriate option." name="a" value="1">
  <sc-radio value="1">Option 1</sc-radio>
  <sc-radio value="2">Option 2</sc-radio>
  <sc-radio value="3">Option 3</sc-radio>
</sc-radio-group>
import ScRadio from '@styledc-dev/styledc/dist/react/radio';
import ScRadioGroup from '@styledc-dev/styledc/dist/react/radio-group';

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

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
<sc-radio-group label="Select an option" help-text="Select an option that makes you proud." name="a" value="1">
  <sc-radio-button value="1">Option 1</sc-radio-button>
  <sc-radio-button value="2">Option 2</sc-radio-button>
  <sc-radio-button value="3">Option 3</sc-radio-button>
</sc-radio-group>
import ScRadioButton from '@styledc-dev/styledc/dist/react/radio-button';
import ScRadioGroup from '@styledc-dev/styledc/dist/react/radio-group';

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

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
<sc-radio-group label="Select an option" name="a" value="1">
  <sc-radio value="1">Option 1</sc-radio>
  <sc-radio value="2" disabled>Option 2</sc-radio>
  <sc-radio value="3">Option 3</sc-radio>
</sc-radio-group>
import ScRadio from '@styledc-dev/styledc/dist/react/radio';
import ScRadioGroup from '@styledc-dev/styledc/dist/react/radio-group';

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

Sizing Options

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

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

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

  radioGroup.addEventListener('sc-change', () => {
    radioGroup.size = radioGroup.value;
  });
</script>
import { useState } from 'react';
import ScRadio from '@styledc-dev/styledc/dist/react/radio';
import ScRadioGroup from '@styledc-dev/styledc/dist/react/radio-group';

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

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

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">
  <sc-radio-group label="Select an option" name="a" required>
    <sc-radio value="1">Option 1</sc-radio>
    <sc-radio value="2">Option 2</sc-radio>
    <sc-radio value="3">Option 3</sc-radio>
  </sc-radio-group>
  <br />
  <sc-button type="submit" variant="primary">Submit</sc-button>
</form>

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

  // Handle form submit
  form.addEventListener('submit', event => {
    event.preventDefault();
    alert('All fields are valid!');
  });
</script>
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScIcon from '@styledc-dev/styledc/dist/react/icon';
import ScRadio from '@styledc-dev/styledc/dist/react/radio';
import ScRadioGroup from '@styledc-dev/styledc/dist/react/radio-group';
const App = () => {
  function handleSubmit(event) {
    event.preventDefault();
    alert('All fields are valid!');
  }

  return (
    <form class="custom-validity" onSubmit={handleSubmit}>
      <ScRadioGroup label="Select an option" name="a" required onScChange={handleChange}>
        <ScRadio value="1">
          Option 1
        </ScRadio>
        <ScRadiovalue="2">
          Option 2
        </ScRadio>
        <ScRadio value="3">
          Option 3
        </ScRadio>
      </ScRadioGroup>
      <br />
      <ScButton type="submit" variant="primary">
        Submit
      </ScButton>
    </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">
  <sc-radio-group label="Select an option" name="a" value="1">
    <sc-radio value="1">Not me</sc-radio>
    <sc-radio value="2">Me neither</sc-radio>
    <sc-radio value="3">Choose me</sc-radio>
  </sc-radio-group>
  <br />
  <sc-button type="submit" variant="primary">Submit</sc-button>
</form>

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

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

  // Update validity when a selection is made
  form.addEventListener('sc-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 ScButton from '@styledc-dev/styledc/dist/react/button';
import ScIcon from '@styledc-dev/styledc/dist/react/icon';
import ScRadio from '@styledc-dev/styledc/dist/react/radio';
import ScRadioGroup from '@styledc-dev/styledc/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}>
      <ScRadioGroup ref={radioGroup} label="Select an option" name="a" value="1" onScChange={handleChange}>
        <ScRadio value="1">Not me</ScRadio>
        <ScRadio value="2">Me neither</ScRadio>
        <ScRadio value="3">Choose me</ScRadio>
      </ScRadioGroup>
      <br />
      <ScButton type="submit" variant="primary">
        Submit
      </ScButton>
    </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/@styledc-dev/styledc@1.0.0/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/@styledc-dev/styledc@1.0.0/cdn/components/radio-group/radio-group.js';

To import this component using a bundler:

import '@styledc-dev/styledc/dist/components/radio-group/radio-group.js';

To import this component as a React component:

import ScRadioGroup from '@styledc-dev/styledc/dist/react/radio-group';

Slots

Name Description
(default) The default slot where <sc-radio> or <sc-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
sc-change onScChange Emitted when the radio group’s selected value changes. -
sc-input onScInput Emitted when the radio group receives user input. -
sc-invalid onScInvalid 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.

  • <sc-button-group>