From 052ba98ac3689b9b5608b871a6a8a1f723f693cb Mon Sep 17 00:00:00 2001 From: h00ns <97011015+h00ns@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:05:17 +0900 Subject: [PATCH] fix(table-core): ignore unselectable subtrees in sub-row selection state `isSubRowSelected` returns `false` both when a row's selectable descendants are all unselected and when it has no selectable descendants at all. A parent treated both the same way, so a sub-row whose children are all unselectable marked the parent as "not all selected", even when every selectable descendant was selected. Track the "no selectable descendants" case separately in the recursion and skip those subtrees, so `row.getIsAllSubRowsSelected()` and `row.getIsSomeSelected()` only look at rows that can be selected. Co-Authored-By: Claude Opus 5.5 --- ...sub-row-selection-unselectable-subtrees.md | 5 +++ .../rowSelectionFeature.utils.ts | 25 ++++++++++++--- .../row-selection/rowSelectionFeature.test.ts | 31 +++++++++++++++++-- .../rowSelectionFeature.utils.test.ts | 15 +++++++++ 4 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 .changeset/sub-row-selection-unselectable-subtrees.md diff --git a/.changeset/sub-row-selection-unselectable-subtrees.md b/.changeset/sub-row-selection-unselectable-subtrees.md new file mode 100644 index 0000000000..8e34e7841d --- /dev/null +++ b/.changeset/sub-row-selection-unselectable-subtrees.md @@ -0,0 +1,5 @@ +--- +'@tanstack/table-core': patch +--- + +Ignore nested sub-rows that have no selectable descendants in `row.getIsAllSubRowsSelected()` and `row.getIsSomeSelected()`. A parent whose selectable descendants were all selected reported `getIsSomeSelected() === true` and `getIsAllSubRowsSelected() === false` when one of its sub-rows had only unselectable children. diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts index c90758f7b9..ad9deb37c0 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts @@ -1018,6 +1018,21 @@ export function isSubRowSelected< const rowSelection = row.table.atoms.rowSelection?.get() ?? {} + return getSubRowSelection(row, rowSelection) ?? false +} + +/** + * Same as `isSubRowSelected`, but returns `undefined` when the row has no + * selectable descendants, so a parent can ignore that subtree instead of + * counting it as unselected. + */ +function getSubRowSelection< + TFeatures extends TableFeatures, + TData extends RowData, +>( + row: Row, + rowSelection: RowSelectionState, +): false | 'some' | 'all' | undefined { let someSelected = false let allChildrenSelected = true let someSelectable = false @@ -1039,9 +1054,10 @@ export function isSubRowSelected< } } - // Check row selection of nested subrows + // Check row selection of nested subrows. A nested subtree without any + // selectable rows has nothing to select, so it is skipped. if (subRow.subRows.length) { - const subRowChildrenSelected = isSubRowSelected(subRow) + const subRowChildrenSelected = getSubRowSelection(subRow, rowSelection) if (subRowChildrenSelected === 'all') { someSelected = true someSelectable = true @@ -1049,14 +1065,15 @@ export function isSubRowSelected< someSelected = true allChildrenSelected = false someSelectable = true - } else { + } else if (subRowChildrenSelected === false) { allChildrenSelected = false + someSelectable = true } } } // A row with no selectable descendants can never be in a selected state - if (!someSelectable) return false + if (!someSelectable) return undefined return allChildrenSelected ? 'all' : someSelected ? 'some' : false } diff --git a/packages/table-core/tests/implementation/features/row-selection/rowSelectionFeature.test.ts b/packages/table-core/tests/implementation/features/row-selection/rowSelectionFeature.test.ts index 4bfadca254..68f6ceb94a 100644 --- a/packages/table-core/tests/implementation/features/row-selection/rowSelectionFeature.test.ts +++ b/packages/table-core/tests/implementation/features/row-selection/rowSelectionFeature.test.ts @@ -439,7 +439,7 @@ describe('rowSelectionFeature', () => { expect(result).toEqual(false) }) - it('should return some if no children are selectable, but a grand-child is and is selected', () => { + it('should return all if no children are selectable, but the only selectable grand-child is selected', () => { const data = generateTestData(3, 2, 2) const columns = generateColumnDefs(data) @@ -459,7 +459,7 @@ describe('rowSelectionFeature', () => { const result = RowSelectionUtils.isSubRowSelected(firstRow) - expect(result).toEqual('some') + expect(result).toEqual('all') }) it('should return some if some sub-rows are selected', () => { @@ -537,6 +537,33 @@ describe('rowSelectionFeature', () => { expect(result).toEqual('all') }) + it('should ignore nested sub-rows that have no selectable descendants', () => { + const data = generateTestData(3, 2, 2) + const columns = generateColumnDefs(data) + + const table = constructTable({ + features, + // grand-children can not be selected + enableRowSelection: (row: Row) => + row.depth < 2, + renderFallbackValue: '', + data, + getSubRows: (originalRow: Person, _idx: number) => originalRow.subRows, + initialState: { + rowSelection: { + '0.0': true, + '0.1': true, + }, + }, + columns, + }) + + const firstRow = table.getCoreRowModel().rows[0]! + + const result = RowSelectionUtils.isSubRowSelected(firstRow) + + expect(result).toEqual('all') + }) it('should return some when some nested sub-rows are selected', () => { const data = generateTestData(3, 2, 2) const columns = generateColumnDefs(data) diff --git a/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts b/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts index 162c08eb66..b132d35b0c 100644 --- a/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts +++ b/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts @@ -598,6 +598,21 @@ describe('row_getIsSomeSelected / row_getIsAllSubRowsSelected', () => { expect(row_getIsAllSubRowsSelected(row)).toBe(false) }) + it('should report a full selection when the only selectable descendants are selected', () => { + const table = makeTable( + { + // grand-children can not be selected + enableRowSelection: (row) => row.depth < 2, + initialState: { rowSelection: { '0.0': true, '0.1': true } }, + }, + [3, 2, 2], + ) + const parent = table.getRow('0') + + expect(row_getIsSomeSelected(parent)).toBe(false) + expect(row_getIsAllSubRowsSelected(parent)).toBe(true) + }) + it('should report nothing when no sub-rows are selectable', () => { const table = makeTable({ enableRowSelection: false }, [3, 2]) const parent = table.getRow('0')