UNPKG

7.95 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;
9var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13var _context = require("@rc-component/context");
14var _classnames = _interopRequireDefault(require("classnames"));
15var _ref2 = require("rc-util/lib/ref");
16var _react = _interopRequireWildcard(require("react"));
17var React = _react;
18var _ColGroup = _interopRequireDefault(require("../ColGroup"));
19var _TableContext = _interopRequireDefault(require("../context/TableContext"));
20var _useRenderTimes = _interopRequireDefault(require("../hooks/useRenderTimes"));
21var _excluded = ["className", "noData", "columns", "flattenColumns", "colWidths", "columCount", "stickyOffsets", "direction", "fixHeader", "stickyTopOffset", "stickyBottomOffset", "stickyClassName", "onScroll", "maxContentScroll", "children"];
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" != _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; }
24function useColumnWidth(colWidths, columCount) {
25 return (0, _react.useMemo)(function () {
26 var cloneColumns = [];
27 for (var i = 0; i < columCount; i += 1) {
28 var val = colWidths[i];
29 if (val !== undefined) {
30 cloneColumns[i] = val;
31 } else {
32 return null;
33 }
34 }
35 return cloneColumns;
36 }, [colWidths.join('_'), columCount]);
37}
38var FixedHolder = /*#__PURE__*/React.forwardRef(function (props, ref) {
39 if (process.env.NODE_ENV !== 'production') {
40 (0, _useRenderTimes.default)(props);
41 }
42 var className = props.className,
43 noData = props.noData,
44 columns = props.columns,
45 flattenColumns = props.flattenColumns,
46 colWidths = props.colWidths,
47 columCount = props.columCount,
48 stickyOffsets = props.stickyOffsets,
49 direction = props.direction,
50 fixHeader = props.fixHeader,
51 stickyTopOffset = props.stickyTopOffset,
52 stickyBottomOffset = props.stickyBottomOffset,
53 stickyClassName = props.stickyClassName,
54 onScroll = props.onScroll,
55 maxContentScroll = props.maxContentScroll,
56 children = props.children,
57 restProps = (0, _objectWithoutProperties2.default)(props, _excluded);
58 var _useContext = (0, _context.useContext)(_TableContext.default, ['prefixCls', 'scrollbarSize', 'isSticky', 'getComponent']),
59 prefixCls = _useContext.prefixCls,
60 scrollbarSize = _useContext.scrollbarSize,
61 isSticky = _useContext.isSticky,
62 getComponent = _useContext.getComponent;
63 var TableComponent = getComponent(['header', 'table'], 'table');
64 var combinationScrollBarSize = isSticky && !fixHeader ? 0 : scrollbarSize;
65
66 // Pass wheel to scroll event
67 var scrollRef = React.useRef(null);
68 var setScrollRef = React.useCallback(function (element) {
69 (0, _ref2.fillRef)(ref, element);
70 (0, _ref2.fillRef)(scrollRef, element);
71 }, []);
72 React.useEffect(function () {
73 var _scrollRef$current;
74 function onWheel(e) {
75 var _ref = e,
76 currentTarget = _ref.currentTarget,
77 deltaX = _ref.deltaX;
78 if (deltaX) {
79 onScroll({
80 currentTarget: currentTarget,
81 scrollLeft: currentTarget.scrollLeft + deltaX
82 });
83 e.preventDefault();
84 }
85 }
86 (_scrollRef$current = scrollRef.current) === null || _scrollRef$current === void 0 || _scrollRef$current.addEventListener('wheel', onWheel, {
87 passive: false
88 });
89 return function () {
90 var _scrollRef$current2;
91 (_scrollRef$current2 = scrollRef.current) === null || _scrollRef$current2 === void 0 || _scrollRef$current2.removeEventListener('wheel', onWheel);
92 };
93 }, []);
94
95 // Check if all flattenColumns has width
96 var allFlattenColumnsWithWidth = React.useMemo(function () {
97 return flattenColumns.every(function (column) {
98 return column.width;
99 });
100 }, [flattenColumns]);
101
102 // Add scrollbar column
103 var lastColumn = flattenColumns[flattenColumns.length - 1];
104 var ScrollBarColumn = {
105 fixed: lastColumn ? lastColumn.fixed : null,
106 scrollbar: true,
107 onHeaderCell: function onHeaderCell() {
108 return {
109 className: "".concat(prefixCls, "-cell-scrollbar")
110 };
111 }
112 };
113 var columnsWithScrollbar = (0, _react.useMemo)(function () {
114 return combinationScrollBarSize ? [].concat((0, _toConsumableArray2.default)(columns), [ScrollBarColumn]) : columns;
115 }, [combinationScrollBarSize, columns]);
116 var flattenColumnsWithScrollbar = (0, _react.useMemo)(function () {
117 return combinationScrollBarSize ? [].concat((0, _toConsumableArray2.default)(flattenColumns), [ScrollBarColumn]) : flattenColumns;
118 }, [combinationScrollBarSize, flattenColumns]);
119
120 // Calculate the sticky offsets
121 var headerStickyOffsets = (0, _react.useMemo)(function () {
122 var right = stickyOffsets.right,
123 left = stickyOffsets.left;
124 return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, stickyOffsets), {}, {
125 left: direction === 'rtl' ? [].concat((0, _toConsumableArray2.default)(left.map(function (width) {
126 return width + combinationScrollBarSize;
127 })), [0]) : left,
128 right: direction === 'rtl' ? right : [].concat((0, _toConsumableArray2.default)(right.map(function (width) {
129 return width + combinationScrollBarSize;
130 })), [0]),
131 isSticky: isSticky
132 });
133 }, [combinationScrollBarSize, stickyOffsets, isSticky]);
134 var mergedColumnWidth = useColumnWidth(colWidths, columCount);
135 return /*#__PURE__*/React.createElement("div", {
136 style: (0, _objectSpread2.default)({
137 overflow: 'hidden'
138 }, isSticky ? {
139 top: stickyTopOffset,
140 bottom: stickyBottomOffset
141 } : {}),
142 ref: setScrollRef,
143 className: (0, _classnames.default)(className, (0, _defineProperty2.default)({}, stickyClassName, !!stickyClassName))
144 }, /*#__PURE__*/React.createElement(TableComponent, {
145 style: {
146 tableLayout: 'fixed',
147 visibility: noData || mergedColumnWidth ? null : 'hidden'
148 }
149 }, (!noData || !maxContentScroll || allFlattenColumnsWithWidth) && /*#__PURE__*/React.createElement(_ColGroup.default, {
150 colWidths: mergedColumnWidth ? [].concat((0, _toConsumableArray2.default)(mergedColumnWidth), [combinationScrollBarSize]) : [],
151 columCount: columCount + 1,
152 columns: flattenColumnsWithScrollbar
153 }), children((0, _objectSpread2.default)((0, _objectSpread2.default)({}, restProps), {}, {
154 stickyOffsets: headerStickyOffsets,
155 columns: columnsWithScrollbar,
156 flattenColumns: flattenColumnsWithScrollbar
157 }))));
158});
159if (process.env.NODE_ENV !== 'production') {
160 FixedHolder.displayName = 'FixedHolder';
161}
162
163/** Return a table in div as fixed element which contains sticky info */
164// export default responseImmutable(FixedHolder);
165var _default = exports.default = /*#__PURE__*/React.memo(FixedHolder);
\No newline at end of file