# Radio Group [component-header:sl-radio-group] ```html preview Option 1 Option 2 Option 3 ``` ```jsx react import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; 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, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; 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, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; 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, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( Option 1 Option 2 Option 3 ); ``` ### 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, SlIcon, SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; 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, SlIcon, SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; 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
); }; ``` [component-metadata:sl-radio-group]