-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathVirtualized.tsx
71 lines (66 loc) · 2.24 KB
/
Virtualized.tsx
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
import * as React from 'react';
import { VariableSizeList } from 'react-window';
import AutoSizer from 'react-virtualized-auto-sizer';
import { Header, Body, TableNode } from '@table-library/react-table-library/table/index';
import {
getRowHeight,
SHARED_VIRTUALIZE_STYLE,
} from '@table-library/react-table-library/common/util/virtualized';
import { VirtualizedProps } from '@table-library/react-table-library/types/virtualized';
const Virtualized = <T extends TableNode>({
tableList,
rowHeight,
header,
body,
tableOptions,
rowOptions,
}: VirtualizedProps<T>) => {
return (
<>
{tableOptions?.renderBeforeTable && tableOptions.renderBeforeTable()}
<AutoSizer>
{({ width, height }) => (
<VariableSizeList
height={height}
width={width}
itemCount={tableList.length}
itemSize={(index) => getRowHeight(rowHeight, tableList[index], index)}
innerElementType={React.forwardRef(({ children, ...rest }, ref) => (
<div ref={ref} {...rest}>
<div
style={{
...SHARED_VIRTUALIZE_STYLE,
position: 'sticky',
insetBlockStart: 0,
zIndex: 3,
}}
>
<Header>{header()}</Header>
</div>
<Body>{children}</Body>
</div>
))}
itemData={{ items: tableList }}
>
{({ index, style, data }) => (
<div
style={{
...style,
...SHARED_VIRTUALIZE_STYLE,
top: +(style.top || 0) + getRowHeight(rowHeight, data.items[index], index),
}}
>
{rowOptions?.renderBeforeRow &&
rowOptions.renderBeforeRow(data.items[index], index)}
{body(data.items[index], index)}
{rowOptions?.renderAfterRow && rowOptions.renderAfterRow(data.items[index], index)}
</div>
)}
</VariableSizeList>
)}
</AutoSizer>
{tableOptions?.renderAfterTable && tableOptions.renderAfterTable()}
</>
);
};
export { Virtualized, getRowHeight };