React VtTag 标签
标签列(纯展示):把行里的值画成彩色胶囊。不给 options 时值本身就是标签文案;给了 options 就是一份「值 → 文案 + 配色」的字典(不是候选集)。
示例
微应用尚未挂载。
源码
点击查看源码
tsx
import { VirtTableReact, type ReactTableColumn } from '@virt-table/react';
import { VtTag } from '@virt-table/vanilla/components';
const ROW_COUNT = 5000;
/** 值 → 文案 + 配色的**字典**(不是候选集:VtTag 不参与选择) */
const STATUS = [
{ value: 'run', label: '运行中', color: '#10b981' },
{ value: 'queued', label: '排队', color: '#f59e0b' },
{ value: 'done', label: '已完成', color: '#2563eb' },
{ value: 'failed', label: '失败', color: '#ef4444' },
];
/** 语义色版本:不写死色值,跟着主题走 */
const RESULT = [
{ value: 'pass', label: '通过', variant: 'success' as const },
{ value: 'warn', label: '告警', variant: 'warning' as const },
{ value: 'fail', label: '不通过', variant: 'danger' as const },
];
const LEVELS = ['青铜', '白银', '黄金', '铂金', '钻石'];
const columns: ReactTableColumn[] = [
{ key: 'id', title: 'ID', width: 70, align: 'center' },
{ key: 'name', title: '任务', width: 110 },
// ① 字典:行里存 code,显示文案 + 逐项自定义色
VtTag({ key: 'status', title: '状态(字典)', width: 150, options: STATUS }),
// ② 字典 + 语义色:颜色跟主题,不写死
VtTag({ key: 'result', title: '结果(语义色)', width: 140, options: RESULT }),
// ③ 不给 options:值本身就是标签文案
VtTag({ key: 'level', title: '等级(值即文案)', width: 150 }),
// ④ 按行动态上色:优先级最高,压过列级 variant
VtTag({
key: 'level',
title: '等级(按行上色)',
width: 150,
color: (row) => ((row as { levelIndex: number }).levelIndex >= 3 ? '#a855f7' : undefined),
}),
// ⑤ 空值:不画空胶囊,走 placeholder
VtTag({ key: 'owner', title: '负责人(可空)', width: 130, placeholder: '—' }),
];
const list = Array.from({ length: ROW_COUNT }, (_, i) => ({
id: i,
name: `任务 ${i}`,
status: STATUS[i % STATUS.length]!.value,
result: RESULT[i % RESULT.length]!.value,
level: LEVELS[i % LEVELS.length],
levelIndex: i % LEVELS.length,
owner: i % 4 === 0 ? '' : `成员 ${i % 7}`,
}));
export default function VtTagTable() {
return (
<div className="demo-wrapper">
<h3 className="demo-title">React VtTag 标签</h3>
<div className="status-text">
VtTag:纯展示,没有 renderEditor —— 它没有候选集,值本身就是内容,配色能按行算。
要「从候选集里选、选中的画成胶囊」用 VtSelect({'{'} display: "tag" {'}'}):胶囊外观
两者共用同一个工厂,长相必然一致,差别只在能不能选。
</div>
<div style={{ width: 900, height: 600 }} className="demo-container">
<VirtTableReact
columns={columns}
options={{ list, itemKey: 'id', estimatedSize: 40, buffer: 4, border: true }}
/>
</div>
</div>
);
}说明
它不参与选择,因此不提供 renderEditor,也不需要任何插件。空值不画空胶囊,走 placeholder。
配色优先级只有一处实现,所以「纯展示的标签」和「选出来的标签」必然长得一样、跟主题也只跟一次:
options[].color > options[].variant > 列级 color > 列级 variant > 默认主色要「从候选集里选」用 VtSelect —— 「长什么样」是参数,不该是组件名:
| 要做的事 | 怎么写 |
|---|---|
| 只读的标签列 | VtTag({ key, options? }) |
| 可选的标签列(单值) | VtSelect({ key, options, display: 'tag' }) |
| 可选的标签列(多值) | VtSelect({ key, options, display: 'tag', multiple: true }) |
胶囊外观两者共用同一个工厂(createTagChip),差别只在能不能选。更多写法(多值折叠、只读多值、 运行时换形态)见 VtSelect · 常见配方。
相关 API
| 字段 | 作用 |
|---|---|
options | 「值 → 文案 + 配色」字典,可不给 |
variant / color | 列级配色,可写成 (row, rowIndex) => … |
全量选项表见 单元格组件;公共选项 (
placeholder/disabled/clearable/onChange)、asCell()、三类交互模型见 同页上方;列的通用字段见 列配置。上面的字段在 React 端原样透传(实例方法通过组件
ref调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 JSX、ref转发了哪些方法 —— 见 React API。