UNPKG

5.3 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;
9exports.getColumnWidth = getColumnWidth;
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
12var _context = require("@rc-component/context");
13var _classnames = _interopRequireDefault(require("classnames"));
14var React = _interopRequireWildcard(require("react"));
15var _BodyRow = require("../Body/BodyRow");
16var _Cell = _interopRequireDefault(require("../Cell"));
17var _context2 = require("./context");
18function _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); }
19function _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; }
20/**
21 * Return the width of the column by `colSpan`.
22 * When `colSpan` is `0` will be trade as `1`.
23 */
24function getColumnWidth(colIndex, colSpan, columnsOffset) {
25 var mergedColSpan = colSpan || 1;
26 return columnsOffset[colIndex + mergedColSpan] - (columnsOffset[colIndex] || 0);
27}
28function VirtualCell(props) {
29 var rowInfo = props.rowInfo,
30 column = props.column,
31 colIndex = props.colIndex,
32 indent = props.indent,
33 index = props.index,
34 component = props.component,
35 renderIndex = props.renderIndex,
36 record = props.record,
37 style = props.style,
38 className = props.className,
39 inverse = props.inverse,
40 getHeight = props.getHeight;
41 var render = column.render,
42 dataIndex = column.dataIndex,
43 columnClassName = column.className,
44 colWidth = column.width;
45 var _useContext = (0, _context.useContext)(_context2.GridContext, ['columnsOffset']),
46 columnsOffset = _useContext.columnsOffset;
47 var _getCellProps = (0, _BodyRow.getCellProps)(rowInfo, column, colIndex, indent, index),
48 key = _getCellProps.key,
49 fixedInfo = _getCellProps.fixedInfo,
50 appendCellNode = _getCellProps.appendCellNode,
51 additionalCellProps = _getCellProps.additionalCellProps;
52 var cellStyle = additionalCellProps.style,
53 _additionalCellProps$ = additionalCellProps.colSpan,
54 colSpan = _additionalCellProps$ === void 0 ? 1 : _additionalCellProps$,
55 _additionalCellProps$2 = additionalCellProps.rowSpan,
56 rowSpan = _additionalCellProps$2 === void 0 ? 1 : _additionalCellProps$2;
57
58 // ========================= ColWidth =========================
59 // column width
60 var startColIndex = colIndex - 1;
61 var concatColWidth = getColumnWidth(startColIndex, colSpan, columnsOffset);
62
63 // margin offset
64 var marginOffset = colSpan > 1 ? colWidth - concatColWidth : 0;
65
66 // ========================== Style ===========================
67 var mergedStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, cellStyle), style), {}, {
68 flex: "0 0 ".concat(concatColWidth, "px"),
69 width: "".concat(concatColWidth, "px"),
70 marginRight: marginOffset,
71 pointerEvents: 'auto'
72 });
73
74 // When `colSpan` or `rowSpan` is `0`, should skip render.
75 var needHide = React.useMemo(function () {
76 if (inverse) {
77 return rowSpan <= 1;
78 } else {
79 return colSpan === 0 || rowSpan === 0 || rowSpan > 1;
80 }
81 }, [rowSpan, colSpan, inverse]);
82
83 // 0 rowSpan or colSpan should not render
84 if (needHide) {
85 mergedStyle.visibility = 'hidden';
86 } else if (inverse) {
87 mergedStyle.height = getHeight === null || getHeight === void 0 ? void 0 : getHeight(rowSpan);
88 }
89 var mergedRender = needHide ? function () {
90 return null;
91 } : render;
92
93 // ========================== Render ==========================
94 var cellSpan = {};
95
96 // Virtual should reset `colSpan` & `rowSpan`
97 if (rowSpan === 0 || colSpan === 0) {
98 cellSpan.rowSpan = 1;
99 cellSpan.colSpan = 1;
100 }
101 return /*#__PURE__*/React.createElement(_Cell.default, (0, _extends2.default)({
102 className: (0, _classnames.default)(columnClassName, className),
103 ellipsis: column.ellipsis,
104 align: column.align,
105 scope: column.rowScope,
106 component: component,
107 prefixCls: rowInfo.prefixCls,
108 key: key,
109 record: record,
110 index: index,
111 renderIndex: renderIndex,
112 dataIndex: dataIndex,
113 render: mergedRender,
114 shouldCellUpdate: column.shouldCellUpdate
115 }, fixedInfo, {
116 appendNode: appendCellNode,
117 additionalProps: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, additionalCellProps), {}, {
118 style: mergedStyle
119 }, cellSpan)
120 }));
121}
122var _default = exports.default = VirtualCell;
\No newline at end of file