Skip to content

Commit

Permalink
fix(chips): support focusing first/last item using home/end (#11892)
Browse files Browse the repository at this point in the history
Based on the accessibility guidelines, grid cells should support moving focus to the first/last items via the Home and End keys.
  • Loading branch information
crisbeto authored and jelbourn committed Aug 29, 2018
1 parent 0fcdae4 commit b735e48
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
42 changes: 41 additions & 1 deletion src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import {FocusKeyManager} from '@angular/cdk/a11y';
import {Directionality, Direction} from '@angular/cdk/bidi';
import {BACKSPACE, DELETE, ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE, TAB} from '@angular/cdk/keycodes';
import {
BACKSPACE,
DELETE,
ENTER,
LEFT_ARROW,
RIGHT_ARROW,
SPACE,
TAB,
HOME,
END,
} from '@angular/cdk/keycodes';
import {
createKeyboardEvent,
dispatchFakeEvent,
Expand Down Expand Up @@ -296,6 +306,36 @@ describe('MatChipList', () => {
.toBe(initialActiveIndex, 'Expected focused item not to have changed.');
});

it('should focus the first item when pressing HOME', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
const HOME_EVENT = createKeyboardEvent('keydown', HOME, lastNativeChip);
const array = chips.toArray();
const lastItem = array[array.length - 1];

lastItem.focus();
expect(manager.activeItemIndex).toBe(array.length - 1);

chipListInstance._keydown(HOME_EVENT);
fixture.detectChanges();

expect(manager.activeItemIndex).toBe(0);
expect(HOME_EVENT.defaultPrevented).toBe(true);
});

it('should focus the last item when pressing END', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const END_EVENT = createKeyboardEvent('keydown', END, nativeChips[0]);

expect(manager.activeItemIndex).toBe(-1);

chipListInstance._keydown(END_EVENT);
fixture.detectChanges();

expect(manager.activeItemIndex).toBe(chips.length - 1);
expect(END_EVENT.defaultPrevented).toBe(true);
});

});

describe('RTL', () => {
Expand Down
13 changes: 11 additions & 2 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {FocusKeyManager} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {SelectionModel} from '@angular/cdk/collections';
import {BACKSPACE} from '@angular/cdk/keycodes';
import {BACKSPACE, HOME, END} from '@angular/cdk/keycodes';
import {
AfterContentInit,
ChangeDetectionStrategy,
Expand Down Expand Up @@ -483,7 +483,16 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
this._keyManager.setLastItemActive();
event.preventDefault();
} else if (target && target.classList.contains('mat-chip')) {
this._keyManager.onKeydown(event);
if (event.keyCode === HOME) {
this._keyManager.setFirstItemActive();
event.preventDefault();
} else if (event.keyCode === END) {
this._keyManager.setLastItemActive();
event.preventDefault();
} else {
this._keyManager.onKeydown(event);
}

this.stateChanges.next();
}
}
Expand Down

0 comments on commit b735e48

Please sign in to comment.