-
Notifications
You must be signed in to change notification settings - Fork 2
/
ckBody.js
85 lines (77 loc) · 2.27 KB
/
ckBody.js
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
import React, { useContext, useRef, useEffect } from 'react';
import { useVirtual } from "react-virtual";
import Context from '../context';
import CkRow from './ckRow';
const Body = () => {
const bodyRef = useRef(null);
const { dispatch, state, tableInstance, size = 'small' } = useContext(Context);
const { prepareRow, rows, getTableBodyProps } = tableInstance;
const rowVirtualizer = useVirtual({
size: rows.length,
parentRef: bodyRef,
estimateSize: React.useCallback(() => {
switch (size) {
case 'large':
return 64
case 'small':
return 48
case 'mini':
return 36
default:
return 48
}
}, [size]),
overscan: 2
});
const onScroll = (e) => {
const { scrollLeft, scrollWidth, clientWidth } = e.target;
if (scrollLeft !== state.curScroll.scrollLeft) {
if (state.headerElem) { state.headerElem.current.scrollLeft = scrollLeft };
if (state.footerElem) { state.footerElem.current.scrollLeft = scrollLeft };
}
dispatch({
type: 'changeScroll',
curScroll: { scrollLeft, scrollWidth, clientWidth },
});
};
useEffect(() => {
if (bodyRef) {
dispatch({
type: 'bodyElem',
bodyElem: bodyRef
});
};
}, []);
return (
<>
<div className="ck-body" {...getTableBodyProps()}>
<div ref={bodyRef} className='ck-tbody' onScroll={onScroll}>
<div
style={{
height: `${rowVirtualizer.totalSize}px`,
width: "100%",
position: "relative"
}}
>
{rowVirtualizer.virtualItems.map(virtualRow => {
prepareRow(rows[virtualRow.index]);
const style = {
position: "absolute",
top: 0,
left: 0,
minWidth: '100%',
transform: `translateY(${virtualRow.start}px)`,
}
return <CkRow row={rows[virtualRow.index]} {...rows[virtualRow.index].getRowProps({ style })} />
})}
</div>
{/* {rows.map((row) => {
prepareRow(row);
return <CkRow row={row} {...row.getRowProps()} />
})} */}
</div>
</div>
</>
);
};
export default Body;