108 lines
4.7 KiB
JavaScript
108 lines
4.7 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = void 0;
|
|
var _context = require("@rc-component/context");
|
|
var React = _interopRequireWildcard(require("react"));
|
|
var _TableContext = _interopRequireWildcard(require("../context/TableContext"));
|
|
var _useRenderTimes = _interopRequireDefault(require("../hooks/useRenderTimes"));
|
|
var _HeaderRow = _interopRequireDefault(require("./HeaderRow"));
|
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
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(props) {
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
(0, _useRenderTimes.default)(props);
|
|
}
|
|
var stickyOffsets = props.stickyOffsets,
|
|
columns = props.columns,
|
|
flattenColumns = props.flattenColumns,
|
|
onHeaderRow = props.onHeaderRow;
|
|
var _useContext = (0, _context.useContext)(_TableContext.default, ['prefixCls', 'getComponent']),
|
|
prefixCls = _useContext.prefixCls,
|
|
getComponent = _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');
|
|
var tdComponent = getComponent(['header', 'cell'], 'td');
|
|
return /*#__PURE__*/React.createElement(WrapperComponent, {
|
|
className: "".concat(prefixCls, "-thead")
|
|
}, rows.map(function (row, rowIndex) {
|
|
var rowNode = /*#__PURE__*/React.createElement(_HeaderRow.default, {
|
|
key: rowIndex,
|
|
flattenColumns: flattenColumns,
|
|
cells: row,
|
|
stickyOffsets: stickyOffsets,
|
|
rowComponent: trComponent,
|
|
cellComponent: thComponent,
|
|
tdCellComponent: tdComponent,
|
|
onHeaderRow: onHeaderRow,
|
|
index: rowIndex
|
|
});
|
|
return rowNode;
|
|
}));
|
|
}
|
|
var _default = (0, _TableContext.responseImmutable)(Header);
|
|
exports.default = _default; |