Skip to content

Commit

Permalink
[DataGrid] Fix grid size calculation when .MuiDataGrid-main has bor…
Browse files Browse the repository at this point in the history
…der (#8882)
  • Loading branch information
cherniavskii committed May 18, 2023
1 parent efd46f4 commit d199f47
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,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 @@ -1099,4 +1099,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);
// The width should not increase infinitely
expect(virtualScroller.clientWidth).to.equal(initialVirtualScrollerWidth);
});
});

0 comments on commit d199f47

Please sign in to comment.