import { Table as AntdTable } from 'antd'; import type { ColumnsType, ColumnType, TableProps } from 'antd/es/table'; import { createContext, isValidElement, useContext, useState, type ReactNode } from 'react'; import { CompactTableCell } from './CompactTableCell'; import { tableTextAlignment } from './tableTextAlignment'; export const CompactManagementTables = createContext(false); function titleText(value: ReactNode): string { if (typeof value === 'string' || typeof value === 'number') return String(value); if (Array.isArray(value)) return value.map(titleText).join(''); if (isValidElement<{ children?: ReactNode }>(value)) return titleText(value.props.children); return ''; } export function alignTableColumns(columns: ColumnsType, rows: readonly T[]): ColumnsType { return columns.map(column => { if ('children' in column) return { ...column, children: alignTableColumns(column.children, rows) }; const path = column.dataIndex === undefined ? [] : Array.isArray(column.dataIndex) ? column.dataIndex : [column.dataIndex]; const values = path.length ? rows.map(row => path.reduce((value, key) => value && typeof value === 'object' ? (value as Record)[String(key)] : undefined, row)) : []; const label = typeof column.title === 'function' ? '' : titleText(column.title); const alignment = tableTextAlignment(label || String(column.key ?? path.join('.')), values); return alignment === 'left' ? { ...column, align: 'left', className: [column.className, 'quality-table-text-column'].filter(Boolean).join(' '), } : { ...column, align: column.align ?? 'center' }; }); } function compactColumns(columns: ColumnsType): ColumnsType { return columns.map(column => { const title = typeof column.title === 'function' ? column.title : {column.title}; if ('children' in column) return { ...column, title, children: compactColumns(column.children) }; return { ...column, title, ellipsis: { showTitle: false }, render: (value, record, index) => { const content = column.render ? column.render(value, record, index) : value; if (content && typeof content === 'object' && !isValidElement(content) && !Array.isArray(content) && 'children' in content) { return { ...content, children: {content.children} }; } return {content}; }, }; }); } type QualityTableProps = TableProps & { /** Offset for tables whose page is controlled outside Ant Design. */ indexOffset?: number; showRowNumbers?: boolean; }; export function QualityTable({ columns, dataSource, pagination, indexOffset = 0, showRowNumbers = true, className, size = 'small', onChange, ...props }: QualityTableProps) { const compact = useContext(CompactManagementTables); const alignedColumns = alignTableColumns(columns ?? [], dataSource ?? []); const pageConfig = typeof pagination === 'object' && pagination !== null ? pagination : undefined; const [innerPage, setInnerPage] = useState(() => ({ current: pageConfig?.defaultCurrent ?? 1, pageSize: pageConfig?.defaultPageSize ?? 15 })); const pageSize = pageConfig?.pageSize ?? innerPage.pageSize; const total = pageConfig?.total ?? dataSource?.length ?? 0; const page = Math.min(pageConfig?.current ?? innerPage.current, Math.max(1, Math.ceil(total / pageSize))); const pageOffset = indexOffset + (pagination === false ? 0 : Math.max(0, page - 1) * pageSize); const handleChange: TableProps['onChange'] = (next, filters, sorter, extra) => { setInnerPage({ current: next.current ?? 1, pageSize: next.pageSize ?? pageSize }); onChange?.(next, filters, sorter, extra); }; const indexColumn: ColumnType = { title: '序号', key: '__quality-row-number__', width: 68, align: 'center', fixed: 'left', className: 'quality-table-index-column', render: (_value, _record, index) => pageOffset + index + 1, }; return (
{...props} className={['quality-table', className].filter(Boolean).join(' ')} tableLayout={props.tableLayout ?? (compact ? 'fixed' : undefined)} columns={[...(showRowNumbers ? [indexColumn] : []), ...(compact ? compactColumns(alignedColumns) : alignedColumns)]} dataSource={dataSource} pagination={pagination === false ? false : { defaultPageSize: 15, pageSizeOptions: ['15', '30', '50', '100'], ...pageConfig }} onChange={handleChange} size={size} bordered />
); }