Skip to content
Source
Sign inSign up
TanStack/table

row.getIsSomeSelected returning true for row with disabled subrows #5173

Closedlokalise-mark(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe the bug

Problem

Hi, I'm running in to an issue where row.getIsSomeSelected is returning true despite no subrows being selected. The issue appears where you have a row where all subrows are disabled. Here is an example for clarity.

Screenshot 2023-11-23 at 16 03 59

Here, I am trying to use getIsSomeSelected to determine the indeterminate state of the parent checkbox (labelled Entry: Product Links). This row contains a subrow where all children are disabled (Featured pages).

Cause

Digging through the source-code, I believe that the issue stems from recent changes to the isSubRowSelected function. This was updated in this commit and released in v8.9.10. I have confirmed that reverting to version v8.9.9 fixes the issue.

row.getIsSomeSelected makes use of the isSubRowSelected function (here) to recursively check if any descendents are selected.

The problem is that the conditions that are supposed to set allChildrenSelected to false are not executed when:

  1. The row is disabled
  2. The row does no have further subrows

As in this case we have a disabled parent with four disabled children, neither condition can be triggered. This leads to allChildrenSelected remaining true throughout and isSubRowSelected then returning true itself.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/snowy-silence-mznd62?file=%2Fsrc%2Findex.tsx%3A152%2C19&layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522clpbktpo300073b6hm8x4qp8z%2522%252C%2522sizes%2522%253A%255B70%252C30%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522clpbktpo300033b6hnm6jbjw7%2522%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522clpbktpo300043b6haqq7ppek%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522clpbktpo300063b6hb49kmgk1%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B72.62965349408113%252C27.370346505918874%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522clpbktpo300033b6hnm6jbjw7%2522%253A%257B%2522id%2522%253A%2522clpbktpo300033b6hnm6jbjw7%2522%252C%2522activeTabId%2522%253A%2522clpbl6vgq00up3b6h9t4pi34d%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252Fsrc%252Findex.tsx%2522%252C%2522initialSelections%2522%253A%255B%257B%2522startLineNumber%2522%253A152%252C%2522startColumn%2522%253A19%252C%2522endLineNumber%2522%253A152%252C%2522endColumn%2522%253A19%257D%255D%252C%2522id%2522%253A%2522clpbl6vgq00up3b6h9t4pi34d%2522%252C%2522mode%2522%253A%2522temporary%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%255D%257D%252C%2522clpbktpo300063b6hb49kmgk1%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clpbktpo300053b6hc67gynaa%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A0%252C%2522path%2522%253A%2522%2522%257D%255D%252C%2522id%2522%253A%2522clpbktpo300063b6hb49kmgk1%2522%252C%2522activeTabId%2522%253A%2522clpbktpo300053b6hc67gynaa%2522%257D%252C%2522clpbktpo300043b6haqq7ppek%2522%253A%257B%2522tabs%2522%253A%255B%255D%252C%2522id%2522%253A%2522clpbktpo300043b6haqq7ppek%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Atrue%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D

Steps to reproduce

  1. Instantiate a table with a subrow containing only disabled entries
  2. Check the result of row.getIsSomeSelected for the parent row
  3. See that it returns true even when no subrows are selected/selectable

Expected behavior

row.getIsSomeSelected returns false

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

MacOS, Chrome

react-table version

8.10.7

TypeScript version

5.2.2

Additional context

No response

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.
  1. I'm also encountering this issue. If all subRows of a row are not selectable, the parent row is marked as indeterminate.

    Image

  2. for me, I had to disable the selection for the parent row in order to fix this.

    enableRowSelection: (row) => {
    		// if is the parent/expandable row
        if (row.getCanExpand()) {
    		// enable selection only if one or more subRow is selectable
          return (
            !row.original.disabled &&
            row?.subRows?.some((subRow) => !subRow.original.disabled)
          );
        }
    	// default check for subRows / non expandable rows
        return !row.original.disabled;
      },
  3. @lokalise-mark I believe what you are seeing is a bug due to the implementation of isSubRowSelected particularly for its use in getIsAllSubRowsSelected

    The simplest demonstration is this:

    - [ ] selectableParentRow
      -     non-selectableChild1
      -     non-selectableChild2

    In this state, selectableParentRow.getIsAllSubRowsSelected will return true because its invocation of isSubRowSelected returns "all". IMO that is incorrect.

    The bug within getIsAllSubRowsSelected is that it does not filter out non-selectable children, and instead it starts by assuming allChildrenSelected = true.

    So how does this impact your original report? The row titled "Featured pages" is now counted as "selected" when considered as a child of "Entry: Product Links". The line that's responsible: https://github.com/TanStack/table/blob/e172109fca4cc403a07236ed8fa103450ceba5e9/packages/table-core/src/features/RowSelection.ts#L666

  4. Here's a simple test we can add to packages/table-core/tests/RowSelection.test.ts that will fail

    it('should return false if there are only non-selectable sub-rows', () => {
      const data = makeData(1, 2)
      const columns = generateColumns(data)
    
      const table = createTable<Person>({
        enableRowSelection: (row) => row.depth === 0, // only root row is selectable, subrows are not
        onStateChange() {},
        renderFallbackValue: '',
        data,
        getSubRows: (row) => row.subRows,
        state: {
          rowSelection: {},  // initialize with nothing selected
        },
        columns,
        getCoreRowModel: getCoreRowModel(),
      })
    
      const firstRow = table.getCoreRowModel().rows[0]
    
      const result = RowSelection.isSubRowSelected(
        firstRow,
        table.getState().rowSelection,
        table,
      )
    
      expect(result).toEqual(false);
    })
  5. KevinVandy closed this

Sign in to join this conversation.