Skip to content

Commit

Permalink
fix: conditional selection getIsAllPageRowsSelected filter issue (#4761)
Browse files Browse the repository at this point in the history
* fixes selection logic for getIsAllPageRowsSelected

* Only consider rows that pass row.getCanSelect()
  • Loading branch information
renomateo committed Mar 26, 2023
1 parent d8c54a4 commit 4674e99
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
72 changes: 72 additions & 0 deletions packages/react-table/__tests__/features/RowSelection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ const defaultColumns: ColumnDef<Person>[] = [
accessorKey: 'firstName',
},
]
const defaultPaginatedColumns: ColumnDef<Person>[] = [
{
id: 'select',
header: ({ table }) => {
return (
<input
data-testid="select-all-page"
aria-checked={table.getIsSomePageRowsSelected() ? 'mixed' : undefined}
type="checkbox"
disabled
checked={table.getIsAllPageRowsSelected()}
onChange={table.getToggleAllRowsSelectedHandler()}
/>
)
},
cell: ({ row }) => {
return ( row.getCanSelect() ? (
<input
data-testid="select-single"
type="checkbox"
disabled={row.getCanSelect()}
checked={row.getIsSelected()}
onChange={row.getToggleSelectedHandler()}
/>
):null)
},
},
{
header: 'First Name',
accessorKey: 'firstName',
},
]

const TableComponent: FC<{ options?: Partial<TableOptions<Person>> }> = ({
options = {},
Expand Down Expand Up @@ -134,6 +166,46 @@ test(`Select all do not select rows which are not available for selection`, () =
expect(title).not.toBePartiallyChecked()
expect(notSelected).not.toBeChecked()
expect(selected).not.toBeChecked()

})

// issue #4757
test(`Select all is unchecked for current page if all rows are not available for selection`, () => {
let condition = row => row.original.age > 50;

const {rerender} = render(
<TableComponent
options={{
columns: defaultPaginatedColumns,
data: defaultData,
enableRowSelection: condition
}}
/>
)

expect(screen.queryByTestId('select-single')).not.toBeInTheDocument()
let selectedOnPage = screen.getByTestId('select-all-page')
expect(selectedOnPage).not.toBeChecked()
expect(selectedOnPage).not.toHaveAttribute('aria-checked', 'mixed')

condition = row => row.original.age > 40;
rerender(<TableComponent
options={{
columns: defaultPaginatedColumns,
data: defaultData,
enableRowSelection: condition
}}
/>
)

expect(screen.queryByTestId('select-single')).toBeInTheDocument()
selectedOnPage = screen.getByTestId('select-all-page')
expect(selectedOnPage).not.toBeChecked()
expect(selectedOnPage).not.toHaveAttribute('aria-checked', 'mixed')

fireEvent.click(screen.queryByTestId('select-single'))
expect(selectedOnPage).toBeChecked()

})

test(`Select all when all rows are available for selection`, () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/table-core/src/features/RowSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ export const RowSelection: TableFeature = {
},

getIsAllPageRowsSelected: () => {
const paginationFlatRows = table.getPaginationRowModel().flatRows
const paginationFlatRows = table.getPaginationRowModel().flatRows.filter(row => row.getCanSelect())
const { rowSelection } = table.getState()

let isAllPageRowsSelected = !!paginationFlatRows.length

if (
isAllPageRowsSelected &&
paginationFlatRows.some(
row => row.getCanSelect() && !rowSelection[row.id]
row => !rowSelection[row.id]
)
) {
isAllPageRowsSelected = false
Expand All @@ -319,7 +319,7 @@ export const RowSelection: TableFeature = {
const paginationFlatRows = table.getPaginationRowModel().flatRows
return table.getIsAllPageRowsSelected()
? false
: paginationFlatRows.some(
: paginationFlatRows.filter(row => row.getCanSelect()).some(
d => d.getIsSelected() || d.getIsSomeSelected()
)
},
Expand Down

0 comments on commit 4674e99

Please sign in to comment.