Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiDataGrid] improve height calculation #5447

106 changes: 73 additions & 33 deletions src/components/datagrid/body/data_grid_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
useMutationObserver,
} from '../../observer/mutation_observer';
import { useResizeObserver } from '../../observer/resize_observer';
import { DEFAULT_ROW_HEIGHT } from '../row_height_utils';
import { DEFAULT_ROW_HEIGHT, RowHeightUtils } from '../row_height_utils';
import { EuiDataGridCell } from './data_grid_cell';
import {
DataGridSortingContext,
Expand All @@ -44,6 +44,7 @@ import {
import {
EuiDataGridBodyProps,
EuiDataGridInMemoryValues,
EuiDataGridRowHeightsOptions,
EuiDataGridRowManager,
EuiDataGridSchemaDetector,
} from '../data_grid_types';
Expand Down Expand Up @@ -254,6 +255,67 @@ export function getParentCellContent(_element: Node | HTMLElement) {
return element;
}

// computes the unconstrained (total possible) height of a grid
const useUnconstrainedHeight = ({
Copy link
Member

Choose a reason for hiding this comment

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

This entire commit + set of comments is amazing and really helps me understand what this PR is doing. Thanks for taking the time to do it!! 😍

In the future, I might poke a bit more at splitting up even more of data_grid_body's logic into separate utils/hooks like this for organization - I personally think it's super useful for dev experience and unit testing. Thanks for the awesome headstart!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Breaking out into more hooks would be great; likely a great time to pair as well

rowHeightUtils,
startRow,
endRow,
getCorrectRowIndex,
rowHeightsOptions,
defaultHeight,
headerRowHeight,
footerRowHeight,
}: {
rowHeightUtils: RowHeightUtils;
startRow: number;
endRow: number;
getCorrectRowIndex: (rowIndex: number) => number;
rowHeightsOptions?: EuiDataGridRowHeightsOptions;
defaultHeight: number;
headerRowHeight: number;
footerRowHeight: number;
}) => {
// when a row height is updated, force a re-render of the grid body to update the unconstrained height
const forceRender = useForceRender();
useEffect(() => {
rowHeightUtils.setRerenderGridBody(forceRender);
}, [rowHeightUtils, forceRender]);

let knownHeight = 0; // tracks the pixel height of rows we know the size of
let knownRowCount = 0; // how many rows we know the size of
for (let i = startRow; i < endRow; i++) {
const correctRowIndex = getCorrectRowIndex(i); // map visible row to logical row

// lookup the height configuration of this row
const rowHeightOption = rowHeightUtils.getRowHeightOption(
correctRowIndex,
rowHeightsOptions
);

if (rowHeightOption) {
// this row's height is known
knownRowCount++;
knownHeight += rowHeightUtils.getCalculatedHeight(
rowHeightOption,
defaultHeight,
correctRowIndex,
rowHeightUtils.isRowHeightOverride(correctRowIndex, rowHeightsOptions)
);
}
}

// how many rows to provide space for on the screen
const rowCountToAffordFor = endRow - startRow;

const unconstrainedHeight =
defaultHeight * (rowCountToAffordFor - knownRowCount) + // guess how much space is required for unknown rows
knownHeight + // computed pixel height of the known rows
headerRowHeight + // account for header
footerRowHeight; // account for footer

return unconstrainedHeight;
};

export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
props
) => {
Expand Down Expand Up @@ -509,11 +571,6 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
[rowHeightUtils]
);

const forceRender = useForceRender();
useEffect(() => {
rowHeightUtils.setRerenderGridBody(forceRender);
}, [rowHeightUtils, forceRender]);

const [minRowHeight, setRowHeight] = useState(DEFAULT_ROW_HEIGHT);

const defaultHeight = useMemo(() => {
Expand Down Expand Up @@ -579,33 +636,16 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
}
}, [getRowHeight]);

let knownHeight = 0;
let knownRowCount = 0;
for (let i = startRow; i < endRow; i++) {
const correctRowIndex = getCorrectRowIndex(i);
const rowHeightOption = rowHeightUtils.getRowHeightOption(
correctRowIndex,
rowHeightsOptions
);
if (rowHeightOption) {
knownRowCount++;
knownHeight += rowHeightUtils.getCalculatedHeight(
rowHeightOption,
defaultHeight,
correctRowIndex,
rowHeightUtils.isRowHeightOverride(correctRowIndex, rowHeightsOptions)
);
}
}

const rowCountToAffordFor = pagination
? pagination.pageSize
: visibleRowIndices.length;
const unconstrainedHeight =
defaultHeight * (rowCountToAffordFor - knownRowCount) +
knownHeight +
headerRowHeight +
footerRowHeight;
const unconstrainedHeight = useUnconstrainedHeight({
rowHeightUtils,
startRow,
endRow,
getCorrectRowIndex,
rowHeightsOptions,
defaultHeight,
headerRowHeight,
footerRowHeight,
});

// unable to determine this until the container's size is known anyway
const unconstrainedWidth = 0;
Expand Down