Skip to content

Commit

Permalink
fix(selection-list): allow jumping to first/last item using home/end (#…
Browse files Browse the repository at this point in the history
…9062)

Allows users to jump focus to the first and last items in a selection list using the home and end keys.
  • Loading branch information
crisbeto authored and jelbourn committed Jan 4, 2018
1 parent 3eb5b2e commit bd36529
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
27 changes: 25 additions & 2 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {DOWN_ARROW, SPACE, ENTER, UP_ARROW} from '@angular/cdk/keycodes';
import {DOWN_ARROW, SPACE, ENTER, UP_ARROW, HOME, END} from '@angular/cdk/keycodes';
import {Platform} from '@angular/cdk/platform';
import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
import {createKeyboardEvent, dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
import {Component, DebugElement} from '@angular/core';
import {async, ComponentFixture, fakeAsync, inject, TestBed, tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
Expand Down Expand Up @@ -264,6 +264,29 @@ describe('MatSelectionList without forms', () => {
expect(manager.activeItemIndex).toEqual(3);
});

it('should focus the first non-disabled item when pressing HOME', () => {
const manager = selectionList.componentInstance._keyManager;
expect(manager.activeItemIndex).toBe(-1);

const event = dispatchKeyboardEvent(selectionList.nativeElement, 'keydown', HOME);
fixture.detectChanges();

// Note that the first item is disabled so we expect the second one to be focused.
expect(manager.activeItemIndex).toBe(1);
expect(event.defaultPrevented).toBe(true);
});

it('should focus the last item when pressing END', () => {
const manager = selectionList.componentInstance._keyManager;
expect(manager.activeItemIndex).toBe(-1);

const event = dispatchKeyboardEvent(selectionList.nativeElement, 'keydown', END);
fixture.detectChanges();

expect(manager.activeItemIndex).toBe(3);
expect(event.defaultPrevented).toBe(true);
});

it('should be able to select all options', () => {
const list: MatSelectionList = selectionList.componentInstance;

Expand Down
8 changes: 7 additions & 1 deletion src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
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} from '@angular/cdk/keycodes';
import {SPACE, ENTER, HOME, END} from '@angular/cdk/keycodes';
import {
AfterContentInit,
Attribute,
Expand Down Expand Up @@ -356,6 +356,12 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
// Always prevent space from scrolling the page since the list has focus
event.preventDefault();
break;
case HOME:
case END:
event.keyCode === HOME ? this._keyManager.setFirstItemActive() :
this._keyManager.setLastItemActive();
event.preventDefault();
break;
default:
this._keyManager.onKeydown(event);
}
Expand Down

0 comments on commit bd36529

Please sign in to comment.