-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
useContainerProps.ts
98 lines (86 loc) · 3.43 KB
/
useContainerProps.ts
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
import * as React from 'react';
import { ContainerProps, ElementSize, GridOptions } from '../../models';
import { useLogger } from '../utils/useLogger';
type ReturnType = (
options: GridOptions,
columnsTotalWidth: number,
rowsCount: number,
) => ContainerProps | null; // [ContainerProps | null, () => void];
export const useContainerProps = (windowRef: React.RefObject<HTMLDivElement>): ReturnType => {
const logger = useLogger('useContainerProps');
const windowSizesRef = React.useRef<ElementSize>({ width: 0, height: 0 });
const getContainerProps = React.useCallback(
(options: GridOptions, columnsTotalWidth: number, rowsCount: number): ContainerProps | null => {
if (!windowRef || !windowRef.current) {
return null;
}
logger.debug('Calculating container sizes.');
const window = windowRef.current.getBoundingClientRect();
windowSizesRef.current = { width: window.width, height: window.height };
logger.debug(
`window Size - W: ${windowSizesRef.current.width} H: ${windowSizesRef.current.height} `,
);
const rowHeight = options.rowHeight;
const hasScrollY =
options.autoPageSize || options.autoHeight
? false
: windowSizesRef.current.height < rowsCount * rowHeight;
const hasScrollX = columnsTotalWidth > windowSizesRef.current.width;
const scrollBarSize = {
y: hasScrollY ? options.scrollbarSize : 0,
x: hasScrollX ? options.scrollbarSize : 0,
};
const viewportSize = {
width: windowSizesRef.current!.width - scrollBarSize.y,
height: options.autoHeight
? rowsCount * rowHeight
: windowSizesRef.current!.height - scrollBarSize.x,
};
let viewportPageSize = viewportSize.height / rowHeight;
viewportPageSize = options.pagination
? Math.floor(viewportPageSize)
: Math.round(viewportPageSize);
// We multiply by 2 for virtualization
// TODO allow buffer with fixed nb rows
const rzPageSize = viewportPageSize * 2;
const viewportMaxPage = options.autoPageSize ? 1 : Math.ceil(rowsCount / viewportPageSize);
logger.debug(
`viewportPageSize: ${viewportPageSize}, rzPageSize: ${rzPageSize}, viewportMaxPage: ${viewportMaxPage}`,
);
const renderingZoneHeight = rzPageSize * rowHeight + rowHeight + scrollBarSize.x;
const dataContainerWidth = columnsTotalWidth - scrollBarSize.y;
let totalHeight =
(options.autoPageSize ? 1 : rowsCount / viewportPageSize) * viewportSize.height +
(hasScrollY ? scrollBarSize.x : 0);
if (options.autoHeight) {
totalHeight = rowsCount * rowHeight + scrollBarSize.x;
}
const indexes: ContainerProps = {
renderingZonePageSize: rzPageSize,
viewportPageSize,
hasScrollY,
hasScrollX,
scrollBarSize: options.scrollbarSize,
totalSizes: {
width: columnsTotalWidth,
height: totalHeight || 1,
},
dataContainerSizes: {
width: dataContainerWidth,
height: totalHeight || 1,
},
renderingZone: {
width: dataContainerWidth,
height: renderingZoneHeight,
},
windowSizes: windowSizesRef.current,
viewportSize,
lastPage: viewportMaxPage,
};
logger.debug('returning container props', indexes);
return indexes;
},
[windowRef, logger],
);
return getContainerProps;
};