Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
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
122 changes: 122 additions & 0 deletions packages/table-core/__tests__/getSelectedRowModel.test.ts
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)
})
})
})
3 changes: 2 additions & 1 deletion packages/table-core/src/features/RowSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ export const RowSelection: TableFeature = {
table.setRowSelection(old => {
value = typeof value !== 'undefined' ? value : !isSelected

if (isSelected === value) {
const willUpdateSubRows = row.subRows?.length && row.getCanSelectSubRows()

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?

if (!willUpdateSubRows && isSelected === value) {
return old
}

Expand Down