Skip to content

Commit 087865d

Browse files
committed
feat(listbox): added shift key selection.
1 parent 0712e77 commit 087865d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/cdk-experimental/listbox/listbox.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
QueryList
1717
} from '@angular/core';
1818
import {ActiveDescendantKeyManager, Highlightable, ListKeyManagerOption} from '@angular/cdk/a11y';
19-
import {END, ENTER, HOME, SPACE} from '@angular/cdk/keycodes';
19+
import {DOWN_ARROW, END, ENTER, HOME, SPACE, UP_ARROW} from '@angular/cdk/keycodes';
2020
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
2121
import {SelectionChange, SelectionModel} from '@angular/cdk/collections';
2222
import {defer, merge, Observable, Subject} from 'rxjs';
@@ -259,7 +259,10 @@ export class CdkListbox implements AfterContentInit, OnDestroy, OnInit {
259259

260260
private _initKeyManager() {
261261
this._listKeyManager = new ActiveDescendantKeyManager(this._options)
262-
.withWrap().withVerticalOrientation().withTypeAhead();
262+
.withWrap()
263+
.withVerticalOrientation()
264+
.withTypeAhead()
265+
.withAllowedModifierKeys(['shiftKey']);
263266

264267
this._listKeyManager.change.pipe(takeUntil(this._destroyed)).subscribe(() => {
265268
this._updateActiveOption();
@@ -287,6 +290,7 @@ export class CdkListbox implements AfterContentInit, OnDestroy, OnInit {
287290

288291
const manager = this._listKeyManager;
289292
const {keyCode} = event;
293+
const previousActiveIndex = manager.activeItemIndex;
290294

291295
if (keyCode === HOME || keyCode === END) {
292296
event.preventDefault();
@@ -300,6 +304,12 @@ export class CdkListbox implements AfterContentInit, OnDestroy, OnInit {
300304
} else {
301305
manager.onKeydown(event);
302306
}
307+
308+
/** Will select an option if shift was pressed while navigating to the option */
309+
const isArrow = (keyCode === UP_ARROW || keyCode === DOWN_ARROW);
310+
if (isArrow && event.shiftKey && previousActiveIndex !== this._listKeyManager.activeItemIndex) {
311+
this._toggleActiveOption();
312+
}
303313
}
304314

305315
/** Emits a selection change event, called when an option has its selected state changed. */

0 commit comments

Comments
 (0)