Skip to content

feat(a11y): Add optional home/end key support to ListKeyManager #19834

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
Jul 11, 2020
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
30 changes: 29 additions & 1 deletion src/cdk/a11y/key-manager/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW, TAB, UP_ARROW} from '@angular/cdk/keycodes';
import {DOWN_ARROW, END, HOME, LEFT_ARROW, RIGHT_ARROW, TAB, UP_ARROW} from '@angular/cdk/keycodes';
import {createKeyboardEvent} from '@angular/cdk/testing/private';
import {QueryList} from '@angular/core';
import {fakeAsync, tick} from '@angular/core/testing';
Expand Down Expand Up @@ -51,6 +51,8 @@ describe('Key managers', () => {
leftArrow: KeyboardEvent,
rightArrow: KeyboardEvent,
tab: KeyboardEvent,
home: KeyboardEvent,
end: KeyboardEvent,
unsupported: KeyboardEvent
};

Expand All @@ -62,6 +64,8 @@ describe('Key managers', () => {
leftArrow: createKeyboardEvent('keydown', LEFT_ARROW),
rightArrow: createKeyboardEvent('keydown', RIGHT_ARROW),
tab: createKeyboardEvent('keydown', TAB),
home: createKeyboardEvent('keydown', HOME),
end: createKeyboardEvent('keydown', END),
unsupported: createKeyboardEvent('keydown', 192) // corresponds to the tilde character (~)
};
});
Expand Down Expand Up @@ -195,6 +199,30 @@ describe('Key managers', () => {
expect(fakeKeyEvents.downArrow.defaultPrevented).toBe(false);
});

describe('withHomeAndEnd', () => {
beforeEach(() => {
keyManager.withHomeAndEnd();
});

it('should focus the first item when Home is pressed', () => {
keyManager.setActiveItem(1);
expect(keyManager.activeItemIndex).toBe(1);

keyManager.onKeydown(fakeKeyEvents.home);

expect(keyManager.activeItemIndex).toBe(0);
});

it('should focus the last item when End is pressed', () => {
keyManager.setActiveItem(0);
expect(keyManager.activeItemIndex).toBe(0);

keyManager.onKeydown(fakeKeyEvents.end);

expect(keyManager.activeItemIndex).toBe(itemList.items.length - 1);
});
});

describe('with `vertical` direction', function(this: KeyEventTestContext) {
beforeEach(() => {
keyManager.withVerticalOrientation();
Expand Down
28 changes: 28 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
ZERO,
NINE,
hasModifierKey,
HOME,
END,
} from '@angular/cdk/keycodes';
import {debounceTime, filter, map, tap} from 'rxjs/operators';

Expand Down Expand Up @@ -47,6 +49,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
private _vertical = true;
private _horizontal: 'ltr' | 'rtl' | null;
private _allowedModifierKeys: ListKeyManagerModifierKey[] = [];
private _homeAndEnd = false;

/**
* Predicate function that can be used to check whether an item should be skipped
Expand Down Expand Up @@ -174,6 +177,15 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
return this;
}

/**
* Configures the key manager to focus the first and last items
* respectively when the Home key and End Key are pressed.
*/
withHomeAndEnd(): this {
this._homeAndEnd = true;
return this;
}

/**
* Sets the active item to the item at the index specified.
* @param index The index of the item to be set as active.
Expand Down Expand Up @@ -244,6 +256,22 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
return;
}

case HOME:
if (this._homeAndEnd && isModifierAllowed) {
this.setFirstItemActive();
break;
} else {
return;
}

case END:
if (this._homeAndEnd && isModifierAllowed) {
this.setLastItemActive();
break;
} else {
return;
}

default:
if (isModifierAllowed || hasModifierKey(event, 'shiftKey')) {
// Attempt to use the `event.key` which also maps it to the user's keyboard language,
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/cdk/a11y.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export declare class ListKeyManager<T extends ListKeyManagerOption> {
updateActiveItem(index: number): void;
updateActiveItem(item: T): void;
withAllowedModifierKeys(keys: ListKeyManagerModifierKey[]): this;
withHomeAndEnd(): this;
withHorizontalOrientation(direction: 'ltr' | 'rtl' | null): this;
withTypeAhead(debounceInterval?: number): this;
withVerticalOrientation(enabled?: boolean): this;
Expand Down