Skip to content

feat(chips): Add chip avatar and chip trailing icon #9557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/demo-app/chips/chips-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h4>Simple</h4>
<mat-chip-list>
<mat-chip>Chip 1</mat-chip>
<mat-chip>Chip 2</mat-chip>
<mat-chip>Chip 3</mat-chip>
<mat-chip disabled>Chip 3</mat-chip>
</mat-chip-list>

<h4>Unstyled</h4>
Expand All @@ -31,6 +31,56 @@ <h4>Advanced</h4>
</mat-chip>
</mat-chip-list>
<div>{{message}}</div>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add color example with avatar

<h4>With avatar and icons</h4>


<mat-chip-list>
<mat-chip>
<mat-icon matChipAvatar>home</mat-icon>
Home
<mat-icon matChipRemove>cancel</mat-icon>
</mat-chip>
<mat-chip color="accent" selected="true">
<mat-chip-avatar>P</mat-chip-avatar>
Portel
<mat-icon matChipRemove>cancel</mat-icon>
</mat-chip>

<mat-chip>
<mat-chip-avatar>M</mat-chip-avatar>
Molly
</mat-chip>

<mat-chip>
Koby
<mat-icon matChipRemove>cancel</mat-icon>
</mat-chip>

<mat-chip>
Razzle
</mat-chip>

<mat-chip>
<img src="https://material.angularjs.org/material2_assets/ngconf/Mal.png" matChipAvatar>
Mal
</mat-chip>

<mat-chip selected="true" color="warn">
<img src="https://material.angularjs.org/material2_assets/ngconf/Husi.png" matChipAvatar>
Husi
<mat-icon matChipRemove>cancel</mat-icon>
</mat-chip>

<mat-chip>
Good
<mat-icon matChipTrailingIcon>star</mat-icon>
</mat-chip>
<mat-chip>
Bad
<mat-icon matChipTrailingIcon>star_border</mat-icon>
</mat-chip>
</mat-chip-list>
</mat-card-content>
</mat-card>

Expand Down
5 changes: 3 additions & 2 deletions src/lib/chips/_chips-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ $mat-chip-remove-font-size: 18px;
$unselected-foreground: mat-color($foreground, text);


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

.mat-chip.mat-chip-selected {
.mat-chip.mat-standard-chip.mat-chip-selected {

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

.mat-chip-trailing-icon.mat-icon,
.mat-chip-remove.mat-icon {
font-size: $mat-chip-remove-font-size;
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
DoCheck,
ElementRef,
EventEmitter,
Inject,
Input,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -752,3 +753,4 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
});
}
}

49 changes: 43 additions & 6 deletions src/lib/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {FocusableOption} from '@angular/cdk/a11y';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {BACKSPACE, DELETE, SPACE} from '@angular/cdk/keycodes';
import {
ContentChild,
Directive,
ElementRef,
EventEmitter,
forwardRef,
Input,
OnDestroy,
Output,
Expand Down Expand Up @@ -47,17 +49,27 @@ export class MatChipBase {

export const _MatChipMixinBase = mixinColor(mixinDisabled(MatChipBase), 'primary');

const CHIP_ATTRIBUTE_NAMES = ['mat-basic-chip'];

/**
* Dummy directive to add CSS class to basic chips.
* Dummy directive to add CSS class to chip avatar.
* @docs-private
*/
@Directive({
selector: `mat-basic-chip, [mat-basic-chip]`,
host: {'class': 'mat-basic-chip'},
selector: 'mat-chip-avatar, [matChipAvatar]',
host: {'class': 'mat-chip-avatar'}
})
export class MatBasicChip {
}
export class MatChipAvatar {}

/**
* Dummy directive to add CSS class to chip trailing icon.
* @docs-private
*/
@Directive({
selector: 'mat-chip-trailing-icon, [matChipTrailingIcon]',
host: {'class': 'mat-chip-trailing-icon'}
})
export class MatChipTrailingIcon {}

/**
* Material design styled Chip component. Used inside the MatChipList component.
Expand All @@ -71,6 +83,8 @@ export class MatBasicChip {
'[attr.tabindex]': 'disabled ? null : -1',
'role': 'option',
'[class.mat-chip-selected]': 'selected',
'[class.mat-chip-with-avatar]': 'avatar',
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.aria-selected]': 'ariaSelected',
Expand All @@ -95,6 +109,15 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
/** Whether the chip has focus. */
_hasFocus: boolean = false;

/** The chip avatar */
@ContentChild(MatChipAvatar) avatar: MatChipAvatar;

/** The chip's trailing icon. */
@ContentChild(MatChipTrailingIcon) trailingIcon: MatChipTrailingIcon;

/** The chip's remove toggler. */
@ContentChild(forwardRef(() => MatChipRemove)) removeIcon: MatChipRemove;

/** Whether the chip is selected. */
@Input()
get selected(): boolean { return this._selected; }
Expand Down Expand Up @@ -170,6 +193,20 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes

constructor(public _elementRef: ElementRef) {
super(_elementRef);

this._addHostClassName();
}

_addHostClassName() {
// Add class for the different chips
for (const attr of CHIP_ATTRIBUTE_NAMES) {
if (this._elementRef.nativeElement.hasAttribute(attr) ||
this._elementRef.nativeElement.tagName.toLowerCase() === attr) {
(this._elementRef.nativeElement as HTMLElement).classList.add(attr);
return;
}
}
(this._elementRef.nativeElement as HTMLElement).classList.add('mat-standard-chip');
}

ngOnDestroy(): void {
Expand Down Expand Up @@ -300,7 +337,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
@Directive({
selector: '[matChipRemove]',
host: {
'class': 'mat-chip-remove',
'class': 'mat-chip-remove mat-chip-trailing-icon',
'(click)': '_handleClick()',
}
})
Expand Down
14 changes: 11 additions & 3 deletions src/lib/chips/chips-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
import {NgModule} from '@angular/core';
import {ErrorStateMatcher} from '@angular/material/core';
import {MatChipList} from './chip-list';
import {MatBasicChip, MatChip, MatChipRemove} from './chip';
import {MatChip, MatChipRemove, MatChipAvatar, MatChipTrailingIcon} from './chip';
import {MatChipInput} from './chip-input';

const CHIP_DECLARATIONS = [
MatChipList,
MatChip,
MatChipInput,
MatChipRemove,
MatChipAvatar,
MatChipTrailingIcon,
];

@NgModule({
imports: [],
exports: [MatChipList, MatChip, MatChipInput, MatChipRemove, MatChipRemove, MatBasicChip],
declarations: [MatChipList, MatChip, MatChipInput, MatChipRemove, MatChipRemove, MatBasicChip],
exports: CHIP_DECLARATIONS,
declarations: CHIP_DECLARATIONS,
providers: [ErrorStateMatcher]
})
export class MatChipsModule {}
Loading