FixedHeader.js
5.07 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
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import * as React from 'react';
import { useMemo } from 'react';
import classNames from 'classnames';
import { fillRef } from "rc-util/es/ref";
import Header from './Header';
import ColGroup from '../ColGroup';
import TableContext from '../context/TableContext';
function useColumnWidth(colWidths, columCount) {
return 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 = _objectWithoutProperties(_ref, ["noData", "columns", "flattenColumns", "colWidths", "columCount", "stickyOffsets", "direction", "fixHeader", "offsetHeader", "stickyClassName", "onScroll"]);
var _React$useContext = React.useContext(TableContext),
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) {
fillRef(ref, element);
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 = useMemo(function () {
return combinationScrollBarSize ? [].concat(_toConsumableArray(columns), [ScrollBarColumn]) : columns;
}, [combinationScrollBarSize, columns]);
var flattenColumnsWithScrollbar = useMemo(function () {
return combinationScrollBarSize ? [].concat(_toConsumableArray(flattenColumns), [ScrollBarColumn]) : flattenColumns;
}, [combinationScrollBarSize, flattenColumns]); // Calculate the sticky offsets
var headerStickyOffsets = useMemo(function () {
var right = stickyOffsets.right,
left = stickyOffsets.left;
return _objectSpread(_objectSpread({}, stickyOffsets), {}, {
left: direction === 'rtl' ? [].concat(_toConsumableArray(left.map(function (width) {
return width + combinationScrollBarSize;
})), [0]) : left,
right: direction === 'rtl' ? right : [].concat(_toConsumableArray(right.map(function (width) {
return width + combinationScrollBarSize;
})), [0]),
isSticky: isSticky
});
}, [combinationScrollBarSize, stickyOffsets, isSticky]);
var mergedColumnWidth = useColumnWidth(colWidths, columCount);
return React.createElement("div", {
style: _objectSpread({
overflow: 'hidden'
}, isSticky ? {
top: offsetHeader
} : {}),
ref: setScrollRef,
className: classNames("".concat(prefixCls, "-header"), _defineProperty({}, stickyClassName, !!stickyClassName))
}, React.createElement("table", {
style: {
tableLayout: 'fixed',
visibility: noData || mergedColumnWidth ? null : 'hidden'
}
}, React.createElement(ColGroup, {
colWidths: mergedColumnWidth ? [].concat(_toConsumableArray(mergedColumnWidth), [combinationScrollBarSize]) : [],
columCount: columCount + 1,
columns: flattenColumnsWithScrollbar
}), React.createElement(Header, Object.assign({}, props, {
stickyOffsets: headerStickyOffsets,
columns: columnsWithScrollbar,
flattenColumns: flattenColumnsWithScrollbar
}))));
});
FixedHeader.displayName = 'FixedHeader';
export default FixedHeader;