Skip to content

Commit

Permalink
[DataGrid] Fix checkbox selection keeping selection when filtering (@…
Browse files Browse the repository at this point in the history
…g1mishra) (#12485)

Co-authored-by: Jeevan Kumar <g1mishra.dev@gmail.com>
Co-authored-by: Andrew Cherniavskyi <andrew@mui.com>
  • Loading branch information
3 people authored Mar 18, 2024
1 parent 2ab6b49 commit 0958d54
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {
} from './gridRowSelectionSelector';
import { gridPaginatedVisibleSortedGridRowIdsSelector } from '../pagination';
import { gridFocusCellSelector } from '../focus/gridFocusStateSelector';
import { gridExpandedSortedRowIdsSelector } from '../filter/gridFilterSelector';
import {
gridExpandedSortedRowIdsSelector,
gridFilterModelSelector,
} from '../filter/gridFilterSelector';
import { GRID_CHECKBOX_SELECTION_COL_DEF, GRID_ACTIONS_COLUMN_TYPE } from '../../../colDef';
import { GridCellModes } from '../../../models/gridEditRowModel';
import { isKeyboardEvent, isNavigationKey } from '../../../utils/keyboardUtils';
Expand Down Expand Up @@ -446,7 +449,8 @@ export const useGridRowSelection = (
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef)
: gridExpandedSortedRowIdsSelector(apiRef);

apiRef.current.selectRows(rowsToBeSelected, params.value);
const filterModel = gridFilterModelSelector(apiRef);
apiRef.current.selectRows(rowsToBeSelected, params.value, filterModel?.items.length > 0);
},
[apiRef, props.checkboxSelectionVisibleOnly, props.pagination],
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createRenderer, fireEvent, screen, act, userEvent } from '@mui-internal/test-utils';
import {
createRenderer,
fireEvent,
screen,
act,
userEvent,
waitFor,
} from '@mui-internal/test-utils';
import {
DataGrid,
DataGridProps,
Expand All @@ -10,6 +17,8 @@ import {
GridEditModes,
useGridApiRef,
GridApi,
GridPreferencePanelsValue,
gridClasses,
} from '@mui/x-data-grid';
import {
getCell,
Expand Down Expand Up @@ -342,6 +351,53 @@ describe('<DataGrid /> - Row selection', () => {
expect(screen.queryByRole('checkbox', { name: 'Select row' })).to.equal(null);
expect(screen.queryByRole('checkbox', { name: 'Unselect row' })).not.to.equal(null);
});

it('should only select filtered items when "select all" is toggled after applying a filter', async () => {
render(
<TestDataGridSelection
checkboxSelection
initialState={{
preferencePanel: {
open: true,
openedPanelValue: GridPreferencePanelsValue.filters,
},
}}
/>,
);
const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' });
fireEvent.click(selectAllCheckbox);
await waitFor(() => {
expect(getSelectedRowIds()).to.deep.equal([0, 1, 2, 3]);
expect(document.querySelector(`.${gridClasses.selectedRowCount}`)?.textContent).to.equal(
'4 rows selected',
);
});

fireEvent.change(screen.getByRole('spinbutton', { name: 'Value' }), {
target: { value: 1 },
});
await waitFor(() => {
// Previous selection remains, but only one row is visible
expect(getSelectedRowIds()).to.deep.equal([1]);
expect(document.querySelector(`.${gridClasses.selectedRowCount}`)?.textContent).to.equal(
'4 rows selected',
);
});

fireEvent.click(selectAllCheckbox); // Unselect all
await waitFor(() => {
expect(getSelectedRowIds()).to.deep.equal([]);
expect(document.querySelector(`.${gridClasses.selectedRowCount}`)).to.equal(null);
});

fireEvent.click(selectAllCheckbox); // Select all filtered rows
await waitFor(() => {
expect(getSelectedRowIds()).to.deep.equal([1]);
expect(document.querySelector(`.${gridClasses.selectedRowCount}`)?.textContent).to.equal(
'1 row selected',
);
});
});
});

describe('prop: checkboxSelection = true (multi selection), with keyboard events', () => {
Expand Down

0 comments on commit 0958d54

Please sign in to comment.