-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
height_hack.ts
73 lines (62 loc) · 2.55 KB
/
height_hack.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useState, useLayoutEffect } from 'react';
// It will recalculate DataGrid height after this time interval.
const TIME_INTERVAL = 50;
/**
* You are probably asking yourself "Why 3?". But that is the wrong mindset. You should be asking yourself "why not 3?".
* 3 (three) is a number, numeral and digit. It is the natural number following 2 and preceding 4, and is the smallest
* odd prime number and the only prime preceding a square number. It has religious or cultural significance in many societies.
*/
const MAGIC_GAP = 3;
// Hard coded height for every page size
const DATA_GRID_HEIGHT_BY_PAGE_SIZE: { [key: number]: number } = {
10: 457,
25: 967,
50: 1817,
100: 3517,
};
/**
* HUGE HACK!!!
* DataGrtid height isn't properly calculated when the grid has horizontal scroll.
* https://github.com/elastic/eui/issues/5030
*
* In order to get around this bug we are calculating `DataGrid` height here and setting it as a prop.
*
* Please delete me and allow DataGrid to calculate its height when the bug is fixed.
*/
const dataGridRowHeight = 36;
const headerSectionHeight = 32;
const additionalFiltersHeight = 44;
export const useDataGridHeightHack = (pageSize: number, rowCount: number) => {
const [height, setHeight] = useState(DATA_GRID_HEIGHT_BY_PAGE_SIZE[pageSize]);
useLayoutEffect(() => {
setTimeout(() => {
const gridVirtualized = document.querySelector('#body-data-grid .euiDataGrid__virtualized');
if (rowCount === pageSize) {
setHeight(DATA_GRID_HEIGHT_BY_PAGE_SIZE[pageSize]);
} else if (rowCount <= pageSize) {
// This is unnecessary if we add rowCount > pageSize below
setHeight(dataGridRowHeight * rowCount + (headerSectionHeight + additionalFiltersHeight));
} else if (
// rowCount > pageSize && // This will fix the issue but is always full height so has a lot of empty state
gridVirtualized &&
gridVirtualized.children[0].clientHeight !== gridVirtualized.clientHeight // check if it has vertical scroll
) {
setHeight((currHeight) => {
return (
currHeight +
gridVirtualized.children[0].clientHeight -
gridVirtualized.clientHeight +
MAGIC_GAP
);
});
}
}, TIME_INTERVAL);
}, [pageSize, rowCount]);
return height;
};