Skip to content

Commit c9e237b

Browse files
tinayuangaommalerba
authored andcommitted
feat(chips): Add chip avatar and chip trailing icon (#9557)
* feat(chips): Add chip avatar and chip trailing icon * Removed MatBasicChip and MatStandardChip * Add mat-chip-trailing-icon style to MatChipRemove and add examples in demo
1 parent 966a160 commit c9e237b

File tree

7 files changed

+204
-62
lines changed

7 files changed

+204
-62
lines changed

src/demo-app/chips/chips-demo.html

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h4>Simple</h4>
88
<mat-chip-list>
99
<mat-chip>Chip 1</mat-chip>
1010
<mat-chip>Chip 2</mat-chip>
11-
<mat-chip>Chip 3</mat-chip>
11+
<mat-chip disabled>Chip 3</mat-chip>
1212
</mat-chip-list>
1313

1414
<h4>Unstyled</h4>
@@ -31,6 +31,56 @@ <h4>Advanced</h4>
3131
</mat-chip>
3232
</mat-chip-list>
3333
<div>{{message}}</div>
34+
35+
<h4>With avatar and icons</h4>
36+
37+
38+
<mat-chip-list>
39+
<mat-chip>
40+
<mat-icon matChipAvatar>home</mat-icon>
41+
Home
42+
<mat-icon matChipRemove>cancel</mat-icon>
43+
</mat-chip>
44+
<mat-chip color="accent" selected="true">
45+
<mat-chip-avatar>P</mat-chip-avatar>
46+
Portel
47+
<mat-icon matChipRemove>cancel</mat-icon>
48+
</mat-chip>
49+
50+
<mat-chip>
51+
<mat-chip-avatar>M</mat-chip-avatar>
52+
Molly
53+
</mat-chip>
54+
55+
<mat-chip>
56+
Koby
57+
<mat-icon matChipRemove>cancel</mat-icon>
58+
</mat-chip>
59+
60+
<mat-chip>
61+
Razzle
62+
</mat-chip>
63+
64+
<mat-chip>
65+
<img src="https://material.angularjs.org/material2_assets/ngconf/Mal.png" matChipAvatar>
66+
Mal
67+
</mat-chip>
68+
69+
<mat-chip selected="true" color="warn">
70+
<img src="https://material.angularjs.org/material2_assets/ngconf/Husi.png" matChipAvatar>
71+
Husi
72+
<mat-icon matChipRemove>cancel</mat-icon>
73+
</mat-chip>
74+
75+
<mat-chip>
76+
Good
77+
<mat-icon matChipTrailingIcon>star</mat-icon>
78+
</mat-chip>
79+
<mat-chip>
80+
Bad
81+
<mat-icon matChipTrailingIcon>star_border</mat-icon>
82+
</mat-chip>
83+
</mat-chip-list>
3484
</mat-card-content>
3585
</mat-card>
3686

src/lib/chips/_chips-theme.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ $mat-chip-remove-font-size: 18px;
3737
$unselected-foreground: mat-color($foreground, text);
3838

3939

40-
.mat-chip:not(.mat-basic-chip) {
40+
.mat-chip.mat-standard-chip {
4141
@include mat-chips-color($unselected-foreground, $unselected-background);
4242
}
4343

44-
.mat-chip.mat-chip-selected {
44+
.mat-chip.mat-standard-chip.mat-chip-selected {
4545

4646
&.mat-primary {
4747
@include mat-chips-theme-color($primary);
@@ -62,6 +62,7 @@ $mat-chip-remove-font-size: 18px;
6262
font-size: $mat-chip-font-size;
6363
line-height: $mat-chip-line-height;
6464

65+
.mat-chip-trailing-icon.mat-icon,
6566
.mat-chip-remove.mat-icon {
6667
font-size: $mat-chip-remove-font-size;
6768
}

src/lib/chips/chip-list.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
DoCheck,
2222
ElementRef,
2323
EventEmitter,
24+
Inject,
2425
Input,
2526
OnDestroy,
2627
OnInit,
@@ -747,3 +748,4 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
747748
});
748749
}
749750
}
751+

src/lib/chips/chip.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import {FocusableOption} from '@angular/cdk/a11y';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111
import {BACKSPACE, DELETE, SPACE} from '@angular/cdk/keycodes';
1212
import {
13+
ContentChild,
1314
Directive,
1415
ElementRef,
1516
EventEmitter,
17+
forwardRef,
1618
Input,
1719
OnDestroy,
1820
Output,
@@ -47,17 +49,27 @@ export class MatChipBase {
4749

4850
export 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
})

src/lib/chips/chips-module.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99
import {NgModule} from '@angular/core';
1010
import {ErrorStateMatcher} from '@angular/material/core';
1111
import {MatChipList} from './chip-list';
12-
import {MatBasicChip, MatChip, MatChipRemove} from './chip';
12+
import {MatChip, MatChipRemove, MatChipAvatar, MatChipTrailingIcon} from './chip';
1313
import {MatChipInput} from './chip-input';
1414

15+
const CHIP_DECLARATIONS = [
16+
MatChipList,
17+
MatChip,
18+
MatChipInput,
19+
MatChipRemove,
20+
MatChipAvatar,
21+
MatChipTrailingIcon,
22+
];
1523

1624
@NgModule({
1725
imports: [],
18-
exports: [MatChipList, MatChip, MatChipInput, MatChipRemove, MatChipRemove, MatBasicChip],
19-
declarations: [MatChipList, MatChip, MatChipInput, MatChipRemove, MatChipRemove, MatBasicChip],
26+
exports: CHIP_DECLARATIONS,
27+
declarations: CHIP_DECLARATIONS,
2028
providers: [ErrorStateMatcher]
2129
})
2230
export class MatChipsModule {}

0 commit comments

Comments
 (0)