Skip to content

Commit

Permalink
[DataGrid] Fix checkboxSelectionVisibleOnly behavior with server-si…
Browse files Browse the repository at this point in the history
…de pagination (#14083)
  • Loading branch information
MBilalShafi authored Aug 7, 2024
1 parent 36ec2d7 commit 883d1f8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,49 @@ describe('<DataGridPro /> - Row selection', () => {
});
expect(selectAllCheckbox).to.have.attr('data-indeterminate', 'false');
});

it('should allow to select all the current page rows when props.paginationMode="server"', () => {
function TestDataGridSelectionServerSide({
rowLength = 4,
}: Omit<DataGridProProps, 'rows' | 'columns' | 'apiRef'> &
Partial<Pick<DataGridProProps, 'rows' | 'columns'>> & { rowLength?: number }) {
apiRef = useGridApiRef();
const paginationModel = { pageSize: 2, page: 1 };

const data = React.useMemo(() => getBasicGridData(rowLength, 2), [rowLength]);

const rows = data.rows.slice(
paginationModel.pageSize * paginationModel.page,
paginationModel.pageSize * (paginationModel.page + 1),
);

return (
<div style={{ width: 300, height: 300 }}>
<DataGridPro
{...data}
rows={rows}
checkboxSelection
checkboxSelectionVisibleOnly
initialState={{ pagination: { paginationModel } }}
pagination
paginationMode="server"
pageSizeOptions={[2]}
apiRef={apiRef}
rowCount={rowLength}
disableVirtualization
/>
</div>
);
}
render(<TestDataGridSelectionServerSide />);

const selectAllCheckbox = screen.getByRole('checkbox', {
name: /select all rows/i,
});

fireEvent.click(selectAllCheckbox);
expect(apiRef.current.getSelectedRows()).to.have.length(2);
});
});

describe('apiRef: getSelectedRows', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,15 @@ export const useGridRowSelection = (
GridEventListener<'headerSelectionCheckboxChange'>
>(
(params) => {
const shouldLimitSelectionToCurrentPage =
props.checkboxSelectionVisibleOnly && props.pagination;

const rowsToBeSelected = shouldLimitSelectionToCurrentPage
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef)
: gridExpandedSortedRowIdsSelector(apiRef);
const rowsToBeSelected =
props.pagination && props.checkboxSelectionVisibleOnly && props.paginationMode === 'client'
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef)
: gridExpandedSortedRowIdsSelector(apiRef);

const filterModel = gridFilterModelSelector(apiRef);
apiRef.current.selectRows(rowsToBeSelected, params.value, filterModel?.items.length > 0);
},
[apiRef, props.checkboxSelectionVisibleOnly, props.pagination],
[apiRef, props.checkboxSelectionVisibleOnly, props.pagination, props.paginationMode],
);

const handleCellKeyDown = React.useCallback<GridEventListener<'cellKeyDown'>>(
Expand Down

0 comments on commit 883d1f8

Please sign in to comment.