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] Fix checkboxSelectionVisibleOnly behavior with server-side pagination #14083

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 @@ -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 @@ -446,14 +446,15 @@ export const useGridRowSelection = (
const shouldLimitSelectionToCurrentPage =
props.checkboxSelectionVisibleOnly && props.pagination;

const rowsToBeSelected = shouldLimitSelectionToCurrentPage
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef)
: gridExpandedSortedRowIdsSelector(apiRef);
const rowsToBeSelected =
shouldLimitSelectionToCurrentPage && props.paginationMode === 'client'
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved
? 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
Loading