Skip to content
Draft
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 @@ -243,18 +243,21 @@ export function column_toggleSorting<
let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
const nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'

const isMultiMode = !!(
old.length &&
column_getCanMultiSort(column) &&
multi
)

// Multi-mode
if (old.length && column_getCanMultiSort(column) && multi) {
if (isMultiMode) {
if (existingSorting) {
sortAction = 'toggle'
} else {
sortAction = 'add'
}
} else {
// Normal mode
if (old.length && existingIndex !== old.length - 1) {
sortAction = 'replace'
} else if (existingSorting) {
if (existingSorting) {
sortAction = 'toggle'
} else {
sortAction = 'replace'
Expand Down Expand Up @@ -289,17 +292,19 @@ export function column_toggleSorting<
)
} else if (sortAction === 'toggle') {
// This flips (or sets) the
newSorting = old.map((d) => {
if (d.id === column.id) {
return {
...d,
desc: nextDesc,
}
}
return d
})
newSorting = isMultiMode
? old.map((d) => {
if (d.id === column.id) {
return {
...d,
desc: nextDesc,
}
}
return d
})
: [{ id: column.id, desc: nextDesc }]
} else if (sortAction === 'remove') {
newSorting = old.filter((d) => d.id !== column.id)
newSorting = isMultiMode ? old.filter((d) => d.id !== column.id) : []
} else {
newSorting = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,22 @@ describe('column_toggleSorting', () => {
])
})

it('should discard other sorts when toggling the last column in single-sort mode', () => {
const sorting: SortingState = [
{ id: 'age', desc: true },
{ id: 'firstName', desc: false },
]
const { table, onSortingChange } = makeTableWithMockOnSortingChange({
initialState: { sorting },
})

column_toggleSorting(table.getColumn('firstName')!, undefined, false)

expect(getUpdaterResult(onSortingChange, sorting)).toEqual([
{ id: 'firstName', desc: true },
])
})

it('should append to the sorting state in multi mode', () => {
const sorting: SortingState = [{ id: 'age', desc: true }]
const { table, onSortingChange } = makeTableWithMockOnSortingChange({
Expand Down
Loading