UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

113 lines (96 loc) 3.34 kB
import * as React from 'react'; import isFunction from 'lodash/isFunction'; import get from 'lodash/get'; import ColumnGroup from '../ColumnGroup'; import isNullOrUndefined from './isNullOrUndefined'; function cloneCell(Cell, props) { return React.cloneElement(Cell, props); } function mergeCells(cells) { var nextCells = []; for (var i = 0; i < cells.length; i += 1) { var _cells$i$props = cells[i].props, width = _cells$i$props.width, colSpan = _cells$i$props.colSpan, groupCount = _cells$i$props.groupCount, groupHeader = _cells$i$props.groupHeader, isHeaderCell = _cells$i$props.isHeaderCell, headerHeight = _cells$i$props.headerHeight, verticalAlign = _cells$i$props.verticalAlign; var groupChildren = []; /** * 为列头添加分组 */ if (groupCount && isHeaderCell) { var nextWidth = width; var left = 0; for (var j = 0; j < groupCount; j += 1) { var nextCell = cells[i + j]; var _nextCell$props = nextCell.props, nextCellWidth = _nextCell$props.width, children = _nextCell$props.children; if (j !== 0) { nextWidth += nextCellWidth; left += cells[i + j - 1].props.width; cells[i + j] = cloneCell(nextCell, { removed: true }); } groupChildren.push( // @ts-ignore React.createElement("div", { key: j, style: { width: nextCellWidth, left: left } }, children)); } nextCells.push( // @ts-ignore cloneCell(cells[i], { width: nextWidth, children: React.createElement(ColumnGroup, { width: nextWidth, headerHeight: headerHeight, header: groupHeader, verticalAlign: verticalAlign }, groupChildren) })); continue; } else if (colSpan) { /** * 如果存在 colSpan 属性,就去找它的下一个 Cell, * 看看值是否是 isNullOrUndefined,,如果为空这可以合并这个单元格 */ var _nextWidth = width; for (var _j = 0; _j < colSpan; _j += 1) { var _nextCell = cells[i + _j]; if (_nextCell) { var _nextCell$props2 = _nextCell.props, rowData = _nextCell$props2.rowData, rowIndex = _nextCell$props2.rowIndex, _children = _nextCell$props2.children, colSpanWidth = _nextCell$props2.width, _isHeaderCell = _nextCell$props2.isHeaderCell, dataKey = _nextCell$props2.dataKey; var cellText = isFunction(_children) ? _children(rowData, rowIndex) : get(rowData, dataKey); if (rowData && isNullOrUndefined(cellText) || _isHeaderCell && isNullOrUndefined(_children)) { _nextWidth += colSpanWidth; cells[i + _j] = cloneCell(_nextCell, { removed: true }); } } } nextCells.push( // @ts-ignore cloneCell(cells[i], { width: _nextWidth, 'aria-colspan': colSpan && _nextWidth > width ? colSpan : undefined })); continue; } // @ts-ignore nextCells.push(cells[i]); } return nextCells; } export default mergeCells; //# sourceMappingURL=mergeCells.js.map