import { Table } from "antd"; import type { ColumnsType, TablePaginationConfig, TableRowSelection } from "antd/es/table"; import "./ListTable.css"; export type ListTableProps> = { columns: ColumnsType; dataSource: T[]; rowKey?: string; selectedRowKeys?: React.Key[]; onSelectionChange?: (keys: React.Key[]) => void; isAllPagesSelected?: boolean; totalCount?: number; onSelectAllPages?: () => void; onClearSelection?: () => void; pagination?: TablePaginationConfig | false; scroll?: { x?: number | true | string }; onRowClick?: (record: T) => void; selectedRow?: T | null; loading?: boolean; className?: string; }; function ListTable>({ columns, dataSource, rowKey = "id", selectedRowKeys = [], onSelectionChange, isAllPagesSelected = false, totalCount, onSelectAllPages, onClearSelection, pagination = { pageSize: 10, showSizeChanger: true, showQuickJumper: true, }, scroll = { x: 1200 }, onRowClick, selectedRow, loading = false, className = "", }: ListTableProps) { const rowSelection: TableRowSelection | undefined = onSelectionChange ? { selectedRowKeys, onChange: (newSelectedRowKeys) => { onSelectionChange?.(newSelectedRowKeys); }, getCheckboxProps: () => ({ disabled: isAllPagesSelected, }), } : undefined; const mergedPagination = pagination === false ? false : { ...pagination, showTotal: (total: number) => (
{isAllPagesSelected ? ( <> 已选择 {totalCount || total} {onClearSelection && ( 清除选择 )} ) : selectedRowKeys.length > 0 ? ( <> 已选择 {selectedRowKeys.length} {onSelectAllPages && selectedRowKeys.length < (totalCount || total) && ( 选择全部 {totalCount || total} 项 )} {onClearSelection && ( 清除 )} ) : ( 已选择 0 项 )}
), }; return (
({ onClick: () => onRowClick?.(record), className: selectedRow?.[rowKey] === record[rowKey] ? "row-selected" : "", })} /> ); } export default ListTable;