Skip to content

Commit 951a5d7

Browse files
committed
Add chip list container
1 parent 66575ee commit 951a5d7

File tree

7 files changed

+67
-85
lines changed

7 files changed

+67
-85
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,19 @@ <h4>Input Container</h4>
4444
<code>&lt;md-input-container&gt;</code>.
4545
</p>
4646

47-
<md-input-container>
47+
<md-input-container mdChipListContainer>
4848
<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>
54-
5554
</md-chip-list>
5655
<input mdInput placeholder="New Contributor..."
5756
[mdChipList] ="chipList" (chipAdded)="add($event)"
5857
[separatorKeys]="separatorKeys" [addOnBlur]="addOnBlur" />
5958
</md-input-container>
6059

61-
<md-input-container>
62-
<input mdInput placeholder="New Contributor..."/>
63-
</md-input-container>
64-
6560
<p>
6661
The example above has overridden the <code>[separatorKeys]</code> input to allow for
6762
<code>ENTER</code>, <code>COMMA</code> and <code>SEMI COLON</code> keys.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ describe('MdChipInput', () => {
102102
@Component({
103103
template: `
104104
<md-chip-list>
105-
<input mdChipInput [addOnBlur]="addOnBlur" [separatorKeys]="separatorKeys"
106-
(chipAdded)="add($event)" />
107105
</md-chip-list>
106+
<input mdInput mdChipList [addOnBlur]="addOnBlur" [separatorKeys]="separatorKeys"
107+
(chipAdded)="add($event)" />
108108
`
109109
})
110110
class TestChipInput {

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('MdChipList', () => {
4444
setupStandardList();
4545
}));
4646

47-
it('adds the `mat-chip-list` class', () => {
47+
it('should add the `mat-chip-list` class', () => {
4848
expect(chipListNativeElement.classList).toContain('mat-chip-list');
4949
});
5050
});
@@ -58,7 +58,7 @@ describe('MdChipList', () => {
5858
manager = chipListInstance._keyManager;
5959
});
6060

61-
it('focuses the first chip on focus', () => {
61+
it('should focus the first chip on focus', () => {
6262
let FOCUS_EVENT = {} as Event;
6363

6464
chipListInstance.focus(FOCUS_EVENT);
@@ -67,7 +67,7 @@ describe('MdChipList', () => {
6767
expect(manager.activeItemIndex).toBe(0);
6868
});
6969

70-
it('watches for chip focus', () => {
70+
it('should watch for chip focus', () => {
7171
let array = chips.toArray();
7272
let lastIndex = array.length - 1;
7373
let lastItem = array[lastIndex];
@@ -79,7 +79,7 @@ describe('MdChipList', () => {
7979
});
8080

8181
describe('on chip destroy', () => {
82-
it('focuses the next item', () => {
82+
it('should focus the next item', () => {
8383
let array = chips.toArray();
8484
let midItem = array[2];
8585

@@ -95,7 +95,7 @@ describe('MdChipList', () => {
9595
});
9696

9797

98-
it('focuses the previous item', () => {
98+
it('should focus the previous item', () => {
9999
let array = chips.toArray();
100100
let lastIndex = array.length - 1;
101101
let lastItem = array[lastIndex];
@@ -121,7 +121,7 @@ describe('MdChipList', () => {
121121
manager = chipListInstance._keyManager;
122122
}));
123123

124-
it('LEFT ARROW focuses previous item', () => {
124+
it('should focus previous item when press LEFT ARROW', () => {
125125
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
126126
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
127127

@@ -142,7 +142,7 @@ describe('MdChipList', () => {
142142
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
143143
});
144144

145-
it('RIGHT ARROW focuses next item', () => {
145+
it('should focus next item when press RIGHT ARROW', () => {
146146
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
147147
let firstNativeChip = nativeChips[0] as HTMLElement;
148148

@@ -172,7 +172,7 @@ describe('MdChipList', () => {
172172
manager = chipListInstance._keyManager;
173173
}));
174174

175-
it('RIGHT ARROW focuses previous item', () => {
175+
it('should focus previous item when press RIGHT ARROW', () => {
176176
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
177177
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
178178

@@ -194,7 +194,7 @@ describe('MdChipList', () => {
194194
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
195195
});
196196

197-
it('LEFT ARROW focuses next item', () => {
197+
it('should focus next item when press LEFT ARROW', () => {
198198
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
199199
let firstNativeChip = nativeChips[0] as HTMLElement;
200200

@@ -215,7 +215,7 @@ describe('MdChipList', () => {
215215
expect(manager.activeItemIndex).toEqual(1);
216216
});
217217

218-
it('allow focus to escape when tabbing away', fakeAsync(() => {
218+
it('should allow focus to escape when tabbing away', fakeAsync(() => {
219219
chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));
220220

221221
expect(chipListInstance._tabIndex)
@@ -240,15 +240,10 @@ describe('MdChipList', () => {
240240
manager = chipListInstance._keyManager;
241241
});
242242

243-
it('SPACE ignores selection', () => {
244-
let SPACE_EVENT = createKeyboardEvent('keydown', SPACE);
245-
let firstChip: MdChip = chips.toArray()[0];
246-
});
247-
248243
describe('when the input has focus', () => {
249244

250-
it('DELETE focuses the last chip', () => {
251-
let nativeInput = chipListNativeElement.querySelector('input');
245+
it('should focus the last chip when press DELETE', () => {
246+
let nativeInput = fixture.nativeElement.querySelector('input');
252247
let DELETE_EVENT: KeyboardEvent =
253248
createKeyboardEvent('keydown', DELETE, nativeInput);
254249

@@ -264,8 +259,8 @@ describe('MdChipList', () => {
264259
expect(manager.activeItemIndex).toEqual(chips.length - 1);
265260
});
266261

267-
it('BACKSPACE focuses the last chip', () => {
268-
let nativeInput = chipListNativeElement.querySelector('input');
262+
it('should focus the last chip when press BACKSPACE', () => {
263+
let nativeInput = fixture.nativeElement.querySelector('input');
269264
let BACKSPACE_EVENT: KeyboardEvent =
270265
createKeyboardEvent('keydown', BACKSPACE, nativeInput);
271266

@@ -336,12 +331,12 @@ class StandardChipList {
336331
@Component({
337332
template: `
338333
<md-input-container>
339-
<md-chip-list>
334+
<md-chip-list mdPrefix #chipList>
340335
<md-chip>Chip 1</md-chip>
341336
<md-chip>Chip 1</md-chip>
342337
<md-chip>Chip 1</md-chip>
343-
<input mdInput name="test" />
344338
</md-chip-list>
339+
<input mdInput name="test" [mdChipList]="chipList"/>
345340
</md-input-container>
346341
`
347342
})

src/lib/chips/chip-list.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ChangeDetectionStrategy,
44
Component,
55
ContentChildren,
6+
Directive,
67
Input,
78
QueryList,
89
ViewEncapsulation,
@@ -20,6 +21,14 @@ import {
2021
} from '../core/keyboard/keycodes';
2122
import {Dir} from '../core/rtl/dir';
2223

24+
@Directive({
25+
selector: '[mdChipListContainer], [matChipListContainer]',
26+
host: {
27+
'[class.mat-chip-list-container]': 'true'
28+
}
29+
})
30+
export class MdChipListContainer {}
31+
2332
/**
2433
* A material design chips component (named ChipList for it's similarity to the List component).
2534
*
@@ -39,7 +48,7 @@ import {Dir} from '../core/rtl/dir';
3948
'[attr.tabindex]': '_tabIndex',
4049
'role': 'listbox',
4150
'[class.mat-chip-list]': 'true',
42-
51+
// Actions
4352
'(focus)': 'focus($event)',
4453
'(keydown)': '_keydown($event)'
4554
},
@@ -118,17 +127,13 @@ export class MdChipList implements AfterContentInit, OnDestroy {
118127
}
119128
}
120129

121-
/**
122-
* Associates an HTML input element with this chip list.
123-
*
124-
* @param inputElement The input to associate.
125-
*/
126130
@Input()
127131
get selectable(): boolean { return this._selectable; }
128132
set selectable(value: boolean) {
129133
this._selectable = coerceBooleanProperty(value);
130134
}
131135

136+
/** Associates an HTML input element with this chip list. */
132137
registerInput(inputElement: HTMLInputElement) {
133138
this._inputElement = inputElement;
134139
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ describe('Chip Remove', () => {
3030
}));
3131

3232
describe('basic behavior', () => {
33-
it('applies the `mat-chip-remove` CSS class', () => {
33+
it('should applies the `mat-chip-remove` CSS class', () => {
3434
let hrefElement = chipNativeElement.querySelector('a');
3535

3636
expect(hrefElement.classList).toContain('mat-chip-remove');
3737
});
3838

39-
it('emits (remove) on click', () => {
39+
it('should emits (remove) on click', () => {
4040
let hrefElement = chipNativeElement.querySelector('a');
4141

4242
testChip.removable = true;
@@ -49,7 +49,7 @@ describe('Chip Remove', () => {
4949
expect(testChip.didRemove).toHaveBeenCalled();
5050
});
5151

52-
it(`monitors the parent chip's [removable] property`, () => {
52+
it(`should monitors the parent chip's [removable] property`, () => {
5353
let hrefElement = chipNativeElement.querySelector('a');
5454

5555
testChip.removable = true;
@@ -67,7 +67,7 @@ describe('Chip Remove', () => {
6767

6868
@Component({
6969
template: `
70-
<md-chip [removable]="removable" (remove)="didRemove()"><a md-chip-remove></a></md-chip>
70+
<md-chip [removable]="removable" (remove)="didRemove()"><a mdChipRemove></a></md-chip>
7171
`
7272
})
7373
class TestChip {

src/lib/chips/chips.scss

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,33 @@ $mat-chip-remove-font-size: 18px;
1919
flex-wrap: wrap;
2020
align-items: flex-start;
2121

22-
/*
23-
* Only apply the margins to chips
24-
*/
2522
.mat-chip:not(.mat-basic-chip) {
2623
margin: $mat-chip-margin;
2724

2825
// Do not apply the special margins inside an input container
29-
:not(.mat-input-wrapper) & {
30-
// Remove the margin from the first element (in both LTR and RTL)
31-
&:first-child {
32-
margin: {
33-
left: 0;
34-
right: $mat-chip-margin;
35-
}
36-
37-
[dir='rtl'] & {
38-
margin: {
39-
left: $mat-chip-margin;
40-
right: 0;
41-
}
42-
}
26+
// Remove the margin from the first element (in both LTR and RTL)
27+
&:first-child {
28+
margin-left: 0;
29+
margin-right: $mat-chip-margin;
30+
31+
[dir='rtl'] & {
32+
margin-right: 0;
33+
margin-left: $mat-chip-margin;
4334
}
35+
}
36+
37+
// Remove the margin from the last element (in both LTR and RTL)
38+
&:last-child {
39+
margin-right: 0;
40+
margin-left: $mat-chip-margin;
4441

45-
// Remove the margin from the last element (in both LTR and RTL)
46-
&:last-child {
47-
margin: {
48-
left: $mat-chip-margin;
49-
right: 0;
50-
}
51-
52-
[dir='rtl'] & {
53-
margin: {
54-
left: 0;
55-
right: $mat-chip-margin;
56-
}
57-
}
42+
[dir='rtl'] & {
43+
margin-left: 0;
44+
margin-right: $mat-chip-margin;
5845
}
5946
}
6047
}
48+
6149
}
6250

6351
.mat-input-prefix .mat-chip-list-wrapper {
@@ -87,15 +75,6 @@ $mat-chip-remove-font-size: 18px;
8775
display: block;
8876
margin: 0;
8977
margin-bottom: $mat-chip-vertical-padding;
90-
91-
[dir='rtl'] & {
92-
margin: 0;
93-
margin-bottom: $mat-chip-vertical-padding;
94-
}
95-
96-
&:last-child, [dir='rtl'] &:last-child {
97-
margin-bottom: 0;
98-
}
9978
}
10079
}
10180

@@ -115,6 +94,18 @@ $mat-chip-remove-font-size: 18px;
11594
display: none;
11695
}
11796

97+
.mat-chip-list-container .mat-input-placeholder-wrapper {
98+
top: -4px;
99+
100+
[dir='rtl'] & {
101+
right: 8px;
102+
}
103+
104+
[dir='ltr'] & {
105+
margin-left: 8px;
106+
}
107+
}
108+
118109
.mat-chip-input {
119110
[dir='rtl'] & {
120111
margin-right: 8px;

src/lib/chips/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {NgModule} from '@angular/core';
2-
import {MdChipList} from './chip-list';
2+
import {MdChipList, MdChipListContainer} from './chip-list';
33
import {MdChip} from './chip';
44
import {MdChipInput} from './chip-input';
55
import {MdChipRemove} from './chip-remove';
@@ -11,11 +11,7 @@ export * from './chip-remove';
1111

1212
@NgModule({
1313
imports: [],
14-
exports: [MdChipList, MdChip, MdChipInput, MdChipRemove],
15-
declarations: [MdChipList, MdChip, MdChipInput, MdChipRemove]
14+
exports: [MdChipList, MdChip, MdChipInput, MdChipRemove, MdChipListContainer],
15+
declarations: [MdChipList, MdChip, MdChipInput, MdChipRemove, MdChipListContainer]
1616
})
1717
export class MdChipsModule {}
18-
19-
20-
export * from './chip-list';
21-
export * from './chip';

0 commit comments

Comments
 (0)