@@ -10,6 +10,7 @@ import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
10
10
import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
11
11
import {
12
12
AfterViewInit ,
13
+ AfterContentInit ,
13
14
Component ,
14
15
ChangeDetectionStrategy ,
15
16
ChangeDetectorRef ,
@@ -25,6 +26,8 @@ import {
25
26
ViewEncapsulation ,
26
27
ViewChild ,
27
28
Attribute ,
29
+ ContentChildren ,
30
+ QueryList ,
28
31
} from '@angular/core' ;
29
32
import { DOCUMENT } from '@angular/common' ;
30
33
import {
@@ -41,7 +44,7 @@ import {
41
44
RippleGlobalOptions ,
42
45
} from '@angular/material/core' ;
43
46
import { FocusMonitor } from '@angular/cdk/a11y' ;
44
- import { Subject } from 'rxjs' ;
47
+ import { merge , Subject , Subscription } from 'rxjs' ;
45
48
import { take } from 'rxjs/operators' ;
46
49
import { MatChipAvatar , MatChipTrailingIcon , MatChipRemove } from './chip-icons' ;
47
50
import { MatChipAction } from './chip-action' ;
@@ -112,7 +115,14 @@ const _MatChipMixinBase = mixinTabIndex(
112
115
} )
113
116
export class MatChip
114
117
extends _MatChipMixinBase
115
- implements AfterViewInit , CanColor , CanDisableRipple , CanDisable , HasTabIndex , OnDestroy
118
+ implements
119
+ AfterViewInit ,
120
+ AfterContentInit ,
121
+ CanColor ,
122
+ CanDisableRipple ,
123
+ CanDisable ,
124
+ HasTabIndex ,
125
+ OnDestroy
116
126
{
117
127
protected _document : Document ;
118
128
@@ -137,9 +147,24 @@ export class MatChip
137
147
/** Whether moving focus into the chip is pending. */
138
148
private _pendingFocus : boolean ;
139
149
150
+ /** Subscription to changes in the chip's actions. */
151
+ private _actionChanges : Subscription | undefined ;
152
+
140
153
/** Whether animations for the chip are enabled. */
141
154
_animationsDisabled : boolean ;
142
155
156
+ /** All avatars present in the chip. */
157
+ @ContentChildren ( MAT_CHIP_AVATAR , { descendants : true } )
158
+ protected _allLeadingIcons : QueryList < MatChipAvatar > ;
159
+
160
+ /** All trailing icons present in the chip. */
161
+ @ContentChildren ( MAT_CHIP_TRAILING_ICON , { descendants : true } )
162
+ protected _allTrailingIcons : QueryList < MatChipTrailingIcon > ;
163
+
164
+ /** All remove icons present in the chip. */
165
+ @ContentChildren ( MAT_CHIP_REMOVE , { descendants : true } )
166
+ protected _allRemoveIcons : QueryList < MatChipRemove > ;
167
+
143
168
_hasFocus ( ) {
144
169
return this . _hasFocusInternal ;
145
170
}
@@ -259,8 +284,19 @@ export class MatChip
259
284
}
260
285
}
261
286
287
+ ngAfterContentInit ( ) : void {
288
+ // Since the styling depends on the presence of some
289
+ // actions, we have to mark for check on changes.
290
+ this . _actionChanges = merge (
291
+ this . _allLeadingIcons . changes ,
292
+ this . _allTrailingIcons . changes ,
293
+ this . _allRemoveIcons . changes ,
294
+ ) . subscribe ( ( ) => this . _changeDetectorRef . markForCheck ( ) ) ;
295
+ }
296
+
262
297
ngOnDestroy ( ) {
263
298
this . _focusMonitor . stopMonitoring ( this . _elementRef ) ;
299
+ this . _actionChanges ?. unsubscribe ( ) ;
264
300
this . destroyed . emit ( { chip : this } ) ;
265
301
this . destroyed . complete ( ) ;
266
302
}
0 commit comments