@@ -10,9 +10,11 @@ import {FocusableOption} from '@angular/cdk/a11y';
1010import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
1111import { BACKSPACE , DELETE , SPACE } from '@angular/cdk/keycodes' ;
1212import {
13+ ContentChild ,
1314 Directive ,
1415 ElementRef ,
1516 EventEmitter ,
17+ forwardRef ,
1618 Input ,
1719 OnDestroy ,
1820 Output ,
@@ -47,17 +49,27 @@ export class MatChipBase {
4749
4850export const _MatChipMixinBase = mixinColor ( mixinDisabled ( MatChipBase ) , 'primary' ) ;
4951
52+ const CHIP_ATTRIBUTE_NAMES = [ 'mat-basic-chip' ] ;
5053
5154/**
52- * Dummy directive to add CSS class to basic chips .
55+ * Dummy directive to add CSS class to chip avatar .
5356 * @docs -private
5457 */
5558@Directive ( {
56- selector : ` mat-basic- chip, [mat-basic-chip]` ,
57- host : { 'class' : 'mat-basic- chip' } ,
59+ selector : ' mat-chip-avatar , [matChipAvatar]' ,
60+ host : { 'class' : 'mat-chip-avatar' }
5861} )
59- export class MatBasicChip {
60- }
62+ export class MatChipAvatar { }
63+
64+ /**
65+ * Dummy directive to add CSS class to chip trailing icon.
66+ * @docs -private
67+ */
68+ @Directive ( {
69+ selector : 'mat-chip-trailing-icon, [matChipTrailingIcon]' ,
70+ host : { 'class' : 'mat-chip-trailing-icon' }
71+ } )
72+ export class MatChipTrailingIcon { }
6173
6274/**
6375 * Material design styled Chip component. Used inside the MatChipList component.
@@ -71,6 +83,8 @@ export class MatBasicChip {
7183 '[attr.tabindex]' : 'disabled ? null : -1' ,
7284 'role' : 'option' ,
7385 '[class.mat-chip-selected]' : 'selected' ,
86+ '[class.mat-chip-with-avatar]' : 'avatar' ,
87+ '[class.mat-chip-with-trailing-icon]' : 'trailingIcon || removeIcon' ,
7488 '[attr.disabled]' : 'disabled || null' ,
7589 '[attr.aria-disabled]' : 'disabled.toString()' ,
7690 '[attr.aria-selected]' : 'ariaSelected' ,
@@ -86,6 +100,15 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
86100 /** Whether the chip has focus. */
87101 _hasFocus : boolean = false ;
88102
103+ /** The chip avatar */
104+ @ContentChild ( MatChipAvatar ) avatar : MatChipAvatar ;
105+
106+ /** The chip's trailing icon. */
107+ @ContentChild ( MatChipTrailingIcon ) trailingIcon : MatChipTrailingIcon ;
108+
109+ /** The chip's remove toggler. */
110+ @ContentChild ( forwardRef ( ( ) => MatChipRemove ) ) removeIcon : MatChipRemove ;
111+
89112 /** Whether the chip is selected. */
90113 @Input ( )
91114 get selected ( ) : boolean { return this . _selected ; }
@@ -167,6 +190,20 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
167190
168191 constructor ( public _elementRef : ElementRef ) {
169192 super ( _elementRef ) ;
193+
194+ this . _addHostClassName ( ) ;
195+ }
196+
197+ _addHostClassName ( ) {
198+ // Add class for the different chips
199+ for ( const attr of CHIP_ATTRIBUTE_NAMES ) {
200+ if ( this . _elementRef . nativeElement . hasAttribute ( attr ) ||
201+ this . _elementRef . nativeElement . tagName . toLowerCase ( ) === attr ) {
202+ ( this . _elementRef . nativeElement as HTMLElement ) . classList . add ( attr ) ;
203+ return ;
204+ }
205+ }
206+ ( this . _elementRef . nativeElement as HTMLElement ) . classList . add ( 'mat-standard-chip' ) ;
170207 }
171208
172209 ngOnDestroy ( ) {
@@ -297,7 +334,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
297334@Directive ( {
298335 selector : '[matChipRemove]' ,
299336 host : {
300- 'class' : 'mat-chip-remove' ,
337+ 'class' : 'mat-chip-remove mat-chip-trailing-icon ' ,
301338 '(click)' : '_handleClick()' ,
302339 }
303340} )
0 commit comments