Table.tsx
5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { isValidElement, type ReactNode } from 'react';
import styles from './Shared.module.css';
import { Empty, type EmptyProps } from './Navigation';
export interface TableColumn<T extends Record<string, unknown>> {
title: ReactNode;
dataIndex?: keyof T;
key?: string;
sorter?: boolean;
render?: (value: unknown, record: T, index: number) => ReactNode;
}
export interface TableProps<T extends Record<string, unknown>> {
/** rowKey 固定每行身份,是列表选择、更新和测试定位的基础。 */
rowKey: keyof T | ((record: T) => string);
columns: Array<TableColumn<T>>;
dataSource: T[];
loading?: boolean;
/** empty 是表格空数据时的统一插槽;新代码优先使用它承载说明、操作或完整自定义节点。 */
empty?: ReactNode | EmptyProps;
/** emptyText 保留兼容早期纯文本空状态,后续业务建议迁移到 empty。 */
emptyText?: ReactNode;
rowSelection?: {
selectedRowKeys: string[];
onChange?: (keys: string[], rows: T[]) => void;
};
onSort?: (key: string, order: 'ascend' | 'descend') => void;
}
function getKey<T extends Record<string, unknown>>(rowKey: TableProps<T>['rowKey'], record: T) {
return typeof rowKey === 'function' ? rowKey(record) : String(record[rowKey]);
}
function isEmptyConfig(empty: TableProps<Record<string, unknown>>['empty']): empty is EmptyProps {
return Boolean(empty && typeof empty === 'object' && !Array.isArray(empty) && !isValidElement(empty));
}
function renderEmptyState(empty: TableProps<Record<string, unknown>>['empty'], emptyText: ReactNode) {
if (isEmptyConfig(empty)) {
return <Empty {...empty} />;
}
return empty ?? <Empty description={emptyText} />;
}
function renderLoadingRows(columnCount: number) {
return Array.from({ length: 3 }, (_, rowIndex) => (
<tr key={rowIndex} aria-label="加载占位行">
{Array.from({ length: columnCount }, (_, columnIndex) => (
<td key={columnIndex}>
<span className={styles.tableSkeletonLine} />
</td>
))}
</tr>
));
}
export function Table<T extends Record<string, unknown>>({ columns, dataSource, empty, emptyText = '暂无数据', loading, rowKey, rowSelection, onSort }: TableProps<T>) {
const columnCount = columns.length + (rowSelection ? 1 : 0);
if (loading) {
return (
<div role="status" aria-label="表格加载中" className={styles.tableLoading}>
<table className={styles.table}>
<thead>
<tr>
{rowSelection ? <th>选择</th> : null}
{columns.map((column) => {
const key = String(column.key ?? column.dataIndex ?? column.title);
return <th key={key} scope="col">{column.title}</th>;
})}
</tr>
</thead>
<tbody>{renderLoadingRows(columnCount)}</tbody>
</table>
</div>
);
}
return (
<table className={styles.table}>
<thead>
<tr>
{rowSelection ? <th>选择</th> : null}
{columns.map((column) => {
const key = String(column.key ?? column.dataIndex ?? column.title);
return (
<th key={key} scope="col">
{column.sorter ? (
<button className={styles.sortButton} type="button" aria-label={`按${column.title}排序`} onClick={() => onSort?.(key, 'ascend')}>
{column.title}
</button>
) : (
column.title
)}
</th>
);
})}
</tr>
</thead>
<tbody>
{dataSource.length === 0 ? (
<tr>
<td className={styles.tableEmptyCell} colSpan={columnCount}>
<div role="status" aria-label="表格空状态" className={styles.tableEmpty}>
{renderEmptyState(empty, emptyText)}
</div>
</td>
</tr>
) : null}
{dataSource.map((record, rowIndex) => {
const key = getKey(rowKey, record);
const selected = rowSelection?.selectedRowKeys.includes(key) ?? false;
return (
<tr key={key}>
{rowSelection ? (
<td>
<input
aria-label={`选择${String(record.name ?? key)}`}
checked={selected}
type="checkbox"
onChange={() => {
const nextKeys = selected ? rowSelection.selectedRowKeys.filter((item) => item !== key) : [...rowSelection.selectedRowKeys, key];
const nextRows = dataSource.filter((item) => nextKeys.includes(getKey(rowKey, item)));
rowSelection.onChange?.(nextKeys, nextRows);
}}
/>
</td>
) : null}
{columns.map((column, columnIndex) => {
const columnKey = String(column.key ?? column.dataIndex ?? columnIndex);
const value = column.dataIndex ? record[column.dataIndex] : undefined;
return <td key={columnKey}>{column.render ? column.render(value, record, rowIndex) : String(value ?? '-')}</td>;
})}
</tr>
);
})}
</tbody>
</table>
);
}