FixedHeader.js
5.81 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
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _ref2 = require("rc-util/lib/ref");
var _Header = _interopRequireDefault(require("./Header"));
var _ColGroup = _interopRequireDefault(require("../ColGroup"));
var _TableContext = _interopRequireDefault(require("../context/TableContext"));
function useColumnWidth(colWidths, columCount) {
return (0, React.useMemo)(function () {
var cloneColumns = [];
for (var i = 0; i < columCount; i += 1) {
var val = colWidths[i];
if (val !== undefined) {
cloneColumns[i] = val;
} else {
return null;
}
}
return cloneColumns;
}, [colWidths.join('_'), columCount]);
}
var FixedHeader = React.forwardRef(function (_ref, ref) {
var noData = _ref.noData,
columns = _ref.columns,
flattenColumns = _ref.flattenColumns,
colWidths = _ref.colWidths,
columCount = _ref.columCount,
stickyOffsets = _ref.stickyOffsets,
direction = _ref.direction,
fixHeader = _ref.fixHeader,
offsetHeader = _ref.offsetHeader,
stickyClassName = _ref.stickyClassName,
onScroll = _ref.onScroll,
props = (0, _objectWithoutProperties2.default)(_ref, ["noData", "columns", "flattenColumns", "colWidths", "columCount", "stickyOffsets", "direction", "fixHeader", "offsetHeader", "stickyClassName", "onScroll"]);
var _React$useContext = React.useContext(_TableContext.default),
prefixCls = _React$useContext.prefixCls,
scrollbarSize = _React$useContext.scrollbarSize,
isSticky = _React$useContext.isSticky;
var combinationScrollBarSize = isSticky && !fixHeader ? 0 : scrollbarSize; // Pass wheel to scroll event
var scrollRef = React.useRef(null);
var setScrollRef = React.useCallback(function (element) {
(0, _ref2.fillRef)(ref, element);
(0, _ref2.fillRef)(scrollRef, element);
}, []);
React.useEffect(function () {
var _scrollRef$current;
function onWheel(e) {
var currentTarget = e.currentTarget,
deltaX = e.deltaX;
if (deltaX) {
onScroll({
currentTarget: currentTarget,
scrollLeft: currentTarget.scrollLeft + deltaX
});
e.preventDefault();
}
}
(_scrollRef$current = scrollRef.current) === null || _scrollRef$current === void 0 ? void 0 : _scrollRef$current.addEventListener('wheel', onWheel);
return function () {
var _scrollRef$current2;
(_scrollRef$current2 = scrollRef.current) === null || _scrollRef$current2 === void 0 ? void 0 : _scrollRef$current2.removeEventListener('wheel', onWheel);
};
}, []); // Add scrollbar column
var lastColumn = flattenColumns[flattenColumns.length - 1];
var ScrollBarColumn = {
fixed: lastColumn ? lastColumn.fixed : null,
onHeaderCell: function onHeaderCell() {
return {
className: "".concat(prefixCls, "-cell-scrollbar")
};
}
};
var columnsWithScrollbar = (0, React.useMemo)(function () {
return combinationScrollBarSize ? [].concat((0, _toConsumableArray2.default)(columns), [ScrollBarColumn]) : columns;
}, [combinationScrollBarSize, columns]);
var flattenColumnsWithScrollbar = (0, React.useMemo)(function () {
return combinationScrollBarSize ? [].concat((0, _toConsumableArray2.default)(flattenColumns), [ScrollBarColumn]) : flattenColumns;
}, [combinationScrollBarSize, flattenColumns]); // Calculate the sticky offsets
var headerStickyOffsets = (0, React.useMemo)(function () {
var right = stickyOffsets.right,
left = stickyOffsets.left;
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, stickyOffsets), {}, {
left: direction === 'rtl' ? [].concat((0, _toConsumableArray2.default)(left.map(function (width) {
return width + combinationScrollBarSize;
})), [0]) : left,
right: direction === 'rtl' ? right : [].concat((0, _toConsumableArray2.default)(right.map(function (width) {
return width + combinationScrollBarSize;
})), [0]),
isSticky: isSticky
});
}, [combinationScrollBarSize, stickyOffsets, isSticky]);
var mergedColumnWidth = useColumnWidth(colWidths, columCount);
return React.createElement("div", {
style: (0, _objectSpread2.default)({
overflow: 'hidden'
}, isSticky ? {
top: offsetHeader
} : {}),
ref: setScrollRef,
className: (0, _classnames.default)("".concat(prefixCls, "-header"), (0, _defineProperty2.default)({}, stickyClassName, !!stickyClassName))
}, React.createElement("table", {
style: {
tableLayout: 'fixed',
visibility: noData || mergedColumnWidth ? null : 'hidden'
}
}, React.createElement(_ColGroup.default, {
colWidths: mergedColumnWidth ? [].concat((0, _toConsumableArray2.default)(mergedColumnWidth), [combinationScrollBarSize]) : [],
columCount: columCount + 1,
columns: flattenColumnsWithScrollbar
}), React.createElement(_Header.default, Object.assign({}, props, {
stickyOffsets: headerStickyOffsets,
columns: columnsWithScrollbar,
flattenColumns: flattenColumnsWithScrollbar
}))));
});
FixedHeader.displayName = 'FixedHeader';
var _default = FixedHeader;
exports.default = _default;