MDUI文档
预设颜色
自选颜色
从壁纸提取颜色
请选择一张壁纸
开发指南
样式
与框架集成
组件
Avatar 头像Badge 徽标BottomAppBar 底部应用栏Button 按钮ButtonIcon 图标按钮Card 卡片Checkbox 复选框Chip 纸片CircularProgress 圆形进度指示器Collapse 折叠面板Dialog 对话框Divider 分割线Dropdown 下拉组件Fab 浮动操作按钮Icon 图标Layout 布局LinearProgress 线性进度指示器List 列表Menu 菜单NavigationBar 底部导航栏NavigationDrawer 侧边抽屉栏NavigationRail 侧边导航栏Radio 单选框RangeSlider 范围滑块Select 选择框SegmentedButton 分段按钮Slider 滑块Snackbar 消息条Switch 开关切换Tabs 选项卡TextField 文本框Tooltip 工具提示TopAppBar 顶部应用栏
工具函数
独立包

Radio 单选框

The radio group component is designed for selecting a single option from a set of options.

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:

Chinese English
<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

属性

HTML 属性JavaScript 属性Reflect类型默认值
disableddisabledbooleanfalse

Disables the radio group when set.

formformstring-

Associates the radio group with a <form> element. The value should be the id of a <form> in the same document. If not set, the radio group is associated with its parent <form>, if any.

This attribute allows radio group elements to be associated with <form>s anywhere in the document, not just inside a <form>.

namenamestring''

The name of the radio group, which is submitted with form data.

valuevaluestring''

The value of the selected radio button, which is submitted with form data.

defaultValuestring''

The default selected value. Resets to this value when form is reset. This value can only be set via JavaScript property.

requiredrequiredbooleanfalse

Requires a radio selection when the form is submitted.

validityValidityState-

A ValidityState object that represents the element's validity states.

validationMessagestring-

The element's validation message. This is empty if the element meets its constraints.

方法

名称参数返回值
checkValidity
boolean

Checks the validity of the form field. If it's invalid, it triggers an invalid event and returns false. If it's valid, it returns true.

reportValidity
boolean

Checks the validity of the form field. If it's invalid, it triggers an invalid event, returns false, and displays a validation message. If it's valid, it returns true.

setCustomValidity
  • message: string
void

Sets a custom error message. If the text is non-empty, it indicates that the field is invalid.

事件

名称
change

Emitted when the selected value changes.

input

Emitted when the selected value changes.

invalid

Emitted when the form control's validity is checked and it doesn't meet the constraints.

Slots

名称
默认

<mdui-radio> elements.

mdui-radio API

属性

HTML 属性JavaScript 属性Reflect类型默认值
valuevaluestring''

Specifies the value of the radio.

disableddisabledbooleanfalse

Disables the radio when set.

checkedcheckedbooleanfalse

Sets the radio to the checked state.

unchecked-iconuncheckedIconstring-

Specifies the Material Icons name for the unchecked state. Alternatively, use slot="unchecked-icon".

checked-iconcheckedIconstring-

Specifies the Material Icons name for the checked state. Alternatively, use slot="checked-icon".

autofocusautofocusbooleanfalse

Determines if the element should be focused when the page loads.

tabindextabIndexnumber-

Specifies the order in which the element receives focus when navigating with the Tab key.

方法

名称参数返回值
click
void

Simulates a mouse click on the element.

focus
  • options: FocusOptions (可选)
void

Sets focus on the element. An optional parameter can be an object with a preventScroll property. If preventScroll is set to true, the page will not scroll to bring the focused element into view.

blur
void

Removes focus from the element.

事件

名称
focus

Emitted when the radio gains focus.

blur

Emitted when the radio loses focus.

change

Emitted when the radio is selected.

Slots

名称
默认

Text content.

unchecked-icon

Icon for the unchecked state.

checked-icon

Icon for the checked state.

CSS Parts

名称
control

Container for the left icon.

unchecked-icon

Icon for the unchecked state.

checked-icon

Icon for the checked state.

label

Text content.

本页目录