99import { FocusKeyManager } from '@angular/cdk/a11y' ;
1010import { Directionality } from '@angular/cdk/bidi' ;
1111import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
12- import { END , HOME } from '@angular/cdk/keycodes' ;
12+ import { HOME , END } from '@angular/cdk/keycodes' ;
1313import {
1414 AfterContentInit ,
1515 ChangeDetectionStrategy ,
@@ -26,7 +26,7 @@ import {
2626 ViewEncapsulation
2727} from '@angular/core' ;
2828import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
29- import { MDCChipSetFoundation } from '@material/chips' ;
29+ import { MDCChipSetAdapter , MDCChipSetFoundation } from '@material/chips' ;
3030import { merge , Observable , Subscription } from 'rxjs' ;
3131import { startWith , takeUntil } from 'rxjs/operators' ;
3232import { MatChip , MatChipEvent } from './chip' ;
@@ -96,6 +96,22 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
9696 /** Subscription to focus changes in the chips. */
9797 private _chipFocusSubscription : Subscription | null ;
9898
99+ /**
100+ * Implementation of the MDC chip-set adapter interface.
101+ * These methods are called by the chip set foundation.
102+ *
103+ * Overrides the base MatChipSet adapter to provide a setSelected method.
104+ */
105+ protected _chipSetAdapter : MDCChipSetAdapter = {
106+ hasClass : ( className : string ) => this . _hasMdcClass ( className ) ,
107+ // No-op. We keep track of chips via ContentChildren, which will be updated when a chip is
108+ // removed.
109+ removeChip : ( ) => { } ,
110+ setSelected : ( chipId : string , selected : boolean ) => {
111+ this . _setSelected ( chipId , selected ) ;
112+ }
113+ } ;
114+
99115 /** The FocusKeyManager which handles focus. */
100116 _keyManager : FocusKeyManager < MatChip > ;
101117
@@ -207,11 +223,8 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
207223
208224 constructor ( protected _elementRef : ElementRef ,
209225 _changeDetectorRef : ChangeDetectorRef ,
210- @Optional ( ) _dir : Directionality ) {
211- super ( _elementRef , _changeDetectorRef , _dir ) ;
212- this . _chipSetAdapter . selectChipAtIndex = ( index : number , selected : boolean ) => {
213- this . _setSelected ( index , selected ) ;
214- } ;
226+ @Optional ( ) private _dir : Directionality ) {
227+ super ( _elementRef , _changeDetectorRef ) ;
215228 // Reinitialize the foundation with our overridden adapter
216229 this . _chipSetFoundation = new MDCChipSetFoundation ( this . _chipSetAdapter ) ;
217230 this . _updateMdcSelectionClasses ( ) ;
@@ -306,8 +319,8 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
306319 }
307320
308321 /** Selects or deselects a chip by id. */
309- _setSelected ( index : number , selected : boolean ) {
310- const chip = this . _chips . toArray ( ) [ index ] ;
322+ _setSelected ( chipId : string , selected : boolean ) {
323+ const chip = this . _chips . find ( c => c . id === chipId ) ;
311324 if ( chip && chip . selected != selected ) {
312325 chip . toggleSelected ( true ) ;
313326 }
@@ -455,7 +468,7 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
455468
456469 /** Initializes the key manager to manage focus. */
457470 private _initKeyManager ( ) {
458- this . _keyManager = new FocusKeyManager < MatChip > ( this . _chips )
471+ this . _keyManager = new FocusKeyManager < MatChipOption > ( this . _chips )
459472 . withWrap ( )
460473 . withVerticalOrientation ( )
461474 . withHorizontalOrientation ( this . _dir ? this . _dir . value : 'ltr' ) ;
@@ -530,7 +543,7 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
530543 this . _chipSelectionSubscription = this . chipSelectionChanges . subscribe (
531544 ( chipSelectionChange : MatChipSelectionChange ) => {
532545 this . _chipSetFoundation . handleChipSelection (
533- chipSelectionChange . source . id , chipSelectionChange . selected , false ) ;
546+ chipSelectionChange . source . id , chipSelectionChange . selected ) ;
534547 if ( chipSelectionChange . isUserInput ) {
535548 this . _propagateChanges ( ) ;
536549 }
0 commit comments