Arco Design React Result 组件之 Info 状态:操作结果反馈页面的落地实践
Arco Design React Result 组件之 Info 状态:操作结果反馈页面的落地实践
导读
本文聚焦 arco-design 仓库中 Result(结果反馈)组件的 info 状态,讲解如何用一行配置呈现"操作已完成"类的信息型反馈页面。你将掌握 Result 的状态枚举与默认图标映射、title/subTitle/extra 的组合用法、自定义图标与 status={null} 的配合方式,以及从源码层面理解样式令牌与图标渲染机制,可直接套用到表单提交成功、任务处理完成等业务场景。
从示例看 Info 状态:展示处理结果
在 components/Result/demo/info.md 中,Info 状态的核心语义是"展示处理结果"(en-US: Show processing results),即用于告知用户某次操作已经完成、无需再处理。官方给出的最小示例代码如下:
import { Result, Button } from '@arco-design/web-react';
const App = () => {
return (
<div>
<Result
title="Your operation has been performed."
extra={<Button type="primary">Back</Button>}
></Result>
</div>
);
};
export default App;
这段代码没有显式声明 status,但 Result 组件的默认状态正是 info(见 components/Result/interface.ts 中 @defaultValue info 的注释,以及 components/Result/index.tsx 中 defaultProps = { status: 'info' } 的实现),因此页面会自动渲染一个蓝色圆形底纹的 info 图标、居中的标题文字,以及一个"Back"主按钮作为额外操作区。
status 状态体系与默认图标映射
Result 的 status 属性类型定义如下(components/Result/interface.ts):
status?: 'success' | 'error' | 'info' | 'warning' | '404' | '403' | '500' | null;
源码 components/Result/index.tsx 中维护了一张默认图标映射表:
const defaultIcons = {
success: <IconCheck />,
info: <IconInfo />,
warning: <IconExclamation />,
error: <IconClose />,
'404': <Image404 />,
'403': <Image403 />,
'500': <Image500 />,
};
可见其设计分为两类:
- 语义状态(
success/info/warning/error):使用线性小图标,图标外层有圆形背景色底纹(由 components/Result/style/token.less 中的result-color-icon-bg_*系列变量控制)。 - HTTP 异常状态(
403/404/500):使用内嵌在组件里的定制 SVG 插画(404.tsx、403.tsx、500.tsx),无需标题也能形成完整页面,适合接入路由 404 页、无权限页与服务器错误页。
各状态对应的颜色令牌(token.less):
| 状态 | 图标颜色 | 图标底纹背景 |
|---|---|---|
| info | @color-primary-6(品牌蓝) |
--color-primary-light-1 |
| success | @color-success-6(成功绿) |
--color-success-light-1 |
| error | @color-danger-6(危险红) |
--color-danger-light-1 |
| warning | @color-warning-6(警告橙) |
--color-warning-light-1 |
这些令牌进一步由 index.less 中的 .icon-status(@status) 混入(mixin)生成最终的 color 与 background-color,并通过 arco-result-icon-{status} 类名挂载到图标节点上。测试用例 components/Result/test/index.test.tsx 对 ['success', 'error', 'info', 'warning', '404', '403', '500'] 全量状态逐一断言了 arco-result-is-${x} 与 arco-result-icon-${x} 类名的存在,可验证该映射机制。
核心 API:title、subTitle、extra 与自定义图标
Result 的完整 API 见 components/Result/README.zh-CN.md,其中与本例直接相关的参数:
| 参数名 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| status | 不同状态,传入 null 时,需要通过 icon 属性设置图标,并且默认没有背景色以及图标颜色 |
'success' | 'error' | 'info' | 'warning' | '404' | '403' | '500' | null |
info |
| title | 标题文字 | ReactNode | - |
| subTitle | 子标题文字 | ReactNode | - |
| extra | 额外内容 | ReactNode | - |
| icon | 自定义图标 | ReactNode | - |
| className | 节点类名 | string | string[] | - |
| style | 节点样式 | CSSProperties | - |
从渲染结构看(index.tsx),组件按序输出五段式布局:icon → title → subTitle → extra → children(content),且各段只有在传入对应属性时才渲染。其中 children 用于放置更复杂的补充说明内容,例如"完整功能"示例 demo/all.md 中通过 children 传入 Typography 段落来展示排查建议:
import { Result, Button, Typography } from '@arco-design/web-react';
import { IconFaceFrownFill } from '@arco-design/web-react/icon';
const App = () => {
return (
<div>
<Result
status="error"
icon={<IconFaceFrownFill />}
title="No internet"
subTitle="DNS_PROBE_FINISHED_NO_INTERNET"
extra={<Button type="primary">Refresh</Button>}
>
<Typography
className="result-content"
style={{ background: 'var(--color-fill-2)', padding: 24 }}
>
<Typography.Paragraph>Try:</Typography.Paragraph>
<ul>
<li> Checking the network cables, modem, and router </li>
<li> Reconnecting to Wi-Fi </li>
</ul>
</Typography>
</Result>
</div>
);
};
extra 支持传入数组以渲染多个操作按钮,见 demo/basic.md 与 demo/error.md 中 extra={[<Button key="again">Again</Button>, <Button key="back" type="primary">Back</Button>]} 的写法,适合"重试 / 返回"成对操作。
自定义图标:status={null} 的进阶用法
当默认图标无法满足品牌化需求时,可将 status 设为 null 并配合 icon 属性传入任意 ReactNode,示例见 demo/icon.md:
import { Result, Button } from '@arco-design/web-react';
import { IconFaceSmileFill } from '@arco-design/web-react/icon';
const App = () => {
return (
<div>
<Result
status={null}
icon={<IconFaceSmileFill style={{ color: 'rgb(var(--arcoblue-6))' }} />}
title="Your operation has been performed."
extra={<Button type="primary">Back</Button>}
></Result>
</div>
);
};
这一特性自 2.3.0 版本引入(见 components/Result/changelog/index.zh-CN.md):传入 status = null 时组件不设置默认背景色,并调整自定义图标大小。在样式层面,index.less 中的 .arco-result-icon-custom 类会重置图标容器的宽高与行高,并将图标尺寸提升到 @result-size-icon_custom: 45px,颜色则继承自 @result-color-icon_default(默认 inherit),因此自定义图标的外观完全由传入的 style 决定。
值得注意的是图标优先级逻辑:const icon = 'icon' in props ? propsIcon : defaultIcons[status];(index.tsx)——一旦显式传入了 icon,即使 status 为 success 等枚举值,也会以自定义图标为准;而状态色底纹类名(arco-result-icon-${status})仍会保留。测试 index.test.tsx 中的 render custom icon correctly 用例即验证了 icon 覆盖逻辑(<span>custom icon</span> 被完整渲染进 arco-result-icon-info 节点)。
样式令牌与尺寸体系
若需调整 Info 页面的视觉尺寸,可直接覆盖 token.less 中的 LESS 变量,关键项如下:
- 页面内边距:
@result-padding-top(@spacing-9)、@result-padding-top_icon(@spacing-10)、@result-padding-bottom、@result-padding-horizontal - 图标区:
@result-size-icon-wrapper: 45px(圆形底纹直径)、@result-size-icon: @size-5、@result-margin-icon-bottom: @spacing-7 - 文本区:
@result-font-title-size: @font-size-body-3、@result-font-title-weight: @font-weight-500,标题颜色--color-text-1、副标题颜色--color-text-2 - 间距:
@result-margin-extra-top: @spacing-8、@result-margin-content-top: @spacing-8
对于 403/404/500 状态,index.less 会额外将图标区域放大为 @result-size-image-width: @size-23 的正方形,以适配整幅 SVG 插画。
组件接入方式与全局配置
Result 由 components/index.tsx 统一导出(export { default as Result } from './Result'),使用时直接:
import { Result } from '@arco-design/web-react';
若需要批量配置所有 Result 的默认属性(例如统一默认标题文案),可利用 ConfigProvider 的 componentConfig 能力:组件内部通过 useMergeProps(components/_util/hooks/useMergeProps.ts)将"用户传入 props → 组件默认值 → ConfigProvider 全局配置"三级合并,用户显式传入的属性优先级最高。同时,Result 也支持 ConfigContext 提供的 rtl 属性,开启后会追加 arco-result-rtl 类名以适配 RTL 布局。
小结
Result 的 Info 状态是搭建"操作完成"反馈页的最快路径:不传 status 即可获得默认的蓝色信息图标 + 标题 + 操作按钮的完整布局。将其与 status 枚举体系、status={null} 自定义图标、subTitle/children 补充说明以及 extra 按钮组配合使用,即可覆盖从轻量提示到 403/404/500 整页反馈的全部场景。相关代码与测试均可在仓库 components/Result 目录下查阅。