From 0bd58068c7eaca86449c381dc057033cf96fd089 Mon Sep 17 00:00:00 2001 From: frykten Date: Tue, 19 Nov 2019 17:47:28 +0100 Subject: [PATCH] bugfix: first multiselection has no _lastSelectedIndex --- addon-test-support/pages/ember-table.js | 15 ++++++++++-- addon/-private/collapse-tree.js | 6 +++-- .../integration/components/selection-test.js | 24 +++++++++++++------ 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/addon-test-support/pages/ember-table.js b/addon-test-support/pages/ember-table.js index 742dad870..7931a5656 100644 --- a/addon-test-support/pages/ember-table.js +++ b/addon-test-support/pages/ember-table.js @@ -66,13 +66,24 @@ export default PageObject.extend({ }, /** - * Selects a range of rows in the body + * Selects a range of rows in the body with simple click first * * @param {number} beginIndex * @param {number} endIndex */ - async selectRange(beginIndex, endIndex) { + async selectRangeFromClick(beginIndex, endIndex) { await this.body.rows.objectAt(beginIndex).click(); await this.body.rows.objectAt(endIndex).clickWith({ shiftKey: true }); }, + + /** + * Selects a range of rows in the body with shift+click first + * + * @param {number} beginIndex + * @param {number} endIndex + */ + async selectRangeFromShiftClick(beginIndex, endIndex) { + await this.body.rows.objectAt(beginIndex).clickWith({ shiftKey: true }); + await this.body.rows.objectAt(endIndex).clickWith({ shiftKey: true }); + }, }); diff --git a/addon/-private/collapse-tree.js b/addon/-private/collapse-tree.js index daa3724e2..8636acf16 100644 --- a/addon/-private/collapse-tree.js +++ b/addon/-private/collapse-tree.js @@ -163,8 +163,10 @@ export const TableRowMeta = EmberObject.extend({ // Use a set to avoid item duplication let { _lastSelectedIndex } = tree; - let minIndex = Math.min(_lastSelectedIndex, rowIndex); - let maxIndex = Math.max(_lastSelectedIndex, rowIndex); + let isFirstIndexDefined = typeof _lastSelectedIndex === 'number'; + + let minIndex = isFirstIndexDefined ? Math.min(_lastSelectedIndex, rowIndex) : rowIndex; + let maxIndex = isFirstIndexDefined ? Math.max(_lastSelectedIndex, rowIndex) : rowIndex; for (let i = minIndex; i <= maxIndex; i++) { selection.add(tree.objectAt(i)); diff --git a/tests/integration/components/selection-test.js b/tests/integration/components/selection-test.js index d6e64432d..a3d29aa97 100644 --- a/tests/integration/components/selection-test.js +++ b/tests/integration/components/selection-test.js @@ -115,12 +115,22 @@ module('Integration | selection', () => { assert.ok(table.validateSelected(), 'the rows are toggled back off'); }); - test('Can select a range with shift', async function(assert) { + test('Can select a range with shift from click', async function(assert) { await generateTable(this); assert.ok(table.validateSelected(), 'rows are not selected'); - await table.selectRange(0, 2); + await table.selectRangeFromClick(0, 2); + + assert.ok(table.validateSelected(0, 1, 2), 'rows are selected'); + }); + + test('Can select a range with shift from shift-click', async function(assert) { + await generateTable(this); + + assert.ok(table.validateSelected(), 'rows are not selected'); + + await table.selectRangeFromShiftClick(0, 2); assert.ok(table.validateSelected(0, 1, 2), 'rows are selected'); }); @@ -130,7 +140,7 @@ module('Integration | selection', () => { assert.ok(table.validateSelected(), 'rows are not selected'); - await table.selectRange(3, 5); + await table.selectRangeFromClick(3, 5); assert.ok(table.validateSelected(3, 4, 5), 'rows are selected'); }); @@ -144,7 +154,7 @@ module('Integration | selection', () => { assert.ok(table.validateSelected(4), 'middle row selected'); - await table.selectRange(3, 5); + await table.selectRangeFromClick(3, 5); assert.ok(table.validateSelected(3, 4, 5), 'all rows are selected'); }); @@ -288,7 +298,7 @@ module('Integration | selection', () => { assert.ok(table.validateSelected(), 'no rows are not selected'); - await table.selectRange(0, 3); + await table.selectRangeFromClick(0, 3); assert.ok(table.validateSelected(3), 'last row only is selected'); }); @@ -473,7 +483,7 @@ module('Integration | selection', () => { assert.ok(table.validateSelected(), 'rows are not selected'); - await table.selectRange(1, 3); + await table.selectRangeFromClick(1, 3); assert.ok(table.validateSelected(0, 1, 2, 3), 'row and its children are selected'); }); @@ -487,7 +497,7 @@ module('Integration | selection', () => { assert.ok(table.validateSelected(), 'rows are not selected'); - await table.selectRange(1, 3); + await table.selectRangeFromClick(1, 3); assert.ok(table.validateSelected(1, 2, 3), 'only children are selected'); });