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')