From d2fb5600c471abfe24e836f08426abd182663424 Mon Sep 17 00:00:00 2001 From: Kevin Van Cott Date: Fri, 17 Jul 2026 15:17:36 -0500 Subject: [PATCH] fix sorting replacement in single-sort mode --- .../row-sorting/rowSortingFeature.utils.ts | 35 +++++++++++-------- .../rowSortingFeature.utils.test.ts | 16 +++++++++ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/table-core/src/features/row-sorting/rowSortingFeature.utils.ts b/packages/table-core/src/features/row-sorting/rowSortingFeature.utils.ts index f0aac2d012..6b9f523005 100644 --- a/packages/table-core/src/features/row-sorting/rowSortingFeature.utils.ts +++ b/packages/table-core/src/features/row-sorting/rowSortingFeature.utils.ts @@ -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' @@ -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 = [ { diff --git a/packages/table-core/tests/unit/features/row-sorting/rowSortingFeature.utils.test.ts b/packages/table-core/tests/unit/features/row-sorting/rowSortingFeature.utils.test.ts index 193f39e2e5..217046cfa2 100644 --- a/packages/table-core/tests/unit/features/row-sorting/rowSortingFeature.utils.test.ts +++ b/packages/table-core/tests/unit/features/row-sorting/rowSortingFeature.utils.test.ts @@ -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({