Radio Group
<wa-radio-group>
Radio groups are used to group multiple radios so they function as a single form control.
<wa-radio-group label="Select an option" name="a" value="1"> <wa-radio value="1">Option 1</wa-radio> <wa-radio value="2">Option 2</wa-radio> <wa-radio value="3">Option 3</wa-radio> </wa-radio-group>
Examples
Jump to heading
Hint
Jump to heading
Add descriptive hint to a radio group with the hint attribute. For hints that contain HTML, use the hint slot instead.
<wa-radio-group label="Select an option" hint="Choose the most appropriate option." name="a" value="1"> <wa-radio value="1">Option 1</wa-radio> <wa-radio value="2">Option 2</wa-radio> <wa-radio value="3">Option 3</wa-radio> </wa-radio-group>
Radio Buttons
Jump to heading
Set the appearance attribute to button on all radios to render a radio button group.
<wa-radio-group label="Horizontal options" hint="Select an option that makes you proud." orientation="horizontal" name="a" value="1" > <wa-radio appearance="button" value="1">Option 1</wa-radio> <wa-radio appearance="button" value="2">Option 2</wa-radio> <wa-radio appearance="button" value="3">Option 3</wa-radio> </wa-radio-group> <br /> <wa-radio-group label="Vertical options" hint="Select an option that makes you proud." orientation="vertical" name="a" value="1" style="max-width: 300px;" > <wa-radio appearance="button" value="1">Option 1</wa-radio> <wa-radio appearance="button" value="2">Option 2</wa-radio> <wa-radio appearance="button" value="3">Option 3</wa-radio> </wa-radio-group>
Disabling
Jump to heading
To disable the entire radio group, add the disabled attribute to the radio group.
<wa-radio-group label="Select an option" disabled> <wa-radio value="1">Option 1</wa-radio> <wa-radio value="2" disabled>Option 2</wa-radio> <wa-radio value="3">Option 3</wa-radio> </wa-radio-group>
To disable individual options, add the disabled attribute to the respective options.
<wa-radio-group label="Select an option"> <wa-radio value="1">Option 1</wa-radio> <wa-radio value="2" disabled>Option 2</wa-radio> <wa-radio value="3">Option 3</wa-radio> </wa-radio-group>
Orientation
Jump to heading
The default orientation for radio items is vertical. Set the orientation to horizontal to items on the same row.
<wa-radio-group label="Select an option" hint="Choose the most appropriate option." orientation="horizontal" name="a" value="1" > <wa-radio value="1">Option 1</wa-radio> <wa-radio value="2">Option 2</wa-radio> <wa-radio value="3">Option 3</wa-radio> </wa-radio-group>
Sizing Options
Jump to heading
The size of radios will be determined by the Radio Group's size attribute.
<wa-radio-group label="Select an option" size="medium" value="medium" onchange="this.size = this.value"> <wa-radio value="small">Small</wa-radio> <wa-radio value="medium">Medium</wa-radio> <wa-radio value="large">Large</wa-radio> </wa-radio-group>
Radios also have a size attribute,
which will override the inherited size when used.
Validation
Jump to heading
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.
<form class="validation"> <wa-radio-group label="Select an option" name="a" required> <wa-radio value="1">Option 1</wa-radio> <wa-radio value="2">Option 2</wa-radio> <wa-radio value="3">Option 3</wa-radio> </wa-radio-group> <br /> <wa-button type="submit" variant="brand">Submit</wa-button> </form> <script> const form = document.querySelector('.validation'); // Handle form submit form.addEventListener('submit', event => { event.preventDefault(); alert('All fields are valid!'); }); </script>
Custom Validity
Jump to heading
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.
<form class="custom-validity"> <wa-radio-group label="Select an option" name="a" value="1"> <wa-radio value="1">Not me</wa-radio> <wa-radio value="2">Me neither</wa-radio> <wa-radio value="3">Choose me</wa-radio> </wa-radio-group> <br /> <wa-button type="submit" variant="brand">Submit</wa-button> </form> <script> const form = document.querySelector('.custom-validity'); const radioGroup = form.querySelector('wa-radio-group'); const errorMessage = 'You must choose the last option'; // Set initial validity as soon as the element is defined customElements.whenDefined('wa-radio-group').then(() => { radioGroup.setCustomValidity(errorMessage); }); // Update validity when a selection is made form.addEventListener('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>
Slots
Jump to heading
Learn more about using slots.
| Name | Description |
|---|---|
| (default) | The default slot where <wa-radio> elements are placed. |
label
|
The radio group's label. Required for proper accessibility. Alternatively, you can use the label attribute. |
hint
|
Text that describes how to use the radio group. Alternatively, you can use the hint attribute. |
Attributes & Properties
Jump to heading
Learn more about attributes and properties.
| Name | Description | Reflects | |
|---|---|---|---|
labellabel |
The radio group's label. Required for proper accessibility. If you need to display HTML, use the
label slot
instead.Type
stringDefault
'' |
||
hinthint |
The radio groups's hint. If you need to display HTML, use the
hint slot instead.Type
stringDefault
'' |
||
namename |
The name of the radio group, submitted as a name/value pair with form data.
Type
string | nullDefault
null |
|
|
disableddisabled |
Disables the radio group and all child radios.
Type
booleanDefault
false |
|
|
orientationorientation |
The orientation in which to show radio items.
Type
'horizontal' | 'vertical'Default
'vertical' |
|
|
value |
The current value of the radio group, submitted as a name/value pair with form data.
|
||
defaultValuevalue |
The default value of the form control. Primarily used for resetting the form control.
Type
string | null |
|
|
sizesize |
The radio group's size. This size will be applied to all child radios and radio buttons, except when explicitly overridden.
Type
'small' | 'medium' | 'large'Default
'medium' |
|
|
requiredrequired |
Ensures a child radio is checked before allowing the containing form to submit.
Type
booleanDefault
false |
|
|
withLabelwith-label |
Used for SSR. if true, will show slotted label on initial render.
Type
booleanDefault
false |
||
withHintwith-hint |
Used for SSR. if true, will show slotted hint on initial render.
Type
booleanDefault
false |
||
validationTarget |
We use the first available radio as the validationTarget similar to native HTML that shows the validation popup on
the first radio element.
|
Methods
Jump to heading
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
focus() |
Sets focus on the radio group. |
options: FocusOptions
|
Events
Jump to heading
Learn more about events.
| Name | Description |
|---|---|
input |
Emitted when the radio group receives user input. |
change |
Emitted when the radio group's selected value changes. |
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
CSS parts
Jump to heading
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
form-control |
The form control that wraps the label, input, and hint. |
::part(form-control)
|
form-control-label |
The label's wrapper. |
::part(form-control-label)
|
form-control-input |
The input's wrapper. |
::part(form-control-input)
|
radios |
The wrapper than surrounds radio items, styled as a flex container by default. |
::part(radios)
|
hint |
The hint's wrapper. |
::part(hint)
|
Dependencies
Jump to heading
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.
Importing
Jump to heading
Autoloading components via projects is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
Let your project code do the work! Sign up for free to use a project with your very own CDN — it's the fastest and easiest way to use Web Awesome.
To manually import this component from NPM, use the following code.
import '@awesome.me/webawesome/dist/components/radio-group/radio-group.js';
To manually import this component from React, use the following code.
import WaRadioGroup from '@awesome.me/webawesome/dist/react/radio-group';