UNPKG

4.42 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4var _typeof = require("@babel/runtime/helpers/typeof");
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9var _context = require("@rc-component/context");
10var React = _interopRequireWildcard(require("react"));
11var _TableContext = _interopRequireWildcard(require("../context/TableContext"));
12var _useRenderTimes = _interopRequireDefault(require("../hooks/useRenderTimes"));
13var _HeaderRow = _interopRequireDefault(require("./HeaderRow"));
14function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
15function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
16function parseHeaderRows(rootColumns) {
17 var rows = [];
18 function fillRowCells(columns, colIndex) {
19 var rowIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
20 // Init rows
21 rows[rowIndex] = rows[rowIndex] || [];
22 var currentColIndex = colIndex;
23 var colSpans = columns.filter(Boolean).map(function (column) {
24 var cell = {
25 key: column.key,
26 className: column.className || '',
27 children: column.title,
28 column: column,
29 colStart: currentColIndex
30 };
31 var colSpan = 1;
32 var subColumns = column.children;
33 if (subColumns && subColumns.length > 0) {
34 colSpan = fillRowCells(subColumns, currentColIndex, rowIndex + 1).reduce(function (total, count) {
35 return total + count;
36 }, 0);
37 cell.hasSubColumns = true;
38 }
39 if ('colSpan' in column) {
40 colSpan = column.colSpan;
41 }
42 if ('rowSpan' in column) {
43 cell.rowSpan = column.rowSpan;
44 }
45 cell.colSpan = colSpan;
46 cell.colEnd = cell.colStart + colSpan - 1;
47 rows[rowIndex].push(cell);
48 currentColIndex += colSpan;
49 return colSpan;
50 });
51 return colSpans;
52 }
53
54 // Generate `rows` cell data
55 fillRowCells(rootColumns, 0);
56
57 // Handle `rowSpan`
58 var rowCount = rows.length;
59 var _loop = function _loop(rowIndex) {
60 rows[rowIndex].forEach(function (cell) {
61 if (!('rowSpan' in cell) && !cell.hasSubColumns) {
62 // eslint-disable-next-line no-param-reassign
63 cell.rowSpan = rowCount - rowIndex;
64 }
65 });
66 };
67 for (var rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
68 _loop(rowIndex);
69 }
70 return rows;
71}
72var Header = function Header(props) {
73 if (process.env.NODE_ENV !== 'production') {
74 (0, _useRenderTimes.default)(props);
75 }
76 var stickyOffsets = props.stickyOffsets,
77 columns = props.columns,
78 flattenColumns = props.flattenColumns,
79 onHeaderRow = props.onHeaderRow;
80 var _useContext = (0, _context.useContext)(_TableContext.default, ['prefixCls', 'getComponent']),
81 prefixCls = _useContext.prefixCls,
82 getComponent = _useContext.getComponent;
83 var rows = React.useMemo(function () {
84 return parseHeaderRows(columns);
85 }, [columns]);
86 var WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
87 var trComponent = getComponent(['header', 'row'], 'tr');
88 var thComponent = getComponent(['header', 'cell'], 'th');
89 return /*#__PURE__*/React.createElement(WrapperComponent, {
90 className: "".concat(prefixCls, "-thead")
91 }, rows.map(function (row, rowIndex) {
92 var rowNode = /*#__PURE__*/React.createElement(_HeaderRow.default, {
93 key: rowIndex,
94 flattenColumns: flattenColumns,
95 cells: row,
96 stickyOffsets: stickyOffsets,
97 rowComponent: trComponent,
98 cellComponent: thComponent,
99 onHeaderRow: onHeaderRow,
100 index: rowIndex
101 });
102 return rowNode;
103 }));
104};
105var _default = exports.default = (0, _TableContext.responseImmutable)(Header);
\No newline at end of file