Skip to content

Commit 40ff7d9

Browse files
committed
feat(chips): Add remove functionality/styling.
Add events, styling and keyboard controls to allow removable chips. - Add basic styling for a user-provided remove icon. - Add keyboard controls for backspace/delete. - Add `(remove)` event which is emitted when the remove icon or one of the delete keys is pressed. - Add `md-chip-remove` directive which can be applied to `<md-icon>` (or others) to inform the chip of the remove request. Additionally, fix some issues with dark theme and add styling/support for usage inside the `md-input-container`. BREAKING CHANGE - The `selectable` property of the `md-chip-list` has now been moved to `md-chip` to maintain consistency with the new `removable` option. If you used the following code, ```html <md-chip-list [selectable]="selectable"> <md-chip>My Chip</md-chip> </md-chip-list> ``` you should switch it to ```html <md-chip-list> <md-chip [selectable]="selectable">My Chip</md-chip> </md-chip-list> ``` References #120.
1 parent dccbe41 commit 40ff7d9

File tree

12 files changed

+855
-264
lines changed

12 files changed

+855
-264
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ <h4>Advanced</h4>
2323

2424
<md-chip-list selectable="false">
2525
<md-chip color="accent" selected="true">Selected/Colored</md-chip>
26+
2627
<md-chip color="warn" selected="true" *ngIf="visible"
27-
(destroy)="alert('chip destroyed')" (click)="toggleVisible()">
28+
(destroy)="alert('chip destroyed')" (remove)="toggleVisible()">
29+
<md-icon md-chip-remove>cancel</md-icon>
2830
With Events
2931
</md-chip>
3032
</md-chip-list>
@@ -37,16 +39,28 @@ <h4>Advanced</h4>
3739
<md-card-content>
3840
<h4>Input Container</h4>
3941

40-
<md-chip-list>
41-
<md-chip *ngFor="let person of people" [color]="color">
42-
{{person.name}}
43-
</md-chip>
44-
</md-chip-list>
42+
<p>
43+
You can easily put the the <code>&lt;md-chip-list&gt;</code> inside of an
44+
<code>&lt;md-input-container&gt;</code>.
45+
</p>
4546

4647
<md-input-container>
47-
<input md-input #input (keyup.enter)="add(input)" placeholder="New Contributor..."/>
48+
<md-chip-list>
49+
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
50+
[removable]="removable" (remove)="remove(person)">
51+
{{person.name}}
52+
<md-icon md-chip-remove>cancel</md-icon>
53+
</md-chip>
54+
55+
<input md-input #input (keyup.enter)="add(input)" placeholder="Material contributors..." />
56+
</md-chip-list>
4857
</md-input-container>
4958

59+
<p>
60+
<md-checkbox name="selectable" [(ngModel)]="selectable">Selectable</md-checkbox>
61+
<md-checkbox name="removable" [(ngModel)]="removable">Removable</md-checkbox>
62+
</p>
63+
5064
<h4>Stacked Chips</h4>
5165

5266
<p>

src/demo-app/chips/chips-demo.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
.md-basic-chip {
2121
margin: auto 10px;
2222
}
23+
24+
md-chip-list input {
25+
width: 150px;
26+
}
2327
}

src/demo-app/chips/chips-demo.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface DemoColor {
1919
export class ChipsDemo {
2020
visible: boolean = true;
2121
color: string = '';
22+
selectable: boolean = true;
23+
removable: boolean = true;
2224

2325
people: Person[] = [
2426
{ name: 'Kara' },
@@ -36,6 +38,9 @@ export class ChipsDemo {
3638
{ name: 'Warn', color: 'warn' }
3739
];
3840

41+
constructor() {
42+
}
43+
3944
alert(message: string): void {
4045
alert(message);
4146
}
@@ -47,6 +52,16 @@ export class ChipsDemo {
4752
}
4853
}
4954

55+
remove(person: Person): void {
56+
if (person) {
57+
let index = this.people.indexOf(person);
58+
59+
if (index >= 0) {
60+
this.people.splice(index, 1);
61+
}
62+
}
63+
}
64+
5065
toggleVisible(): void {
5166
this.visible = false;
5267
}

src/lib/chips/_chips-theme.scss

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// The spec only provides guidance for light-themed chips. When inside of a dark theme, fall back
1818
// to standard background and foreground colors.
19-
$unselected-background: if($is-dark-theme, md-color($background, card), #e0e0e0);
19+
$unselected-background: if($is-dark-theme, #656565, #e0e0e0);
2020
$unselected-foreground: if($is-dark-theme, md-color($foreground, text), $light-foreground);
2121

2222
$selected-background: if($is-dark-theme, md-color($background, app-bar), #808080);
@@ -25,25 +25,70 @@
2525
.md-chip:not(.md-basic-chip) {
2626
background-color: $unselected-background;
2727
color: $unselected-foreground;
28+
29+
.md-chip-remove {
30+
color: $unselected-foreground;
31+
opacity: 0.3;
32+
33+
&:hover {
34+
opacity: 0.54;
35+
}
36+
}
2837
}
2938

3039
.md-chip.md-chip-selected:not(.md-basic-chip) {
3140
background-color: $selected-background;
3241
color: $selected-foreground;
3342

43+
.md-chip-remove {
44+
color: $selected-foreground;
45+
opacity: 0.4;
46+
47+
&:hover {
48+
opacity: 0.54;
49+
}
50+
}
51+
3452
&.md-primary {
3553
background-color: md-color($primary, 500);
3654
color: md-contrast($primary, 500);
55+
56+
.md-chip-remove {
57+
color: md-contrast($primary, 500);
58+
opacity: 0.4;
59+
60+
&:hover {
61+
opacity: 0.54;
62+
}
63+
}
3764
}
3865

3966
&.md-accent {
4067
background-color: md-color($accent, 500);
4168
color: md-contrast($accent, 500);
69+
70+
.md-chip-remove {
71+
color: md-contrast($accent, 500);
72+
opacity: 0.4;
73+
74+
&:hover {
75+
opacity: 0.54;
76+
}
77+
}
4278
}
4379

4480
&.md-warn {
4581
background-color: md-color($warn, 500);
4682
color: md-contrast($warn, 500);
83+
84+
.md-chip-remove {
85+
color: md-contrast($warn, 500);
86+
opacity: 0.4;
87+
88+
&:hover {
89+
opacity: 0.54;
90+
}
91+
}
4792
}
4893
}
4994
}

0 commit comments

Comments
 (0)