Skip to content

Commit

Permalink
fix(select): support using shift + arrow key to toggle items in a mul…
Browse files Browse the repository at this point in the history
…ti-select (#9037)

Based on the accessibility guidelines for a multi-selection listbox (https://www.w3.org/TR/wai-aria-practices-1.1/#Listbox), potentially we can support toggling the previous/next item if the user presses shift + up/down arrow.
  • Loading branch information
crisbeto authored and tinayuangao committed Jan 10, 2018
1 parent ec959e8 commit 5592404
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,68 @@ describe('MatSelect', () => {
.toBe(false, 'Expected panel to stay closed.');
}));

it('should toggle the next option when pressing shift + DOWN_ARROW on a multi-select',
fakeAsync(() => {
fixture.destroy();

const multiFixture = TestBed.createComponent(MultiSelect);
const event = createKeyboardEvent('keydown', DOWN_ARROW);
Object.defineProperty(event, 'shiftKey', {get: () => true});

multiFixture.detectChanges();
select = multiFixture.debugElement.query(By.css('mat-select')).nativeElement;

multiFixture.componentInstance.select.open();
multiFixture.detectChanges();
flush();

expect(multiFixture.componentInstance.select.value).toBeFalsy();

dispatchEvent(select, event);
multiFixture.detectChanges();

expect(multiFixture.componentInstance.select.value).toEqual(['pizza-1']);

dispatchEvent(select, event);
multiFixture.detectChanges();

expect(multiFixture.componentInstance.select.value).toEqual(['pizza-1', 'tacos-2']);
}));

it('should toggle the previous option when pressing shift + UP_ARROW on a multi-select',
fakeAsync(() => {
fixture.destroy();

const multiFixture = TestBed.createComponent(MultiSelect);
const event = createKeyboardEvent('keydown', UP_ARROW);
Object.defineProperty(event, 'shiftKey', {get: () => true});

multiFixture.detectChanges();
select = multiFixture.debugElement.query(By.css('mat-select')).nativeElement;

multiFixture.componentInstance.select.open();
multiFixture.detectChanges();
flush();

// Move focus down first.
for (let i = 0; i < 5; i++) {
dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
multiFixture.detectChanges();
}

expect(multiFixture.componentInstance.select.value).toBeFalsy();

dispatchEvent(select, event);
multiFixture.detectChanges();

expect(multiFixture.componentInstance.select.value).toEqual(['chips-4']);

dispatchEvent(select, event);
multiFixture.detectChanges();

expect(multiFixture.componentInstance.select.value).toEqual(['sandwich-3', 'chips-4']);
}));

it('should prevent the default action when pressing space', fakeAsync(() => {
const event = dispatchKeyboardEvent(select, 'keydown', SPACE);
expect(event.defaultPrevented).toBe(true);
Expand Down
8 changes: 8 additions & 0 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,15 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
event.preventDefault();
this._keyManager.activeItem._selectViaInteraction();
} else {
const isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW;
const previouslyFocusedIndex = this._keyManager.activeItemIndex;

this._keyManager.onKeydown(event);

if (this._multiple && isArrowKey && event.shiftKey && this._keyManager.activeItem &&
this._keyManager.activeItemIndex !== previouslyFocusedIndex) {
this._keyManager.activeItem._selectViaInteraction();
}
}
}

Expand Down

0 comments on commit 5592404

Please sign in to comment.