BodyRow.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import classNames from 'classnames';
  5. import * as React from 'react';
  6. import Cell from "../Cell";
  7. import { responseImmutable } from "../context/TableContext";
  8. import devRenderTimes from "../hooks/useRenderTimes";
  9. import useRowInfo from "../hooks/useRowInfo";
  10. import ExpandedRow from "./ExpandedRow";
  11. import { computedExpandedClassName } from "../utils/expandUtil";
  12. // ==================================================================================
  13. // == getCellProps ==
  14. // ==================================================================================
  15. export function getCellProps(rowInfo, column, colIndex, indent, index) {
  16. var _column$onCell;
  17. var rowKeys = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
  18. var expandedRowOffset = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 0;
  19. var record = rowInfo.record,
  20. prefixCls = rowInfo.prefixCls,
  21. columnsKey = rowInfo.columnsKey,
  22. fixedInfoList = rowInfo.fixedInfoList,
  23. expandIconColumnIndex = rowInfo.expandIconColumnIndex,
  24. nestExpandable = rowInfo.nestExpandable,
  25. indentSize = rowInfo.indentSize,
  26. expandIcon = rowInfo.expandIcon,
  27. expanded = rowInfo.expanded,
  28. hasNestChildren = rowInfo.hasNestChildren,
  29. onTriggerExpand = rowInfo.onTriggerExpand,
  30. expandable = rowInfo.expandable,
  31. expandedKeys = rowInfo.expandedKeys;
  32. var key = columnsKey[colIndex];
  33. var fixedInfo = fixedInfoList[colIndex];
  34. // ============= Used for nest expandable =============
  35. var appendCellNode;
  36. if (colIndex === (expandIconColumnIndex || 0) && nestExpandable) {
  37. appendCellNode = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
  38. style: {
  39. paddingLeft: "".concat(indentSize * indent, "px")
  40. },
  41. className: "".concat(prefixCls, "-row-indent indent-level-").concat(indent)
  42. }), expandIcon({
  43. prefixCls: prefixCls,
  44. expanded: expanded,
  45. expandable: hasNestChildren,
  46. record: record,
  47. onExpand: onTriggerExpand
  48. }));
  49. }
  50. var additionalCellProps = ((_column$onCell = column.onCell) === null || _column$onCell === void 0 ? void 0 : _column$onCell.call(column, record, index)) || {};
  51. // Expandable row has offset
  52. if (expandedRowOffset) {
  53. var _additionalCellProps$ = additionalCellProps.rowSpan,
  54. rowSpan = _additionalCellProps$ === void 0 ? 1 : _additionalCellProps$;
  55. // For expandable row with rowSpan,
  56. // We should increase the rowSpan if the row is expanded
  57. if (expandable && rowSpan && colIndex < expandedRowOffset) {
  58. var currentRowSpan = rowSpan;
  59. for (var i = index; i < index + rowSpan; i += 1) {
  60. var rowKey = rowKeys[i];
  61. if (expandedKeys.has(rowKey)) {
  62. currentRowSpan += 1;
  63. }
  64. }
  65. additionalCellProps.rowSpan = currentRowSpan;
  66. }
  67. }
  68. return {
  69. key: key,
  70. fixedInfo: fixedInfo,
  71. appendCellNode: appendCellNode,
  72. additionalCellProps: additionalCellProps
  73. };
  74. }
  75. // ==================================================================================
  76. // == getCellProps ==
  77. // ==================================================================================
  78. function BodyRow(props) {
  79. if (process.env.NODE_ENV !== 'production') {
  80. devRenderTimes(props);
  81. }
  82. var className = props.className,
  83. style = props.style,
  84. record = props.record,
  85. index = props.index,
  86. renderIndex = props.renderIndex,
  87. rowKey = props.rowKey,
  88. rowKeys = props.rowKeys,
  89. _props$indent = props.indent,
  90. indent = _props$indent === void 0 ? 0 : _props$indent,
  91. RowComponent = props.rowComponent,
  92. cellComponent = props.cellComponent,
  93. scopeCellComponent = props.scopeCellComponent,
  94. expandedRowInfo = props.expandedRowInfo;
  95. var rowInfo = useRowInfo(record, rowKey, index, indent);
  96. var prefixCls = rowInfo.prefixCls,
  97. flattenColumns = rowInfo.flattenColumns,
  98. expandedRowClassName = rowInfo.expandedRowClassName,
  99. expandedRowRender = rowInfo.expandedRowRender,
  100. rowProps = rowInfo.rowProps,
  101. expanded = rowInfo.expanded,
  102. rowSupportExpand = rowInfo.rowSupportExpand;
  103. // Force render expand row if expanded before
  104. var expandedRef = React.useRef(false);
  105. expandedRef.current || (expandedRef.current = expanded);
  106. if (process.env.NODE_ENV !== 'production') {
  107. devRenderTimes(props);
  108. }
  109. // 若没有 expandedRowRender 参数, 将使用 baseRowNode 渲染 Children
  110. // 此时如果 level > 1 则说明是 expandedRow, 一样需要附加 computedExpandedRowClassName
  111. var expandedClsName = computedExpandedClassName(expandedRowClassName, record, index, indent);
  112. // ======================== Base tr row ========================
  113. var baseRowNode = /*#__PURE__*/React.createElement(RowComponent, _extends({}, rowProps, {
  114. "data-row-key": rowKey,
  115. className: classNames(className, "".concat(prefixCls, "-row"), "".concat(prefixCls, "-row-level-").concat(indent), rowProps === null || rowProps === void 0 ? void 0 : rowProps.className, _defineProperty({}, expandedClsName, indent >= 1)),
  116. style: _objectSpread(_objectSpread({}, style), rowProps === null || rowProps === void 0 ? void 0 : rowProps.style)
  117. }), flattenColumns.map(function (column, colIndex) {
  118. var render = column.render,
  119. dataIndex = column.dataIndex,
  120. columnClassName = column.className;
  121. var _getCellProps = getCellProps(rowInfo, column, colIndex, indent, index, rowKeys, expandedRowInfo === null || expandedRowInfo === void 0 ? void 0 : expandedRowInfo.offset),
  122. key = _getCellProps.key,
  123. fixedInfo = _getCellProps.fixedInfo,
  124. appendCellNode = _getCellProps.appendCellNode,
  125. additionalCellProps = _getCellProps.additionalCellProps;
  126. return /*#__PURE__*/React.createElement(Cell, _extends({
  127. className: columnClassName,
  128. ellipsis: column.ellipsis,
  129. align: column.align,
  130. scope: column.rowScope,
  131. component: column.rowScope ? scopeCellComponent : cellComponent,
  132. prefixCls: prefixCls,
  133. key: key,
  134. record: record,
  135. index: index,
  136. renderIndex: renderIndex,
  137. dataIndex: dataIndex,
  138. render: render,
  139. shouldCellUpdate: column.shouldCellUpdate
  140. }, fixedInfo, {
  141. appendNode: appendCellNode,
  142. additionalProps: additionalCellProps
  143. }));
  144. }));
  145. // ======================== Expand Row =========================
  146. var expandRowNode;
  147. if (rowSupportExpand && (expandedRef.current || expanded)) {
  148. var expandContent = expandedRowRender(record, index, indent + 1, expanded);
  149. expandRowNode = /*#__PURE__*/React.createElement(ExpandedRow, {
  150. expanded: expanded,
  151. className: classNames("".concat(prefixCls, "-expanded-row"), "".concat(prefixCls, "-expanded-row-level-").concat(indent + 1), expandedClsName),
  152. prefixCls: prefixCls,
  153. component: RowComponent,
  154. cellComponent: cellComponent,
  155. colSpan: expandedRowInfo ? expandedRowInfo.colSpan : flattenColumns.length,
  156. stickyOffset: expandedRowInfo === null || expandedRowInfo === void 0 ? void 0 : expandedRowInfo.sticky,
  157. isEmpty: false
  158. }, expandContent);
  159. }
  160. return /*#__PURE__*/React.createElement(React.Fragment, null, baseRowNode, expandRowNode);
  161. }
  162. if (process.env.NODE_ENV !== 'production') {
  163. BodyRow.displayName = 'BodyRow';
  164. }
  165. export default responseImmutable(BodyRow);