Skip to content

React VtInput 文本输入 ​

文本输入列:常态显示纯文本,进入编辑态后在浮层里改,值实时写回行数据。

编辑态走 renderEditor,必须装 vtCellEditor 插件才会被触发。

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
tsx
import { VirtTableReact, type ReactTableColumn, vtCellEditor } from '@virt-table/react';
import { VtInput } from '@virt-table/vanilla/components';

// 单元格组件是与框架无关的列工厂:直接从 vanilla 引,返回的列可以原样交给 VirtTableReact
const ROW_COUNT = 5000;

const columns: ReactTableColumn[] = [
  { key: 'id', title: 'ID', width: 80, align: 'center' },
  VtInput({ key: 'name', title: '姓名', width: 180, placeholder: '请输入姓名' }),
  VtInput({ key: 'email', title: '邮箱', width: 240, placeholder: '请输入邮箱', clearable: true }),
  VtInput({
    key: 'remark',
    title: '备注',
    width: 260,
    placeholder: '请输入备注',
    maxLength: 50,
    clearable: true,
  }),
  VtInput({ key: 'readonly', title: '只读列', width: 180, disabled: true }),
];

const list = Array.from({ length: ROW_COUNT }, (_, i) => ({
  id: i,
  name: `用户 ${i}`,
  email: `user${i}@example.com`,
  remark: i % 3 === 0 ? `备注信息 ${i}` : '',
  readonly: `不可编辑 ${i}`,
}));

// 编辑态走 renderEditor,必须装 vtCellEditor 才会被触发
const plugins = [vtCellEditor()];

export default function VtInputTable() {
  return (
    <div className="demo-wrapper">
      <h3 className="demo-title">React VtInput 文本输入</h3>
      <div className="status-text">
        VtInput 单元格组件:{ROW_COUNT} 行(点击单元格编辑,支持 placeholder、clearable、maxLength、disabled)
      </div>
      <div style={{ width: 960, height: 600 }} className="demo-container">
        <VirtTableReact
          columns={columns}
          options={{
            list,
            itemKey: 'id',
            estimatedSize: 40,
            buffer: 4,
            border: true,
            textOverflow: 'ellipsis',
            plugins,
          }}
        />
      </div>
    </div>
  );
}

相关 API ​

字段作用
maxLength最大输入长度

全量选项表见 单元格组件;公共选项 (placeholder / disabled / clearable / onChange)、asCell()、三类交互模型见 同页上方;列的通用字段见 列配置。

编辑态需装 vtCellEditor,见 插件机制。

上面的字段在 React 端原样透传(实例方法通过组件 ref 调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 JSX、ref 转发了哪些方法 —— 见 React API。