arco-design 单选框 Radio 嵌入图标实战:带图标的单选框示例与 children 渲染原理解析

原创2026-10-15 09:20:51765 阅读
文章标签:UI组件前端设计系统

arco-design 单选框 Radio 嵌入图标实战:带图标的单选框示例与 children 渲染原理解析

本篇技术指南以 arco-design 仓库中 带图标的单选框示例 为核心,讲解如何让单选框 Radio 在文字标签旁嵌入图片图标,并深入剖析其底层的 children 渲染机制、Radio.Group 上下文联动与样式对齐细节。读完本文,你将掌握在 Arco Design React 的 Radio 中自由组合图标、图片与自定义内容的方法,并理解示例背后从组件源码到快照测试的完整链路。

一、示例效果与完整代码:单选框如何与图标组合

原文档 components/Radio/__demo__/icon.md 给出的核心结论非常简洁:单选框可以与图标进行组合(You can display icons in children)。这里的 "children" 正是关键——Radio 的图标并不是通过某个 icon 属性传入的,而是直接写在组件标签内部的子节点中。

以下为该演示的完整代码(原样继承自 icon.md):

import { Radio } from '@arco-design/web-react';
import { IconXiguaColor, IconLarkColor, IconTiktokColor } from '@arco-design/web-react/icon';
const RadioGroup = Radio.Group;
const imgStyle = {
  width: 30,
  height: 30,
  verticalAlign: 'middle',
};

const App = () => {
  return (
    <div>
      <RadioGroup>
        <Radio value="BCY">
          <img
            src="//p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/c621ef94232e4b91917c7d2e5d9eae00~tplv-uwbnlip3yd-image.image"
            style={imgStyle}
          />
          BCY
        </Radio>
        <Radio value="pipidance">
          <img
            src="//p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/cb76fb049c3c44128b26f02902a0373b~tplv-uwbnlip3yd-image.image"
            style={imgStyle}
          />
          Pipidance
        </Radio>
        <Radio disabled value="xigua">
          <img
            src="//p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/74fb860e37aa4840a8eb235e6df6e6e0~tplv-uwbnlip3yd-image.image"
            style={imgStyle}
          />
          Xigua Video
        </Radio>
      </RadioGroup>
    </div>
  );
};

export default App;

这段示例展示了三个关键使用要点:

  1. 图标放在 children 中:<img> 与文字(如 BCY)一同作为 Radio 的子节点,单选框的圆点标记仍然渲染在最左侧,图标与文字排列在右侧文本区;
  2. Radio.Group 组合互斥:三个 Radio 通过 Radio.Group 包裹,同一时刻只能选中一个,选中项由 Radio 的 value 标识(BCY、pipidance、xigua);
  3. 禁用态可以同时作用于带图标的选项:第三个 Radio 设置了 disabled,即使其内容带有图片,整项仍保持禁用视觉与行为。

示例还从 @arco-design/web-react/icon 引入了 IconXiguaColor、IconLarkColor、IconTiktokColor 三个彩色图标组件。在 icon/icons.json 中可以确认,这三个组件属于 logo 类别的 color 图标(西瓜视频、飞书、抖音的彩色品牌图标),相关类型声明见 icon/index.d.ts。它们与示例中通过 img 标签渲染的图片共同印证了"图标 + 单选框"的两种接入路径:既可以使用 Arco 内置图标组件,也可以直接使用任意图片资源。

二、源码原理:children 是如何被渲染进单选框的

要理解"图标写进 children 即可"的背后机制,需要看 Radio 的核心实现 components/Radio/radio.tsx。组件渲染的核心逻辑如下:

{isFunction(children) ? (
  children({ checked, disabled })
) : context.type === 'radio' ? (
  <>
    <IconHover
      prefix={prefixCls}
      className={`${prefixCls}-mask-wrapper`}
      disabled={checked || disabled}
    >
      <div className={`${prefixCls}-mask`} />
    </IconHover>
    {!isNullOrUndefined(children) && <span className={`${prefixCls}-text`}>{children}</span>}
  </>
) : (
  context.type === 'button' && <span className={`${prefixCls}-button-inner`}>{children}</span>
)}

从源码可以梳理出 children 的三种渲染分支:

  • 函数式 children:若 children 是函数,则调用 children({ checked, disabled }),把当前选中态与禁用态传给调用方,适合完全自定义的单选内容(接口定义见 components/Radio/interface.ts);
  • 普通单选(type 为 radio):先渲染左侧的圆点标记(IconHover 包裹的 arco-radio-mask),再将 children 原样包进 <span class="arco-radio-text">。示例中的 <img> 图标和文字就是落在这个文本容器里,因此无需任何额外属性即可混排图片;
  • 按钮型单选(type 为 button):children 被放入 arco-radio-button-inner,此时同样可以放入图标,只是视觉容器从圆点+文字变成了按钮形态。

外层结构也值得注意:整个 Radio 最终渲染为一个 <label> 元素,内部包含一个视觉上隐藏(opacity: 0)的原生 <input type="radio">,点击 label 任意位置(包括图片区域)都会触发原生 input 的选中行为——这正是"图标可点击切换选中"的底层保障。样式层面对隐藏 input 与焦点态的处理见 components/Radio/style/index.less。

三、Radio.Group 如何协调带图标的选项:上下文与选中值

示例中三个带图标的 Radio 被 Radio.Group 包裹,互斥逻辑由 Group 组件统一管理。核心实现在 components/Radio/group.tsx:

  • Group 通过 useMergeValue 维护选中值(受控 value / 非受控 defaultValue 均支持);
  • 通过 RadioGroupContext.Provider 向下注入 onChangeValue、value、disabled、name、type 等上下文(见 group.tsx);
  • 每个 Radio 在渲染时从 useContext(RadioGroupContext) 取到上下文,若 context.group 为真,则用 context.value === props.value 覆盖自身选中态、用 context.disabled 兜底禁用态(见 radio.tsx)。

因此,带图标的选项与普通选项在数据流上完全一致:点击图标区域 → label 内的 input 触发 change → Radio 调用 context.onChangeValue → Group 更新选中值并回调 onChange(value, event)。图标只是"视觉负载",不参与任何状态逻辑。

对于不想逐个手写 Radio 的场景,Group 也支持 options 数组配置方式。注意其元素类型为 string | number | { label: ReactNode; value: any; disabled?: boolean }(见 interface.ts),由于 label 是 ReactNode,同样可以传入 <img> 或图标组件来生成带图标的选项(对应 group.tsx 的 options.map 渲染逻辑)。

四、样式细节:图片尺寸、基线对齐与文本间距

示例中定义了一个统一的图片样式:

const imgStyle = {
  width: 30,
  height: 30,
  verticalAlign: 'middle',
};

这个 verticalAlign: 'middle' 是有讲究的。在 Radio 的默认(radio 类型)布局中,children 被渲染进 arco-radio-text 文本容器,而该容器与左侧圆点标记同处一行。若不设置垂直对齐,图片会按基线对齐导致图标与文字、圆点在垂直方向上错位。相关样式在 components/Radio/style/index.less:

&-text {
  color: @radio-color-text;
  margin-left: @radio-margin-text-left;
}

&-mask-wrapper {
  vertical-align: middle;
  top: -0.09em;
  position: relative;
  line-height: 1;
}

可以推断:arco-radio-mask-wrapper 本身就通过 vertical-align: middle 与行内基线对齐,示例为图片同样设置 vertical-align: middle,正是为了让图片、文字、圆点三者处于同一视觉中轴线。圆点标记本身由 arco-radio-mask 渲染——一个带边框的圆形 div,选中时内部白色圆点通过 ::after 的 transform: scale(0.4) 呈现(见 style/index.less)。

五、边界情况与组合建议

结合源码与示例,使用"带图标的单选框"时有几点值得注意:

  1. 禁用项中的图标:示例第三个选项 disabled 后,arco-radio-text 的文字颜色会变为禁用色(@radio-color-text_disabled),但图片本身不会被滤镜处理。若需要图片整体呈现禁用态,建议自行在图片外层叠加透明度等样式;
  2. 图标大小与单选框尺寸:Radio.Group 的 size 仅对 type="button" 生效(见 interface.ts),默认单选形态下不受 size 影响,图片尺寸由 imgStyle 自行控制;
  3. 函数式 children 更自由:如果需要根据选中/禁用状态动态切换图标(如选中时换高亮图标),可直接使用 children({ checked, disabled }) 函数形式;
  4. 图片协议:示例中 src 使用了 // 协议相对地址(跟随页面协议自动选择 http/https),接入时建议改用真实可访问的图片地址或 CDN 地址;
  5. 可访问性:因为渲染结构是 <label><input type="radio">...</label>,键盘操作与屏幕阅读器语义依然完整,无需额外处理。

六、API 速查:与图标组合相关的核心属性

完整的 Radio API 表格见 components/Radio/README.zh-CN.md,以下摘录与"图标 + 内容组合"最相关的部分:

参数名 描述 类型 默认值
checked 是否选中(受控模式) boolean -
defaultChecked 初始是否选中 boolean -
disabled 是否禁用 boolean -
value 控件的 value T -
onChange 值变化的回调 (checked: boolean, event: ChangeEvent) => void -
children 内容(可以是 ReactNode 或渲染函数) ReactNode | ((value: { checked; disabled }) => ReactNode) -

其中 Radio.Group 的 value / defaultValue / onChange(value, event) 用于互斥组的状态管理,disabled 可一键禁用整组。

七、测试验证:快照测试如何保证示例稳定

仓库为每个 __demo__/*.md 文档都注册了快照测试:测试入口 tests/demoTest.ts 会通过 glob 遍历 components/${component}/__demo__/*.md,将每个 md 中的示例代码渲染为 React 组件并与快照比对。本示例对应的快照记录在 components/Radio/test/snapshots/demo.test.ts.snap 中,从中可以看到渲染后的真实 DOM 结构:

<div class="arco-radio-group arco-radio-size-default arco-radio-mode-outline" role="radiogroup">
  <label class="arco-radio">
    <input type="radio" value="BCY" />
    <span class="arco-icon-hover arco-radio-icon-hover arco-radio-mask-wrapper">
      <div class="arco-radio-mask" />
    </span>
    <span class="arco-radio-text">
      <img src="//p1-arco.byteimg.com/..." style="width:30px;height:30px;vertical-align:middle" />
      BCY
    </span>
  </label>
  ...
  <label class="arco-radio arco-radio-disabled">
    <input disabled="" type="radio" value="xigua" />
    ...
    Xigua Video
  </label>
</div>

这份快照与第一节的示例代码一一对应:三个带图片的 Radio、第三个带有 arco-radio-disabled 类、Group 容器默认应用 arco-radio-size-default arco-radio-mode-outline。它同时验证了 children 渲染路径(图片落入 arco-radio-text)与禁用态的类名拼接逻辑。若你修改了示例内容,运行对应的 demo 快照测试即可自动发现差异。

综上,Arco Design React 的单选框对图标组合保持了极高的开放性:无需任何特殊 API,把图标(图片或 Icon 组件)写进 children 即可,选中、禁用、互斥等所有交互逻辑均由 radio.tsx 与 group.tsx 的内部机制接管。对于品牌 Logo 场景,仓库还内置了 IconXiguaColor、IconLarkColor、IconTiktokColor 等彩色图标(见 icon/icons.json),可直接替换示例中的 img 使用。

登录后查看全文
arco-design