import React from "react"; import { Table } from "antd"; import type { TablePaginationConfig, TableProps } from "antd"; import "./ListTable.css"; import i18n from "../../../i18n"; export type ListTableProps> = { columns: TableProps["columns"]; 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; y?: number | string }; onRowClick?: (record: T) => void; selectedRow?: T | null; loading?: boolean; className?: string; onChange?: TableProps["onChange"]; }; 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 = "", onChange, }: ListTableProps) { const rowSelection: TableProps["rowSelection"] = onSelectionChange ? { selectedRowKeys, onChange: (newSelectedRowKeys: React.Key[]) => { onSelectionChange?.(newSelectedRowKeys); }, getCheckboxProps: () => ({ disabled: isAllPagesSelected, }), } : undefined; const mergedPagination = pagination === false ? false : { ...pagination, className: ["app-global-pagination", pagination.className].filter(Boolean).join(" "), showTotal: (total: number) => (
{isAllPagesSelected ? ( <> 已选择 {totalCount || total} {onClearSelection && ( 清除选择 )} ) : selectedRowKeys.length > 0 ? ( <> 已选择 {selectedRowKeys.length} {onSelectAllPages && selectedRowKeys.length < (totalCount || total) && ( 选择全部 {totalCount || total} 项 )} {onClearSelection && ( 清除 )} ) : ( {i18n.t("common.total", { total: totalCount || total })} )}
), }; return (
({ onClick: () => onRowClick?.(record), className: selectedRow?.[rowKey] === record[rowKey] ? "row-selected" : "", })} /> ); } export default ListTable;