1
This commit is contained in:
+187
@@ -0,0 +1,187 @@
|
||||
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||||
import { clsx } from 'clsx';
|
||||
import * as React from 'react';
|
||||
import Cell from "../Cell";
|
||||
import { responseImmutable } from "../context/TableContext";
|
||||
import devRenderTimes from "../hooks/useRenderTimes";
|
||||
import useRowInfo from "../hooks/useRowInfo";
|
||||
import ExpandedRow from "./ExpandedRow";
|
||||
import { computedExpandedClassName } from "../utils/expandUtil";
|
||||
// ==================================================================================
|
||||
// == getCellProps ==
|
||||
// ==================================================================================
|
||||
export function getCellProps(rowInfo, column, colIndex, indent, index, rowKeys = [], expandedRowOffset = 0) {
|
||||
const {
|
||||
record,
|
||||
prefixCls,
|
||||
columnsKey,
|
||||
fixedInfoList,
|
||||
expandIconColumnIndex,
|
||||
nestExpandable,
|
||||
indentSize,
|
||||
expandIcon,
|
||||
expanded,
|
||||
hasNestChildren,
|
||||
onTriggerExpand,
|
||||
expandable,
|
||||
expandedKeys
|
||||
} = rowInfo;
|
||||
const key = columnsKey[colIndex];
|
||||
const fixedInfo = fixedInfoList[colIndex];
|
||||
|
||||
// ============= Used for nest expandable =============
|
||||
let appendCellNode;
|
||||
if (colIndex === (expandIconColumnIndex || 0) && nestExpandable) {
|
||||
appendCellNode = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
|
||||
style: {
|
||||
paddingLeft: `${indentSize * indent}px`
|
||||
},
|
||||
className: `${prefixCls}-row-indent indent-level-${indent}`
|
||||
}), expandIcon({
|
||||
prefixCls,
|
||||
expanded,
|
||||
expandable: hasNestChildren,
|
||||
record,
|
||||
onExpand: onTriggerExpand
|
||||
}));
|
||||
}
|
||||
const additionalCellProps = column.onCell?.(record, index) || {};
|
||||
|
||||
// Expandable row has offset
|
||||
if (expandedRowOffset) {
|
||||
const {
|
||||
rowSpan = 1
|
||||
} = additionalCellProps;
|
||||
|
||||
// For expandable row with rowSpan,
|
||||
// We should increase the rowSpan if the row is expanded
|
||||
if (expandable && rowSpan && colIndex < expandedRowOffset) {
|
||||
let currentRowSpan = rowSpan;
|
||||
for (let i = index; i < index + rowSpan; i += 1) {
|
||||
const rowKey = rowKeys[i];
|
||||
if (expandedKeys.has(rowKey)) {
|
||||
currentRowSpan += 1;
|
||||
}
|
||||
}
|
||||
additionalCellProps.rowSpan = currentRowSpan;
|
||||
}
|
||||
}
|
||||
return {
|
||||
key,
|
||||
fixedInfo,
|
||||
appendCellNode,
|
||||
additionalCellProps: additionalCellProps
|
||||
};
|
||||
}
|
||||
|
||||
// ==================================================================================
|
||||
// == getCellProps ==
|
||||
// ==================================================================================
|
||||
const BodyRow = props => {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
devRenderTimes(props);
|
||||
}
|
||||
const {
|
||||
className,
|
||||
style,
|
||||
classNames,
|
||||
styles,
|
||||
record,
|
||||
index,
|
||||
renderIndex,
|
||||
rowKey,
|
||||
rowKeys,
|
||||
indent = 0,
|
||||
rowComponent: RowComponent,
|
||||
cellComponent,
|
||||
scopeCellComponent,
|
||||
expandedRowInfo
|
||||
} = props;
|
||||
const rowInfo = useRowInfo(record, rowKey, index, indent);
|
||||
const {
|
||||
prefixCls,
|
||||
flattenColumns,
|
||||
expandedRowClassName,
|
||||
expandedRowRender,
|
||||
rowProps,
|
||||
// Misc
|
||||
expanded,
|
||||
rowSupportExpand
|
||||
} = rowInfo;
|
||||
|
||||
// Force render expand row if expanded before
|
||||
const expandedRef = React.useRef(false);
|
||||
expandedRef.current ||= expanded;
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
devRenderTimes(props);
|
||||
}
|
||||
|
||||
// 若没有 expandedRowRender 参数, 将使用 baseRowNode 渲染 Children
|
||||
// 此时如果 level > 1 则说明是 expandedRow, 一样需要附加 computedExpandedRowClassName
|
||||
const expandedClsName = computedExpandedClassName(expandedRowClassName, record, index, indent);
|
||||
|
||||
// ======================== Base tr row ========================
|
||||
const baseRowNode = /*#__PURE__*/React.createElement(RowComponent, _extends({}, rowProps, {
|
||||
"data-row-key": rowKey,
|
||||
className: clsx(className, `${prefixCls}-row`, `${prefixCls}-row-level-${indent}`, rowProps?.className, classNames.row, {
|
||||
[expandedClsName]: indent >= 1
|
||||
}),
|
||||
style: {
|
||||
...style,
|
||||
...rowProps?.style,
|
||||
...styles.row
|
||||
}
|
||||
}), flattenColumns.map((column, colIndex) => {
|
||||
const {
|
||||
render,
|
||||
dataIndex,
|
||||
className: columnClassName
|
||||
} = column;
|
||||
const {
|
||||
key,
|
||||
fixedInfo,
|
||||
appendCellNode,
|
||||
additionalCellProps
|
||||
} = getCellProps(rowInfo, column, colIndex, indent, index, rowKeys, expandedRowInfo?.offset);
|
||||
return /*#__PURE__*/React.createElement(Cell, _extends({
|
||||
className: clsx(columnClassName, classNames.cell),
|
||||
style: styles.cell,
|
||||
ellipsis: column.ellipsis,
|
||||
align: column.align,
|
||||
scope: column.rowScope,
|
||||
component: column.rowScope ? scopeCellComponent : cellComponent,
|
||||
prefixCls: prefixCls,
|
||||
key: key,
|
||||
record: record,
|
||||
index: index,
|
||||
renderIndex: renderIndex,
|
||||
dataIndex: dataIndex,
|
||||
render: render,
|
||||
shouldCellUpdate: column.shouldCellUpdate
|
||||
}, fixedInfo, {
|
||||
appendNode: appendCellNode,
|
||||
additionalProps: additionalCellProps
|
||||
}));
|
||||
}));
|
||||
|
||||
// ======================== Expand Row =========================
|
||||
let expandRowNode;
|
||||
if (rowSupportExpand && (expandedRef.current || expanded)) {
|
||||
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
|
||||
expandRowNode = /*#__PURE__*/React.createElement(ExpandedRow, {
|
||||
expanded: expanded,
|
||||
className: clsx(`${prefixCls}-expanded-row`, `${prefixCls}-expanded-row-level-${indent + 1}`, expandedClsName),
|
||||
prefixCls: prefixCls,
|
||||
component: RowComponent,
|
||||
cellComponent: cellComponent,
|
||||
colSpan: expandedRowInfo ? expandedRowInfo.colSpan : flattenColumns.length,
|
||||
isEmpty: false,
|
||||
stickyOffset: expandedRowInfo?.sticky
|
||||
}, expandContent);
|
||||
}
|
||||
return /*#__PURE__*/React.createElement(React.Fragment, null, baseRowNode, expandRowNode);
|
||||
};
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
BodyRow.displayName = 'BodyRow';
|
||||
}
|
||||
export default responseImmutable(BodyRow);
|
||||
Reference in New Issue
Block a user