Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctrl-click to toggle single row or column selections #163

Merged
merged 2 commits into from
Apr 1, 2021
Merged
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
19 changes: 17 additions & 2 deletions packages/datagrid/src/basicselectionmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,26 @@ class BasicSelectionModel extends SelectionModel {
c1 = Math.max(0, Math.min(c1, columnCount - 1));
c2 = Math.max(0, Math.min(c2, columnCount - 1));

// Indicate if a row/column has already been selected.
let alreadySelected = false;

// Handle the selection mode.
if (this.selectionMode === 'row') {
c1 = 0;
c2 = columnCount - 1;
alreadySelected = this._selections.filter(selection => selection.r1 === r1).length !== 0;
// Remove from selections if already selected.
this._selections = alreadySelected
? this._selections.filter(selection => selection.r1 !== r1)
: this._selections;
} else if (this.selectionMode === 'column') {
r1 = 0;
r2 = rowCount - 1;
alreadySelected = this._selections.filter(selection => selection.c1 === c1).length !== 0;
// Remove from selections if already selected.
this._selections = alreadySelected
? this._selections.filter(selection => selection.c1 !== c1)
: this._selections;
}

// Alias the cursor row and column.
Expand All @@ -213,8 +226,10 @@ class BasicSelectionModel extends SelectionModel {
this._cursorColumn = cc;
this._cursorRectIndex = this._selections.length;

// Add the new selection.
this._selections.push({ r1, c1, r2, c2 });
// Add the new selection if it wasn't already selected.
if (!alreadySelected) {
this._selections.push({ r1, c1, r2, c2 });
}

// Emit the changed signal.
this.emitChanged();
Expand Down