fix: table-core's getIsAllSubRowsSelected returns false when subrows are unselectable #6177
main fromrefs/pull/6177/head·opened 🎯 Changes#
This PR fixes the root cause for https://github.com/TanStack/table/issues/5173.
The bug was in table-core's getIsAllSubRowsSelected which returned true when all sub-rows are unselectable.
This PR fixes the bug by re-implementing the method using counts: selectedCount == selectableCount && selectedCount > 0
The count is computed with a breadth-first-search traversal rather than the existing recursive approach.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm test:pr.NX Successfully ran targets test:sherif, test:knip, test:docs, test:lib, test:types, build for 99 projects and 1 task they depend on (2m)
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
Bug Fixes
- Row selection now correctly ignores unselectable sub-rows when reporting whether all or some sub-rows are selected.
New Features
- Added a counting helper to accurately report selectable and selected sub-rows across nested hierarchies.
Tests
- Expanded test coverage for selection counting and all/some selection behaviors, including nested scenarios and non-selectable sub-rows.
- 1
📝 Walkthrough
Walkthrough#
Refactors row-selection aggregation to count only selectable sub-rows via a new BFS helper (
getSubRowSelectionsCount), updates related getters (getIsSomeSelected,getIsAllSubRowsSelected,isSubRowSelected), adds tests covering selectable/non-selectable and nested sub-row scenarios, and adds a changeset patch bump for@tanstack/table-core.Changes#
Cohort / File(s) Summary Version & Changelog
./.changeset/cute-walls-judge.mdAdds a patch-level version bump and changelog entry noting a bugfix: row.getIsAllSubRowsSelectedshould return false when subrows are unselectable.Row Selection Implementation
packages/table-core/src/features/RowSelection.tsReplaces recursive aggregation with getSubRowSelectionsCount(BFS counting selectable vs selected sub-rows). UpdatesgetIsSomeSelected,getIsAllSubRowsSelected, andisSubRowSelectedto use the new counts; adjusts internal call sites and JSDoc wording to “selectable sub rows.”Row Selection Tests
packages/table-core/tests/RowSelection.test.tsAdds extensive tests for getSubRowSelectionsCount(selectableCount and selectedCount) and expands coverage forgetIsSomeSelectedandgetIsAllSubRowsSelectedacross no-subrows, non-selectable, mixed, and nested scenarios with various selection states.Estimated code review effort#
🎯 4 (Complex) | ⏱️ ~45 minutes
Poem#
🐰 I counted hops in rows below,
BFS footprints in tidy rows. Selectable friends I sort and see, No more false "all" — that's jubilee. A little rabbit's patch and glee. 🥕
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)#
Check name Status Explanation Resolution Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold. ✏️ Tip: You can configure your own custom pre-merge checks in the settings.✅ Passed checks (2 passed)
Check name Status Explanation Title check ✅ Passed The title clearly and specifically describes the main bug fix: correcting getIsAllSubRowsSelected to return false when subrows are unselectable. Description check ✅ Passed The description follows the repository template with all required sections completed: changes explained, checklist fully checked with test results, and release impact properly documented. ✨ Finishing Touches
- 📝 Generate docstrings (stacked PR)
- 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands and usage tips.Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/table-core/tests/RowSelection.test.ts (1)
464-597: ⚠️ Potential issue | 🟡 MinorMissing test:
getIsAllSubRowsSelectedwhen some sub-rows are non-selectable but all selectable ones are selected.This is the exact scenario from issue
#5173— the current tests cover "all non-selectable → false" and "all selectable & selected → true", but not the mixed case where the selectable subset is fully selected while non-selectable rows exist. Consider adding:it('should return true when all selectable sub-rows are selected and some are non-selectable', () => { const data = makeData(1, 3) const columns = generateColumns(data) const table = createTable<Person>({ enableRowSelection: (row) => row.id !== '0.1', // middle sub-row non-selectable onStateChange() {}, renderFallbackValue: '', data, getSubRows: (row) => row.subRows, state: { rowSelection: { '0.0': true, '0.2': true } }, columns, getCoreRowModel: getCoreRowModel(), }) const firstRow = table.getCoreRowModel().rows[0] expect(firstRow.getIsAllSubRowsSelected()).toBe(true) })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/table-core/tests/RowSelection.test.ts` around lines 464 - 597, Add a test covering the mixed selectable/non-selectable sub-rows case: create data with three sub-rows via makeData(1, 3), build a table using createTable with getSubRows and enableRowSelection configured so one sub-row (e.g., id '0.1') is non-selectable, set state.rowSelection to mark only the selectable sub-rows (e.g., '0.0' and '0.2') as true, then call table.getCoreRowModel().rows[0].getIsAllSubRowsSelected() and assert it returns true; this uses the same helpers from the file (makeData, generateColumns, createTable, getSubRows, enableRowSelection and getIsAllSubRowsSelected) and mirrors the provided example in the review comment.🧹 Nitpick comments (2)
packages/table-core/src/features/RowSelection.ts (2)
647-659: Minor: trailing semicolons after function declarations are inconsistent with the rest of the file.Lines 649 and 659 have trailing
;after the closing}ofexport functiondeclarations, which doesn't match the style used elsewhere in this file.🧹 Suggested fix
export function getSelectableSubRowCount<TData extends RowData>(row: Row<TData>): number { return countSubRows(row, (node) => node.getCanSelect()); -}; +} /** * ... */ export function getSelectedSubRowCount<TData extends RowData>(row: Row<TData>): number { return countSubRows(row, (node) => node.getCanSelect() && node.getIsSelected()); -}; +}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/table-core/src/features/RowSelection.ts` around lines 647 - 659, Remove the stray trailing semicolons after the two exported function declarations to match file style: delete the semicolons following the closing braces of getSelectableSubRowCount and getSelectedSubRowCount so the export function declarations end with just "}" rather than "};".
505-514: Double traversal: each call togetIsSomeSelected/getIsAllSubRowsSelectedperforms two full BFS traversals.Both methods call
getSelectableSubRowCountandgetSelectedSubRowCountindependently, each traversing the entire sub-tree. A single pass returning both counts would cut the work in half.Additionally,
Array.shift()is O(n) due to reindexing — using an index pointer avoids this.♻️ Suggested combined traversal
+function countSubRowSelections<TData extends RowData>( + row: Row<TData>, +): { selectable: number; selected: number } { + const q = [...row.subRows] + let selectable = 0 + let selected = 0 + let i = 0 + while (i < q.length) { + const node = q[i++]! + if (node.getCanSelect()) { + selectable++ + if (node.getIsSelected()) { + selected++ + } + } + for (const child of node.subRows) { + q.push(child) + } + } + return { selectable, selected } +}Then in the call sites:
row.getIsSomeSelected = () => { - const selectable = getSelectableSubRowCount(row) - const selected = getSelectedSubRowCount(row) + const { selectable, selected } = countSubRowSelections(row) return selected > 0 && selected < selectable; } row.getIsAllSubRowsSelected = () => { - const selectable = getSelectableSubRowCount(row) - const selected = getSelectedSubRowCount(row) + const { selectable, selected } = countSubRowSelections(row) return selected > 0 && selected === selectable; }Also applies to: 668-679
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/table-core/src/features/RowSelection.ts` around lines 505 - 514, The two methods row.getIsSomeSelected and row.getIsAllSubRowsSelected currently call getSelectableSubRowCount and getSelectedSubRowCount separately causing two full BFS traversals; replace them with a single combined traversal helper that returns both selectable and selected counts in one pass and have both methods use that result, and in the traversal avoid Array.shift() by using a numeric index pointer over the queue/array to iterate (this pattern should also be applied to the similar logic later in the file that mirrors lines 668-679).🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@packages/table-core/tests/RowSelection.test.ts`: - Around line 150-153: The inline comment claiming getSelectableSubRowCount and getSelectedSubRowCount are not part of the public interface is wrong; update or remove that comment in the test file so it accurately reflects that both functions are exported from RowSelection.ts and are part of the public API (referencing getSelectableSubRowCount and getSelectedSubRowCount); if you need to keep explanatory text, reword it to acknowledge these helpers are exported and clarify why the two RowSelectionRow.* test suites are more useful rather than asserting they are not exposed. - Around line 298-299: The inline comment on the createTable<Person> call is misleading: the predicate enableRowSelection: (row) => row.id !== '0.1' actually makes the row with id '0.1' non-selectable, not selectable; update the inline comment near the enableRowSelection usage (next to row.id !== '0.1') to state that '0.1' is the non-selectable sub-row (or invert the predicate if you intended the opposite). --- Outside diff comments: In `@packages/table-core/tests/RowSelection.test.ts`: - Around line 464-597: Add a test covering the mixed selectable/non-selectable sub-rows case: create data with three sub-rows via makeData(1, 3), build a table using createTable with getSubRows and enableRowSelection configured so one sub-row (e.g., id '0.1') is non-selectable, set state.rowSelection to mark only the selectable sub-rows (e.g., '0.0' and '0.2') as true, then call table.getCoreRowModel().rows[0].getIsAllSubRowsSelected() and assert it returns true; this uses the same helpers from the file (makeData, generateColumns, createTable, getSubRows, enableRowSelection and getIsAllSubRowsSelected) and mirrors the provided example in the review comment. --- Nitpick comments: In `@packages/table-core/src/features/RowSelection.ts`: - Around line 647-659: Remove the stray trailing semicolons after the two exported function declarations to match file style: delete the semicolons following the closing braces of getSelectableSubRowCount and getSelectedSubRowCount so the export function declarations end with just "}" rather than "};". - Around line 505-514: The two methods row.getIsSomeSelected and row.getIsAllSubRowsSelected currently call getSelectableSubRowCount and getSelectedSubRowCount separately causing two full BFS traversals; replace them with a single combined traversal helper that returns both selectable and selected counts in one pass and have both methods use that result, and in the traversal avoid Array.shift() by using a numeric index pointer over the queue/array to iterate (this pattern should also be applied to the similar logic later in the file that mirrors lines 668-679).packages/table-core/tests/RowSelection.test.ts lines 150 to 153OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -147,6 +147,7 @@147147 expect(result).toEqual(false)148148 })149149 })150- describe('isSubRowSelected', () => {151- it('should return false if there are no sub-rows', () => {152- const data = makeData(3)150+151+ // IMO the following 2 tests suites are not useful as they are not exposed to the public interface152+ // Instead the 2 RowSelectionRow.* test suites are more useful.153+ describe('getSelectableSubRowCount', () => {⚠️ Potential issue | 🟡 Minor
Incorrect comment:
getSelectableSubRowCountandgetSelectedSubRowCountare part of the public interface.Both functions are
export functioninRowSelection.ts, so they are exposed to consumers. The comment on lines 151–152 claims otherwise. Consider removing or correcting it.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/table-core/tests/RowSelection.test.ts` around lines 150 - 153, The inline comment claiming getSelectableSubRowCount and getSelectedSubRowCount are not part of the public interface is wrong; update or remove that comment in the test file so it accurately reflects that both functions are exported from RowSelection.ts and are part of the public API (referencing getSelectableSubRowCount and getSelectedSubRowCount); if you need to keep explanatory text, reword it to acknowledge these helpers are exported and clarify why the two RowSelectionRow.* test suites are more useful rather than asserting they are not exposed.✅ Addressed in commit 28e3729
packages/table-core/tests/RowSelection.test.ts lines 298 to 299OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -212,0 +279,21 @@279+ enableRowSelection: true,280+ onStateChange() {},281+ renderFallbackValue: '',282+ data,283+ getSubRows: (row) => row.subRows,284+ state: { rowSelection: {} },285+ columns,286+ getCoreRowModel: getCoreRowModel(),287+ })288+289+ const firstRow = table.getCoreRowModel().rows[0]290+ const result = RowSelection.getSelectedSubRowCount(firstRow)291+ expect(result).toBe(0)292+ })293+294+ it('should return number of selectable and selected sub-rows', () => {295+ const data = makeData(1, 2)296+ const columns = generateColumns(data)297+298+ const table = createTable<Person>({299+ enableRowSelection: (row) => row.id !== '0.1', // second sub-row is selectable⚠️ Potential issue | 🟡 Minor
Misleading comment:
'0.1'is the non-selectable sub-row, not the selectable one.The predicate
row.id !== '0.1'makes'0.1'non-selectable. The inline comment says the opposite.🧹 Suggested fix
- enableRowSelection: (row) => row.id !== '0.1', // second sub-row is selectable + enableRowSelection: (row) => row.id !== '0.1', // second sub-row is NOT selectable📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change298- const table = createTable<Person>({299- enableRowSelection: (row) => row.id !== '0.1', // second sub-row is selectable298+ const table = createTable<Person>({299+ enableRowSelection: (row) => row.id !== '0.1', // second sub-row is NOT selectable🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/table-core/tests/RowSelection.test.ts` around lines 298 - 299, The inline comment on the createTable<Person> call is misleading: the predicate enableRowSelection: (row) => row.id !== '0.1' actually makes the row with id '0.1' non-selectable, not selectable; update the inline comment near the enableRowSelection usage (next to row.id !== '0.1') to state that '0.1' is the non-selectable sub-row (or invert the predicate if you intended the opposite).✅ Addressed in commit 28e3729
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@packages/table-core/tests/RowSelection.test.ts`: - Around line 724-769: Add a positive regression test that verifies getIsAllSubRowsSelected returns true when some sub-rows are non-selectable but all selectable sub-rows are selected: create hierarchical data with makeData(1,2,2) (or similar), configure createTable with enableRowSelection:true, getSubRows:(r)=>r.subRows, and a state.rowSelection that marks only the selectable sub-rows (e.g., '0.0', '0.0.0', '0.0.1', '0.1', '0.1.0', '0.1.1') as true; then call table.getCoreRowModel().rows[0] and assert firstRow.getIsAllSubRowsSelected() === true to exercise getSubRowSelectionsCount path and cover the regression.packages/table-core/tests/RowSelection.test.ts lines 724 to 769Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -322,0 +749,21 @@749+ onStateChange() {},750+ renderFallbackValue: '',751+ data,752+ getSubRows: (row) => row.subRows,753+ state: {754+ rowSelection: {755+ '0.0': true,756+ '0.0.0': true,757+ '0.0.1': true,758+ '0.1': true,759+ '0.1.0': true,760+ '0.1.1': true,761+ },762+ },763+ columns,764+ getCoreRowModel: getCoreRowModel(),765+ })766+767+ const firstRow = table.getCoreRowModel().rows[0]768+ expect(firstRow.getIsAllSubRowsSelected()).toBe(true)769+ })⚠️ Potential issue | 🟡 Minor
Missing positive-case regression test for the core bug fix.
The six tests in
getIsAllSubRowsSelectedcover:- No sub-rows →
false - All sub-rows non-selectable →
false← negative fix case - Some/all not selected →
false - All rows selectable and all selected →
true
There is no test for the critical complementary scenario: some sub-rows are non-selectable, and all selectable sub-rows are selected → should return
true. This is the positive regression test for issue#5173. BecausegetIsAllSubRowsSelectednow usesgetSubRowSelectionsCountdirectly (independent of theisSubRowSelectedpath covered at line 264), this gap means the fix's "happy path" goes untested at this layer.✅ Suggested missing test
it('should return true if all selectable sub-rows are selected', () => { // ...existing test at line 724 }) + + it('should return true when all selectable sub-rows are selected and some are non-selectable', () => { + const data = makeData(1, 2) + const columns = generateColumns(data) + + const table = createTable<Person>({ + enableRowSelection: (row) => row.id !== '0.1', // second sub-row is NOT selectable + onStateChange() {}, + renderFallbackValue: '', + data, + getSubRows: (row) => row.subRows, + state: { rowSelection: { '0.0': true } }, // only selectable sub-row selected + columns, + getCoreRowModel: getCoreRowModel(), + }) + + const firstRow = table.getCoreRowModel().rows[0] + expect(firstRow.getIsAllSubRowsSelected()).toBe(true) + })Would you like me to also add the equivalent test for
getIsSomeSelected(should returnfalsewhen the only selectable sub-row is selected, i.e., selectedCount == selectableCount)?🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/table-core/tests/RowSelection.test.ts` around lines 724 - 769, Add a positive regression test that verifies getIsAllSubRowsSelected returns true when some sub-rows are non-selectable but all selectable sub-rows are selected: create hierarchical data with makeData(1,2,2) (or similar), configure createTable with enableRowSelection:true, getSubRows:(r)=>r.subRows, and a state.rowSelection that marks only the selectable sub-rows (e.g., '0.0', '0.0.0', '0.0.1', '0.1', '0.1.0', '0.1.1') as true; then call table.getCoreRowModel().rows[0] and assert firstRow.getIsAllSubRowsSelected() === true to exercise getSubRowSelectionsCount path and cover the regression.✅ Addressed in commit c4bd664
- No sub-rows →
🧹 Nitpick comments (1)
packages/table-core/tests/RowSelection.test.ts (1)
326-343: Optional: Extract a table-creation helper to reduce boilerplate.Many tests repeat the same
createTablecall differing only inenableRowSelection,data/makeDataargs,getSubRows, andstate.rowSelection. A small factory helper (e.g.,createTestTable(opts)with sensible defaults) would cut ~10 lines per test and make the distinguishing parameters more prominent.This is purely a readability nit — the existing style in the file is consistent, so feel free to defer.
Also applies to: 365-383, 407-424, 446-464
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/table-core/tests/RowSelection.test.ts` around lines 326 - 343, Extract a small helper (e.g., createTestTable) to reduce repeated createTable boilerplate across tests that call createTable with similar defaults (createTable, enableRowSelection, onStateChange, renderFallbackValue, getCoreRowModel) and varying inputs (data from makeData, columns from generateColumns, getSubRows, and state.rowSelection); update the tests that currently call createTable and then use table.getCoreRowModel() / RowSelection.getSubRowSelectionsCount(firstRow) to call the helper instead, passing only the differing options so each test only specifies its unique args (data/getSubRows/state) while the helper supplies the common defaults and returns the created table.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Nitpick comments: In `@packages/table-core/tests/RowSelection.test.ts`: - Around line 326-343: Extract a small helper (e.g., createTestTable) to reduce repeated createTable boilerplate across tests that call createTable with similar defaults (createTable, enableRowSelection, onStateChange, renderFallbackValue, getCoreRowModel) and varying inputs (data from makeData, columns from generateColumns, getSubRows, and state.rowSelection); update the tests that currently call createTable and then use table.getCoreRowModel() / RowSelection.getSubRowSelectionsCount(firstRow) to call the helper instead, passing only the differing options so each test only specifies its unique args (data/getSubRows/state) while the helper supplies the common defaults and returns the created table.@KevinVandy @riccardoperra is there anything else you'd like to see to get this this PR moving?
Also running into this problem! Thank you for this!
fixed v9
KevinVandy closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: c4bd664660d5ae0641f89bdf4972aa7a109255f5
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR