组件Semantic
MetricRow
@mox-hub/ui 组件 · Semantic · canonical
metric-row@mox-hub/uiSemanticcanonicalsrc/components/metric-row.tsx变体演示metric-row
存储62%
请求38%
错误率20%
Props API
| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
| label* | ReactNode | — | — |
| value* | number | — | — |
| max* | number | 100 | — |
| tone* | MetricRowTone | "default" | — |
| trailing* | ReactNode | — | — |
| icon* | ReactNode | — | — |
源码
import type * as React from "react"
import { Progress } from "./progress"
import { cn } from "../lib/utils"
type MetricRowTone = "default" | "red" | "yellow" | "green" | "blue" | "purple" | "orange"
/**
* tone → Progress indicator 色,沿用仓内既有手法(project-team-settings-panel /
* settings models-tab 同款任意变体选择器),不新增着色机制;default 不覆盖,
* 保留 ui/progress 基线 bg-primary。
*/
const TONE_INDICATOR_CLASS: Record<MetricRowTone, string> = {
default: "",
red: "[&_[data-slot=progress-indicator]]:bg-accent-red",
yellow: "[&_[data-slot=progress-indicator]]:bg-accent-yellow",
green: "[&_[data-slot=progress-indicator]]:bg-accent-green",
blue: "[&_[data-slot=progress-indicator]]:bg-accent-blue",
purple: "[&_[data-slot=progress-indicator]]:bg-accent-purple",
orange: "[&_[data-slot=progress-indicator]]:bg-accent-orange",
}
/**
* MetricRow —— 「label + 进度条 + 数值」单行指标(G 类 semantic 批二)
*
* analytics(ProfileHealthRow / 成本占比 share 列)、dashboard-page 面板、
* team-stats-section 项目进度列等 8+ 处「dot/icon + label + Progress + value」行的
* 唯一实现。形态取现用最常见写法:flex items-center gap-2 行,Progress flex-1,
* 数值 `w-8 shrink-0 text-end text-xs tabular-nums text-muted-foreground`,
* 百分号由本件统一补(现用处均为 `{n}%` 手拼)。
*
* **props 面封闭(裁决 G8,同 chip.tsx)**:不接 `className`、不透传 variant、
* 不 extends HTMLAttributes。与立项 props 面的一处偏差:`label` 落为可选——抽象
* 来源中表格进度列(team-stats 进度列 / analytics 成本占比 share 列)行内无 label
* (label 在表格首列),必填会迫使调用方造假 label;以现用形态为准(同 StatTile
* icon 类型偏差的裁决口径),label 缺省时行内只留 Progress + 数值。
*
* value/max 换算:显示与 Progress 值均为 `round(value/max*100)`(clamp 0-100),
* max 默认 100(value 即百分数);原始值/自定义文案用 `trailing` 槽位补充。
*/
function MetricRow({
label,
value,
max = 100,
tone = "default",
trailing,
icon,
}: {
/** 行首标签(text-xs text-muted-foreground truncate;表格列内可缺省) */
label?: React.ReactNode
/** 指标值(按 value/max 换算为百分数,max 默认 100) */
value: number
/** 换算分母,默认 100 */
max?: number
/** 进度条语义色档,default 保留 ui/progress 基线 bg-primary */
tone?: MetricRowTone
/** 数值之后的尾部附加(如「3/12 槽位」式原文案) */
trailing?: React.ReactNode
/** 行首图标节点(如状态圆点 span,size 档由调用方给) */
icon?: React.ReactNode
}) {
const pct = max > 0 ? Math.min(100, Math.max(0, Math.round((value / max) * 100))) : 0
return (
<div data-slot="metric-row" className="flex items-center gap-2">
{icon}
{label != null && (
<span
data-slot="metric-row-label"
className="min-w-0 truncate text-xs text-muted-foreground"
>
{label}
</span>
)}
<Progress
value={pct}
aria-label={typeof label === "string" ? label : undefined}
className={cn("flex-1", TONE_INDICATOR_CLASS[tone])}
/>
<span
data-slot="metric-row-value"
className="w-8 shrink-0 text-end text-xs tabular-nums text-muted-foreground"
>
{pct}%
</span>
{trailing}
</div>
)
}
export { MetricRow }
export type { MetricRowTone }
可访问性与 RTL
- 键盘:交互件保留
:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。 - RTL:间距、对齐与方向性图标使用逻辑属性 (
ms-*/text-start/rtl:-scale-x-100);通过ThemeProvider或I18nProvider的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。 - 文案:优先用组件
labels/translate/translatorprops,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。 - 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。
用法说明
指标行,标签、数值与可选趋势;详情页概览区使用。