Header.js
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _HeaderRow = _interopRequireDefault(require("./HeaderRow"));
var _TableContext = _interopRequireDefault(require("../context/TableContext"));
function parseHeaderRows(rootColumns) {
var rows = [];
function fillRowCells(columns, colIndex) {
var rowIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
// Init rows
rows[rowIndex] = rows[rowIndex] || [];
var currentColIndex = colIndex;
var colSpans = columns.filter(Boolean).map(function (column) {
var cell = {
key: column.key,
className: column.className || '',
children: column.title,
column: column,
colStart: currentColIndex
};
var colSpan = 1;
var subColumns = column.children;
if (subColumns && subColumns.length > 0) {
colSpan = fillRowCells(subColumns, currentColIndex, rowIndex + 1).reduce(function (total, count) {
return total + count;
}, 0);
cell.hasSubColumns = true;
}
if ('colSpan' in column) {
colSpan = column.colSpan;
}
if ('rowSpan' in column) {
cell.rowSpan = column.rowSpan;
}
cell.colSpan = colSpan;
cell.colEnd = cell.colStart + colSpan - 1;
rows[rowIndex].push(cell);
currentColIndex += colSpan;
return colSpan;
});
return colSpans;
} // Generate `rows` cell data
fillRowCells(rootColumns, 0); // Handle `rowSpan`
var rowCount = rows.length;
var _loop = function _loop(rowIndex) {
rows[rowIndex].forEach(function (cell) {
if (!('rowSpan' in cell) && !cell.hasSubColumns) {
// eslint-disable-next-line no-param-reassign
cell.rowSpan = rowCount - rowIndex;
}
});
};
for (var rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
_loop(rowIndex);
}
return rows;
}
function Header(_ref) {
var stickyOffsets = _ref.stickyOffsets,
columns = _ref.columns,
flattenColumns = _ref.flattenColumns,
onHeaderRow = _ref.onHeaderRow;
var _React$useContext = React.useContext(_TableContext.default),
prefixCls = _React$useContext.prefixCls,
getComponent = _React$useContext.getComponent;
var rows = React.useMemo(function () {
return parseHeaderRows(columns);
}, [columns]);
var WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
var trComponent = getComponent(['header', 'row'], 'tr');
var thComponent = getComponent(['header', 'cell'], 'th');
return React.createElement(WrapperComponent, {
className: "".concat(prefixCls, "-thead")
}, rows.map(function (row, rowIndex) {
var rowNode = React.createElement(_HeaderRow.default, {
key: rowIndex,
flattenColumns: flattenColumns,
cells: row,
stickyOffsets: stickyOffsets,
rowComponent: trComponent,
cellComponent: thComponent,
onHeaderRow: onHeaderRow,
index: rowIndex
});
return rowNode;
}));
}
var _default = Header;
exports.default = _default;