From bfb0e3aac4f65345bc0f359a0ebbca1648826e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20=E2=80=98Budzio=E2=80=99=20Budnik?= <571316+budnix@users.noreply.github.com> Date: Wed, 3 Apr 2024 12:49:13 +0200 Subject: [PATCH] Fix column sorting using ENTER key (#10873) --- .../__tests__/keyboardShortcuts.spec.js | 30 ++++++++++++++++++ .../plugins/columnSorting/columnSorting.js | 6 ++-- .../__tests__/keyboardShortcuts.spec.js | 31 +++++++++++++++++++ .../src/plugins/nestedRows/nestedRows.js | 15 ++++----- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/handsontable/src/plugins/columnSorting/__tests__/keyboardShortcuts.spec.js b/handsontable/src/plugins/columnSorting/__tests__/keyboardShortcuts.spec.js index 3f1fc66775..129d95d8b3 100644 --- a/handsontable/src/plugins/columnSorting/__tests__/keyboardShortcuts.spec.js +++ b/handsontable/src/plugins/columnSorting/__tests__/keyboardShortcuts.spec.js @@ -103,6 +103,36 @@ describe('ColumnSorting keyboard shortcut', () => { }]); }); + it('should be possible to sort a column when a column header is selected and NestedRows plugin is enabled (#dev-1817)', () => { + handsontable({ + data: [ + { + category: 'Best Rock Performance', + artist: null, + __children: [ + { + artist: 'Alabama Shakes', + }, + ], + } + ], + colHeaders: true, + rowHeaders: true, + navigableHeaders: true, + columnSorting: true, + nestedRows: true, + }); + + selectCell(-1, 1); + listen(); + keyDownUp('enter'); + + expect(getPlugin('columnSorting').getSortConfig()).toEqual([{ + column: 1, + sortOrder: 'asc', + }]); + }); + it('should not be possible to sort a column when a range of the columns are selected', () => { handsontable({ data: createSpreadsheetData(3, 8), diff --git a/handsontable/src/plugins/columnSorting/columnSorting.js b/handsontable/src/plugins/columnSorting/columnSorting.js index 523a16e212..bcd187d5aa 100644 --- a/handsontable/src/plugins/columnSorting/columnSorting.js +++ b/handsontable/src/plugins/columnSorting/columnSorting.js @@ -223,9 +223,7 @@ export class ColumnSorting extends BasePlugin { callback: () => { const { highlight } = this.hot.getSelectedRangeLast(); - if (highlight.row === -1 && highlight.col >= 0) { - this.sort(this.getColumnNextConfig(highlight.col)); - } + this.sort(this.getColumnNextConfig(highlight.col)); // prevent default Enter behavior (move to the next row within a selection range) return false; @@ -234,7 +232,7 @@ export class ColumnSorting extends BasePlugin { const highlight = this.hot.getSelectedRangeLast()?.highlight; return highlight && this.hot.getSelectedRangeLast()?.isSingle() && - this.hot.selection.isCellVisible(highlight) && highlight.isHeader(); + this.hot.selection.isCellVisible(highlight) && highlight.row === -1 && highlight.col >= 0; }, relativeToGroup: SHORTCUTS_GROUP_EDITOR, position: 'before', diff --git a/handsontable/src/plugins/nestedRows/__tests__/keyboardShortcuts.spec.js b/handsontable/src/plugins/nestedRows/__tests__/keyboardShortcuts.spec.js index d1973baa76..cb1fc5aa4c 100644 --- a/handsontable/src/plugins/nestedRows/__tests__/keyboardShortcuts.spec.js +++ b/handsontable/src/plugins/nestedRows/__tests__/keyboardShortcuts.spec.js @@ -163,6 +163,37 @@ describe('NestedRows keyboard shortcut', () => { ]); }); + it('should be possible to collapse a single row when a single row header is selected and ColumnSorting plugin is enabled (#dev-1817)', () => { + handsontable({ + data: getMoreComplexNestedData(), + colHeaders: true, + rowHeaders: true, + navigableHeaders: true, + nestedRows: true, + columnSorting: true, + }); + + selectCell(0, -1); + listen(); + keyDownUp('enter'); + + expect(getData()).toEqual([ + ['a0', 'b0'], + // ['a0-a0', 'b0-b0'], + // ['a0-a1', 'b0-b1'], + // ['a0-a2', 'b0-b2'], + // ['a0-a2-a0', 'b0-b2-b0'], + // ['a0-a2-a0-a0', 'b0-b2-b0-b0'], + // ['a0-a3', 'b0-b3'], + ['a1', 'b1'], + ['a2', 'b2'], + ['a2-a0', 'b2-b0'], + ['a2-a1', 'b2-b1'], + ['a2-a1-a0', 'b2-b1-b0'], + ['a2-a1-a1', 'b2-b1-b1'], + ]); + }); + it('should not be possible to collapse a single row when a range of the rows are selected', () => { handsontable({ data: getMoreComplexNestedData(), diff --git a/handsontable/src/plugins/nestedRows/nestedRows.js b/handsontable/src/plugins/nestedRows/nestedRows.js index dc872220d8..0360a47640 100644 --- a/handsontable/src/plugins/nestedRows/nestedRows.js +++ b/handsontable/src/plugins/nestedRows/nestedRows.js @@ -165,15 +165,12 @@ export class NestedRows extends BasePlugin { keys: [['Enter']], callback: () => { const { highlight } = this.hot.getSelectedRangeLast(); + const row = this.collapsingUI.translateTrimmedRow(highlight.row); - if (highlight.col === -1 && highlight.row >= 0) { - const row = this.collapsingUI.translateTrimmedRow(highlight.row); - - if (this.collapsingUI.areChildrenCollapsed(row)) { - this.collapsingUI.expandChildren(row); - } else { - this.collapsingUI.collapseChildren(row); - } + if (this.collapsingUI.areChildrenCollapsed(row)) { + this.collapsingUI.expandChildren(row); + } else { + this.collapsingUI.collapseChildren(row); } // prevent default Enter behavior (move to the next row within a selection range) @@ -183,7 +180,7 @@ export class NestedRows extends BasePlugin { const highlight = this.hot.getSelectedRangeLast()?.highlight; return highlight && this.hot.getSelectedRangeLast()?.isSingle() && - this.hot.selection.isCellVisible(highlight) && highlight.isHeader(); + this.hot.selection.isCellVisible(highlight) && highlight.col === -1 && highlight.row >= 0; }, group: SHORTCUTS_GROUP, relativeToGroup: SHORTCUTS_GROUP_EDITOR,