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(list-key-manager): prevent the default keyboard actions #2009

Merged
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
40 changes: 35 additions & 5 deletions src/lib/core/a11y/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ class FakeQueryList<T> extends QueryList<T> {
}
}

const DOWN_ARROW_EVENT = { keyCode: DOWN_ARROW } as KeyboardEvent;
const UP_ARROW_EVENT = { keyCode: UP_ARROW } as KeyboardEvent;
const TAB_EVENT = { keyCode: TAB } as KeyboardEvent;
const HOME_EVENT = { keyCode: HOME } as KeyboardEvent;
const END_EVENT = { keyCode: END } as KeyboardEvent;
class FakeEvent {
defaultPrevented: boolean = false;
constructor(public keyCode: number) {}
preventDefault() {
this.defaultPrevented = true;
}
}


describe('ListKeyManager', () => {
let keyManager: ListKeyManager;
let itemList: FakeQueryList<FakeFocusable>;
let DOWN_ARROW_EVENT: KeyboardEvent;
let UP_ARROW_EVENT: KeyboardEvent;
let TAB_EVENT: KeyboardEvent;
let HOME_EVENT: KeyboardEvent;
let END_EVENT: KeyboardEvent;

beforeEach(() => {
itemList = new FakeQueryList<FakeFocusable>();
Expand All @@ -35,6 +43,12 @@ describe('ListKeyManager', () => {

keyManager = new ListKeyManager(itemList);

DOWN_ARROW_EVENT = new FakeEvent(DOWN_ARROW) as KeyboardEvent;
UP_ARROW_EVENT = new FakeEvent(UP_ARROW) as KeyboardEvent;
TAB_EVENT = new FakeEvent(TAB) as KeyboardEvent;
HOME_EVENT = new FakeEvent(HOME) as KeyboardEvent;
END_EVENT = new FakeEvent(END) as KeyboardEvent;

// first item is already focused
keyManager.focusFirstItem();

Expand Down Expand Up @@ -166,6 +180,22 @@ describe('ListKeyManager', () => {
expect(tabOutEmitted).toBe(true);
});

it('should prevent the default keyboard action', () => {
expect(DOWN_ARROW_EVENT.defaultPrevented).toBe(false);

keyManager.onKeydown(DOWN_ARROW_EVENT);

expect(DOWN_ARROW_EVENT.defaultPrevented).toBe(true);
});

it('should not prevent the default keyboard action when pressing tab', () => {
expect(TAB_EVENT.defaultPrevented).toBe(false);

keyManager.onKeydown(TAB_EVENT);

expect(TAB_EVENT.defaultPrevented).toBe(false);
});

});

describe('programmatic focus', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/core/a11y/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ export class ListKeyManager {
this.focusLastItem();
break;
case TAB:
// Note that we shouldn't prevent the default action on tab.
this._tabOut.next(null);
break;
return;
default:
return;
}

event.preventDefault();
}

/** Focuses the first enabled item in the list. */
Expand Down