Radio
The radio group component is designed for selecting one option from a set of choices.
Usage
Import the component:
import 'mdui/components/radio-group.js';
import 'mdui/components/radio.js';
Import the TypeScript type:
import type { RadioGroup } from 'mdui/components/radio-group.js';
import type { Radio } from 'mdui/components/radio.js';
Example:
<mdui-radio-group value="chinese">
<mdui-radio value="chinese">Chinese</mdui-radio>
<mdui-radio value="english">English</mdui-radio>
</mdui-radio-group>Examples
Checked State
The value property of the <mdui-radio-group> component corresponds to the value of the currently selected <mdui-radio> component. You can change the selected radio button by updating the value property of the <mdui-radio-group> component.
The <mdui-radio> component can also be used independently. In this case, use the checked property to access and modify the checked state.
Disabled State
To disable the entire radio group, add the disabled attribute to the <mdui-radio-group> component.
To disable a specific radio button, add the disabled attribute to the <mdui-radio> component.
Icons
You can set Material Icons for the unchecked and checked states of the radio button using the unchecked-icon and checked-icon attributes, respectively. Alternatively, you can use the unchecked-icon and checked-icon slots.
mdui-radio-group API
Properties
| Attribute | Property | Reflect | Type | Default |
|---|---|---|---|---|
disabled | disabled | boolean | false | |
Disables the radio group. | ||||
form | form | string | - | |
Associates the radio group with a This lets the radio group work with any form in the document, not just the one it is nested in. | ||||
name | name | string | '' | |
The name of the radio group, which is submitted with form data. | ||||
value | value | string | '' | |
The value of the selected radio button, which is submitted with form data. | ||||
defaultValue | string | '' | ||
The default selected value. The radio group resets to this value when the form is reset. JavaScript only. | ||||
required | required | boolean | false | |
Requires a radio selection when the form is submitted. | ||||
validity | ValidityState | - | ||
A | ||||
validationMessage | string | - | ||
Validation message. Empty string when valid. | ||||
Methods
| Name | Parameters | Returns |
|---|---|---|
checkValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
reportValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
setCustomValidity |
| void |
Sets a custom error message. If the message is non-empty, the field is considered invalid. | ||
mdui-radio API
Properties
| Attribute | Property | Reflect | Type | Default |
|---|---|---|---|---|
value | value | string | '' | |
Specifies the value of the radio. | ||||
disabled | disabled | boolean | false | |
Disables the radio. | ||||
checked | checked | boolean | false | |
Sets the radio to the checked state. | ||||
unchecked-icon | uncheckedIcon | string | - | |
Specifies the Material Icons name for the unchecked state. Alternatively, use | ||||
checked-icon | checkedIcon | string | - | |
Specifies the Material Icons name for the checked state. Alternatively, use | ||||
autofocus | autofocus | boolean | false | |
Whether the element is focused when the page loads. | ||||
tabindex | tabIndex | number | - | |
The element's tab order when navigating with the Tab key. | ||||
Methods
| Name | Parameters | Returns |
|---|---|---|
click | void | |
Simulates a mouse click on the element. | ||
focus |
| void |
Sets focus on the element. An optional object parameter may include a | ||
blur | void | |
Removes focus from the element. | ||
Slots
| Name |
|---|
| (default) |
Text content. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
CSS Parts
| Name |
|---|
control |
Container for the left icon. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
label |
Text content. |