Skip to content

Commit aeed674

Browse files
committed
sync chip
1 parent ac534ac commit aeed674

File tree

12 files changed

+109
-128
lines changed

12 files changed

+109
-128
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ <h4>Advanced</h4>
2626

2727
<md-chip color="warn" selected="true" *ngIf="visible"
2828
(destroy)="alert('chip destroyed')" (remove)="toggleVisible()">
29-
<md-icon md-chip-remove>cancel</md-icon>
29+
<md-icon mdChipRemove>cancel</md-icon>
3030
With Events
3131
</md-chip>
3232
</md-chip-list>
@@ -44,18 +44,22 @@ <h4>Input Container</h4>
4444
<code>&lt;md-input-container&gt;</code>.
4545
</p>
4646

47-
<md-input-container [floatPlaceholder]="people.length > 0 ? 'always' : 'auto'">
48-
<md-chip-list>
47+
<md-input-container>
48+
<md-chip-list mdPrefix #chipList>
4949
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
5050
[removable]="removable" (remove)="remove(person)">
5151
{{person.name}}
5252
<md-icon mdChipRemove>cancel</md-icon>
5353
</md-chip>
5454

55-
<input mdInput placeholder="New Contributor..."
56-
mdChipInput (chipAdded)="add($event)"
57-
[separatorKeys]="separatorKeys" [addOnBlur]="addOnBlur" />
5855
</md-chip-list>
56+
<input mdInput placeholder="New Contributor..."
57+
[mdChipList] ="chipList" (chipAdded)="add($event)"
58+
[separatorKeys]="separatorKeys" [addOnBlur]="addOnBlur" />
59+
</md-input-container>
60+
61+
<md-input-container>
62+
<input mdInput placeholder="New Contributor..."/>
5963
</md-input-container>
6064

6165
<p>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@
2424
md-chip-list input {
2525
width: 150px;
2626
}
27-
}
27+
28+
.mat-chip-remove.mat-icon {
29+
font-size: 18px;
30+
}
31+
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ export class ChipsDemo {
4646
alert(message);
4747
}
4848

49-
add(input: HTMLInputElement): void {
50-
if (input.value && input.value.trim() != '') {
51-
this.people.push({ name: input.value.trim() });
52-
input.value = '';
53-
=======
5449
add(event: MdChipInputEvent): void {
5550
let input = event.input;
5651
let value = event.value;
@@ -71,7 +66,6 @@ export class ChipsDemo {
7166

7267
if (index >= 0) {
7368
this.people.splice(index, 1);
74-
>>>>>>> feat(chips): Add remove functionality/styling.
7569
}
7670
}
7771

src/lib/chips/_chips-theme.scss

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $mat-chip-line-height: 16px;
2222

2323
// The spec only provides guidance for light-themed chips. When inside of a dark theme, fall back
2424
// to standard background and foreground colors.
25-
$unselected-background: if($is-dark-theme, #656565, #e0e0e0);
25+
$unselected-background: if($is-dark-theme, mat-color($background, card), #e0e0e0);
2626
$unselected-foreground: if($is-dark-theme, mat-color($foreground, text), $light-foreground);
2727

2828
$selected-background: if($is-dark-theme, mat-color($background, app-bar), #808080);
@@ -46,10 +46,10 @@ $mat-chip-line-height: 16px;
4646
.mat-chip-remove {
4747
color: $unselected-foreground;
4848
opacity: 0.3;
49+
}
4950

50-
&:hover {
51-
opacity: 0.54;
52-
}
51+
.mat-chip-remove:hover {
52+
opacity: 0.54;
5353
}
5454
}
5555

@@ -60,10 +60,10 @@ $mat-chip-line-height: 16px;
6060
.mat-chip-remove {
6161
color: $selected-foreground;
6262
opacity: 0.4;
63+
}
6364

64-
&:hover {
65-
opacity: 0.54;
66-
}
65+
.mat-chip-remove:hover {
66+
opacity: 0.54;
6767
}
6868

6969
&.mat-primary {
@@ -79,17 +79,16 @@ $mat-chip-line-height: 16px;
7979
&.mat-warn {
8080
background-color: mat-color($warn);
8181
color: mat-color($warn, default-contrast);
82-
background-color: mat-color($primary, 500);
83-
color: mat-contrast($primary, 500);
8482

8583
.mat-chip-remove {
8684
color: mat-contrast($primary, 500);
8785
opacity: 0.4;
86+
}
8887

89-
&:hover {
90-
opacity: 0.54;
91-
}
88+
.mat-chip-remove:hover {
89+
opacity: 0.54;
9290
}
91+
9392
}
9493

9594
&.mat-accent {
@@ -99,10 +98,10 @@ $mat-chip-line-height: 16px;
9998
.mat-chip-remove {
10099
color: mat-contrast($accent, 500);
101100
opacity: 0.4;
101+
}
102102

103-
&:hover {
104-
opacity: 0.54;
105-
}
103+
.mat-chip-remove:hover {
104+
opacity: 0.54;
106105
}
107106
}
108107

@@ -113,10 +112,10 @@ $mat-chip-line-height: 16px;
113112
.mat-chip-remove {
114113
color: mat-contrast($warn, 500);
115114
opacity: 0.4;
115+
}
116116

117-
&:hover {
118-
opacity: 0.54;
119-
}
117+
.mat-chip-remove:hover {
118+
opacity: 0.54;
120119
}
121120
}
122121
}

src/lib/chips/chip-input.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {Component, DebugElement} from '@angular/core';
44
import {MdChipInput, MdChipInputEvent} from './chip-input';
55
import {By} from '@angular/platform-browser';
66
import {Dir} from '../core/rtl/dir';
7-
import {FakeKeyboardEvent} from './chip-list.spec';
7+
import {createKeyboardEvent} from '../core/testing/event-objects';
8+
89
import {ENTER, COMMA} from '../core/keyboard/keycodes';
910

1011
describe('MdChipInput', () => {
@@ -42,7 +43,7 @@ describe('MdChipInput', () => {
4243

4344
describe('basic behavior', () => {
4445
it('emits the (chipAdded) on enter keyup', () => {
45-
let ENTER_EVENT = new FakeKeyboardEvent(ENTER, inputNativeElement) as any;
46+
let ENTER_EVENT = createKeyboardEvent('keydown', ENTER, inputNativeElement) as any;
4647

4748
spyOn(testChipInput, 'add');
4849

@@ -75,7 +76,7 @@ describe('MdChipInput', () => {
7576

7677
describe('[separatorKeys]', () => {
7778
it('does not emit (chipAdded) when a non-separator key is pressed', () => {
78-
let ENTER_EVENT = new FakeKeyboardEvent(ENTER, inputNativeElement) as any;
79+
let ENTER_EVENT = createKeyboardEvent('keydown', ENTER, inputNativeElement) as any;
7980
spyOn(testChipInput, 'add');
8081

8182
testChipInput.separatorKeys = [COMMA];
@@ -86,7 +87,7 @@ describe('MdChipInput', () => {
8687
});
8788

8889
it('emits (chipAdded) when a custom separator keys is pressed', () => {
89-
let COMMA_EVENT = new FakeKeyboardEvent(COMMA, inputNativeElement) as any;
90+
let COMMA_EVENT = createKeyboardEvent('keydown', COMMA, inputNativeElement) as any;
9091
spyOn(testChipInput, 'add');
9192

9293
testChipInput.separatorKeys = [COMMA];

src/lib/chips/chip-input.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
import {Directive, Output, EventEmitter, Renderer, ElementRef, Input} from '@angular/core';
22
import {ENTER} from '../core/keyboard/keycodes';
3+
import {MdChipList} from './chip-list';
34

45
export interface MdChipInputEvent {
56
input: HTMLInputElement;
67
value: string;
78
}
89

910
@Directive({
10-
selector: '[mdChipInput], [matChipInput]',
11+
selector: 'input[mdChipList], input[matChipList]',
1112
host: {
13+
'class': 'mat-chip-input',
1214
'(keydown)': '_keydown($event)',
1315
'(blur)': '_blur()'
1416
}
1517
})
1618
export class MdChipInput {
1719

20+
_chipList: MdChipList;
21+
22+
/** Register input for chip list */
23+
@Input()
24+
set mdChipList(value: MdChipList) {
25+
if (value) {
26+
this._chipList = value;
27+
this._chipList.registerInput(this._inputElement);
28+
}
29+
}
30+
1831
/**
1932
* Whether or not the chipAdded event will be emitted when the input is blurred.
2033
*

src/lib/chips/chip-list.spec.ts

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
import {async, ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
22
import {Component, DebugElement, QueryList} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
45
import {MdChip, MdChipList, MdChipsModule} from './index';
56
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
6-
import {SPACE, LEFT_ARROW, RIGHT_ARROW, TAB} from '../core/keyboard/keycodes';
77
import {createKeyboardEvent} from '../core/testing/event-objects';
88

99
import {MdInputModule} from '../input/index';
10-
import {FakeEvent} from '../core/a11y/list-key-manager.spec';
11-
import {LEFT_ARROW, RIGHT_ARROW, BACKSPACE, DELETE} from '../core/keyboard/keycodes';
10+
import {LEFT_ARROW, RIGHT_ARROW, BACKSPACE, DELETE, SPACE, TAB} from '../core/keyboard/keycodes';
1211
import {Dir} from '../core/rtl/dir';
1312

14-
export class FakeKeyboardEvent extends FakeEvent {
15-
constructor(keyCode: number, protected target: HTMLElement) {
16-
super(keyCode);
17-
18-
this.target = target;
19-
}
20-
}
21-
2213
describe('MdChipList', () => {
2314
let fixture: ComponentFixture<any>;
2415
let chipListDebugElement: DebugElement;
@@ -32,9 +23,9 @@ describe('MdChipList', () => {
3223

3324
beforeEach(async(() => {
3425
TestBed.configureTestingModule({
35-
imports: [MdChipsModule, MdInputModule],
26+
imports: [MdChipsModule, MdInputModule, NoopAnimationsModule],
3627
declarations: [
37-
StaticChipList, StandardChipList, InputContainerChipList
28+
StandardChipList, InputContainerChipList
3829
],
3930
providers: [{
4031
provide: Dir, useFactory: () => {
@@ -122,10 +113,6 @@ describe('MdChipList', () => {
122113
expect(manager.activeItemIndex).toEqual(2);
123114
});
124115

125-
let LEFT_EVENT = createKeyboardEvent('keydown', LEFT_ARROW, lastNativeChip);
126-
let array = chips.toArray();
127-
let lastIndex = array.length - 1;
128-
let lastItem = array[lastIndex];
129116

130117
it('focuses the previous item', () => {
131118
let array = chips.toArray();
@@ -153,15 +140,11 @@ describe('MdChipList', () => {
153140
manager = chipListInstance._keyManager;
154141
}));
155142

156-
let RIGHT_EVENT = createKeyboardEvent('keydown', RIGHT_ARROW, firstNativeChip);
157-
let array = chips.toArray();
158-
let firstItem = array[0];
159-
160143
it('LEFT ARROW focuses previous item', () => {
161144
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
162145
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
163146

164-
let LEFT_EVENT = new FakeKeyboardEvent(LEFT_ARROW, lastNativeChip) as any;
147+
let LEFT_EVENT = createKeyboardEvent('keydown', LEFT_ARROW, lastNativeChip);
165148
let array = chips.toArray();
166149
let lastIndex = array.length - 1;
167150
let lastItem = array[lastIndex];
@@ -183,7 +166,7 @@ describe('MdChipList', () => {
183166
let firstNativeChip = nativeChips[0] as HTMLElement;
184167

185168
let RIGHT_EVENT: KeyboardEvent =
186-
new FakeKeyboardEvent(RIGHT_ARROW, firstNativeChip) as any;
169+
createKeyboardEvent('keydown', RIGHT_ARROW, firstNativeChip);
187170
let array = chips.toArray();
188171
let firstItem = array[0];
189172

@@ -208,17 +191,12 @@ describe('MdChipList', () => {
208191
manager = chipListInstance._keyManager;
209192
}));
210193

211-
let SPACE_EVENT = createKeyboardEvent('keydown', SPACE, firstNativeChip);
212-
let firstChip: MdChip = chips.toArray()[0];
213-
214194
it('RIGHT ARROW focuses previous item', () => {
215-
fixture.detectChanges();
216-
217195
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
218196
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
219197

220198
let RIGHT_EVENT: KeyboardEvent =
221-
new FakeKeyboardEvent(RIGHT_ARROW, lastNativeChip) as any;
199+
createKeyboardEvent('keydown', RIGHT_ARROW, lastNativeChip);
222200
let array = chips.toArray();
223201
let lastIndex = array.length - 1;
224202
let lastItem = array[lastIndex];
@@ -239,7 +217,8 @@ describe('MdChipList', () => {
239217
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
240218
let firstNativeChip = nativeChips[0] as HTMLElement;
241219

242-
let LEFT_EVENT: KeyboardEvent = new FakeKeyboardEvent(LEFT_ARROW, firstNativeChip) as any;
220+
let LEFT_EVENT: KeyboardEvent =
221+
createKeyboardEvent('keydown', LEFT_ARROW, firstNativeChip);
243222
let array = chips.toArray();
244223
let firstItem = array[0];
245224

@@ -255,18 +234,17 @@ describe('MdChipList', () => {
255234
expect(manager.activeItemIndex).toEqual(1);
256235
});
257236

258-
});
259-
260-
it('allow focus to escape when tabbing away', fakeAsync(() => {
261-
chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));
237+
it('allow focus to escape when tabbing away', fakeAsync(() => {
238+
chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));
262239

263-
expect(chipListInstance._tabIndex)
240+
expect(chipListInstance._tabIndex)
264241
.toBe(-1, 'Expected tabIndex to be set to -1 temporarily.');
265242

266-
tick();
243+
tick();
267244

268-
expect(chipListInstance._tabIndex).toBe(0, 'Expected tabIndex to be reset back to 0');
269-
}));
245+
expect(chipListInstance._tabIndex).toBe(0, 'Expected tabIndex to be reset back to 0');
246+
}));
247+
});
270248
});
271249
});
272250

@@ -290,7 +268,8 @@ describe('MdChipList', () => {
290268

291269
it('DELETE focuses the last chip', () => {
292270
let nativeInput = chipListNativeElement.querySelector('input');
293-
let DELETE_EVENT: KeyboardEvent = new FakeKeyboardEvent(DELETE, nativeInput) as any;
271+
let DELETE_EVENT: KeyboardEvent =
272+
createKeyboardEvent('keydown', DELETE, nativeInput);
294273

295274
// Focus the input
296275
nativeInput.focus();
@@ -306,7 +285,8 @@ describe('MdChipList', () => {
306285

307286
it('BACKSPACE focuses the last chip', () => {
308287
let nativeInput = chipListNativeElement.querySelector('input');
309-
let BACKSPACE_EVENT: KeyboardEvent = new FakeKeyboardEvent(BACKSPACE, nativeInput) as any;
288+
let BACKSPACE_EVENT: KeyboardEvent =
289+
createKeyboardEvent('keydown', BACKSPACE, nativeInput);
310290

311291
// Focus the input
312292
nativeInput.focus();
@@ -376,7 +356,6 @@ class StandardChipList {
376356
<md-chip>Chip 1</md-chip>
377357
<md-chip>Chip 1</md-chip>
378358
<md-chip>Chip 1</md-chip>
379-
380359
<input mdInput name="test" />
381360
</md-chip-list>
382361
</md-input-container>

0 commit comments

Comments
 (0)