Skip to content

Commit 95e5c79

Browse files
[DataGrid] Add reason param for onRowSelectionModelChange callback (#17545)
Signed-off-by: sai chand <[email protected]> Co-authored-by: Olivier Tassinari <[email protected]>
1 parent e2dceb2 commit 95e5c79

File tree

6 files changed

+50
-13
lines changed

6 files changed

+50
-13
lines changed

docs/pages/x/api/data-grid/grid-api.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@
425425
},
426426
"setRows": { "type": { "description": "(rows: GridRowModel[]) =&gt; void" }, "required": true },
427427
"setRowSelectionModel": {
428-
"type": { "description": "(rowSelectionModel: GridRowSelectionModel) =&gt; void" },
428+
"type": {
429+
"description": "(rowSelectionModel: GridRowSelectionModel, reason?: GridControlledStateReasonLookup['rowSelection']) =&gt; void"
430+
},
429431
"required": true
430432
},
431433
"setSortModel": {

docs/pages/x/api/data-grid/grid-row-selection-api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
{
2626
"name": "setRowSelectionModel",
2727
"description": "Sets the new row selection model.<br>⚠️ Caution: <code>setRowSelectionModel</code> doesn&#39;t apply the selection propagation automatically. Pass model returned by API method <code>getPropagatedRowSelectionModel</code> instead to apply the selection propagation.",
28-
"type": "(rowSelectionModel: GridRowSelectionModel) => void"
28+
"type": "(rowSelectionModel: GridRowSelectionModel, reason?: GridControlledStateReasonLookup['rowSelection']) => void"
2929
}
3030
]
3131
}

packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const useGridRowSelection = (
160160
* API METHODS
161161
*/
162162
const setRowSelectionModel = React.useCallback<GridRowSelectionApi['setRowSelectionModel']>(
163-
(model) => {
163+
(model, reason) => {
164164
if (
165165
props.signature === GridSignature.DataGrid &&
166166
!canHaveMultipleSelection &&
@@ -176,10 +176,13 @@ export const useGridRowSelection = (
176176
const currentModel = gridRowSelectionStateSelector(apiRef);
177177
if (currentModel !== model) {
178178
logger.debug(`Setting selection model`);
179-
apiRef.current.setState((state) => ({
180-
...state,
181-
rowSelection: props.rowSelection ? model : emptyModel,
182-
}));
179+
apiRef.current.setState(
180+
(state) => ({
181+
...state,
182+
rowSelection: props.rowSelection ? model : emptyModel,
183+
}),
184+
reason,
185+
);
183186
}
184187
},
185188
[apiRef, logger, props.rowSelection, props.signature, canHaveMultipleSelection],
@@ -250,7 +253,7 @@ export const useGridRowSelection = (
250253
}
251254
}
252255

253-
apiRef.current.setRowSelectionModel(newSelectionModel);
256+
apiRef.current.setRowSelectionModel(newSelectionModel, 'singleRowSelection');
254257
} else {
255258
logger.debug(`Toggling selection for row ${id}`);
256259

@@ -297,7 +300,7 @@ export const useGridRowSelection = (
297300
(newSelectionModel.type === 'include' && newSelectionModel.ids.size < 2) ||
298301
canHaveMultipleSelection;
299302
if (isSelectionValid) {
300-
apiRef.current.setRowSelectionModel(newSelectionModel);
303+
apiRef.current.setRowSelectionModel(newSelectionModel, 'singleRowSelection');
301304
}
302305
}
303306
},
@@ -406,7 +409,7 @@ export const useGridRowSelection = (
406409
(newSelectionModel.type === 'include' && newSelectionModel.ids.size < 2) ||
407410
canHaveMultipleSelection;
408411
if (isSelectionValid) {
409-
apiRef.current.setRowSelectionModel(newSelectionModel);
412+
apiRef.current.setRowSelectionModel(newSelectionModel, 'multipleRowsSelection');
410413
}
411414
},
412415
[
@@ -601,7 +604,7 @@ export const useGridRowSelection = (
601604
apiRef.current.selectRows(Array.from(newSelectionModel.ids), true, true);
602605
}
603606
} else {
604-
apiRef.current.setRowSelectionModel(newSelectionModel);
607+
apiRef.current.setRowSelectionModel(newSelectionModel, 'multipleRowsSelection');
605608
}
606609
}
607610
},

packages/x-data-grid/src/models/api/gridRowSelectionApi.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { GridRowId, GridRowModel } from '../gridRows';
22
import type { GridRowSelectionModel } from '../gridRowSelectionModel';
3+
import { GridControlledStateReasonLookup } from '../events';
34

45
/**
56
* The selection API interface that is available in the grid [[apiRef]].
@@ -35,9 +36,13 @@ export interface GridRowSelectionApi {
3536
*
3637
* ⚠️ Caution: `setRowSelectionModel` doesn't apply the selection propagation automatically.
3738
* Pass model returned by API method `getPropagatedRowSelectionModel` instead to apply the selection propagation.
38-
* @param {gridRowSelectionModel} rowSelectionModel The new row selection model
39+
* @param {gridRowSelectionModel} rowSelectionModel The new row selection model.
40+
* @param {string} reason The reason for the state change.
3941
*/
40-
setRowSelectionModel: (rowSelectionModel: GridRowSelectionModel) => void;
42+
setRowSelectionModel: (
43+
rowSelectionModel: GridRowSelectionModel,
44+
reason?: GridControlledStateReasonLookup['rowSelection'],
45+
) => void;
4146
}
4247

4348
export interface GridRowMultiSelectionApi {

packages/x-data-grid/src/models/events/gridEventLookup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ export interface GridControlledStateReasonLookup {
390390
| 'removeAllFilterItems';
391391
pagination: 'setPaginationModel' | 'stateRestorePreProcessing';
392392
rows: 'addSkeletonRows';
393+
rowSelection: 'singleRowSelection' | 'multipleRowsSelection';
393394
}
394395

395396
export interface GridEventLookup

packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,32 @@ describe('<DataGrid /> - Row selection', () => {
833833
expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal(includeRowSelection([]));
834834
});
835835

836+
it('should call onRowSelectionModelChange with the correct reason when clicking on a row', async () => {
837+
const onRowSelectionModelChange = spy();
838+
const { user } = render(
839+
<TestDataGridSelection
840+
checkboxSelection
841+
pagination
842+
onRowSelectionModelChange={onRowSelectionModelChange}
843+
/>,
844+
);
845+
await user.click(getCell(0, 0).querySelector('input')!);
846+
expect(onRowSelectionModelChange.lastCall.args[1].reason).to.equal('singleRowSelection');
847+
});
848+
849+
it('should call onRowSelectionModelChange with the correct reason when clicking on the header checkbox', async () => {
850+
const onRowSelectionModelChange = spy();
851+
const { user } = render(
852+
<TestDataGridSelection
853+
checkboxSelection
854+
pagination
855+
onRowSelectionModelChange={onRowSelectionModelChange}
856+
/>,
857+
);
858+
await user.click(getColumnHeaderCell(0).querySelector('input')!);
859+
expect(onRowSelectionModelChange.lastCall.args[1].reason).to.equal('multipleRowsSelection');
860+
});
861+
836862
it('should call onRowSelectionModelChange with an empty array if there is no selected row in the current page when turning off checkboxSelection', async () => {
837863
const onRowSelectionModelChange = spy();
838864
const { setProps, user } = render(

0 commit comments

Comments
 (0)