Skip to content

Commit

Permalink
[DataGrid] Fix last row (#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtassone authored Feb 23, 2021
1 parent ca78a54 commit e87aa0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
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(() => {
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>
);
}

0 comments on commit e87aa0e

Please sign in to comment.