Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ export function isSubRowSelected<

let someSelected = false
let allChildrenSelected = true
let someSelectable = false

for (let i = 0; i < row.subRows.length; i++) {
const subRow = row.subRows[i]!
Expand All @@ -917,6 +918,7 @@ export function isSubRowSelected<
}

if (row_getCanSelect(subRow)) {
someSelectable = true
if (isRowSelected(subRow, rowSelection)) {
someSelected = true
} else {
Expand All @@ -929,14 +931,19 @@ export function isSubRowSelected<
const subRowChildrenSelected = isSubRowSelected(subRow)
if (subRowChildrenSelected === 'all') {
someSelected = true
someSelectable = true
} else if (subRowChildrenSelected === 'some') {
someSelected = true
allChildrenSelected = false
someSelectable = true
} else {
allChildrenSelected = false
}
}
}

// A row with no selectable descendants can never be in a selected state
if (!someSelectable) return false

return allChildrenSelected ? 'all' : someSelected ? 'some' : false
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,52 @@ describe('rowSelectionFeature', () => {
expect(result).toEqual(false)
})

it('should return false if no sub-rows are selectable', () => {
const data = generateTestData(3, 2)
const columns = generateColumnDefs(data)

const table = constructTable<typeof features, Person>({
features,
enableRowSelection: false,
renderFallbackValue: '',
data,
getSubRows: (originalRow: Person, _idx: number) => originalRow.subRows,
initialState: {
rowSelection: {},
},
columns,
})

const firstRow = table.getCoreRowModel().rows[0]!

const result = RowSelectionUtils.isSubRowSelected(firstRow)

expect(result).toEqual(false)
})

it('should return some if no children are selectable, but a grand-child is and is selected', () => {
const data = generateTestData(3, 2, 2)
const columns = generateColumnDefs(data)

const table = constructTable<typeof features, Person>({
features,
enableRowSelection: (row) => row.id === '0.0.1',
renderFallbackValue: '',
data,
getSubRows: (originalRow: Person, _idx: number) => originalRow.subRows,
initialState: {
rowSelection: { '0.0.1': true },
},
columns,
})

const firstRow = table.getCoreRowModel().rows[0]!

const result = RowSelectionUtils.isSubRowSelected(firstRow)

expect(result).toEqual('some')
})

it('should return some if some sub-rows are selected', () => {
const data = generateTestData(3, 2)
const columns = generateColumnDefs(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ describe('row_getIsSomeSelected / row_getIsAllSubRowsSelected', () => {
expect(row_getIsSomeSelected(row)).toBe(false)
expect(row_getIsAllSubRowsSelected(row)).toBe(false)
})

it('should report nothing when no sub-rows are selectable', () => {
const table = makeTable({ enableRowSelection: false }, [3, 2])
const parent = table.getRow('0')

expect(row_getIsSomeSelected(parent)).toBe(false)
expect(row_getIsAllSubRowsSelected(parent)).toBe(false)
})
})

describe('row selection flags', () => {
Expand Down
Loading