UNPKG

3.18 kBJavaScriptView Raw
1import { useContext } from '@rc-component/context';
2import * as React from 'react';
3import TableContext, { responseImmutable } from "../context/TableContext";
4import devRenderTimes from "../hooks/useRenderTimes";
5import HeaderRow from "./HeaderRow";
6function parseHeaderRows(rootColumns) {
7 var rows = [];
8 function fillRowCells(columns, colIndex) {
9 var rowIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
10 // Init rows
11 rows[rowIndex] = rows[rowIndex] || [];
12 var currentColIndex = colIndex;
13 var colSpans = columns.filter(Boolean).map(function (column) {
14 var cell = {
15 key: column.key,
16 className: column.className || '',
17 children: column.title,
18 column: column,
19 colStart: currentColIndex
20 };
21 var colSpan = 1;
22 var subColumns = column.children;
23 if (subColumns && subColumns.length > 0) {
24 colSpan = fillRowCells(subColumns, currentColIndex, rowIndex + 1).reduce(function (total, count) {
25 return total + count;
26 }, 0);
27 cell.hasSubColumns = true;
28 }
29 if ('colSpan' in column) {
30 colSpan = column.colSpan;
31 }
32 if ('rowSpan' in column) {
33 cell.rowSpan = column.rowSpan;
34 }
35 cell.colSpan = colSpan;
36 cell.colEnd = cell.colStart + colSpan - 1;
37 rows[rowIndex].push(cell);
38 currentColIndex += colSpan;
39 return colSpan;
40 });
41 return colSpans;
42 }
43
44 // Generate `rows` cell data
45 fillRowCells(rootColumns, 0);
46
47 // Handle `rowSpan`
48 var rowCount = rows.length;
49 var _loop = function _loop(rowIndex) {
50 rows[rowIndex].forEach(function (cell) {
51 if (!('rowSpan' in cell) && !cell.hasSubColumns) {
52 // eslint-disable-next-line no-param-reassign
53 cell.rowSpan = rowCount - rowIndex;
54 }
55 });
56 };
57 for (var rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
58 _loop(rowIndex);
59 }
60 return rows;
61}
62var Header = function Header(props) {
63 if (process.env.NODE_ENV !== 'production') {
64 devRenderTimes(props);
65 }
66 var stickyOffsets = props.stickyOffsets,
67 columns = props.columns,
68 flattenColumns = props.flattenColumns,
69 onHeaderRow = props.onHeaderRow;
70 var _useContext = useContext(TableContext, ['prefixCls', 'getComponent']),
71 prefixCls = _useContext.prefixCls,
72 getComponent = _useContext.getComponent;
73 var rows = React.useMemo(function () {
74 return parseHeaderRows(columns);
75 }, [columns]);
76 var WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
77 var trComponent = getComponent(['header', 'row'], 'tr');
78 var thComponent = getComponent(['header', 'cell'], 'th');
79 return /*#__PURE__*/React.createElement(WrapperComponent, {
80 className: "".concat(prefixCls, "-thead")
81 }, rows.map(function (row, rowIndex) {
82 var rowNode = /*#__PURE__*/React.createElement(HeaderRow, {
83 key: rowIndex,
84 flattenColumns: flattenColumns,
85 cells: row,
86 stickyOffsets: stickyOffsets,
87 rowComponent: trComponent,
88 cellComponent: thComponent,
89 onHeaderRow: onHeaderRow,
90 index: rowIndex
91 });
92 return rowNode;
93 }));
94};
95export default responseImmutable(Header);
\No newline at end of file