101 lines
4.7 KiB
TypeScript
101 lines
4.7 KiB
TypeScript
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<T extends object>(columns: ColumnsType<T>, rows: readonly T[]): ColumnsType<T> {
|
|
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<unknown>((value, key) => value && typeof value === 'object' ? (value as Record<string, unknown>)[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<T extends object>(columns: ColumnsType<T>): ColumnsType<T> {
|
|
return columns.map(column => {
|
|
const title = typeof column.title === 'function' ? column.title : <CompactTableCell>{column.title}</CompactTableCell>;
|
|
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: <CompactTableCell>{content.children}</CompactTableCell> };
|
|
}
|
|
return <CompactTableCell>{content}</CompactTableCell>;
|
|
},
|
|
};
|
|
});
|
|
}
|
|
|
|
type QualityTableProps<T extends object> = TableProps<T> & {
|
|
/** Offset for tables whose page is controlled outside Ant Design. */
|
|
indexOffset?: number;
|
|
showRowNumbers?: boolean;
|
|
};
|
|
|
|
export function QualityTable<T extends object>({
|
|
columns,
|
|
dataSource,
|
|
pagination,
|
|
indexOffset = 0,
|
|
showRowNumbers = true,
|
|
className,
|
|
size = 'small',
|
|
onChange,
|
|
...props
|
|
}: QualityTableProps<T>) {
|
|
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<T>['onChange'] = (next, filters, sorter, extra) => {
|
|
setInnerPage({ current: next.current ?? 1, pageSize: next.pageSize ?? pageSize });
|
|
onChange?.(next, filters, sorter, extra);
|
|
};
|
|
const indexColumn: ColumnType<T> = {
|
|
title: '序号',
|
|
key: '__quality-row-number__',
|
|
width: 68,
|
|
align: 'center',
|
|
fixed: 'left',
|
|
className: 'quality-table-index-column',
|
|
render: (_value, _record, index) => pageOffset + index + 1,
|
|
};
|
|
|
|
return (
|
|
<div className={`quality-table-frame${compact ? ' quality-table-compact' : ''}`}>
|
|
<AntdTable<T>
|
|
{...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
|
|
/>
|
|
</div>
|
|
);
|
|
}
|