@@ -11,24 +11,23 @@ import {
1111 ChangeDetectionStrategy ,
1212 Component ,
1313 ContentChildren ,
14- Directive ,
1514 Input ,
1615 QueryList ,
1716 ViewEncapsulation ,
1817 OnDestroy ,
18+ Optional ,
1919 ElementRef ,
2020 Renderer2 ,
2121} from '@angular/core' ;
2222
2323import { MdChip } from './chip' ;
2424import { FocusKeyManager } from '../core/a11y/focus-key-manager' ;
25- import { SPACE , LEFT_ARROW , RIGHT_ARROW , TAB } from '../core/keyboard/keycodes' ;
2625import { coerceBooleanProperty } from '@angular/cdk' ;
2726import { Subscription } from 'rxjs/Subscription' ;
2827import {
29- LEFT_ARROW , RIGHT_ARROW , SPACE , BACKSPACE , DELETE , UP_ARROW , DOWN_ARROW
28+ LEFT_ARROW , RIGHT_ARROW , BACKSPACE , DELETE , UP_ARROW , DOWN_ARROW
3029} from '../core/keyboard/keycodes' ;
31- import { Dir } from '../core/rtl/dir ' ;
30+ import { Directionality } from '../core' ;
3231
3332/** Utility to check if an input element has no value. */
3433function _isInputEmpty ( element : HTMLElement ) : boolean {
@@ -60,7 +59,7 @@ function _isInputEmpty(element: HTMLElement): boolean {
6059 'role' : 'listbox' ,
6160 'class' : 'mat-chip-list' ,
6261
63- '(focus)' : 'focus($event )' ,
62+ '(focus)' : 'focus()' ,
6463 '(keydown)' : '_keydown($event)'
6564 } ,
6665 queries : {
@@ -73,7 +72,7 @@ function _isInputEmpty(element: HTMLElement): boolean {
7372export class MdChipList implements AfterContentInit , OnDestroy {
7473
7574 /** When a chip is destroyed, we track the index so we can focus the appropriate next chip. */
76- protected _lastDestroyedIndex : number = null ;
75+ protected _lastDestroyedIndex : number | null = null ;
7776
7877 /** Track which chips we're listening to for focus/destruction. */
7978 protected _chipSet : WeakMap < MdChip , boolean > = new WeakMap ( ) ;
@@ -96,7 +95,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
9695 chips : QueryList < MdChip > ;
9796
9897 constructor ( protected _renderer : Renderer2 , protected _elementRef : ElementRef ,
99- protected _dir : Dir ) {
98+ @ Optional ( ) private _dir : Directionality ) {
10099 }
101100
102101 ngAfterContentInit ( ) : void {
@@ -157,7 +156,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
157156 * Focuses the the first non-disabled chip in this chip list, or the associated input when there
158157 * are no eligible chips.
159158 */
160- focus ( event ?: Event ) {
159+ focus ( ) {
161160 // TODO: ARIA says this should focus the first `selected` chip if any are selected.
162161 if ( this . chips . length > 0 ) {
163162 this . _keyManager . setFirstItemActive ( ) ;
@@ -180,7 +179,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
180179 let code = event . keyCode ;
181180 let target = event . target as HTMLElement ;
182181 let isInputEmpty = _isInputEmpty ( target ) ;
183- let isRtl = this . _dir . value == 'rtl' ;
182+ let isRtl = this . _dir && this . _dir . value == 'rtl' ;
184183
185184 let isPrevKey = ( code == ( isRtl ? RIGHT_ARROW : LEFT_ARROW ) ) ;
186185 let isNextKey = ( code == ( isRtl ? LEFT_ARROW : RIGHT_ARROW ) ) ;
@@ -258,15 +257,19 @@ export class MdChipList implements AfterContentInit, OnDestroy {
258257 // On destroy, remove the item from our list, and setup our destroyed focus check
259258 chip . destroy . subscribe ( ( ) => {
260259 let chipIndex : number = this . chips . toArray ( ) . indexOf ( chip ) ;
261-
262- if ( this . _isValidIndex ( chipIndex ) && chip . _hasFocus ) {
263- // Check whether the chip is the last item
264- if ( chipIndex < this . chips . length - 1 ) {
265- this . _keyManager . setActiveItem ( chipIndex ) ;
266- } else if ( chipIndex - 1 >= 0 ) {
267- this . _keyManager . setActiveItem ( chipIndex - 1 ) ;
260+ if ( this . _isValidIndex ( chipIndex ) ) {
261+ if ( chip . _hasFocus ) {
262+ // Check whether the chip is the last item
263+ if ( chipIndex < this . chips . length - 1 ) {
264+ this . _keyManager . setActiveItem ( chipIndex ) ;
265+ } else if ( chipIndex - 1 >= 0 ) {
266+ this . _keyManager . setActiveItem ( chipIndex - 1 ) ;
267+ }
268268 }
269- this . _lastDestroyedIndex = chipIndex ;
269+ if ( this . _keyManager . activeItemIndex == chipIndex ) {
270+ this . _lastDestroyedIndex = chipIndex ;
271+ }
272+
270273 }
271274
272275 this . _chipSet . delete ( chip ) ;
@@ -282,12 +285,12 @@ export class MdChipList implements AfterContentInit, OnDestroy {
282285 */
283286 protected _updateFocusForDestroyedChips ( ) {
284287 let chipsArray = this . chips ;
285- let focusChip : MdChip ;
286288
287289 if ( this . _lastDestroyedIndex != null && chipsArray . length > 0 ) {
288290 // Check whether the destroyed chip was the last item
289291 const newFocusIndex = Math . min ( this . _lastDestroyedIndex , chipsArray . length - 1 ) ;
290292 this . _keyManager . setActiveItem ( newFocusIndex ) ;
293+ let focusChip = this . _keyManager . activeItem ;
291294
292295 // Focus the chip
293296 if ( focusChip ) {
0 commit comments