界面:表格长文本列统一左对齐,保留名称与属性对齐
This commit is contained in:
@@ -1,10 +1,31 @@
|
||||
import { Table as AntdTable } from 'antd';
|
||||
import type { ColumnsType, ColumnType, TableProps } from 'antd/es/table';
|
||||
import { createContext, isValidElement, useContext, useState } from 'react';
|
||||
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>;
|
||||
@@ -40,6 +61,7 @@ export function QualityTable<T extends object>({
|
||||
...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;
|
||||
@@ -66,7 +88,7 @@ export function QualityTable<T extends object>({
|
||||
{...props}
|
||||
className={['quality-table', className].filter(Boolean).join(' ')}
|
||||
tableLayout={props.tableLayout ?? (compact ? 'fixed' : undefined)}
|
||||
columns={[...(showRowNumbers ? [indexColumn] : []), ...(compact ? compactColumns(columns ?? []) : columns ?? [])]}
|
||||
columns={[...(showRowNumbers ? [indexColumn] : []), ...(compact ? compactColumns(alignedColumns) : alignedColumns)]}
|
||||
dataSource={dataSource}
|
||||
pagination={pagination === false ? false : { defaultPageSize: 15, pageSizeOptions: ['15', '30', '50', '100'], ...pageConfig }}
|
||||
onChange={handleChange}
|
||||
|
||||
Reference in New Issue
Block a user