row.getIsSomeSelected returning true for row with disabled subrows #5173
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.
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:
- The row is disabled
- 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#
Steps to reproduce#
- Instantiate a table with a subrow containing only disabled entries
- Check the result of row.getIsSomeSelected for the parent row
- 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.
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; },@lokalise-mark I believe what you are seeing is a bug due to the implementation of
isSubRowSelectedparticularly for its use ingetIsAllSubRowsSelectedThe simplest demonstration is this:
- [ ] selectableParentRow - non-selectableChild1 - non-selectableChild2In this state,
selectableParentRow.getIsAllSubRowsSelectedwill return true because its invocation ofisSubRowSelectedreturns"all". IMO that is incorrect.The bug within
getIsAllSubRowsSelectedis 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
Here's a simple test we can add to
packages/table-core/tests/RowSelection.test.tsthat will failit('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); })KevinVandy closed this
Sign in to join this conversation.
I'm also encountering this issue. If all subRows of a row are not selectable, the parent row is marked as indeterminate.