Skip to content

Commit 4674e99

Browse files
authored
fix: conditional selection getIsAllPageRowsSelected filter issue (#4761)
* fixes selection logic for getIsAllPageRowsSelected * Only consider rows that pass row.getCanSelect()
1 parent d8c54a4 commit 4674e99

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

packages/react-table/__tests__/features/RowSelection.test.tsx

+72
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ const defaultColumns: ColumnDef<Person>[] = [
6666
accessorKey: 'firstName',
6767
},
6868
]
69+
const defaultPaginatedColumns: ColumnDef<Person>[] = [
70+
{
71+
id: 'select',
72+
header: ({ table }) => {
73+
return (
74+
<input
75+
data-testid="select-all-page"
76+
aria-checked={table.getIsSomePageRowsSelected() ? 'mixed' : undefined}
77+
type="checkbox"
78+
disabled
79+
checked={table.getIsAllPageRowsSelected()}
80+
onChange={table.getToggleAllRowsSelectedHandler()}
81+
/>
82+
)
83+
},
84+
cell: ({ row }) => {
85+
return ( row.getCanSelect() ? (
86+
<input
87+
data-testid="select-single"
88+
type="checkbox"
89+
disabled={row.getCanSelect()}
90+
checked={row.getIsSelected()}
91+
onChange={row.getToggleSelectedHandler()}
92+
/>
93+
):null)
94+
},
95+
},
96+
{
97+
header: 'First Name',
98+
accessorKey: 'firstName',
99+
},
100+
]
69101

70102
const TableComponent: FC<{ options?: Partial<TableOptions<Person>> }> = ({
71103
options = {},
@@ -134,6 +166,46 @@ test(`Select all do not select rows which are not available for selection`, () =
134166
expect(title).not.toBePartiallyChecked()
135167
expect(notSelected).not.toBeChecked()
136168
expect(selected).not.toBeChecked()
169+
170+
})
171+
172+
// issue #4757
173+
test(`Select all is unchecked for current page if all rows are not available for selection`, () => {
174+
let condition = row => row.original.age > 50;
175+
176+
const {rerender} = render(
177+
<TableComponent
178+
options={{
179+
columns: defaultPaginatedColumns,
180+
data: defaultData,
181+
enableRowSelection: condition
182+
}}
183+
/>
184+
)
185+
186+
expect(screen.queryByTestId('select-single')).not.toBeInTheDocument()
187+
let selectedOnPage = screen.getByTestId('select-all-page')
188+
expect(selectedOnPage).not.toBeChecked()
189+
expect(selectedOnPage).not.toHaveAttribute('aria-checked', 'mixed')
190+
191+
condition = row => row.original.age > 40;
192+
rerender(<TableComponent
193+
options={{
194+
columns: defaultPaginatedColumns,
195+
data: defaultData,
196+
enableRowSelection: condition
197+
}}
198+
/>
199+
)
200+
201+
expect(screen.queryByTestId('select-single')).toBeInTheDocument()
202+
selectedOnPage = screen.getByTestId('select-all-page')
203+
expect(selectedOnPage).not.toBeChecked()
204+
expect(selectedOnPage).not.toHaveAttribute('aria-checked', 'mixed')
205+
206+
fireEvent.click(screen.queryByTestId('select-single'))
207+
expect(selectedOnPage).toBeChecked()
208+
137209
})
138210

139211
test(`Select all when all rows are available for selection`, () => {

packages/table-core/src/features/RowSelection.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,15 @@ export const RowSelection: TableFeature = {
288288
},
289289

290290
getIsAllPageRowsSelected: () => {
291-
const paginationFlatRows = table.getPaginationRowModel().flatRows
291+
const paginationFlatRows = table.getPaginationRowModel().flatRows.filter(row => row.getCanSelect())
292292
const { rowSelection } = table.getState()
293293

294294
let isAllPageRowsSelected = !!paginationFlatRows.length
295295

296296
if (
297297
isAllPageRowsSelected &&
298298
paginationFlatRows.some(
299-
row => row.getCanSelect() && !rowSelection[row.id]
299+
row => !rowSelection[row.id]
300300
)
301301
) {
302302
isAllPageRowsSelected = false
@@ -319,7 +319,7 @@ export const RowSelection: TableFeature = {
319319
const paginationFlatRows = table.getPaginationRowModel().flatRows
320320
return table.getIsAllPageRowsSelected()
321321
? false
322-
: paginationFlatRows.some(
322+
: paginationFlatRows.filter(row => row.getCanSelect()).some(
323323
d => d.getIsSelected() || d.getIsSomeSelected()
324324
)
325325
},

0 commit comments

Comments
 (0)