-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue toggling row selection for a grouped row #4760
Open
mollykreis
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
mollykreis:fix-toggle-group-selection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
packages/table-core/__tests__/getSelectedRowModel.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { ColumnDef, getCoreRowModel, getGroupedRowModel, RowSelectionState, Table, Updater } from '../src' | ||
import { createColumnHelper } from '../src/columnHelper' | ||
import { createTable } from '../src/core/table' | ||
import { makeData, Person } from './makeTestData' | ||
|
||
type personKeys = keyof Person | ||
type PersonColumn = ColumnDef<Person, string | number | Person[] | undefined> | ||
|
||
function generateColumns(people: Person[]): PersonColumn[] { | ||
const columnHelper = createColumnHelper<Person>() | ||
const person = people[0] | ||
return Object.keys(person).map(key => { | ||
const typedKey = key as personKeys | ||
return columnHelper.accessor(typedKey, { id: typedKey }) | ||
}) | ||
} | ||
|
||
describe('row selection', () => { | ||
let table: Table<Person> | ||
let data: Person[] | ||
|
||
beforeEach(() => { | ||
data = makeData(10) | ||
const columns = generateColumns(data) | ||
|
||
function setRowSelection(updater: Updater<RowSelectionState>) { | ||
const selectionState = updater instanceof Function | ||
? updater(table.getState().rowSelection) | ||
: updater | ||
|
||
table.setOptions({ | ||
...table.options, | ||
state: { | ||
...table.options.state, | ||
rowSelection: selectionState | ||
} | ||
}) | ||
} | ||
|
||
table = createTable<Person>({ | ||
onStateChange() {}, | ||
onRowSelectionChange: setRowSelection, | ||
renderFallbackValue: '', | ||
data, | ||
state: { rowSelection: {} }, | ||
columns, | ||
getCoreRowModel: getCoreRowModel(), | ||
enableRowSelection: true | ||
}) | ||
}) | ||
|
||
it('rows initialize to not selected', () => { | ||
const allRows = table.getCoreRowModel().rows | ||
allRows.forEach(row => expect(row.getIsSelected()).toBe(false)) | ||
}) | ||
|
||
it('can toggle row selection', () => { | ||
const firstRow = table.getCoreRowModel().rows[0] | ||
firstRow.toggleSelected() | ||
expect(firstRow.getIsSelected()).toBe(true) | ||
firstRow.toggleSelected() | ||
expect(firstRow.getIsSelected()).toBe(false) | ||
}) | ||
|
||
it('cannot select row that is not selectable', () => { | ||
const rows = table.getCoreRowModel().rows | ||
const firstRow = rows[0] | ||
const secondRow = rows[1] | ||
table.options.enableRowSelection = (row) => { return row !== firstRow } | ||
|
||
firstRow.toggleSelected() | ||
expect(firstRow.getIsSelected()).toBe(false) | ||
secondRow.toggleSelected() | ||
expect(secondRow.getIsSelected()).toBe(true) | ||
}) | ||
|
||
describe('group selection', () => { | ||
beforeEach(() => { | ||
data.forEach(p => (p.firstName = 'Fixed')) | ||
|
||
table.setOptions({ | ||
...table.options, | ||
enableRowSelection: (row) => !row.getIsGrouped(), | ||
enableGrouping: true, | ||
getGroupedRowModel: getGroupedRowModel(), | ||
data: [...data], | ||
state: { | ||
...table.options.state, | ||
grouping: ['firstName'] | ||
} | ||
}) | ||
}) | ||
|
||
it('can toggle grouped row selection to select and deselect subrows', () => { | ||
const groupedRow = table.getRowModel().rowsById['firstName:Fixed'] | ||
const subrows = groupedRow.subRows | ||
|
||
groupedRow.toggleSelected(true) | ||
expect(groupedRow.getIsSelected()).toBe(false) | ||
expect(groupedRow.getIsSomeSelected()).toBe(false) | ||
expect(groupedRow.getIsAllSubRowsSelected()).toBe(true) | ||
subrows.forEach(row => expect(row.getIsSelected()).toBe(true)) | ||
|
||
groupedRow.toggleSelected(false) | ||
expect(groupedRow.getIsSelected()).toBe(false) | ||
expect(groupedRow.getIsSomeSelected()).toBe(false) | ||
expect(groupedRow.getIsAllSubRowsSelected()).toBe(false) | ||
subrows.forEach(row => expect(row.getIsSelected()).toBe(false)) | ||
}) | ||
|
||
it('grouped row returns correct subrow selection state when one subrow is selected', () => { | ||
const groupedRow = table.getRowModel().rowsById['firstName:Fixed'] | ||
const firstSubRow = groupedRow.subRows[0] | ||
|
||
firstSubRow.toggleSelected() | ||
expect(firstSubRow.getIsSelected()).toBe(true) | ||
expect(groupedRow.getIsSelected()).toBe(false) | ||
expect(groupedRow.getIsSomeSelected()).toBe(true) | ||
expect(groupedRow.getIsAllSubRowsSelected()).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
row.subRows?.length can be undefined, how about using !! to explicitly convert it to a boolean?