Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
29 changes: 29 additions & 0 deletions docs/data/data-grid/row-selection/row-selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,37 @@
Use the `rowSelectionModel` prop to control the selection.
Each time this prop changes, the `onRowSelectionModelChange` callback is called with the new selection value.

The row selection model has the following structure:

```tsx
const [rowSelectionModel, setRowSelectionModel] =
React.useState<GridRowSelectionModel>({
type: 'include', // or 'exclude'
ids: new Set<GridRowId>(['row1', 'row2']),
});
```

The model can be either:

- **`type: 'include'`**: Only the rows with IDs in the `ids` set are selected
- **`type: 'exclude'`**: All rows are selected except those with IDs in the `ids` set

{{"demo": "ControlledSelectionGrid.js", "bg": "inline"}}

### Opting out of exclude model optimization

By default, the Data Grid uses an exclude model optimization when selecting all rows (e.g., via "Select all" checkbox) for better performance with large datasets.

Check failure on line 70 in docs/data/data-grid/row-selection/row-selection.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [MUI.CorrectRererenceCased] Use 'e.g.' instead of 'e.g' Raw Output: {"message": "[MUI.CorrectRererenceCased] Use 'e.g.' instead of 'e.g'", "location": {"path": "docs/data/data-grid/row-selection/row-selection.md", "range": {"start": {"line": 70, "column": 87}}}, "severity": "ERROR"}

Check failure on line 70 in docs/data/data-grid/row-selection/row-selection.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [MUI.GoogleLatin] Use 'for example' instead of 'e.g.' Raw Output: {"message": "[MUI.GoogleLatin] Use 'for example' instead of 'e.g.'", "location": {"path": "docs/data/data-grid/row-selection/row-selection.md", "range": {"start": {"line": 70, "column": 87}}}, "severity": "ERROR"}

To opt out of the `exclude` model and always use `include` model and a predictable behavior with explicit row IDs, pass `disableRowSelectionExcludeModel` prop to the Data Grid.

```tsx
<DataGrid
disableRowSelectionExcludeModel
// This ensures the selection model always uses type: 'include'
// with explicit row IDs, even when selecting all rows
/>
```

## Checkbox selection

To activate checkbox selection set `checkboxSelection={true}`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ You have to import it from `@mui/x-license` instead:
rowSelectionManager.has(rowId);
```

**Opting out of exclude model optimization**

If you prefer the previous behavior where the selection model always contains explicit row IDs (even when selecting all rows), you can use the new `disableRowSelectionExcludeModel` prop:

```tsx
<DataGrid
disableRowSelectionExcludeModel
// This disables the exclude model optimization and ensures
// the selection model always uses type: 'include' with explicit row IDs
/>
```

- The `selectedIdsLookupSelector` selector has been removed. Use the `gridRowSelectionManagerSelector` or `gridRowSelectionStateSelector` selectors instead.
- ✅ The `selectedGridRowsSelector` has been renamed to `gridRowSelectionIdsSelector`.
- ✅ The `selectedGridRowsCountSelector` has been renamed to `gridRowSelectionCountSelector`.
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
},
"disablePivoting": { "type": { "name": "bool" }, "default": "false" },
"disableRowGrouping": { "type": { "name": "bool" }, "default": "false" },
"disableRowSelectionExcludeModel": { "type": { "name": "bool" }, "default": "false" },
"disableRowSelectionOnClick": { "type": { "name": "bool" }, "default": "false" },
"disableVirtualization": { "type": { "name": "bool" }, "default": "false" },
"editMode": {
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"type": { "name": "bool" },
"default": "false (`!props.checkboxSelection` for MIT Data Grid)"
},
"disableRowSelectionExcludeModel": { "type": { "name": "bool" }, "default": "false" },
"disableRowSelectionOnClick": { "type": { "name": "bool" }, "default": "false" },
"disableVirtualization": { "type": { "name": "bool" }, "default": "false" },
"editMode": {
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"type": { "name": "bool" },
"default": "false (`!props.checkboxSelection` for MIT Data Grid)"
},
"disableRowSelectionExcludeModel": { "type": { "name": "bool" }, "default": "false" },
"disableRowSelectionOnClick": { "type": { "name": "bool" }, "default": "false" },
"disableVirtualization": { "type": { "name": "bool" }, "default": "false" },
"editMode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
},
"disablePivoting": { "description": "If <code>true</code>, the pivoting feature is disabled." },
"disableRowGrouping": { "description": "If <code>true</code>, the row grouping is disabled." },
"disableRowSelectionExcludeModel": {
"description": "If <code>true</code>, the Data Grid will not use the exclude model optimization when selecting all rows."
},
"disableRowSelectionOnClick": {
"description": "If <code>true</code>, the selection on click on a row or cell is disabled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
"disableMultipleRowSelection": {
"description": "If <code>true</code>, multiple selection using the Ctrl/CMD or Shift key is disabled. The MIT DataGrid will ignore this prop, unless <code>checkboxSelection</code> is enabled."
},
"disableRowSelectionExcludeModel": {
"description": "If <code>true</code>, the Data Grid will not use the exclude model optimization when selecting all rows."
},
"disableRowSelectionOnClick": {
"description": "If <code>true</code>, the selection on click on a row or cell is disabled."
},
Expand Down
3 changes: 3 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
"disableMultipleRowSelection": {
"description": "If <code>true</code>, multiple selection using the Ctrl/CMD or Shift key is disabled. The MIT DataGrid will ignore this prop, unless <code>checkboxSelection</code> is enabled."
},
"disableRowSelectionExcludeModel": {
"description": "If <code>true</code>, the Data Grid will not use the exclude model optimization when selecting all rows."
},
"disableRowSelectionOnClick": {
"description": "If <code>true</code>, the selection on click on a row or cell is disabled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ DataGridPremiumRaw.propTypes = {
* @default false
*/
disableRowGrouping: PropTypes.bool,
/**
* If `true`, the Data Grid will not use the exclude model optimization when selecting all rows.
* @default false
*/
disableRowSelectionExcludeModel: PropTypes.bool,
/**
* If `true`, the selection on click on a row or cell is disabled.
* @default false
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ DataGridProRaw.propTypes = {
* @default false (`!props.checkboxSelection` for MIT Data Grid)
*/
disableMultipleRowSelection: PropTypes.bool,
/**
* If `true`, the Data Grid will not use the exclude model optimization when selecting all rows.
* @default false
*/
disableRowSelectionExcludeModel: PropTypes.bool,
/**
* If `true`, the selection on click on a row or cell is disabled.
* @default false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1355,4 +1355,27 @@ describe('<DataGridPro /> - Row selection', () => {
});
});
});

describe('prop: disableRowSelectionExcludeModel', () => {
it('should work with disableRowSelectionExcludeModel=true in DataGridPro', async () => {
const onRowSelectionModelChange = spy();
const { user } = render(
<TestDataGridSelection
checkboxSelection
disableRowSelectionExcludeModel
onRowSelectionModelChange={onRowSelectionModelChange}
/>,
);

// Click "Select all" checkbox
const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' });
await user.click(selectAllCheckbox);

expect(onRowSelectionModelChange.callCount).to.equal(1);
const selectionModel = onRowSelectionModelChange.lastCall.args[0];
// With disableRowSelectionExcludeModel=true, it should use include model with all IDs
expect(selectionModel.type).to.equal('include');
expect(selectionModel.ids.size).to.equal(4);
});
});
});
5 changes: 5 additions & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ DataGridRaw.propTypes = {
* @default false (`!props.checkboxSelection` for MIT Data Grid)
*/
disableMultipleRowSelection: PropTypes.bool,
/**
* If `true`, the Data Grid will not use the exclude model optimization when selecting all rows.
* @default false
*/
disableRowSelectionExcludeModel: PropTypes.bool,
/**
* If `true`, the selection on click on a row or cell is disabled.
* @default false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DATA_GRID_PROPS_DEFAULT_VALUES: DataGridPropsWithDefaultValues = {
disableMultipleColumnsSorting: false,
disableMultipleRowSelection: false,
disableRowSelectionOnClick: false,
disableRowSelectionExcludeModel: false,
disableVirtualization: false,
editMode: GridEditModes.Cell,
filterDebounceMs: 150,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const useGridRowSelection = (
| 'onRowSelectionModelChange'
| 'disableMultipleRowSelection'
| 'disableRowSelectionOnClick'
| 'disableRowSelectionExcludeModel'
| 'isRowSelectable'
| 'checkboxSelectionVisibleOnly'
| 'pagination'
Expand Down Expand Up @@ -729,7 +730,8 @@ export const useGridRowSelection = (
!props.isRowSelectable &&
!props.checkboxSelectionVisibleOnly &&
(!isNestedData || props.rowSelectionPropagation?.descendants) &&
!hasFilters
!hasFilters &&
!props.disableRowSelectionExcludeModel
) {
apiRef.current.setRowSelectionModel(
{
Expand All @@ -748,6 +750,7 @@ export const useGridRowSelection = (
props.checkboxSelectionVisibleOnly,
props.isRowSelectable,
props.rowSelectionPropagation?.descendants,
props.disableRowSelectionExcludeModel,
isNestedData,
],
);
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid/src/models/props/DataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ export interface DataGridPropsWithDefaultValues<R extends GridValidRowModel = an
* @default false
*/
disableRowSelectionOnClick: boolean;
/**
* If `true`, the Data Grid will not use the exclude model optimization when selecting all rows.
* @default false
*/
disableRowSelectionExcludeModel: boolean;
/**
* If `true`, the virtualization is disabled.
* @default false
Expand Down
44 changes: 44 additions & 0 deletions packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,50 @@ describe('<DataGrid /> - Row selection', () => {
});
});

describe('prop: disableRowSelectionExcludeModel', () => {
it('should use include model when disableRowSelectionExcludeModel is true', async () => {
const onRowSelectionModelChange = spy();
const { user } = render(
<TestDataGridSelection
checkboxSelection
disableRowSelectionExcludeModel
onRowSelectionModelChange={onRowSelectionModelChange}
/>,
);

// Click "Select all" checkbox
const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' });
await user.click(selectAllCheckbox);

expect(onRowSelectionModelChange.callCount).to.equal(1);
const selectionModel = onRowSelectionModelChange.lastCall.args[0];
// With disableRowSelectionExcludeModel=true, it should use include model with all IDs
expect(selectionModel.type).to.equal('include');
expect(selectionModel.ids.size).to.equal(4); // 4 rows in defaultData
expect(Array.from(selectionModel.ids)).to.deep.equal([0, 1, 2, 3]);
});

it('should use exclude model by default when conditions are met', async () => {
const onRowSelectionModelChange = spy();
const { user } = render(
<TestDataGridSelection
checkboxSelection
onRowSelectionModelChange={onRowSelectionModelChange}
/>,
);

// Click "Select all" checkbox
const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' });
await user.click(selectAllCheckbox);

expect(onRowSelectionModelChange.callCount).to.equal(1);
const selectionModel = onRowSelectionModelChange.lastCall.args[0];
// By default (disableRowSelectionExcludeModel=false), it should use exclude model with empty IDs
expect(selectionModel.type).to.equal('exclude');
expect(selectionModel.ids.size).to.equal(0);
});
});

describe('performance', () => {
it('should not rerender unrelated nodes', async () => {
// Couldn't use <RenderCounter> because we need to track multiple components
Expand Down
Loading