From ae70fa69041b23f8311c554ff11e2e058a15c52a Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Sat, 3 Aug 2024 16:29:50 +0500 Subject: [PATCH 1/3] [DataGrid] Fix checkboxSelectionVisibleOnly behavior with server-side pagination --- .../tests/rowSelection.DataGridPro.test.tsx | 43 +++++++++++++++++++ .../rowSelection/useGridRowSelection.ts | 9 ++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx index 3c5eda242358..8d0af590260d 100644 --- a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx @@ -210,6 +210,49 @@ describe(' - 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 & + Partial> & { 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 ( +
+ +
+ ); + } + render(); + + const selectAllCheckbox = screen.getByRole('checkbox', { + name: /select all rows/i, + }); + + fireEvent.click(selectAllCheckbox); + expect(apiRef.current.getSelectedRows()).to.have.length(2); + }); }); describe('apiRef: getSelectedRows', () => { diff --git a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts index 2888ec5929a1..11263932168e 100644 --- a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts +++ b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts @@ -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' + ? 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>( From 4ea19363f27612bc15eff9f685f3a036677b0b46 Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Tue, 6 Aug 2024 20:19:19 +0500 Subject: [PATCH 2/3] Update condition --- .../hooks/features/rowSelection/useGridRowSelection.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts index 11263932168e..8b1b711a7e66 100644 --- a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts +++ b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts @@ -56,9 +56,7 @@ export const rowSelectionStateInitializer: GridStateInitializer< Pick > = (state, props) => ({ ...state, - rowSelection: props.rowSelection - ? (getSelectionModelPropValue(props.rowSelectionModel) ?? []) - : [], + rowSelection: props.rowSelection ? getSelectionModelPropValue(props.rowSelectionModel) ?? [] : [], }); /** @@ -443,11 +441,8 @@ export const useGridRowSelection = ( GridEventListener<'headerSelectionCheckboxChange'> >( (params) => { - const shouldLimitSelectionToCurrentPage = - props.checkboxSelectionVisibleOnly && props.pagination; - const rowsToBeSelected = - shouldLimitSelectionToCurrentPage && props.paginationMode === 'client' + props.pagination && props.checkboxSelectionVisibleOnly && props.paginationMode === 'client' ? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef) : gridExpandedSortedRowIdsSelector(apiRef); From d2f9abfa7f62d6fb94fb443ff530a4a73aff25c3 Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Wed, 7 Aug 2024 14:18:51 +0500 Subject: [PATCH 3/3] prettier --- .../src/hooks/features/rowSelection/useGridRowSelection.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts index 8b1b711a7e66..fe7495699b33 100644 --- a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts +++ b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts @@ -56,7 +56,9 @@ export const rowSelectionStateInitializer: GridStateInitializer< Pick > = (state, props) => ({ ...state, - rowSelection: props.rowSelection ? getSelectionModelPropValue(props.rowSelectionModel) ?? [] : [], + rowSelection: props.rowSelection + ? (getSelectionModelPropValue(props.rowSelectionModel) ?? []) + : [], }); /**