UNPKG

10.1 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4var _typeof3 = require("@babel/runtime/helpers/typeof");
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
10var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
11var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
12var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
14var _context = require("@rc-component/context");
15var _classnames = _interopRequireDefault(require("classnames"));
16var React = _interopRequireWildcard(require("react"));
17var _TableContext = _interopRequireDefault(require("../context/TableContext"));
18var _useRenderTimes = _interopRequireDefault(require("../hooks/useRenderTimes"));
19var _useCellRender3 = _interopRequireDefault(require("./useCellRender"));
20var _useHoverState3 = _interopRequireDefault(require("./useHoverState"));
21var _rcUtil = require("rc-util");
22function _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); }
23function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof3(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; }
24var getTitleFromCellRenderChildren = function getTitleFromCellRenderChildren(_ref) {
25 var ellipsis = _ref.ellipsis,
26 rowType = _ref.rowType,
27 children = _ref.children;
28 var title;
29 var ellipsisConfig = ellipsis === true || ellipsis === undefined ? {
30 showTitle: true
31 } : ellipsis;
32 if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === 'header')) {
33 if (typeof children === 'string' || typeof children === 'number') {
34 title = children.toString();
35 } else if ( /*#__PURE__*/React.isValidElement(children) && typeof children.props.children === 'string') {
36 title = children.props.children;
37 }
38 }
39 return title;
40};
41function Cell(props) {
42 var _ref2, _ref3, _legacyCellProps$colS, _ref4, _ref5, _legacyCellProps$rowS, _additionalProps$titl, _classNames;
43 if (process.env.NODE_ENV !== 'production') {
44 (0, _useRenderTimes.default)(props);
45 }
46 var Component = props.component,
47 children = props.children,
48 ellipsis = props.ellipsis,
49 scope = props.scope,
50 prefixCls = props.prefixCls,
51 className = props.className,
52 align = props.align,
53 record = props.record,
54 render = props.render,
55 dataIndex = props.dataIndex,
56 renderIndex = props.renderIndex,
57 shouldCellUpdate = props.shouldCellUpdate,
58 index = props.index,
59 rowType = props.rowType,
60 colSpan = props.colSpan,
61 rowSpan = props.rowSpan,
62 fixLeft = props.fixLeft,
63 fixRight = props.fixRight,
64 firstFixLeft = props.firstFixLeft,
65 lastFixLeft = props.lastFixLeft,
66 firstFixRight = props.firstFixRight,
67 lastFixRight = props.lastFixRight,
68 appendNode = props.appendNode,
69 _props$additionalProp = props.additionalProps,
70 additionalProps = _props$additionalProp === void 0 ? {} : _props$additionalProp,
71 isSticky = props.isSticky;
72 var cellPrefixCls = "".concat(prefixCls, "-cell");
73 var _useContext = (0, _context.useContext)(_TableContext.default, ['supportSticky', 'allColumnsFixedLeft', 'rowHoverable']),
74 supportSticky = _useContext.supportSticky,
75 allColumnsFixedLeft = _useContext.allColumnsFixedLeft,
76 rowHoverable = _useContext.rowHoverable;
77
78 // ====================== Value =======================
79 var _useCellRender = (0, _useCellRender3.default)(record, dataIndex, renderIndex, children, render, shouldCellUpdate),
80 _useCellRender2 = (0, _slicedToArray2.default)(_useCellRender, 2),
81 childNode = _useCellRender2[0],
82 legacyCellProps = _useCellRender2[1];
83
84 // ====================== Fixed =======================
85 var fixedStyle = {};
86 var isFixLeft = typeof fixLeft === 'number' && supportSticky;
87 var isFixRight = typeof fixRight === 'number' && supportSticky;
88 if (isFixLeft) {
89 fixedStyle.position = 'sticky';
90 fixedStyle.left = fixLeft;
91 }
92 if (isFixRight) {
93 fixedStyle.position = 'sticky';
94 fixedStyle.right = fixRight;
95 }
96
97 // ================ RowSpan & ColSpan =================
98 var mergedColSpan = (_ref2 = (_ref3 = (_legacyCellProps$colS = legacyCellProps === null || legacyCellProps === void 0 ? void 0 : legacyCellProps.colSpan) !== null && _legacyCellProps$colS !== void 0 ? _legacyCellProps$colS : additionalProps.colSpan) !== null && _ref3 !== void 0 ? _ref3 : colSpan) !== null && _ref2 !== void 0 ? _ref2 : 1;
99 var mergedRowSpan = (_ref4 = (_ref5 = (_legacyCellProps$rowS = legacyCellProps === null || legacyCellProps === void 0 ? void 0 : legacyCellProps.rowSpan) !== null && _legacyCellProps$rowS !== void 0 ? _legacyCellProps$rowS : additionalProps.rowSpan) !== null && _ref5 !== void 0 ? _ref5 : rowSpan) !== null && _ref4 !== void 0 ? _ref4 : 1;
100
101 // ====================== Hover =======================
102 var _useHoverState = (0, _useHoverState3.default)(index, mergedRowSpan),
103 _useHoverState2 = (0, _slicedToArray2.default)(_useHoverState, 2),
104 hovering = _useHoverState2[0],
105 onHover = _useHoverState2[1];
106 var onMouseEnter = (0, _rcUtil.useEvent)(function (event) {
107 var _additionalProps$onMo;
108 if (record) {
109 onHover(index, index + mergedRowSpan - 1);
110 }
111 additionalProps === null || additionalProps === void 0 || (_additionalProps$onMo = additionalProps.onMouseEnter) === null || _additionalProps$onMo === void 0 || _additionalProps$onMo.call(additionalProps, event);
112 });
113 var onMouseLeave = (0, _rcUtil.useEvent)(function (event) {
114 var _additionalProps$onMo2;
115 if (record) {
116 onHover(-1, -1);
117 }
118 additionalProps === null || additionalProps === void 0 || (_additionalProps$onMo2 = additionalProps.onMouseLeave) === null || _additionalProps$onMo2 === void 0 || _additionalProps$onMo2.call(additionalProps, event);
119 });
120
121 // ====================== Render ======================
122 if (mergedColSpan === 0 || mergedRowSpan === 0) {
123 return null;
124 }
125
126 // >>>>> Title
127 var title = (_additionalProps$titl = additionalProps.title) !== null && _additionalProps$titl !== void 0 ? _additionalProps$titl : getTitleFromCellRenderChildren({
128 rowType: rowType,
129 ellipsis: ellipsis,
130 children: childNode
131 });
132
133 // >>>>> ClassName
134 var mergedClassName = (0, _classnames.default)(cellPrefixCls, className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left"), isFixLeft && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-first"), firstFixLeft && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-last"), lastFixLeft && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-all"), lastFixLeft && allColumnsFixedLeft && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right"), isFixRight && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-first"), firstFixRight && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-last"), lastFixRight && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-ellipsis"), ellipsis), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-with-append"), appendNode), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-sticky"), (isFixLeft || isFixRight) && isSticky && supportSticky), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-row-hover"), !legacyCellProps && hovering), _classNames), additionalProps.className, legacyCellProps === null || legacyCellProps === void 0 ? void 0 : legacyCellProps.className);
135
136 // >>>>> Style
137 var alignStyle = {};
138 if (align) {
139 alignStyle.textAlign = align;
140 }
141 var mergedStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, fixedStyle), additionalProps.style), alignStyle), legacyCellProps === null || legacyCellProps === void 0 ? void 0 : legacyCellProps.style);
142
143 // >>>>> Children Node
144 var mergedChildNode = childNode;
145
146 // Not crash if final `childNode` is not validate ReactNode
147 if ((0, _typeof2.default)(mergedChildNode) === 'object' && !Array.isArray(mergedChildNode) && ! /*#__PURE__*/React.isValidElement(mergedChildNode)) {
148 mergedChildNode = null;
149 }
150 if (ellipsis && (lastFixLeft || firstFixRight)) {
151 mergedChildNode = /*#__PURE__*/React.createElement("span", {
152 className: "".concat(cellPrefixCls, "-content")
153 }, mergedChildNode);
154 }
155 return /*#__PURE__*/React.createElement(Component, (0, _extends2.default)({}, legacyCellProps, additionalProps, {
156 className: mergedClassName,
157 style: mergedStyle
158 // A11y
159 ,
160 title: title,
161 scope: scope
162 // Hover
163 ,
164 onMouseEnter: rowHoverable ? onMouseEnter : undefined,
165 onMouseLeave: rowHoverable ? onMouseLeave : undefined
166 //Span
167 ,
168 colSpan: mergedColSpan !== 1 ? mergedColSpan : null,
169 rowSpan: mergedRowSpan !== 1 ? mergedRowSpan : null
170 }), appendNode, mergedChildNode);
171}
172var _default = exports.default = /*#__PURE__*/React.memo(Cell);
\No newline at end of file