Skip to content

React 无障碍 ARIA ​

表格自动输出 ARIA 语义:role=grid + aria-rowcount/colcount,表头 role=columnheader + aria-sort,行 role=row + aria-rowindex(真实数据索引),单元格 role=gridcell,加载态 aria-busy,便于屏幕阅读器识别。

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
tsx
import React from 'react';
import { VirtTableReact, type ReactTableColumn } from '@virt-table/react';
import { faker } from '@faker-js/faker';

interface Row extends Record<string, unknown> {
  id: number;
  name: string;
  age: number;
  city: string;
}

const columns: ReactTableColumn<Row>[] = [
  { key: 'id', title: 'ID', width: 80 },
  { key: 'name', title: '姓名', width: 200, sortable: true },
  { key: 'age', title: '年龄', width: 140, sortable: true },
  { key: 'city', title: '城市', width: 200 },
];

const list: Row[] = Array.from({ length: 500 }, (_, i) => ({
  id: i + 1,
  name: faker.person.fullName(),
  age: faker.number.int({ min: 18, max: 60 }),
  city: faker.location.city(),
}));

export default function AriaTable() {
  const containerRef = React.useRef<HTMLDivElement>(null);
  const [out, setOut] = React.useState('');

  return (
    <div className="demo-wrapper">
      <h3 className="demo-title">React 无障碍 ARIA</h3>
      <div className="demo-hint">
        表格自动输出 ARIA 语义,便于屏幕阅读器识别。点击「检查」查看实际属性。
      </div>
      <div className="virt-table-controls">
        <button
          type="button"
          className="virt-table-btn virt-table-btn-primary"
          onClick={() => {
            const container = containerRef.current;
            if (!container) return;
            const grid = container.querySelector('[role="grid"]');
            const th = container.querySelector('[role="columnheader"]');
            const cell = container.querySelector('[role="gridcell"]');
            setOut(
              `grid: aria-rowcount=${grid?.getAttribute('aria-rowcount')}, ` +
                `aria-colcount=${grid?.getAttribute('aria-colcount')} · ` +
                `columnheader aria-sort=${th?.getAttribute('aria-sort')} · ` +
                `gridcell role=${cell?.getAttribute('role')}`,
            );
          }}
        >
          检查 ARIA 属性
        </button>
        <span className="demo-note">{out}</span>
      </div>
      <div ref={containerRef} style={{ width: 680, height: 420 }} className="demo-container">
        <VirtTableReact
          columns={columns}
          options={{ list, itemKey: 'id', estimatedSize: 40, buffer: 6, border: true }}
        />
      </div>
    </div>
  );
}

相关 API ​

字段 / 方法作用详见
自动挂上的 role / aria-*根元素 / 表头 / 行 / 单元格 / 加载态表格配置 · 无障碍
aria-rowindex真实数据索引(不是 DOM 下标)—— 虚拟滚动下这两者不一致表格配置 · 无障碍
vtKeyboardNav键盘导航(读屏用户靠它遍历单元格)插件机制

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