Skip to content

Commit

Permalink
[DataGridPremium] Fix cell selection throwing index error on second p…
Browse files Browse the repository at this point in the history
…age and beyond (mui#10784)
  • Loading branch information
MBilalShafi authored and alexfauquette committed Nov 6, 2023
1 parent 7835fa3 commit b910dcb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ export const useGridCellSelection = (
newClasses.push(gridClasses['cell--rangeTop']);
}

if (rowIndex < visibleRows.range.lastRowIndex) {
if (rowIndex + visibleRows.range.firstRowIndex < visibleRows.range.lastRowIndex) {
const { id: nextRowId } = visibleRows.rows[rowIndex + 1];
if (!apiRef.current.unstable_isCellSelected(nextRowId, field)) {
newClasses.push(gridClasses['cell--rangeBottom']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { stub, SinonStub } from 'sinon';
import { expect } from 'chai';
import { spyApi, getCell } from 'test/utils/helperFn';
import { createRenderer, fireEvent, act, userEvent } from '@mui-internal/test-utils';
import { createRenderer, fireEvent, act, userEvent, screen } from '@mui-internal/test-utils';
import {
DataGridPremium,
DataGridPremiumProps,
Expand Down Expand Up @@ -36,12 +36,12 @@ describe('<DataGridPremium /> - Cell selection', () => {
<div style={{ width, height }}>
<DataGridPremium
{...data}
{...other}
apiRef={apiRef}
rowSelection={false}
unstable_cellSelection
disableVirtualization
hideFooter
{...other}
/>
</div>
);
Expand All @@ -66,6 +66,26 @@ describe('<DataGridPremium /> - Cell selection', () => {
expect(cell11).to.have.class('Mui-selected');
});

// https://github.com/mui/mui-x/issues/10777
it('should work with the paginated grid', () => {
render(
<TestDataGridSelection
initialState={{ pagination: { paginationModel: { page: 0, pageSize: 3 } } }}
rowLength={30}
pagination
pageSizeOptions={[3]}
hideFooter={false}
/>,
);
const cell01 = getCell(2, 0);
fireEvent.click(cell01);
expect(cell01).to.have.class('Mui-selected');
fireEvent.click(screen.getByRole('button', { name: /next page/i }));
const cell02 = getCell(5, 0);
fireEvent.click(cell02);
expect(cell02).to.have.class('Mui-selected');
});

describe('Ctrl + click', () => {
it('should add the clicked cells to the selection', () => {
render(<TestDataGridSelection />);
Expand Down

0 comments on commit b910dcb

Please sign in to comment.