Skip to content

Cannot toggle selection of grouped row to deselect its children #4759

Description

@mollykreis

Describe the bug

I have a table with grouping and selection enabled. I would like to achieve the following behavior:

  • Non-grouped rows are selectable; selection is toggled via a checkbox on the row
  • Grouped rows have selection checkboxes, but they don't have their own selection state
    • the checkbox is checked if all sub rows are checked
    • the checkbox is not checked is all subrows are unchecked
    • the checkbox is indeterminate if some sub rows are checked
    • interactively toggling the checkbox manually selects/deselects all sub rows

To get this behavior, I've configured my table as follows:

  • enableRowSelection: function that returns true for non-grouped rows and false for grouped rows
  • enableMultiRowSelection: true
  • enableSubRowSelection: true
  • state of the checkbox for grouped rows is updated programmatically using row.getIsAllSubRowsSelected() and row.getIsSomeSelected()
  • interaction with checkbox for rows (both grouped and non-grouped) calls row.toggleSelected(newCheckboxState)

Problem:
Interactively trying to uncheck a grouped row's checkbox does nothing when it should deselect all the rows beneath it. The problem is that the grouped row will always return false from row.getIsSelected(). Calling toggleSelection(false) on this grouped row, which should deselect all the sub rows, returns early because it detects that the row is already not selected so there is nothing to be done to deselect it.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/crimson-river-2ysx73

Steps to reproduce

In the code sandbox:

  1. Expand a group
  2. Click the selection checkbox for the expanded group to mark it as selected
  3. See that all the subrows were selected (correct)
  4. Click the selection checkbox for the expanded group again to mark it as not selected
  5. Notice that nothing happened. The group and subrows remained selected when the desired behavior was for the group and subrows to all become not selected.

Expected behavior

I expected to create a grouped row whose selection is based only on the selection of its subrows. Clicking the checkbox associated with that grouped row toggles the selection for all of its subrows.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Not platform specific

react-table version

8.7.9

TypeScript version

No response

Additional context

As a note, I also went down an implementation path where I had enableRowSelection set to true for both grouped rows and non-grouped rows. However, this also did not lead to the behavior I wanted. The primary problem I encountered was that I didn't want a group's selection state to be part of TanStack's row selection state because the group's selection state is always determined programmatically by the state of its subrows.

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions