Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sub-row-selection-unselectable-subtrees.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TFeatures, TData>,
rowSelection: RowSelectionState,
): false | 'some' | 'all' | undefined {
let someSelected = false
let allChildrenSelected = true
let someSelectable = false
Expand All @@ -1039,24 +1054,26 @@ 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
} else if (subRowChildrenSelected === 'some') {
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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', () => {
Expand Down Expand Up @@ -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<typeof features, Person>({
features,
// grand-children can not be selected
enableRowSelection: (row: Row<typeof features, Person>) =>
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down