单选框
用于在多个备选项中选中单个状态。
何时使用
- 用于在多个备选项中选中单个状态。
- 和 Select 的区别是,Radio 所有选项默认可见,方便用户在比较中选择,因此选项不宜过多。
// 使用 Radio.Group 组件时,推荐的写法 ✅
return (
<Radio.Group
value={value}
options={[
{ value: 1, label: 'A' },
{ value: 2, label: 'B' },
{ value: 3, label: 'C' },
]}
/>
);
// 不推荐的写法 🙅🏻♀️
return (
<Radio.Group value={value}>
<Radio value={1}>A</Radio>
<Radio value={2}>B</Radio>
<Radio value={3}>C</Radio>
</Radio.Group>
);
代码演示
- 基本 ·
basic - 不可用 ·
disabled - 单选组合 ·
radiogroup - Radio.Group 垂直 ·
radiogroup-more - Block 单选组合 ·
radiogroup-block - Radio.Group 组合 - 配置方式 ·
radiogroup-options - 按钮样式 ·
radiobutton - 单选组合 - 配合 name 使用 ·
radiogroup-with-name - 大小 ·
size - 填底的按钮样式 ·
radiobutton-solid - 自定义语义结构的样式和类 ·
style-class
API
通用属性参考:通用属性
Radio/Radio.Button
| 参数 | 说明 | 类型 | 默认值 | 版本 | 全局配置 |
|---|---|---|---|---|---|
| checked | 指定当前是否选中 | boolean | false | × | |
| classNames | 用于自定义组件内部各语义化结构的 class,支持对象或函数 | Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string> | - | 6.0.0 | 6.0.0 |
| defaultChecked | 初始是否选中 | boolean | false | × | |
| disabled | 禁用 Radio | boolean | false | × | |
| styles | 用于自定义组件内部各语义化结构的行内 style,支持对象或函数 | Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties> | - | 6.0.0 | 6.0.0 |
| value | 根据 value 进行比较,判断是否选中 | any | - | × |
Radio.Group
单选框组合,用于包裹一组 Radio。
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| block | 将 RadioGroup 宽度调整为其父宽度的选项 | boolean | false | 5.21.0 |
| buttonStyle | RadioButton 的风格样式,目前有描边和填色两种风格 | outline | solid | outline | |
| classNames | 用于自定义组件内部各语义化结构的 class,支持对象或函数 | Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string> | - | 6.0.0 |
| defaultValue | 默认选中的值 | any | - | |
| disabled | 禁选所有子单选器 | boolean | false | |
| name | RadioGroup 下所有 input[type="radio"] 的 name 属性。若未设置,则将回退到随机生成的名称 | string | - | |
| options | 以配置形式设置子元素 | string[] | number[] | Array<CheckboxOptionType> | - | |
| optionType | 用于设置 Radio options 类型 | default | button | default | 4.4.0 |
| orientation | 排列方向 | horizontal | vertical | horizontal | |
| size | 大小,只对按钮样式生效 | large | medium | small | - | |
| styles | 用于自定义组件内部各语义化结构的行内 style,支持对象或函数 | Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties> | - | 6.0.0 |
| value | 用于设置当前选中的值 | any | - | |
| vertical | 值为 true,Radio Group 为垂直方向。与 orientation 同时存在,以 orientation 优先 | boolean | false | |
| onChange | 选项变化时的回调函数 | function(e:Event) | - |
CheckboxOptionType
| 属性 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| label | 用于作为 Radio 选项展示的文本 | string | - | 4.4.0 |
| value | 关联 Radio 选项的值 | string | number | boolean | - | 4.4.0 |
| style | 应用到 Radio 选项的 style | React.CSSProperties | - | 4.4.0 |
| className | Radio 选项的类名 | string | - | 5.25.0 |
| disabled | 指定 Radio 选项是否要禁用 | boolean | false | 4.4.0 |
| title | 添加 Title 属性值 | string | - | 4.4.0 |
| id | 添加 Radio Id 属性值 | string | - | 4.4.0 |
| onChange | 当 Radio Group 的值发送改变时触发 | (e: CheckboxChangeEvent) => void; | - | 4.4.0 |
| required | 指定 Radio 选项是否必填 | boolean | false | 4.4.0 |
方法
Radio
| 名称 | 描述 |
|---|---|
| blur() | 移除焦点 |
| focus() | 获取焦点 |
Semantic DOM
| 语义节点 | 原生说明 | 版本 |
|---|---|---|
root | 根元素,包含布局样式、鼠标样式、禁用状态文字颜色等基础容器样式 | 6.0.0 |
icon | 选中框元素,包含圆角样式、过渡动画、边框样式、悬停状态、焦点状态等交互样式 | 6.0.0 |
label | 文本元素,包含内边距、文字颜色、禁用状态、对齐方式等文本样式 | 6.0.0 |
语义说明来源: vendor/components/radio/demo/_semantic.tsx
主题变量(Design Token)
类型、继承关系和原生令牌声明保持固定源码原文;默认值由原生主题计算,并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。
| 令牌 | 类型 | 原生说明 |
|---|---|---|
radioSize | number | 单选框大小 |
dotSize | number | 单选框圆点大小 |
dotColorDisabled | string | 单选框圆点禁用颜色 |
buttonBg | string | 单选框按钮背景色 |
buttonCheckedBg | string | 单选框按钮选中背景色 |
buttonColor | string | 单选框按钮文本颜色 |
buttonPaddingInline | number | 单选框按钮横向内间距 |
buttonCheckedBgDisabled | string | 单选框按钮选中并禁用时的背景色 |
buttonCheckedColorDisabled | string | 单选框按钮选中并禁用时的文本颜色 |
buttonSolidCheckedColor | string | 单选框实色按钮选中时的文本颜色 |
buttonSolidCheckedBg | string | 单选框实色按钮选中时的背景色 |
buttonSolidCheckedHoverBg | string | 单选框实色按钮选中时的悬浮态背景色 |
buttonSolidCheckedActiveBg | string | 单选框实色按钮选中时的激活态背景色 |
wrapperMarginInlineEnd | number | 单选框右间距 |
radioColor | string | 见原生声明及继承类型 |
radioBgColor | string | 见原生声明及继承类型 |
声明来源: vendor/components/radio/style/index.ts
import type { CSSObject } from '@ant-design/cssinjs';
import { unit } from '@ant-design/cssinjs';
import { genFocusOutline, resetComponent } from '../../style';
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
import { genStyleHooks, mergeToken } from '../../theme/internal';
// ============================== Tokens ==============================
export interface ComponentToken {
// Radio
/**
* @desc 单选框大小
* @descEN Radio size
*/
radioSize: number;
/**
* @desc 单选框圆点大小
* @descEN Size of Radio dot
*/
dotSize: number;
/**
* @desc 单选框圆点禁用颜色
* @descEN Color of disabled Radio dot
*/
dotColorDisabled: string;
// Radio buttons
/**
* @desc 单选框按钮背景色
* @descEN Background color of Radio button
*/
buttonBg: string;
/**
* @desc 单选框按钮选中背景色
* @descEN Background color of checked Radio button
*/
buttonCheckedBg: string;
/**
* @desc 单选框按钮文本颜色
* @descEN Color of Radio button text
*/
buttonColor: string;
/**
* @desc 单选框按钮横向内间距
* @descEN Horizontal padding of Radio button
*/
buttonPaddingInline: number;
/**
* @desc 单选框按钮选中并禁用时的背景色
* @descEN Background color of checked and disabled Radio button
*/
buttonCheckedBgDisabled: string;
/**
* @desc 单选框按钮选中并禁用时的文本颜色
* @descEN Color of checked and disabled Radio button text
*/
buttonCheckedColorDisabled: string;
/**
* @desc 单选框实色按钮选中时的文本颜色
* @descEN Color of checked solid Radio button text
*/
buttonSolidCheckedColor: string;
/**
* @desc 单选框实色按钮选中时的背景色
* @descEN Background color of checked solid Radio button text
*/
buttonSolidCheckedBg: string;
/**
* @desc 单选框实色按钮选中时的悬浮态背景色
* @descEN Background color of checked solid Radio button text when hover
*/
buttonSolidCheckedHoverBg: string;
/**
* @desc 单选框实色按钮选中时的激活态背景色
* @descEN Background color of checked solid Radio button text when active
*/
buttonSolidCheckedActiveBg: string;
/**
* @desc 单选框右间距
* @descEN Margin right of Radio button
*/
wrapperMarginInlineEnd: number;
/** @internal */
radioColor: string;
/** @internal */
radioBgColor: string;
}
/**
* @desc Radio 组件的 Token
* @descEN Token for Radio component
*/
interface RadioToken extends FullToken<'Radio'> {
/**
* @desc 单选框焦点阴影
* @descEN Focus shadow of Radio
*/
radioFocusShadow: string;
/**
* @desc 单选框按钮焦点阴影
* @descEN Focus shadow of Radio button
*/
radioButtonFocusShadow: string;
}
原生通用属性
Tips: 以下通用属性适用于 antd 大部分组件,不支持的组件会单独说明。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| style | 自定义样式 | CSSProperties | - |
| className | 自定义类名 | string | - |
| rootClassName | 添加在组件最外层的 className | string | - |
| autoFocus | 自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效 | boolean | false |
安装并复现示例
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23
使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。
原生类型参考
源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。
vendor/components/radio/index.tsxvendor/components/radio/interface.tsvendor/components/radio/radioButton.tsxvendor/components/radio/style/index.ts
来源与许可
https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd
保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。