index.tsx
4.26 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
import type { ReactNode } from 'react';
import type { ColumnsType, TableProps } from 'antd/es/table';
import { UButton, UTable } from '@uj/ui';
import { PermButton } from '@/access';
import type { ActionSchema } from '../formSchema';
/** 列表行操作配置;配置 permission 时自动使用权限按钮。 */
export interface RowActionSchema<T> extends Omit<ActionSchema, 'onClick'> {
onClick: (record: T) => void;
visible?: (record: T) => boolean;
}
export interface DataTableProps<T> extends Pick<
TableProps<T>,
'dataSource' | 'loading' | 'rowKey' | 'rowSelection'
> {
columns: ColumnsType<T>;
showIndex?: boolean;
pagination?:
| {
current: number;
pageSize: number;
total: number;
onChange: (page: number, pageSize: number) => void;
}
| false;
actions?: ActionSchema[];
/** 行内操作。配置 permission 时自动按权限显示,未配置则使用普通按钮。 */
rowActions?: RowActionSchema<T>[];
/** 行内操作列标题,默认“操作”。 */
actionColumnTitle?: ReactNode;
/** 行内操作列宽度。 */
actionColumnWidth?: number;
/** 接口返回的分页对象;传入后自动从 recordsKey 读取列表数据。 */
pageData?: Record<string, unknown>;
/** 分页对象中的列表字段名,默认 records。 */
recordsKey?: string;
/** 分页对象中的总数、页码和每页条数字段名。 */
totalKey?: string;
currentKey?: string;
sizeKey?: string;
}
export function DataTable<T extends object>({
columns,
showIndex,
pagination,
actions,
rowActions,
actionColumnTitle = '操作',
actionColumnWidth,
pageData,
recordsKey = 'records',
totalKey = 'total',
currentKey = 'current',
sizeKey = 'size',
...props
}: DataTableProps<T>) {
const records = pageData?.[recordsKey];
const dataSource = Array.isArray(records) ? (records as T[]) : props.dataSource;
const resolvedPagination = pagination
? {
...pagination,
current: Number(pageData?.[currentKey] ?? pagination.current),
pageSize: Number(pageData?.[sizeKey] ?? pagination.pageSize),
total: Number(pageData?.[totalKey] ?? pagination.total),
}
: pagination;
const page = resolvedPagination || undefined;
const renderActionButton = (
action: Omit<ActionSchema, 'onClick'>,
onClick: () => void,
key: string,
isRowAction = false,
) => {
const buttonProps = {
key,
danger: action.danger,
disabled: action.disabled,
onClick,
...(isRowAction ? { type: 'link' as const } : {}),
};
return action.permission ? (
<PermButton permission={action.permission} {...buttonProps}>
{action.label}
</PermButton>
) : (
<UButton {...buttonProps}>{action.label}</UButton>
);
};
const actionColumn: ColumnsType<T>[number] | undefined = rowActions?.length
? {
title: actionColumnTitle,
key: '__actions',
width: actionColumnWidth,
render: (_: unknown, record: T) =>
rowActions
.filter((action) => action.visible?.(record) ?? true)
.map((action, index) =>
renderActionButton(action, () => action.onClick(record), `row-action-${index}`, true),
),
}
: undefined;
const tableColumns = showIndex
? [
{
title: '序号',
key: '__index',
width: 72,
render: (_: unknown, _row: T, index: number) =>
((page?.current ?? 1) - 1) * (page?.pageSize ?? 0) + index + 1,
},
...columns,
...(actionColumn ? [actionColumn] : []),
]
: [...columns, ...(actionColumn ? [actionColumn] : [])];
return (
<>
<div>
{actions?.map((action, index) =>
renderActionButton(action, action.onClick, `toolbar-action-${index}`),
)}
</div>
<UTable
{...props}
dataSource={dataSource}
columns={tableColumns}
pagination={
resolvedPagination === false
? false
: resolvedPagination && {
current: resolvedPagination.current,
pageSize: resolvedPagination.pageSize,
total: resolvedPagination.total,
onChange: resolvedPagination.onChange,
}
}
/>
</>
);
}