--- meta: title: Radio Group description: Radio groups are used to group multiple radios or radio buttons so they function as a single form control. layout: component --- ```html:preview Option 1 Option 2 Option 3 ``` ```jsx:react import SlRadio from '@shoelace-style/shoelace/dist/react/radio'; import SlRadioGroup from '@shoelace-style/shoelace/dist/react/radio-group'; const App = () => ( Option 1 Option 2 Option 3 ); ``` ## 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. ```html:preview Option 1 Option 2 Option 3 ``` ```jsx:react import SlRadio from '@shoelace-style/shoelace/dist/react/radio'; import SlRadioGroup from '@shoelace-style/shoelace/dist/react/radio-group'; const App = () => ( Option 1 Option 2 Option 3 ); ``` ### Radio Buttons [Radio buttons](/components/radio-button) offer an alternate way to display radio controls. In this case, an internal [button group](/components/button-group) is used to group the buttons into a single, cohesive control. ```html:preview Option 1 Option 2 Option 3 ``` ```jsx:react import SlRadioButton from '@shoelace-style/shoelace/dist/react/radio-button'; import SlRadioGroup from '@shoelace-style/shoelace/dist/react/radio-group'; const App = () => ( Option 1 Option 2 Option 3 ); ``` ### Disabling Options Radios and radio buttons can be disabled by adding the `disabled` attribute to the respective options inside the radio group. ```html:preview Option 1 Option 2 Option 3 ``` ```jsx:react import SlRadio from '@shoelace-style/shoelace/dist/react/radio'; import SlRadioGroup from '@shoelace-style/shoelace/dist/react/radio-group'; const App = () => ( Option 1 Option 2 Option 3 ); ``` ### Sizing Options The size of [Radios](/components/radio) and [Radio Buttons](/components/radio-buttons) will be determined by the Radio Group's `size` attribute. ```html preview Small Medium Large ``` ```jsx react import { useState } from 'react'; import SlRadio from '@shoelace-style/shoelace/dist/react/radio'; import SlRadioGroup from '@shoelace-style/shoelace/dist/react/radio-group'; const App = () => { const [size, setSize] = useState('medium'); return ( <> setSize(event.target.value)} > Small Medium Large ); }; ``` :::tip [Radios](/components/radio) and [Radio Buttons](/components/radio-button) also have a `size` attribute. This can be useful in certain compositions, but it will be ignored when used inside of a Radio Group. ::: ### 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. ```html:preview
Option 1 Option 2 Option 3
Submit
``` ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlIcon from '@shoelace-style/shoelace/dist/react/icon'; import SlRadio from '@shoelace-style/shoelace/dist/react/radio'; import SlRadioGroup from '@shoelace-style/shoelace/dist/react/radio-group'; const App = () => { function handleSubmit(event) { event.preventDefault(); alert('All fields are valid!'); } return (
Option 1 Option 2 Option 3
Submit
); }; ``` ### 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. ```html:preview
Not me Me neither Choose me
Submit
``` ```jsx:react import { useEffect, useRef } from 'react'; import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlIcon from '@shoelace-style/shoelace/dist/react/icon'; import SlRadio from '@shoelace-style/shoelace/dist/react/radio'; import SlRadioGroup from '@shoelace-style/shoelace/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 (
Not me Me neither Choose me
Submit
); }; ```