Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,40 @@

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
// rowHeight * pageSize + (filtersHeight + headerHeight + paginationHeight + 1)
// +1 is for useLayoutEffect to detect the change
const DATA_GRID_HEIGHT_BY_PAGE_SIZE: { [key: number]: number } = {
10: 457,
25: 967,
50: 1817,
100: 3517,
10: 389,
25: 809,
50: 1509,
100: 2909,
};

/**
* HUGE HACK!!!
* DataGrtid height isn't properly calculated when the grid has horizontal scroll.
* DataGrid 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;
const filtersHeight = 32;
const headerHeight = 40;
const paginationHeight = 36;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is added even if there is no pagination, but since it occupies the whole page, having a gap under the table is ok.

const rowHeight = 28;

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);
if (rowCount < pageSize) {
setHeight(rowHeight * rowCount + (headerHeight + filtersHeight + paginationHeight));
} else {
setHeight(DATA_GRID_HEIGHT_BY_PAGE_SIZE[pageSize]);
}
}, [pageSize, rowCount]);

return height;
Expand Down