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

2 changes: 2 additions & 0 deletions src/components/datagrid/__mocks__/row_height_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const mockRowHeightUtils = ({
getLineCount: jest.fn(actual.getLineCount),
calculateHeightForLineCount: jest.fn(() => 50),
isRowHeightOverride: jest.fn(actual.isRowHeightOverride),
onUpdatedRowHeight: jest.fn(),
offUpdatedRowHeight: jest.fn(),
} as unknown) as ActualRowHeightUtils;

export const RowHeightUtils = jest.fn(() => mockRowHeightUtils);
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ exports[`EuiDataGridCell renders 1`] = `
"getStylesForCell": [MockFunction],
"isAutoHeight": [MockFunction],
"isRowHeightOverride": [MockFunction],
"offUpdatedRowHeight": [MockFunction],
"onUpdatedRowHeight": [MockFunction],
"pruneHiddenColumnHeights": [MockFunction],
"setGrid": [MockFunction],
"setRowHeight": [MockFunction],
Expand Down Expand Up @@ -96,6 +98,8 @@ exports[`EuiDataGridCell renders 1`] = `
"getStylesForCell": [MockFunction],
"isAutoHeight": [MockFunction],
"isRowHeightOverride": [MockFunction],
"offUpdatedRowHeight": [MockFunction],
"onUpdatedRowHeight": [MockFunction],
"pruneHiddenColumnHeights": [MockFunction],
"setGrid": [MockFunction],
"setRowHeight": [MockFunction],
Expand Down
33 changes: 32 additions & 1 deletion src/components/datagrid/body/data_grid_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
]
);

const [, setRenderPass] = useState(0);
useEffect(() => {
const callback = () => {
setRenderPass((x) => x + 1);
};
rowHeightUtils.onUpdatedRowHeight(callback);
return () => rowHeightUtils.offUpdatedRowHeight(callback);
}, [rowHeightUtils]);

const setGridRef = useCallback(
(ref: Grid | null) => {
gridRef.current = ref;
Expand Down Expand Up @@ -573,11 +582,33 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
}
}, [getRowHeight]);

let knownHeight = 0;
let knownRowCount = 0;
for (let i = startRow; i < endRow; i++) {
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
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 + headerRowHeight + footerRowHeight;
defaultHeight * (rowCountToAffordFor - knownRowCount) +
knownHeight +
headerRowHeight +
footerRowHeight;

// unable to determine this until the container's size is known anyway
const unconstrainedWidth = 0;
Expand Down
29 changes: 29 additions & 0 deletions src/components/datagrid/data_grid.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ describe('EuiDataGrid', () => {
.should('have.lengthOf', 0);
});
});

describe('height calculation', async () => {
it('computes a new unconstrained height when switching to auto height', () => {
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
const renderCellValue: EuiDataGridProps['renderCellValue'] = ({
rowIndex,
columnId,
}) => (
<>
row {rowIndex}
<br />
column {columnId}
</>
);

mount(<EuiDataGrid {...baseProps} renderCellValue={renderCellValue} />);

getGridData();
cy.get('[data-test-subj=euiDataGridBody]')
.invoke('outerHeight')
.then((firstHeight) => {
cy.get('[data-test-subj=dataGridDisplaySelectorPopover]').click();
cy.get('[data-text="Auto fit"]').click();

cy.get('[data-test-subj=euiDataGridBody]')
.invoke('outerHeight')
.should('be.greaterThan', firstHeight);
});
});
});
});

function getGridData() {
Expand Down
13 changes: 13 additions & 0 deletions src/components/datagrid/row_height_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const AUTO_HEIGHT = 'auto';
export const DEFAULT_ROW_HEIGHT = 34;

export class RowHeightUtils {
onUpdateCallbacks: Array<() => void> = [];

getRowHeightOption(
rowIndex: number,
rowHeightsOptions?: EuiDataGridRowHeightsOptions
Expand Down Expand Up @@ -173,6 +175,15 @@ export class RowHeightUtils {
return Math.max(...rowHeightValues);
}

onUpdatedRowHeight(callback: () => void) {
this.onUpdateCallbacks.push(callback);
}

offUpdatedRowHeight(callback: () => void) {
const idx = this.onUpdateCallbacks.indexOf(callback);
if (idx !== -1) this.onUpdateCallbacks.splice(idx, 1);
}

chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
setRowHeight(
rowIndex: number,
colId: string,
Expand All @@ -192,6 +203,8 @@ export class RowHeightUtils {
rowHeights.set(colId, adaptedHeight);
this.heightsCache.set(rowIndex, rowHeights);
this.resetRow(visibleRowIndex);

this.onUpdateCallbacks.forEach((callback) => callback());
}

pruneHiddenColumnHeights(visibleColumns: EuiDataGridColumn[]) {
Expand Down