Skip to content
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
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