Skip to content

fix(select): support using shift + arrow key to toggle items in a multi-select #9037

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

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
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
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the key manager do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key manager currently doesn't have a concept of what is selected, it only knows what is focused/highlighted.

}
}
}

Expand Down