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

[DataGridPro] Fix onRowsScrollEnd being triggered instantly when using a bottom pinned row #14602

Merged
merged 3 commits into from
Sep 13, 2024
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 @@ -160,4 +160,52 @@ describe('<DataGridPro /> - Infnite loader', () => {
// should not load more rows because the threshold is not reached
expect(getRow.callCount).to.equal(5);
});

it('should not call `onRowsScrollEnd` if there are rows pinned to the bottom and the viewport scroll is at the top', async function test() {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume it is because of sleep. Is it fine if I add the fix to your PR?

Copy link
Member

Choose a reason for hiding this comment

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

Sure 👍🏻

Copy link
Member

Choose a reason for hiding this comment

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

Failed on this one too

Copy link
Member

Choose a reason for hiding this comment

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

Attempted to fix it in 40e09d0 (#14613)

Copy link
Contributor Author

@arminmeh arminmeh Sep 16, 2024

Choose a reason for hiding this comment

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

I had to add sleep, because the assertion expect(handleRowsScrollEnd.callCount).to.equal(0); will work fine even with the old code. Call happens a bit later, but it seems that sometimes it takes even more than 1ms to complete it. It might be connected with the IntersectionObserver cycle.
Anyway, I have found another way to deal with this and will push to your PR will create another PR to address this

Copy link
Member

Choose a reason for hiding this comment

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

I dropped the sleep because if it's called twice the test will fail anyway.

if (isJSDOM) {
this.skip(); // Needs layout
}
const baseRows = [
{ id: 0, brand: 'Nike' },
{ id: 1, brand: 'Adidas' },
{ id: 2, brand: 'Puma' },
{ id: 3, brand: 'Under Armor' },
{ id: 4, brand: 'Jordan' },
{ id: 5, brand: 'Reebok' },
];
const basePinnedRows = {
bottom: [{ id: 6, brand: 'Unbranded' }],
};

const handleRowsScrollEnd = spy();
function TestCase({
rows,
pinnedRows,
}: {
rows: typeof baseRows;
pinnedRows: typeof basePinnedRows;
}) {
return (
<div style={{ width: 300, height: 300 }}>
<DataGridPro
columns={[{ field: 'brand', width: 100 }]}
rows={rows}
onRowsScrollEnd={handleRowsScrollEnd}
pinnedRows={pinnedRows}
/>
</div>
);
}
const { container } = render(<TestCase rows={baseRows} pinnedRows={basePinnedRows} />);
const virtualScroller = container.querySelector('.MuiDataGrid-virtualScroller')!;
await sleep(1);
// after initial render and a scroll event that did not reach the bottom of the grid
// the `onRowsScrollEnd` should not be called
expect(handleRowsScrollEnd.callCount).to.equal(0);
// arbitrary number to make sure that the bottom of the grid window is reached.
virtualScroller.scrollTop = 12345;
virtualScroller.dispatchEvent(new Event('scroll'));
await sleep(1);
expect(handleRowsScrollEnd.callCount).to.equal(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export const useGridVirtualScroller = () => {
if (panel) {
rows.push(panel);
}
if (isLastVisible) {
if (params.position === undefined && isLastVisibleInSection) {
rows.push(apiRef.current.getInfiniteLoadingTriggerElement?.({ lastRowId: id }));
}
});
Expand Down