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

[DataGrid] Fix grid size calculation when .MuiDataGrid-main has border #8882

Merged
merged 3 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ export function useGridDimensions(
return;
}

const height = mainEl.offsetHeight || 0;
const width = mainEl.offsetWidth || 0;
const height = mainEl.clientHeight || 0;
const width = mainEl.clientWidth || 0;

const win = ownerWindow(mainEl);

Expand Down
30 changes: 29 additions & 1 deletion packages/grid/x-data-grid/src/tests/layout.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@mui/x-data-grid';
import { useBasicDemoData } from '@mui/x-data-grid-generator';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import { getColumnHeaderCell, getColumnValues, getCell, getRow } from 'test/utils/helperFn';
import { getColumnHeaderCell, getColumnValues, getCell, getRow, sleep } from 'test/utils/helperFn';

describe('<DataGrid /> - Layout & Warnings', () => {
const { clock, render } = createRenderer();
Expand Down Expand Up @@ -1082,4 +1082,32 @@ describe('<DataGrid /> - Layout & Warnings', () => {
});
});
});

// See https://github.com/mui/mui-x/issues/8737
it('should not add horizontal scrollbar when .MuiDataGrid-main has border', async function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
// Need layouting
this.skip();
}

render(
<div style={{ height: 300, width: 400, display: 'flex' }}>
<DataGrid
rows={[{ id: 1 }]}
columns={[{ field: 'id', flex: 1 }]}
sx={{ '.MuiDataGrid-main': { border: '2px solid red' } }}
/>
</div>,
);

const virtualScroller = document.querySelector<HTMLElement>('.MuiDataGrid-virtualScroller')!;
const initialVirtualScrollerWidth = virtualScroller.clientWidth;

// It should not have a horizontal scrollbar
expect(virtualScroller.scrollWidth - virtualScroller.clientWidth).to.equal(0);

await sleep(200);
Copy link
Member

Choose a reason for hiding this comment

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

Just curious if clock.tick could be used here alternatively too 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess that's an option too, but we'll need to use a fake clock for this.
If this test turns out to be flaky, I'll give it a try! 👍

// The width should not increase infinitely
expect(virtualScroller.clientWidth).to.equal(initialVirtualScrollerWidth);
});
});