React VtActions 操作列
操作列:一格里排几个链接式按钮,可带二次确认气泡。
示例
微应用尚未挂载。
源码
点击查看源码
tsx
import React from 'react';
import { VirtTableReact, type ReactTableColumn, type VirtTableRef } from '@virt-table/react';
import { VtActions } from '@virt-table/vanilla/components';
interface Row extends Record<string, unknown> {
id: number;
name: string;
enabled: boolean;
draft: boolean;
}
const makeList = (): Row[] =>
Array.from({ length: 5000 }, (_, i) => ({
id: i,
name: `条目 ${i}`,
enabled: i % 2 === 0,
draft: i % 3 === 0,
}));
export default function VtActionsTable() {
const tableRef = React.useRef<VirtTableRef>(null);
const [lastAction, setLastAction] = React.useState('(未操作)');
const [rowCount, setRowCount] = React.useState(5000);
// 行数据是命令式改的(组件直接写回行对象),所以放 ref 而不是 state
const listRef = React.useRef<Row[]>([]);
if (listRef.current.length === 0) listRef.current = makeList();
// 列在建表时被捕获一次:放进 useMemo,回调里只碰 ref 与 setState
const columns = React.useMemo<ReactTableColumn<Row>[]>(
() => [
{ key: 'id', title: 'ID', width: 80, align: 'center' },
{ key: 'name', title: '名称', width: 200 },
{ key: 'state', title: '状态', width: 120, render: ({ row }) => (row.enabled ? '启用' : '停用') },
VtActions<Row>({
key: 'ops',
title: '操作',
width: 240,
fixed: 'right',
align: 'center',
items: [
// 只有草稿能编辑 —— disabled 写成函数,跟着行状态走
{
text: '编辑',
disabled: (row) => !row.draft,
onClick: (row) => setLastAction(`编辑 #${row.id}`),
},
// 文案随行变化;停用/启用是同一个按钮的两面
{
text: (row) => (row.enabled ? '停用' : '启用'),
onClick: (row) => {
row.enabled = !row.enabled;
setLastAction(`${row.enabled ? '启用' : '停用'} #${row.id}`);
tableRef.current?.forceUpdate();
},
},
// 危险动作:语义色 + 二次确认
{
text: '删除',
variant: 'danger',
confirm: '删除后不可恢复,确定吗?',
onClick: (row) => {
listRef.current = listRef.current.filter((r) => r.id !== row.id);
tableRef.current?.setList(listRef.current);
setRowCount(listRef.current.length);
setLastAction(`删除 #${row.id}`);
},
},
],
}),
],
[],
);
return (
<div className="demo-wrapper">
<h3 className="demo-title">React VtActions 操作列</h3>
<div className="status-text">
VtActions:text / disabled / hidden 都可写成 (row) => …;「删除」带二次确认气泡。剩余{' '}
{rowCount} 行 · 最近操作:{lastAction}
</div>
<div style={{ width: 800, height: 600 }} className="demo-container">
<VirtTableReact
ref={tableRef}
columns={columns}
options={{
list: listRef.current,
itemKey: 'id',
estimatedSize: 40,
buffer: 4,
border: true,
}}
/>
</div>
</div>
);
}说明
「直接可点」类组件:交互发生在常态里,不提供 renderEditor,也不需要 vtCellEditor 插件。它不读也不写单元格值,因此没有 placeholder / clearable / onChange。
text / disabled / hidden 都能写成 (row, rowIndex) => …:操作列的可用性几乎总是跟着行状态走(草稿才能编辑、已停用的不能再停用),做成常量就得让使用方回退到手写 render。
确认气泡不接表格的 i18n
单元格组件是纯工厂函数,拿不到 VirtTable 实例,也就读不到 options.locale。默认文案是中文(与默认语言包 zhCN 一致),要别的语言就在 confirm 对象里传 okText / cancelText。
相关 API
| 字段 | 作用 |
|---|---|
items | 按钮列表(必填):text / onClick / variant / disabled / hidden / confirm |
col.fixed | 操作列一般配 right |
全量选项表见 单元格组件;公共选项 (
placeholder/disabled/clearable/onChange)、asCell()、三类交互模型见 同页上方;列的通用字段见 列配置。上面的字段在 React 端原样透传(实例方法通过组件
ref调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 JSX、ref转发了哪些方法 —— 见 React API。