UNPKG

7.18 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
17
18var React = _interopRequireWildcard(require("react"));
19
20var _classnames = _interopRequireDefault(require("classnames"));
21
22var _ref3 = require("rc-util/lib/ref");
23
24var _valueUtil = require("../utils/valueUtil");
25
26function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
27
28function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
30function isRenderCell(data) {
31 return data && (0, _typeof2.default)(data) === 'object' && !Array.isArray(data) && !React.isValidElement(data);
32}
33
34function isRefComponent(component) {
35 // String tag component also support ref
36 if (typeof component === 'string') {
37 return true;
38 }
39
40 return (0, _ref3.supportRef)(component);
41}
42
43function Cell(_ref, ref) {
44 var _classNames;
45
46 var prefixCls = _ref.prefixCls,
47 className = _ref.className,
48 record = _ref.record,
49 index = _ref.index,
50 dataIndex = _ref.dataIndex,
51 render = _ref.render,
52 children = _ref.children,
53 _ref$component = _ref.component,
54 Component = _ref$component === void 0 ? 'td' : _ref$component,
55 colSpan = _ref.colSpan,
56 rowSpan = _ref.rowSpan,
57 fixLeft = _ref.fixLeft,
58 fixRight = _ref.fixRight,
59 firstFixLeft = _ref.firstFixLeft,
60 lastFixLeft = _ref.lastFixLeft,
61 firstFixRight = _ref.firstFixRight,
62 lastFixRight = _ref.lastFixRight,
63 appendNode = _ref.appendNode,
64 _ref$additionalProps = _ref.additionalProps,
65 additionalProps = _ref$additionalProps === void 0 ? {} : _ref$additionalProps,
66 ellipsis = _ref.ellipsis,
67 align = _ref.align,
68 rowType = _ref.rowType;
69 var cellPrefixCls = "".concat(prefixCls, "-cell"); // ==================== Child Node ====================
70
71 var cellProps;
72 var childNode;
73
74 if (children) {
75 childNode = children;
76 } else {
77 var value = (0, _valueUtil.getPathValue)(record, dataIndex); // Customize render node
78
79 childNode = value;
80
81 if (render) {
82 var renderData = render(value, record, index);
83
84 if (isRenderCell(renderData)) {
85 childNode = renderData.children;
86 cellProps = renderData.props;
87 } else {
88 childNode = renderData;
89 }
90 }
91 } // Not crash if final `childNode` is not validate ReactNode
92
93
94 if ((0, _typeof2.default)(childNode) === 'object' && !Array.isArray(childNode) && !React.isValidElement(childNode)) {
95 childNode = null;
96 }
97
98 if (ellipsis && (lastFixLeft || firstFixRight)) {
99 childNode = React.createElement("span", {
100 className: "".concat(cellPrefixCls, "-content")
101 }, childNode);
102 }
103
104 var _ref2 = cellProps || {},
105 cellColSpan = _ref2.colSpan,
106 cellRowSpan = _ref2.rowSpan,
107 cellStyle = _ref2.style,
108 cellClassName = _ref2.className,
109 restCellProps = (0, _objectWithoutProperties2.default)(_ref2, ["colSpan", "rowSpan", "style", "className"]);
110
111 var mergedColSpan = cellColSpan !== undefined ? cellColSpan : colSpan;
112 var mergedRowSpan = cellRowSpan !== undefined ? cellRowSpan : rowSpan;
113
114 if (mergedColSpan === 0 || mergedRowSpan === 0) {
115 return null;
116 } // ====================== Fixed =======================
117
118
119 var fixedStyle = {};
120 var isFixLeft = typeof fixLeft === 'number';
121 var isFixRight = typeof fixRight === 'number';
122
123 if (isFixLeft) {
124 fixedStyle.position = 'sticky';
125 fixedStyle.left = fixLeft;
126 }
127
128 if (isFixRight) {
129 fixedStyle.position = 'sticky';
130 fixedStyle.right = fixRight;
131 } // ====================== Align =======================
132
133
134 var alignStyle = {};
135
136 if (align) {
137 alignStyle.textAlign = align;
138 } // ====================== Render ======================
139
140
141 var title;
142 var ellipsisConfig = ellipsis === true ? {
143 showTitle: true
144 } : ellipsis;
145
146 if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === 'header')) {
147 if (typeof childNode === 'string' || typeof childNode === 'number') {
148 title = childNode.toString();
149 } else if (React.isValidElement(childNode) && typeof childNode.props.children === 'string') {
150 title = childNode.props.children;
151 }
152 }
153
154 var componentProps = _objectSpread(_objectSpread(_objectSpread({
155 title: title
156 }, restCellProps), additionalProps), {}, {
157 colSpan: mergedColSpan && mergedColSpan !== 1 ? mergedColSpan : null,
158 rowSpan: mergedRowSpan && mergedRowSpan !== 1 ? mergedRowSpan : null,
159 className: (0, _classnames.default)(cellPrefixCls, className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left"), isFixLeft), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-first"), firstFixLeft), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-last"), lastFixLeft), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right"), isFixRight), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-first"), firstFixRight), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-last"), lastFixRight), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-ellipsis"), ellipsis), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-with-append"), appendNode), _classNames), additionalProps.className, cellClassName),
160 style: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, additionalProps.style), alignStyle), fixedStyle), cellStyle),
161 ref: isRefComponent(Component) ? ref : null
162 });
163
164 return React.createElement(Component, Object.assign({}, componentProps), appendNode, childNode);
165}
166
167var RefCell = React.forwardRef(Cell);
168RefCell.displayName = 'Cell';
169var MemoCell = React.memo(RefCell, function (prev, next) {
170 if (next.shouldCellUpdate) {
171 return !next.shouldCellUpdate(next.record, prev.record);
172 }
173
174 return false;
175});
176var _default = MemoCell;
177exports.default = _default;
\No newline at end of file