JGuang

单选框

Radio · Ant Design

交互预览
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

单选框

用于在多个备选项中选中单个状态。

何时使用

  • 用于在多个备选项中选中单个状态。
  • 和 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指定当前是否选中booleanfalse×
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-6.0.06.0.0
defaultChecked初始是否选中booleanfalse×
disabled禁用 Radiobooleanfalse×
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-6.0.06.0.0
value根据 value 进行比较,判断是否选中any-×

Radio.Group

单选框组合,用于包裹一组 Radio。

参数说明类型默认值版本
block将 RadioGroup 宽度调整为其父宽度的选项booleanfalse5.21.0
buttonStyleRadioButton 的风格样式,目前有描边和填色两种风格outline | solidoutline
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-6.0.0
defaultValue默认选中的值any-
disabled禁选所有子单选器booleanfalse
nameRadioGroup 下所有 input[type="radio"] 的 name 属性。若未设置,则将回退到随机生成的名称string-
options以配置形式设置子元素string[] | number[] | Array<CheckboxOptionType>-
optionType用于设置 Radio options 类型default | buttondefault4.4.0
orientation排列方向horizontal | verticalhorizontal
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 优先booleanfalse
onChange选项变化时的回调函数function(e:Event)-

CheckboxOptionType

属性说明类型默认值版本
label用于作为 Radio 选项展示的文本string-4.4.0
value关联 Radio 选项的值string | number | boolean-4.4.0
style应用到 Radio 选项的 styleReact.CSSProperties-4.4.0
classNameRadio 选项的类名string-5.25.0
disabled指定 Radio 选项是否要禁用booleanfalse4.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 选项是否必填booleanfalse4.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 覆盖。本表不伪造静态默认值。

令牌类型原生说明
radioSizenumber单选框大小
dotSizenumber单选框圆点大小
dotColorDisabledstring单选框圆点禁用颜色
buttonBgstring单选框按钮背景色
buttonCheckedBgstring单选框按钮选中背景色
buttonColorstring单选框按钮文本颜色
buttonPaddingInlinenumber单选框按钮横向内间距
buttonCheckedBgDisabledstring单选框按钮选中并禁用时的背景色
buttonCheckedColorDisabledstring单选框按钮选中并禁用时的文本颜色
buttonSolidCheckedColorstring单选框实色按钮选中时的文本颜色
buttonSolidCheckedBgstring单选框实色按钮选中时的背景色
buttonSolidCheckedHoverBgstring单选框实色按钮选中时的悬浮态背景色
buttonSolidCheckedActiveBgstring单选框实色按钮选中时的激活态背景色
wrapperMarginInlineEndnumber单选框右间距
radioColorstring见原生声明及继承类型
radioBgColorstring见原生声明及继承类型

声明来源: 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添加在组件最外层的 classNamestring-
autoFocus自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效booleanfalse

安装并复现示例

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。

原生类型参考

源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。

  • vendor/components/radio/index.tsx
  • vendor/components/radio/interface.ts
  • vendor/components/radio/radioButton.tsx
  • vendor/components/radio/style/index.ts

来源与许可

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。