1
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
import * as React from 'react';
|
||||
import type { ColumnsType, ColumnType, Direction, FixedType, GetRowKey, Key, RenderExpandIcon, TriggerEventHandler } from '../../interface';
|
||||
export declare function convertChildrenToColumns<RecordType>(children: React.ReactNode): ColumnsType<RecordType>;
|
||||
/**
|
||||
* Parse `columns` & `children` into `columns`.
|
||||
*/
|
||||
declare function useColumns<RecordType>({ prefixCls, columns, children, expandable, expandedKeys, columnTitle, getRowKey, onTriggerExpand, expandIcon, rowExpandable, expandIconColumnIndex, expandedRowOffset, direction, expandRowByClick, columnWidth, fixed, scrollWidth, clientWidth, }: {
|
||||
prefixCls?: string;
|
||||
columns?: ColumnsType<RecordType>;
|
||||
children?: React.ReactNode;
|
||||
expandable: boolean;
|
||||
expandedKeys: Set<Key>;
|
||||
columnTitle?: React.ReactNode;
|
||||
getRowKey: GetRowKey<RecordType>;
|
||||
onTriggerExpand: TriggerEventHandler<RecordType>;
|
||||
expandIcon?: RenderExpandIcon<RecordType>;
|
||||
rowExpandable?: (record: RecordType) => boolean;
|
||||
expandIconColumnIndex?: number;
|
||||
direction?: Direction;
|
||||
expandRowByClick?: boolean;
|
||||
columnWidth?: number | string;
|
||||
clientWidth: number;
|
||||
fixed?: FixedType;
|
||||
scrollWidth?: number;
|
||||
expandedRowOffset?: number;
|
||||
}, transformColumns: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>): [
|
||||
columns: ColumnsType<RecordType>,
|
||||
flattenColumns: readonly ColumnType<RecordType>[],
|
||||
realScrollWidth: undefined | number
|
||||
];
|
||||
export default useColumns;
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
import toArray from "@rc-component/util/es/Children/toArray";
|
||||
import warning from "@rc-component/util/es/warning";
|
||||
import * as React from 'react';
|
||||
import { EXPAND_COLUMN } from "../../constant";
|
||||
import { INTERNAL_COL_DEFINE } from "../../utils/legacyUtil";
|
||||
import useWidthColumns from "./useWidthColumns";
|
||||
export function convertChildrenToColumns(children) {
|
||||
return toArray(children).filter(node => /*#__PURE__*/React.isValidElement(node)).map(node => {
|
||||
const {
|
||||
key,
|
||||
props
|
||||
} = node;
|
||||
const {
|
||||
children: nodeChildren,
|
||||
...restProps
|
||||
} = props;
|
||||
const column = {
|
||||
key,
|
||||
...restProps
|
||||
};
|
||||
if (nodeChildren) {
|
||||
column.children = convertChildrenToColumns(nodeChildren);
|
||||
}
|
||||
return column;
|
||||
});
|
||||
}
|
||||
function filterHiddenColumns(columns) {
|
||||
return columns.filter(column => column && typeof column === 'object' && !column.hidden).map(column => {
|
||||
const subColumns = column.children;
|
||||
if (subColumns && subColumns.length > 0) {
|
||||
return {
|
||||
...column,
|
||||
children: filterHiddenColumns(subColumns)
|
||||
};
|
||||
}
|
||||
return column;
|
||||
});
|
||||
}
|
||||
function flatColumns(columns, parentKey = 'key') {
|
||||
return columns.filter(column => column && typeof column === 'object').reduce((list, column, index) => {
|
||||
const {
|
||||
fixed
|
||||
} = column;
|
||||
const parsedFixed = fixed === true || fixed === 'left' ? 'start' : fixed === 'right' ? 'end' : fixed;
|
||||
const mergedKey = `${parentKey}-${index}`;
|
||||
const subColumns = column.children;
|
||||
if (subColumns && subColumns.length > 0) {
|
||||
return [...list, ...flatColumns(subColumns, mergedKey).map(subColum => ({
|
||||
...subColum,
|
||||
fixed: subColum.fixed ?? parsedFixed
|
||||
}))];
|
||||
}
|
||||
return [...list, {
|
||||
key: mergedKey,
|
||||
...column,
|
||||
fixed: parsedFixed
|
||||
}];
|
||||
}, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse `columns` & `children` into `columns`.
|
||||
*/
|
||||
function useColumns({
|
||||
prefixCls,
|
||||
columns,
|
||||
children,
|
||||
expandable,
|
||||
expandedKeys,
|
||||
columnTitle,
|
||||
getRowKey,
|
||||
onTriggerExpand,
|
||||
expandIcon,
|
||||
rowExpandable,
|
||||
expandIconColumnIndex,
|
||||
expandedRowOffset = 0,
|
||||
direction,
|
||||
expandRowByClick,
|
||||
columnWidth,
|
||||
fixed,
|
||||
scrollWidth,
|
||||
clientWidth
|
||||
}, transformColumns) {
|
||||
const baseColumns = React.useMemo(() => {
|
||||
const newColumns = columns || convertChildrenToColumns(children) || [];
|
||||
return filterHiddenColumns(newColumns.slice());
|
||||
}, [columns, children]);
|
||||
|
||||
// ========================== Expand ==========================
|
||||
const withExpandColumns = React.useMemo(() => {
|
||||
if (expandable) {
|
||||
let cloneColumns = baseColumns.slice();
|
||||
|
||||
// >>> Warning if use `expandIconColumnIndex`
|
||||
if (process.env.NODE_ENV !== 'production' && expandIconColumnIndex >= 0) {
|
||||
warning(false, '`expandIconColumnIndex` is deprecated. Please use `Table.EXPAND_COLUMN` in `columns` instead.');
|
||||
}
|
||||
|
||||
// >>> Insert expand column if not exist
|
||||
if (!cloneColumns.includes(EXPAND_COLUMN)) {
|
||||
const expandColIndex = expandIconColumnIndex || 0;
|
||||
const insertIndex = expandColIndex === 0 && (fixed === 'right' || fixed === 'end') ? baseColumns.length : expandColIndex;
|
||||
if (insertIndex >= 0) {
|
||||
cloneColumns.splice(insertIndex, 0, EXPAND_COLUMN);
|
||||
}
|
||||
}
|
||||
|
||||
// >>> Deduplicate additional expand column
|
||||
if (process.env.NODE_ENV !== 'production' && cloneColumns.filter(c => c === EXPAND_COLUMN).length > 1) {
|
||||
warning(false, 'There exist more than one `EXPAND_COLUMN` in `columns`.');
|
||||
}
|
||||
const expandColumnIndex = cloneColumns.indexOf(EXPAND_COLUMN);
|
||||
cloneColumns = cloneColumns.filter((column, index) => column !== EXPAND_COLUMN || index === expandColumnIndex);
|
||||
|
||||
// >>> Check if expand column need to fixed
|
||||
const prevColumn = baseColumns[expandColumnIndex];
|
||||
let fixedColumn;
|
||||
if (fixed) {
|
||||
fixedColumn = fixed;
|
||||
} else {
|
||||
fixedColumn = prevColumn ? prevColumn.fixed : null;
|
||||
}
|
||||
|
||||
// >>> Create expandable column
|
||||
const expandColumn = {
|
||||
[INTERNAL_COL_DEFINE]: {
|
||||
className: `${prefixCls}-expand-icon-col`,
|
||||
columnType: 'EXPAND_COLUMN'
|
||||
},
|
||||
title: columnTitle,
|
||||
fixed: fixedColumn,
|
||||
className: `${prefixCls}-row-expand-icon-cell`,
|
||||
width: columnWidth,
|
||||
render: (_, record, index) => {
|
||||
const rowKey = getRowKey(record, index);
|
||||
const expanded = expandedKeys.has(rowKey);
|
||||
const recordExpandable = rowExpandable ? rowExpandable(record) : true;
|
||||
const icon = expandIcon({
|
||||
prefixCls,
|
||||
expanded,
|
||||
expandable: recordExpandable,
|
||||
record,
|
||||
onExpand: onTriggerExpand
|
||||
});
|
||||
if (expandRowByClick) {
|
||||
return /*#__PURE__*/React.createElement("span", {
|
||||
onClick: e => e.stopPropagation()
|
||||
}, icon);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
};
|
||||
return cloneColumns.map((col, index) => {
|
||||
const column = col === EXPAND_COLUMN ? expandColumn : col;
|
||||
if (index < expandedRowOffset) {
|
||||
return {
|
||||
...column,
|
||||
fixed: column.fixed || 'start'
|
||||
};
|
||||
}
|
||||
return column;
|
||||
});
|
||||
}
|
||||
if (process.env.NODE_ENV !== 'production' && baseColumns.includes(EXPAND_COLUMN)) {
|
||||
warning(false, '`expandable` is not config but there exist `EXPAND_COLUMN` in `columns`.');
|
||||
}
|
||||
return baseColumns.filter(col => col !== EXPAND_COLUMN);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [expandable, baseColumns, getRowKey, expandedKeys, expandIcon, direction, expandedRowOffset]);
|
||||
|
||||
// ========================= Transform ========================
|
||||
const mergedColumns = React.useMemo(() => {
|
||||
let finalColumns = withExpandColumns;
|
||||
if (transformColumns) {
|
||||
finalColumns = transformColumns(finalColumns);
|
||||
}
|
||||
|
||||
// Always provides at least one column for table display
|
||||
if (!finalColumns.length) {
|
||||
finalColumns = [{
|
||||
render: () => null
|
||||
}];
|
||||
}
|
||||
return finalColumns;
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [transformColumns, withExpandColumns, direction]);
|
||||
|
||||
// ========================== Flatten =========================
|
||||
const flattenColumns = React.useMemo(() => flatColumns(mergedColumns),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[mergedColumns, direction, scrollWidth]);
|
||||
|
||||
// ========================= FillWidth ========================
|
||||
const [filledColumns, realScrollWidth] = useWidthColumns(flattenColumns, scrollWidth, clientWidth);
|
||||
return [mergedColumns, filledColumns, realScrollWidth];
|
||||
}
|
||||
export default useColumns;
|
||||
Generated
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
import type { ColumnsType } from '../../interface';
|
||||
/**
|
||||
* Fill all column with width
|
||||
*/
|
||||
export default function useWidthColumns(flattenColumns: ColumnsType<any>, scrollWidth: number, clientWidth: number): [columns: ColumnsType<any>, realScrollWidth: number];
|
||||
Generated
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
import * as React from 'react';
|
||||
function parseColWidth(totalWidth, width = '') {
|
||||
if (typeof width === 'number') {
|
||||
return width;
|
||||
}
|
||||
if (width.endsWith('%')) {
|
||||
return totalWidth * parseFloat(width) / 100;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill all column with width
|
||||
*/
|
||||
export default function useWidthColumns(flattenColumns, scrollWidth, clientWidth) {
|
||||
return React.useMemo(() => {
|
||||
// Fill width if needed
|
||||
if (scrollWidth && scrollWidth > 0) {
|
||||
let totalWidth = 0;
|
||||
let missWidthCount = 0;
|
||||
|
||||
// collect not given width column
|
||||
flattenColumns.forEach(col => {
|
||||
const colWidth = parseColWidth(scrollWidth, col.width);
|
||||
if (colWidth) {
|
||||
totalWidth += colWidth;
|
||||
} else {
|
||||
missWidthCount += 1;
|
||||
}
|
||||
});
|
||||
|
||||
// Fill width
|
||||
const maxFitWidth = Math.max(scrollWidth, clientWidth);
|
||||
let restWidth = Math.max(maxFitWidth - totalWidth, missWidthCount);
|
||||
let restCount = missWidthCount;
|
||||
const avgWidth = restWidth / missWidthCount;
|
||||
let realTotal = 0;
|
||||
const filledColumns = flattenColumns.map(col => {
|
||||
const clone = {
|
||||
...col
|
||||
};
|
||||
const colWidth = parseColWidth(scrollWidth, clone.width);
|
||||
if (colWidth) {
|
||||
clone.width = colWidth;
|
||||
} else {
|
||||
const colAvgWidth = Math.floor(avgWidth);
|
||||
clone.width = restCount === 1 ? restWidth : colAvgWidth;
|
||||
restWidth -= colAvgWidth;
|
||||
restCount -= 1;
|
||||
}
|
||||
realTotal += clone.width;
|
||||
return clone;
|
||||
});
|
||||
|
||||
// If realTotal is less than clientWidth,
|
||||
// We need extend column width
|
||||
if (realTotal < maxFitWidth) {
|
||||
const scale = maxFitWidth / realTotal;
|
||||
restWidth = maxFitWidth;
|
||||
filledColumns.forEach((col, index) => {
|
||||
const colWidth = Math.floor(col.width * scale);
|
||||
col.width = index === filledColumns.length - 1 ? restWidth : colWidth;
|
||||
restWidth -= colWidth;
|
||||
});
|
||||
}
|
||||
return [filledColumns, Math.max(realTotal, maxFitWidth)];
|
||||
}
|
||||
return [flattenColumns, scrollWidth];
|
||||
}, [flattenColumns, scrollWidth, clientWidth]);
|
||||
}
|
||||
Reference in New Issue
Block a user