# 分页数据表 DataTable

从 `@ancrel/workspace` 导入 `DataTable`。用 `rowKey` 返回跨页唯一的字符串，例如项目编码。每列提供稳定的 `id`、表头和读取该类型数据的 `cell` 函数。调用方负责筛选、排序，并把当前页的数据传入 `rows`。

```tsx
'use client'

import { useState } from 'react'
import { Button } from '@ancrel/ui'
import { DataTable } from '@ancrel/workspace'
import '@ancrel/tokens/css'
import '@ancrel/ui/styles.css'
import '@ancrel/workspace/styles.css'

const projects = [{ code: 'A', name: 'Atlas' }, { code: 'B', name: 'Beacon' }, { code: 'C', name: 'Cedar' }]

export default function App() {
  const [page, setPage] = useState({ pageIndex: 0, pageSize: 2 })
  const [keys, setKeys] = useState<readonly string[]>([])
  const [pinned, setPinned] = useState<readonly string[]>([])
  const start = page.pageIndex * page.pageSize
  return <DataTable label="Projects" rows={projects.slice(start, start + page.pageSize)}
    rowKey={row => row.code} selectedKeys={keys} onSelectionChange={setKeys}
    pagination={page} totalRows={projects.length} onPaginationChange={setPage}
    columns={[
      { id: 'name', header: 'Project', cell: row => row.name },
      { id: 'pinned', header: <>Pinned <Button size="sm" disabled={!keys.length}
          onClick={() => { setPinned([...new Set([...pinned, ...keys])]); setKeys([]) }}>
          Pin selected ({keys.length})
        </Button></>, cell: row => pinned.includes(row.code) ? 'Yes' : 'No' },
    ]} />
}
```

Expected:

Select A, go to the next page, and select C. Pin selected (2) updates both pages. Return to the first page to see Atlas pinned and Beacon unpinned.

## 分页与批量操作

`pageIndex` 从零开始，显示的页码从一开始。传入正整数 `pageSize` 和总记录数 `totalRows` 可以显示总页数。总数未知时，当前页记录数小于页长就会禁用下一页。翻页按钮通过 `onPaginationChange` 传回新页码与原页长。

`selectedKeys` 保存完整的选择集合。表头复选框仅修改当前页的键，部分选中时呈现混合状态。可以像示例一样在列的 `header` 中放批量操作按钮。若筛选后需要清空选择，由父组件在变更条件时处理。

## 加载与空表

传入空数组时，`emptyState` 可以显示自定义空状态，或在正在取数时显示 UI 包的 `LoadingState`。默认空表显示 No rows。等待异步单元格组件时，表体内部的 Suspense 边界会显示加载行。

`createStaleSafeLoader(load, options)` 返回带有 `load(query)` 和 `cancel()` 的对象。复用同一实例，才能在连续查询间防抖。默认等待 260 毫秒，可通过 `debounceMs` 调整。新查询会中止旧请求。即使底层请求忽略信号，旧 Promise 也只会得到 `undefined`。最新请求失败时，调用方会收到异常。

```ts
const loader = createStaleSafeLoader(fetchPage)
async function update(query: string) {
  const page = await loader.load(query)
  if (page !== undefined) showPage(page)
}
```

组件卸载或查询上下文切换时调用 `loader.cancel()`。

| `Prop` | `Type` | `Required` | `Default` |
| - | - | - | - |
| `className` | `string` | `false` | `—` |
| `columns` | `readonly DataTableColumn<Row>[]` | `true` | `—` |
| `emptyState` | `ReactNode` | `false` | `—` |
| `label` | `string` | `true` | `—` |
| `onPaginationChange` | `((pagination: PaginationState) => void) \| undefined` | `false` | `—` |
| `onSelectionChange` | `((keys: readonly string[]) => void) \| undefined` | `false` | `—` |
| `pagination` | `PaginationState` | `false` | `—` |
| `rowKey` | `(row: Row) => string` | `true` | `—` |
| `rows` | `readonly Row[]` | `true` | `—` |
| `selectedKeys` | `readonly string[]` | `false` | `—` |
| `totalRows` | `number` | `false` | `—` |

```ts
import * as React from 'react';
import type { DataTableProps } from './types.js';
export type * from './types.js';
export { createStaleSafeLoader } from './loader.js';
export declare function DataTable<Row>(props: DataTableProps<Row>): React.ReactElement | null;
```

* default — dark/compact, dark/comfortable, light/compact, light/comfortable
