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 last row #1071

Merged
merged 3 commits into from
Feb 23, 2021
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.
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 @@ -182,6 +182,10 @@ export const useGridVirtualRows = (

const scrollToIndexes = React.useCallback(
(params: GridCellIndexCoordinates) => {
if (totalRowCount === 0 || visibleColumns.length === 0) {
return false;
}

logger.debug(`Scrolling to cell at row ${params.rowIndex}, col: ${params.colIndex} `);

let scrollLeft;
Expand Down Expand Up @@ -240,7 +244,16 @@ export const useGridVirtualRows = (

return needScroll;
},
[logger, apiRef, gridState, windowRef, rowHeight, columnsMeta.positions, visibleColumns],
[
totalRowCount,
visibleColumns,
logger,
apiRef,
gridState,
windowRef,
rowHeight,
columnsMeta.positions,
],
);

const resetScroll = React.useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export const useGridContainerProps = (
(options.autoPageSize ? 1 : rowsCount / viewportPageSize) * viewportSizes.height +
(scrollState.hasScrollY ? scrollState.scrollBarSize.x : 0);

// make sure the last row fit well due to rounding
totalHeight += totalHeight % rowHeight;

if (options.autoHeight) {
totalHeight = rowsCount * rowHeight + scrollState.scrollBarSize.x;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/storybook/src/stories/grid-rows.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,19 @@ const columns: any[] = [
];

export function ScrollIssue() {
const apiRef = useGridApiRef();

React.useEffect(() => {
const timeout = setTimeout(() => {
Copy link
Member

@oliviertassinari oliviertassinari Feb 22, 2021

Choose a reason for hiding this comment

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

The visual tests are waiting in a useEffect: https://github.com/mui-org/material-ui-x/pull/1081/files#diff-df56773d39f0fa9c8f39611f1993bf7df81d046c5d055a47365fccb494407de0R63. It's not waiting for this timeout to run. It won't screenshot it correctly at the right time. So it doesn't work (at least shouldn't in theory or be flaky). What's the setTimeout for?

Copy link
Member Author

Choose a reason for hiding this comment

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

we have to wait until everything is rendered before we can move the scroll down. It seems to only work in a timeout.
I tried with useLayoutEffect...

apiRef.current.scrollToIndexes({ colIndex: 0, rowIndex: 6 });
}, 0);

return () => clearTimeout(timeout);
}, [apiRef]);

return (
<div style={{ height: 300, width: 600 }}>
<XGrid rows={rows} columns={columns} />
<XGrid rows={rows} columns={columns} apiRef={apiRef} />
</div>
);
}