A radio button group that can have a single option selected.
For Use In
Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A lightning-radio-group component represents a group of radio buttons that permit only
one button for selection at a time. The component renders radio button <input> elements
and assigns the same value to the name attribute for each element. The common
name attribute joins the elements in a group. If you select any radio button in that
group, any previously selected button in the group is deselected.
In general, we don’t recommend setting the name attribute in lightning-radio-group.
The component automatically generates a unique value for name if none is provided. The generated value ensures
a common name for the <input> elements rendered for the radio button group, and is unique in the page.
If you specify the required attribute, at least one radio button must be selected.
When a user interacts with the radio button group and doesn’t make a selection, an
error message appears.
If you specify the disabled attribute, you can’t interact with the radio buttons.
Design
lightning-radio-group implements the radio button blueprint in the Salesforce Lightning Design System (SLDS). The radio button adapts to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.
This example creates a radio button group with two options and option1 is selected
by default. One radio button must be selected as the required attribute is
specified.
You can check which values are selected by using the value attribute.
To retrieve the values when the selection is changed, use the onchange event handler and call
event.detail.value.
1import{LightningElement}from "lwc";2export default class MyComponentName extends LightningElement{3 options = [4{label: "Ross", value: "option1"},5{label: "Rachel", value: "option2"},6];78 // Select option1 by default9 value = "option1";1011 handleChange(event){12 const selectedOption = event.detail.value;13 console.log("Option selected with value: " + selectedOption);14}15}
To create radio buttons, pass in these properties to the options attribute.
Property
Type
Description
label
string
The text that displays next to a radio button.
value
string
The string that’s used to identify which radio button is selected.
Input Validation
A radio group that’s disabled is always valid. If the radio group is marked required and no option is selected, after you remove focus from the radio group an error message is displayed.
You can override the default message using message-when-value-missing when a radio group is required and no option is selected. This message is displayed when you remove focus from the radio group.
1<form onsubmit={handleSubmit}>2 <lightning-radio-group name="radioGroup"3 label="Will you be attending the event?"4 options={options}5 value={value}6 type="radio"7 lwc:ref="radiogroup"8 message-when-value-missing="Select an option"9 required></lightning-radio-group>10 <lightning-button variant="brand" label="Submit" onclick={handleSubmit} type="submit"></lightning-button>11</form>
To programmatically display error messages on invalid fields, use the reportValidity() method. This method returns false if the input is invalid.
For custom error messages, display the message using setCustomValidity() and reportValidity(). For more information, see the lightning-input documentation.
1import{LightningElement}from 'lwc';2export default class ValidationExample extends LightningElement{34 value = '';5 get options(){6 return[7{label: 'Yes', value: 'yes'},8{label: 'No', value: 'no'},9];10}1112 handleSubmit(e){13 const allValid = this.refs.radiogroup.reportValidity();14 if(!allValid){15 e.preventDefault();16 this.refs.radiogroup.setCustomValidity("Please select an option");17 this.refs.radiogroup.showHelpMessageIfInvalid();18}19}
To reuse lightning-radio-group in a page or across multiple tabs such as in a Salesforce console app,
follow one of these suggestions.
Omit the name attribute to enable the component to automatically generate a unique name.
Enclose each lightning-radio-group component in a <form> element and provide your own value for name.
If the reused component generates a unique name, each radio button group in the page renders
<input> elements grouped correctly so that one value can be selected in each group.
If you provide your own name value and reuse the
component with the same name, each radio button group in the page uses the same name value for the <input> elements.
The result is that you can select only one value across all radio button groups, instead of
one value within a radio button group. If you require your own name value, enclose the
reused components in <form> elements to enable the page to use the same name value for multiple radio
button groups.
Component Styling
Use a combination of variants and utility classes to customize the radio group.
Set type="button" to create a component that implements the
radio button group blueprint in SLDS.
Variants
Use the variant attribute with one of these values to change the appearance of the radio group.
label-hidden hides the radio group label but makes it available to assistive technology. This variant doesn’t hide the option labels.
label-inline horizontally aligns the label and radio group.
label-stacked places the label above the radio group.
standard is the default variant, which displays the radio group label above the options.
Utility Classes
To apply additional styling, use the SLDS utility classes with the class attribute.
This example adds a margin around the radio group using an SLDS class.
Component styling hooks provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.
The radio group is nested in a fieldset element that contains a legend
element. The legend contains the label value. The fieldset element enables
grouping of related radio buttons to facilitate tabbing navigation and speech
navigation for accessibility purposes. Similarly, the legend element
improves accessibility by assigning a caption to the fieldset.
Attributes
Name
Description
Type
Default
Required
disabled
If present, the radio group is disabled and users cannot interact with it.
boolean
false
label
Text label for the radio group.
string
message-when-value-missing
Optional message displayed when no radio button is selected and the required attribute is set to true.
string
name
Specifies the name of the radio button group. Only only one button can be selected if a name is specified for the group.
string
options
Array of label-value pairs for each radio button.
Array<{ label: string; value: string; }>
required
If present, a radio button must be selected before the form can be submitted.
boolean
false
type
The style of the radio group. Options are radio or button. The default is radio.
string
radio
validity
Represents the validity states that an element can be in, with respect to constraint validation.
object
value
Specifies the value of the selected radio button.
string
variant
The variant changes the appearance of the radio group. Accepted variants include standard, label-hidden, label-inline, and label-stacked. This value defaults to standard. Use label-hidden to hide the label but make it available to assistive technology. Use label-inline to horizontally align the label and radio group. Use label-stacked to place the label above the radio group.
string
standard
Methods
Name
Description
Argument Name
Argument Type
Argument Description
checkValidity
Returns the valid attribute value (Boolean) on the ValidityState object.
focus
Sets focus on the first radio input element.
reportValidity
Displays the error messages and returns false if the input is invalid. If the input is valid, reportValidity() clears displayed error messages and returns true.
setCustomValidity
Sets a custom error message to be displayed when the radio group value is submitted.
message
string
The string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalid
Shows the help message if the form control is in an invalid state.