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: { showSearch: false }, showQuickJumper: true, }, scroll = { x: 1200 }, onRowClick, selectedRow, loading = false, className = "", onChange, }: ListTableProps) { const mergedScroll = React.useMemo(() => { return { x: 1200, ...scroll, }; }, [scroll]); const hasVerticalScroll = mergedScroll.y !== undefined; const rowSelection: TableProps["rowSelection"] = onSelectionChange ? { selectedRowKeys, onChange: (newSelectedRowKeys: React.Key[]) => { onSelectionChange?.(newSelectedRowKeys); }, getCheckboxProps: () => ({ disabled: isAllPagesSelected, }), } : undefined; const mergedPagination = pagination === false ? false : (() => { const mergedShowSizeChanger = pagination.showSizeChanger === undefined || pagination.showSizeChanger === true ? { showSearch: false } : pagination.showSizeChanger; return { ...pagination, className: ["app-global-pagination", pagination.className].filter(Boolean).join(" "), showSizeChanger: mergedShowSizeChanger, 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 })} )}
), }; })(); const wrapperStyle = hasVerticalScroll ? ({ ["--list-table-scroll-y" as string]: typeof mergedScroll.y === "number" ? `${mergedScroll.y}px` : mergedScroll.y, } as React.CSSProperties) : undefined; return (
({ onClick: () => onRowClick?.(record), className: selectedRow?.[rowKey] === record[rowKey] ? "row-selected" : "", })} /> ); } export default ListTable;