9
9
import { FocusKeyManager } from '@angular/cdk/a11y' ;
10
10
import { Directionality } from '@angular/cdk/bidi' ;
11
11
import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
12
- import { END , HOME } from '@angular/cdk/keycodes' ;
12
+ import { HOME , END } from '@angular/cdk/keycodes' ;
13
13
import {
14
14
AfterContentInit ,
15
15
ChangeDetectionStrategy ,
@@ -26,7 +26,7 @@ import {
26
26
ViewEncapsulation
27
27
} from '@angular/core' ;
28
28
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
29
- import { MDCChipSetFoundation } from '@material/chips' ;
29
+ import { MDCChipSetAdapter , MDCChipSetFoundation } from '@material/chips' ;
30
30
import { merge , Observable , Subscription } from 'rxjs' ;
31
31
import { startWith , takeUntil } from 'rxjs/operators' ;
32
32
import { MatChip , MatChipEvent } from './chip' ;
@@ -96,6 +96,22 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
96
96
/** Subscription to focus changes in the chips. */
97
97
private _chipFocusSubscription : Subscription | null ;
98
98
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
+
99
115
/** The FocusKeyManager which handles focus. */
100
116
_keyManager : FocusKeyManager < MatChip > ;
101
117
@@ -207,11 +223,8 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
207
223
208
224
constructor ( protected _elementRef : ElementRef ,
209
225
_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 ) ;
215
228
// Reinitialize the foundation with our overridden adapter
216
229
this . _chipSetFoundation = new MDCChipSetFoundation ( this . _chipSetAdapter ) ;
217
230
this . _updateMdcSelectionClasses ( ) ;
@@ -306,8 +319,8 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
306
319
}
307
320
308
321
/** 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 ) ;
311
324
if ( chip && chip . selected != selected ) {
312
325
chip . toggleSelected ( true ) ;
313
326
}
@@ -455,7 +468,7 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
455
468
456
469
/** Initializes the key manager to manage focus. */
457
470
private _initKeyManager ( ) {
458
- this . _keyManager = new FocusKeyManager < MatChip > ( this . _chips )
471
+ this . _keyManager = new FocusKeyManager < MatChipOption > ( this . _chips )
459
472
. withWrap ( )
460
473
. withVerticalOrientation ( )
461
474
. withHorizontalOrientation ( this . _dir ? this . _dir . value : 'ltr' ) ;
@@ -530,7 +543,7 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
530
543
this . _chipSelectionSubscription = this . chipSelectionChanges . subscribe (
531
544
( chipSelectionChange : MatChipSelectionChange ) => {
532
545
this . _chipSetFoundation . handleChipSelection (
533
- chipSelectionChange . source . id , chipSelectionChange . selected , false ) ;
546
+ chipSelectionChange . source . id , chipSelectionChange . selected ) ;
534
547
if ( chipSelectionChange . isUserInput ) {
535
548
this . _propagateChanges ( ) ;
536
549
}
0 commit comments