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

fix(selection-list): toggle newly-focused item when pressing arrow key + shift #10828

Merged
merged 1 commit into from Apr 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/lib/list/selection-list.spec.ts
Expand Up @@ -233,6 +233,30 @@ describe('MatSelectionList without forms', () => {
expect(manager.activeItemIndex).toEqual(1);
});

it('should focus and toggle the next item when pressing SHIFT + UP_ARROW', () => {
const manager = selectionList.componentInstance._keyManager;
const upKeyEvent = createKeyboardEvent('keydown', UP_ARROW);
Object.defineProperty(upKeyEvent, 'shiftKey', {get: () => true});
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to add a utility function like addModifierKey?

Copy link
Member Author

@crisbeto crisbeto Apr 13, 2018

Choose a reason for hiding this comment

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

I suppose, but I think that we should do a larger wrapper around these events that allows you do more stuff (e.g. also dispatching, creating etc).


dispatchFakeEvent(listOptions[3].nativeElement, 'focus');
expect(manager.activeItemIndex).toBe(3);

expect(listOptions[1].componentInstance.selected).toBe(false);
expect(listOptions[2].componentInstance.selected).toBe(false);

selectionList.componentInstance._keydown(upKeyEvent);
fixture.detectChanges();

expect(listOptions[1].componentInstance.selected).toBe(false);
expect(listOptions[2].componentInstance.selected).toBe(true);

selectionList.componentInstance._keydown(upKeyEvent);
fixture.detectChanges();

expect(listOptions[1].componentInstance.selected).toBe(true);
expect(listOptions[2].componentInstance.selected).toBe(true);
});

it('should focus next item when press DOWN ARROW', () => {
const manager = selectionList.componentInstance._keyManager;

Expand All @@ -245,6 +269,30 @@ describe('MatSelectionList without forms', () => {
expect(manager.activeItemIndex).toEqual(3);
});

it('should focus and toggle the next item when pressing SHIFT + DOWN_ARROW', () => {
const manager = selectionList.componentInstance._keyManager;
const downKeyEvent = createKeyboardEvent('keydown', DOWN_ARROW);
Object.defineProperty(downKeyEvent, 'shiftKey', {get: () => true});

dispatchFakeEvent(listOptions[0].nativeElement, 'focus');
expect(manager.activeItemIndex).toBe(0);

expect(listOptions[1].componentInstance.selected).toBe(false);
expect(listOptions[2].componentInstance.selected).toBe(false);

selectionList.componentInstance._keydown(downKeyEvent);
fixture.detectChanges();

expect(listOptions[1].componentInstance.selected).toBe(true);
expect(listOptions[2].componentInstance.selected).toBe(false);

selectionList.componentInstance._keydown(downKeyEvent);
fixture.detectChanges();

expect(listOptions[1].componentInstance.selected).toBe(true);
expect(listOptions[2].componentInstance.selected).toBe(true);
});

it('should be able to focus the first item when pressing HOME', () => {
const manager = selectionList.componentInstance._keyManager;
expect(manager.activeItemIndex).toBe(-1);
Expand Down
18 changes: 13 additions & 5 deletions src/lib/list/selection-list.ts
Expand Up @@ -9,7 +9,7 @@
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {SelectionModel} from '@angular/cdk/collections';
import {SPACE, ENTER, HOME, END} from '@angular/cdk/keycodes';
import {SPACE, ENTER, HOME, END, UP_ARROW, DOWN_ARROW} from '@angular/cdk/keycodes';
import {
AfterContentInit,
Attribute,
Expand Down Expand Up @@ -370,7 +370,11 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu

/** Passes relevant key presses to our key manager. */
_keydown(event: KeyboardEvent) {
switch (event.keyCode) {
const keyCode = event.keyCode;
const manager = this._keyManager;
const previousFocusIndex = manager.activeItemIndex;

switch (keyCode) {
case SPACE:
case ENTER:
if (!this.disabled) {
Expand All @@ -382,12 +386,16 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
break;
case HOME:
case END:
event.keyCode === HOME ? this._keyManager.setFirstItemActive() :
this._keyManager.setLastItemActive();
keyCode === HOME ? manager.setFirstItemActive() : manager.setLastItemActive();
event.preventDefault();
break;
default:
this._keyManager.onKeydown(event);
manager.onKeydown(event);
}

if ((keyCode === UP_ARROW || keyCode === DOWN_ARROW) && event.shiftKey &&
manager.activeItemIndex !== previousFocusIndex) {
this._toggleSelectOnFocusedOption();
}
}

Expand Down