-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathindex.tsx
More file actions
177 lines (158 loc) · 5.2 KB
/
index.tsx
File metadata and controls
177 lines (158 loc) · 5.2 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import { fillRef } from 'rc-util/lib/ref';
import * as React from 'react';
import { useMemo } from 'react';
import ColGroup from '../ColGroup';
import TableContext from '../context/TableContext';
import type { HeaderProps } from '../Header/Header';
import devRenderTimes from '../hooks/useRenderTimes';
import type { ColumnsType, ColumnType, Direction } from '../interface';
function useColumnWidth(colWidths: readonly number[], columCount: number) {
return useMemo(() => {
const cloneColumns: number[] = [];
for (let i = 0; i < columCount; i += 1) {
const val = colWidths[i];
if (val !== undefined) {
cloneColumns[i] = val;
} else {
return null;
}
}
return cloneColumns;
}, [colWidths.join('_'), columCount]);
}
export interface FixedHeaderProps<RecordType> extends HeaderProps<RecordType> {
className: string;
noData: boolean;
maxContentScroll: boolean;
colWidths: readonly number[];
columCount: number;
direction: Direction;
fixHeader: boolean;
stickyTopOffset?: number;
stickyBottomOffset?: number;
stickyClassName?: string;
onScroll: (info: { currentTarget: HTMLDivElement; scrollLeft?: number }) => void;
children: (info: HeaderProps<RecordType>) => React.ReactNode;
}
const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>((props, ref) => {
if (process.env.NODE_ENV !== 'production') {
devRenderTimes(props);
}
const {
className,
noData,
columns,
flattenColumns,
colWidths,
columCount,
stickyOffsets,
direction,
fixHeader,
stickyTopOffset,
stickyBottomOffset,
stickyClassName,
onScroll,
maxContentScroll,
children,
...restProps
} = props;
const { prefixCls, scrollbarSize, isSticky } = useContext(TableContext, [
'prefixCls',
'scrollbarSize',
'isSticky',
]);
const combinationScrollBarSize = isSticky && !fixHeader ? 0 : scrollbarSize;
// Pass wheel to scroll event
const scrollRef = React.useRef<HTMLDivElement>(null);
const setScrollRef = React.useCallback((element: HTMLElement) => {
fillRef(ref, element);
fillRef(scrollRef, element);
}, []);
React.useEffect(() => {
function onWheel(e: WheelEvent) {
const { currentTarget, deltaX } = e as unknown as React.WheelEvent<HTMLDivElement>;
if (deltaX) {
onScroll({ currentTarget, scrollLeft: currentTarget.scrollLeft + deltaX });
e.preventDefault();
}
}
scrollRef.current?.addEventListener('wheel', onWheel);
return () => {
scrollRef.current?.removeEventListener('wheel', onWheel);
};
}, []);
// Check if all flattenColumns has width
const allFlattenColumnsWithWidth = React.useMemo(
() => flattenColumns.every(column => column.width >= 0),
[flattenColumns],
);
// Add scrollbar column
const lastColumn = flattenColumns[flattenColumns.length - 1];
const ScrollBarColumn: ColumnType<unknown> & { scrollbar: true } = {
fixed: lastColumn ? lastColumn.fixed : null,
scrollbar: true,
onHeaderCell: () => ({
className: `${prefixCls}-cell-scrollbar`,
}),
};
const columnsWithScrollbar = useMemo<ColumnsType<unknown>>(
() => (combinationScrollBarSize ? [...columns, ScrollBarColumn] : columns),
[combinationScrollBarSize, columns],
);
const flattenColumnsWithScrollbar = useMemo(
() => (combinationScrollBarSize ? [...flattenColumns, ScrollBarColumn] : flattenColumns),
[combinationScrollBarSize, flattenColumns],
);
// Calculate the sticky offsets
const headerStickyOffsets = useMemo(() => {
const { right, left } = stickyOffsets;
return {
...stickyOffsets,
left:
direction === 'rtl' ? [...left.map(width => width + combinationScrollBarSize), 0] : left,
right:
direction === 'rtl' ? right : [...right.map(width => width + combinationScrollBarSize), 0],
isSticky,
};
}, [combinationScrollBarSize, stickyOffsets, isSticky]);
const mergedColumnWidth = useColumnWidth(colWidths, columCount);
return (
<div
style={{
overflow: 'hidden',
...(isSticky ? { top: stickyTopOffset, bottom: stickyBottomOffset } : {}),
}}
ref={setScrollRef}
className={classNames(className, {
[stickyClassName]: !!stickyClassName,
})}
>
<table
style={{
tableLayout: 'fixed',
visibility: mergedColumnWidth ? null : 'hidden',
}}
>
{(!noData || !maxContentScroll || allFlattenColumnsWithWidth) && (
<ColGroup
colWidths={mergedColumnWidth ? [...mergedColumnWidth, combinationScrollBarSize] : []}
columCount={columCount + 1}
columns={flattenColumnsWithScrollbar}
/>
)}
{children({
...restProps,
stickyOffsets: headerStickyOffsets,
columns: columnsWithScrollbar,
flattenColumns: flattenColumnsWithScrollbar,
})}
</table>
</div>
);
});
FixedHolder.displayName = 'FixedHolder';
/** Return a table in div as fixed element which contains sticky info */
// export default responseImmutable(FixedHolder);
export default React.memo(FixedHolder);