index.js
5.73 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import * as React from 'react';
import classNames from 'classnames';
import { supportRef } from "rc-util/es/ref";
import { getPathValue } from '../utils/valueUtil';
function isRenderCell(data) {
return data && _typeof(data) === 'object' && !Array.isArray(data) && !React.isValidElement(data);
}
function isRefComponent(component) {
// String tag component also support ref
if (typeof component === 'string') {
return true;
}
return supportRef(component);
}
function Cell(_ref, ref) {
var _classNames;
var prefixCls = _ref.prefixCls,
className = _ref.className,
record = _ref.record,
index = _ref.index,
dataIndex = _ref.dataIndex,
render = _ref.render,
children = _ref.children,
_ref$component = _ref.component,
Component = _ref$component === void 0 ? 'td' : _ref$component,
colSpan = _ref.colSpan,
rowSpan = _ref.rowSpan,
fixLeft = _ref.fixLeft,
fixRight = _ref.fixRight,
firstFixLeft = _ref.firstFixLeft,
lastFixLeft = _ref.lastFixLeft,
firstFixRight = _ref.firstFixRight,
lastFixRight = _ref.lastFixRight,
appendNode = _ref.appendNode,
_ref$additionalProps = _ref.additionalProps,
additionalProps = _ref$additionalProps === void 0 ? {} : _ref$additionalProps,
ellipsis = _ref.ellipsis,
align = _ref.align,
rowType = _ref.rowType,
isSticky = _ref.isSticky;
var cellPrefixCls = "".concat(prefixCls, "-cell"); // ==================== Child Node ====================
var cellProps;
var childNode;
if (children) {
childNode = children;
} else {
var value = getPathValue(record, dataIndex); // Customize render node
childNode = value;
if (render) {
var renderData = render(value, record, index);
if (isRenderCell(renderData)) {
childNode = renderData.children;
cellProps = renderData.props;
} else {
childNode = renderData;
}
}
} // Not crash if final `childNode` is not validate ReactNode
if (_typeof(childNode) === 'object' && !Array.isArray(childNode) && !React.isValidElement(childNode)) {
childNode = null;
}
if (ellipsis && (lastFixLeft || firstFixRight)) {
childNode = React.createElement("span", {
className: "".concat(cellPrefixCls, "-content")
}, childNode);
}
var _ref2 = cellProps || {},
cellColSpan = _ref2.colSpan,
cellRowSpan = _ref2.rowSpan,
cellStyle = _ref2.style,
cellClassName = _ref2.className,
restCellProps = _objectWithoutProperties(_ref2, ["colSpan", "rowSpan", "style", "className"]);
var mergedColSpan = cellColSpan !== undefined ? cellColSpan : colSpan;
var mergedRowSpan = cellRowSpan !== undefined ? cellRowSpan : rowSpan;
if (mergedColSpan === 0 || mergedRowSpan === 0) {
return null;
} // ====================== Fixed =======================
var fixedStyle = {};
var isFixLeft = typeof fixLeft === 'number';
var isFixRight = typeof fixRight === 'number';
if (isFixLeft) {
fixedStyle.position = 'sticky';
fixedStyle.left = fixLeft;
}
if (isFixRight) {
fixedStyle.position = 'sticky';
fixedStyle.right = fixRight;
} // ====================== Align =======================
var alignStyle = {};
if (align) {
alignStyle.textAlign = align;
} // ====================== Render ======================
var title;
var ellipsisConfig = ellipsis === true ? {
showTitle: true
} : ellipsis;
if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === 'header')) {
if (typeof childNode === 'string' || typeof childNode === 'number') {
title = childNode.toString();
} else if (React.isValidElement(childNode) && typeof childNode.props.children === 'string') {
title = childNode.props.children;
}
}
var componentProps = _objectSpread(_objectSpread(_objectSpread({
title: title
}, restCellProps), additionalProps), {}, {
colSpan: mergedColSpan && mergedColSpan !== 1 ? mergedColSpan : null,
rowSpan: mergedRowSpan && mergedRowSpan !== 1 ? mergedRowSpan : null,
className: classNames(cellPrefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-left"), isFixLeft), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-left-first"), firstFixLeft), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-left-last"), lastFixLeft), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-right"), isFixRight), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-right-first"), firstFixRight), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-right-last"), lastFixRight), _defineProperty(_classNames, "".concat(cellPrefixCls, "-ellipsis"), ellipsis), _defineProperty(_classNames, "".concat(cellPrefixCls, "-with-append"), appendNode), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-sticky"), (isFixLeft || isFixRight) && isSticky), _classNames), additionalProps.className, cellClassName),
style: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, additionalProps.style), alignStyle), fixedStyle), cellStyle),
ref: isRefComponent(Component) ? ref : null
});
return React.createElement(Component, Object.assign({}, componentProps), appendNode, childNode);
}
var RefCell = React.forwardRef(Cell);
RefCell.displayName = 'Cell';
var MemoCell = React.memo(RefCell, function (prev, next) {
if (next.shouldCellUpdate) {
return !next.shouldCellUpdate(next.record, prev.record);
}
return false;
});
export default MemoCell;