Skip to content

Commit 47f528e

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` and add styling for focused chips. 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 47f528e

File tree

12 files changed

+870
-268
lines changed

12 files changed

+870
-268
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: 10 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' },
@@ -47,6 +49,14 @@ export class ChipsDemo {
4749
}
4850
}
4951

52+
remove(person: Person): void {
53+
let index = this.people.indexOf(person);
54+
55+
if (index >= 0) {
56+
this.people.splice(index, 1);
57+
}
58+
}
59+
5060
toggleVisible(): void {
5161
this.visible = false;
5262
}

src/lib/chips/_chips-theme.scss

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
@import '../core/theming/palette';
22
@import '../core/theming/theming';
33

4+
//@import './chips';
5+
46
@mixin md-chips-theme($theme) {
57
$is-dark-theme: map-get($theme, is-dark);
68
$primary: map-get($theme, primary);
@@ -16,34 +18,90 @@
1618

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

2224
$selected-background: if($is-dark-theme, md-color($background, app-bar), #808080);
2325
$selected-foreground: if($is-dark-theme, md-color($foreground, text), $light-selected-foreground);
2426

27+
$focus-color: md-color($foreground, text);
28+
2529
.md-chip:not(.md-basic-chip) {
2630
background-color: $unselected-background;
2731
color: $unselected-foreground;
32+
33+
.md-chip-focus-border {
34+
pointer-events: none;
35+
}
36+
37+
&:focus {
38+
outline: none;
39+
border: 2px solid $focus-color;
40+
}
41+
42+
.md-chip-remove {
43+
color: $unselected-foreground;
44+
opacity: 0.3;
45+
46+
&:hover {
47+
opacity: 0.54;
48+
}
49+
}
2850
}
2951

3052
.md-chip.md-chip-selected:not(.md-basic-chip) {
3153
background-color: $selected-background;
3254
color: $selected-foreground;
3355

56+
.md-chip-remove {
57+
color: $selected-foreground;
58+
opacity: 0.4;
59+
60+
&:hover {
61+
opacity: 0.54;
62+
}
63+
}
64+
3465
&.md-primary {
3566
background-color: md-color($primary, 500);
3667
color: md-contrast($primary, 500);
68+
69+
.md-chip-remove {
70+
color: md-contrast($primary, 500);
71+
opacity: 0.4;
72+
73+
&:hover {
74+
opacity: 0.54;
75+
}
76+
}
3777
}
3878

3979
&.md-accent {
4080
background-color: md-color($accent, 500);
4181
color: md-contrast($accent, 500);
82+
83+
.md-chip-remove {
84+
color: md-contrast($accent, 500);
85+
opacity: 0.4;
86+
87+
&:hover {
88+
opacity: 0.54;
89+
}
90+
}
4291
}
4392

4493
&.md-warn {
4594
background-color: md-color($warn, 500);
4695
color: md-contrast($warn, 500);
96+
97+
.md-chip-remove {
98+
color: md-contrast($warn, 500);
99+
opacity: 0.4;
100+
101+
&:hover {
102+
opacity: 0.54;
103+
}
104+
}
47105
}
48106
}
49107
}

0 commit comments

Comments
 (0)