Skip to content

Commit 68aa7b5

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. Add new directive `mdChipInput` for controlling: - `(chipAdded)` - Event fired when a chip should be added. - `[separatorKeys]` - The list of keycodes that will fire the `(chipAdded)` event. - `[addOnBlur]` - Whether or not to fire the `(chipAdded)` event when the input is blurred. 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 f24832c commit 68aa7b5

File tree

14 files changed

+940
-280
lines changed

14 files changed

+940
-280
lines changed

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

Lines changed: 23 additions & 8 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,29 @@ <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>
46+
47+
<md-input-container [floatPlaceholder]="people.length > 0 ? 'always' : 'auto'">
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 mdChipRemove>cancel</md-icon>
53+
</md-chip>
4554

46-
<md-input-container>
47-
<input mdInput #input (keyup.enter)="add(input)" placeholder="New Contributor..."/>
55+
<input mdInput placeholder="New Contributor..."
56+
mdChipInput (chipAdded)="add($event)" [separatorKeys]="keys" [addOnBlur]="true" />
57+
</md-chip-list>
4858
</md-input-container>
4959

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

5267
<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
.mat-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: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Component, ElementRef} from '@angular/core';
1+
import {Component} from '@angular/core';
2+
import {MdChipInputEvent, ENTER, COMMA} from "@angular/material";
23

34
export interface Person {
45
name: string;
@@ -18,6 +19,9 @@ export interface DemoColor {
1819
export class ChipsDemo {
1920
visible: boolean = true;
2021
color: string = '';
22+
selectable: boolean = true;
23+
removable: boolean = true;
24+
keys = [ENTER, COMMA, 186];
2125

2226
people: Person[] = [
2327
{ name: 'Kara' },
@@ -39,10 +43,26 @@ export class ChipsDemo {
3943
alert(message);
4044
}
4145

42-
add(input: ElementRef): void {
43-
if (input.nativeElement.value && input.nativeElement.value.trim() != '') {
44-
this.people.push({ name: input.nativeElement.value.trim() });
45-
input.nativeElement.value = '';
46+
add(event: MdChipInputEvent): void {
47+
let input = event.input;
48+
let value = event.value;
49+
50+
// Add our person
51+
if (value && value.trim() != '') {
52+
this.people.push({ name: value.trim() });
53+
}
54+
55+
// Reset the input value
56+
if (input) {
57+
input.value = '';
58+
}
59+
}
60+
61+
remove(person: Person): void {
62+
let index = this.people.indexOf(person);
63+
64+
if (index >= 0) {
65+
this.people.splice(index, 1);
4666
}
4767
}
4868

src/lib/chips/_chips-theme.scss

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,90 @@
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, mat-color($background, card), #e0e0e0);
19+
$unselected-background: if($is-dark-theme, #656565, #e0e0e0);
2020
$unselected-foreground: if($is-dark-theme, mat-color($foreground, text), $light-foreground);
2121

2222
$selected-background: if($is-dark-theme, mat-color($background, app-bar), #808080);
2323
$selected-foreground: if($is-dark-theme, mat-color($foreground, text), $light-selected-foreground);
2424

25+
$focus-color: mat-color($foreground, text);
26+
2527
.mat-chip:not(.mat-basic-chip) {
2628
background-color: $unselected-background;
2729
color: $unselected-foreground;
30+
31+
.mat-chip-focus-border {
32+
pointer-events: none;
33+
}
34+
35+
&:focus {
36+
outline: none;
37+
border: 2px solid $focus-color;
38+
}
39+
40+
.mat-chip-remove {
41+
color: $unselected-foreground;
42+
opacity: 0.3;
43+
44+
&:hover {
45+
opacity: 0.54;
46+
}
47+
}
2848
}
2949

3050
.mat-chip.mat-chip-selected:not(.mat-basic-chip) {
3151
background-color: $selected-background;
3252
color: $selected-foreground;
3353

54+
.mat-chip-remove {
55+
color: $selected-foreground;
56+
opacity: 0.4;
57+
58+
&:hover {
59+
opacity: 0.54;
60+
}
61+
}
62+
3463
&.mat-primary {
3564
background-color: mat-color($primary, 500);
3665
color: mat-contrast($primary, 500);
66+
67+
.mat-chip-remove {
68+
color: mat-contrast($primary, 500);
69+
opacity: 0.4;
70+
71+
&:hover {
72+
opacity: 0.54;
73+
}
74+
}
3775
}
3876

3977
&.mat-accent {
4078
background-color: mat-color($accent, 500);
4179
color: mat-contrast($accent, 500);
80+
81+
.mat-chip-remove {
82+
color: mat-contrast($accent, 500);
83+
opacity: 0.4;
84+
85+
&:hover {
86+
opacity: 0.54;
87+
}
88+
}
4289
}
4390

4491
&.mat-warn {
4592
background-color: mat-color($warn, 500);
4693
color: mat-contrast($warn, 500);
94+
95+
.mat-chip-remove {
96+
color: mat-contrast($warn, 500);
97+
opacity: 0.4;
98+
99+
&:hover {
100+
opacity: 0.54;
101+
}
102+
}
47103
}
48104
}
49105
}

src/lib/chips/chip-input.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {Directive, Output, EventEmitter, Renderer, ElementRef, Input} from "@angular/core";
2+
import {MdInputDirective} from "../input/input-container";
3+
import {ENTER} from "../core/keyboard/keycodes";
4+
5+
export interface MdChipInputEvent {
6+
input: HTMLInputElement;
7+
value: string;
8+
}
9+
10+
@Directive({
11+
selector: '[mdChipInput], [matChipInput]',
12+
host: {
13+
'(keydown)': '_add($event)',
14+
'(blur)': '_blur()'
15+
}
16+
})
17+
export class MdChipInput {
18+
19+
/**
20+
* Whether or not the chipAdded event will be emitted when the input is blurred.
21+
*
22+
* Default `false`.
23+
*/
24+
@Input() addOnBlur = false;
25+
26+
/**
27+
* The list of key codes that will trigger a chipAdded event.
28+
*
29+
* Defaults to `[ENTER]`.
30+
*/
31+
@Input() separatorKeys: number[] = [ENTER];
32+
33+
/** Emitted when a chip is to be added. */
34+
@Output() chipAdded = new EventEmitter<MdChipInputEvent>();
35+
36+
/** The native input element to which this directive is attached. */
37+
protected _inputElement: HTMLInputElement;
38+
39+
constructor(protected _renderer: Renderer, protected _elementRef: ElementRef) {
40+
this._inputElement = this._elementRef.nativeElement as HTMLInputElement;
41+
}
42+
43+
_add(event?: KeyboardEvent) {
44+
if (!event || this.separatorKeys.indexOf(event.keyCode) > -1) {
45+
this.chipAdded.emit({ input: this._inputElement, value: this._inputElement.value });
46+
47+
if (event) {
48+
event.preventDefault();
49+
}
50+
}
51+
}
52+
53+
_blur() {
54+
if (this.addOnBlur) {
55+
this._add();
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)