import { DownOutlined, UpOutlined } from '@ant-design/icons'; import { DragSortTable, ProTable } from '@ant-design/pro-components'; import type { ColumnsState, ProColumns, ProTableProps } from '@ant-design/pro-table'; import { Divider, Space, Typography } from 'antd'; import Link from 'antd/lib/typography/Link'; import { isBoolean, isFunction, isObjectLike } from 'lodash'; import type { ReactNode } from 'react'; import { memo, useEffect, useMemo, useRef, useState } from 'react'; import { useIntl } from 'umi'; import CustomLink from '../CustomLink'; import ExpandIcon from '../ExpandIcon'; import FeatureTour from './FeatureTour'; import styles from './index.less'; import { handleResize } from './ResizableTableUtil'; import ResizableTitle from './ResizableTitle'; export interface CustomTableProps extends ProTableProps> { loading?: boolean; showTour?: boolean; draggerable?: boolean; onDragSortEnd?: (beforeIndex: number, afterIndex: number, newDataSource: any) => void; tableAlert?: (data: { selectedRowKeys: any; selectedRows: any; onCleanSelected: any }) => any[]; selectHidden?: boolean; overflow?: boolean; } export const CustomTable = >(props: CustomTableProps) => { const { formatMessage } = useIntl(); const { showTour = false, search = {}, columns: parentColumns, expandable = {}, draggerable = false, onDragSortEnd = () => {}, pagination, overflow, loading, tableAlert, rowSelection, ...restProps } = props; const [columns, setColumns] = useState[]>([]); const [columnsStateMap, setColumnsStateMap] = useState>(); useEffect(() => { const list = parentColumns?.map((o, index) => { const { render, width, ellipsis, onHeaderCell, ...col } = o; const ellipsisRender = ( dom: ReactNode, ...restParams: [T, number, any, Record] ) => { const tooltip = (() => { if (isObjectLike(ellipsis)) { const t = (ellipsis as any).tooltip; return isFunction(t) ? t(dom, ...restParams) : t; } return undefined; })(); return ( !!s) .join(' '), }, }} > {dom} ); }; return { ...col, width, ellipsis: ellipsis ? { showTitle: false } : ellipsis, render: ellipsis ? render || ellipsisRender : render, onHeaderCell: (selfColumn: any) => ({ ...onHeaderCell?.(selfColumn), width: selfColumn.width, onResize: handleResize(index, list, setColumns), }), }; }) ?? []; const map = list.reduce((pre, cur) => { const { optionShow, fixed, order, disable, dataIndex } = cur as any; pre[String(dataIndex)] = { fixed, order, disable, show: typeof optionShow === 'boolean' ? optionShow : true, }; return pre; }, {}); setColumns(list); setColumnsStateMap(map); }, [parentColumns]); const collapseRef = useRef(null); const config = useMemo>>( () => ({ rowKey: 'id', size: 'small', components: { header: { cell: ResizableTitle }, }, columnsState: { value: columnsStateMap, onChange: setColumnsStateMap, }, options: { density: false }, search: isBoolean(search) ? search : { labelWidth: 'auto', span: 6, defaultCollapsed: true, collapseRender: (collapsed, showCollapseButton) => showCollapseButton ? (
{collapsed ? formatMessage({ id: 'base.common.expand' }) : formatMessage({ id: 'base.common.collapse' })}
{collapsed ? : }
) : null, ...search, }, scroll: { x: '1400' }, expandable: { expandIcon: ExpandIcon, indentSize: 15, showExpandColumn: true, defaultExpandAllRows: true, ...expandable, }, tableAlertRender: (o) => tableAlert ? ( {formatMessage({ id: 'component.table.batchOperation', defaultMessage: '批量操作', })} : }> {tableAlert(o).map((item, i) => ( ))} ) : ( false ), tableAlertOptionRender: ({ selectedRowKeys, onCleanSelected }) => tableAlert ? ( {formatMessage( { id: 'pages.searchTable.chosen', defaultMessage: '已选择 {count} 项' }, { count: selectedRowKeys?.length ?? 0 }, )} {formatMessage({ id: 'component.table.cancel', defaultMessage: '取消选择' })} ) : ( false ), pagination: isBoolean(pagination) ? pagination : { ...pagination, showSizeChanger: true, }, rowSelection, loading, }), [ columnsStateMap, expandable, formatMessage, loading, pagination, rowSelection, search, tableAlert, ], ); return ( <> {draggerable ? ( ) : ( )} ); }; export default memo(CustomTable);