Skip to content

Commit

Permalink
Fix column sorting using ENTER key (#10873)
Browse files Browse the repository at this point in the history
  • Loading branch information
budnix committed Apr 3, 2024
1 parent 5d3f934 commit bfb0e3a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 2 additions & 4 deletions handsontable/src/plugins/columnSorting/columnSorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 6 additions & 9 deletions handsontable/src/plugins/nestedRows/nestedRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit bfb0e3a

Please sign in to comment.