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

[DataGrid] Emit onSelectionModelChange when selection state changes #1966

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -207,6 +207,9 @@ export const useGridSelection = (apiRef: GridApiRef): void => {
}
});
if (hasChanged) {
apiRef.current.publishEvent(GRID_SELECTION_CHANGE, {
selectionModel: Object.values(newSelectionState),
});
return { ...state, selection: newSelectionState };
}
return state;
Expand All @@ -233,6 +236,9 @@ export const useGridSelection = (apiRef: GridApiRef): void => {
}
});
if (hasChanged) {
apiRef.current.publishEvent(GRID_SELECTION_CHANGE, {
selectionModel: Object.values(newSelectionState),
});
return { ...state, selection: newSelectionState };
}
return state;
Expand Down
16 changes: 16 additions & 0 deletions packages/grid/x-grid/src/tests/selection.XGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ describe('<XGrid /> - Selection', () => {
expect(getSelectedRows(apiRef)).to.deep.equal([0]);
});

it('should call onSelectionModelChange when selection state changes', () => {
const onSelectionModelChange = spy();
DanailH marked this conversation as resolved.
Show resolved Hide resolved
const { setProps } = render(
<Test selectionModel={[0]} onSelectionModelChange={onSelectionModelChange} />,
);
setProps({
rows: [
{
id: 1,
brand: 'Nike',
},
],
});
expect(onSelectionModelChange.callCount).to.equal(2);
DanailH marked this conversation as resolved.
Show resolved Hide resolved
});

it('should select only filtered rows after filter is applied', () => {
render(<Test checkboxSelection />);
const selectAll = screen.getByRole('checkbox', {
Expand Down