Skip to content

Commit 893b446

Browse files
committed
Add chip list container
1 parent aeed674 commit 893b446

File tree

7 files changed

+65
-84
lines changed

7 files changed

+65
-84
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: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('MdChipList', () => {
4343
setupStandardList();
4444
}));
4545

46-
it('adds the `mat-chip-list` class', () => {
46+
it('should add the `mat-chip-list` class', () => {
4747
expect(chipListNativeElement.classList).toContain('mat-chip-list');
4848
});
4949
});
@@ -65,7 +65,6 @@ describe('MdChipList', () => {
6565
let array = chips.toArray();
6666
let lastIndex = array.length - 1;
6767
let lastItem = array[lastIndex];
68-
6968
lastItem.focus();
7069
fixture.detectChanges();
7170

@@ -98,7 +97,7 @@ describe('MdChipList', () => {
9897
});
9998

10099
describe('on chip destroy', () => {
101-
it('focuses the next item', () => {
100+
it('should focus the next item', () => {
102101
let array = chips.toArray();
103102
let midItem = array[2];
104103

@@ -114,7 +113,7 @@ describe('MdChipList', () => {
114113
});
115114

116115

117-
it('focuses the previous item', () => {
116+
it('should focus the previous item', () => {
118117
let array = chips.toArray();
119118
let lastIndex = array.length - 1;
120119
let lastItem = array[lastIndex];
@@ -140,7 +139,7 @@ describe('MdChipList', () => {
140139
manager = chipListInstance._keyManager;
141140
}));
142141

143-
it('LEFT ARROW focuses previous item', () => {
142+
it('should focus previous item when press LEFT ARROW', () => {
144143
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
145144
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
146145

@@ -161,7 +160,7 @@ describe('MdChipList', () => {
161160
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
162161
});
163162

164-
it('RIGHT ARROW focuses next item', () => {
163+
it('should focus next item when press RIGHT ARROW', () => {
165164
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
166165
let firstNativeChip = nativeChips[0] as HTMLElement;
167166

@@ -191,7 +190,7 @@ describe('MdChipList', () => {
191190
manager = chipListInstance._keyManager;
192191
}));
193192

194-
it('RIGHT ARROW focuses previous item', () => {
193+
it('should focus previous item when press RIGHT ARROW', () => {
195194
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
196195
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
197196

@@ -213,7 +212,7 @@ describe('MdChipList', () => {
213212
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
214213
});
215214

216-
it('LEFT ARROW focuses next item', () => {
215+
it('should focus next item when press LEFT ARROW', () => {
217216
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
218217
let firstNativeChip = nativeChips[0] as HTMLElement;
219218

@@ -234,7 +233,7 @@ describe('MdChipList', () => {
234233
expect(manager.activeItemIndex).toEqual(1);
235234
});
236235

237-
it('allow focus to escape when tabbing away', fakeAsync(() => {
236+
it('should allow focus to escape when tabbing away', fakeAsync(() => {
238237
chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));
239238

240239
expect(chipListInstance._tabIndex)
@@ -259,15 +258,10 @@ describe('MdChipList', () => {
259258
manager = chipListInstance._keyManager;
260259
});
261260

262-
it('SPACE ignores selection', () => {
263-
let SPACE_EVENT = createKeyboardEvent('keydown', SPACE);
264-
let firstChip: MdChip = chips.toArray()[0];
265-
});
266-
267261
describe('when the input has focus', () => {
268262

269-
it('DELETE focuses the last chip', () => {
270-
let nativeInput = chipListNativeElement.querySelector('input');
263+
it('should focus the last chip when press DELETE', () => {
264+
let nativeInput = fixture.nativeElement.querySelector('input');
271265
let DELETE_EVENT: KeyboardEvent =
272266
createKeyboardEvent('keydown', DELETE, nativeInput);
273267

@@ -283,8 +277,8 @@ describe('MdChipList', () => {
283277
expect(manager.activeItemIndex).toEqual(chips.length - 1);
284278
});
285279

286-
it('BACKSPACE focuses the last chip', () => {
287-
let nativeInput = chipListNativeElement.querySelector('input');
280+
it('should focus the last chip when press BACKSPACE', () => {
281+
let nativeInput = fixture.nativeElement.querySelector('input');
288282
let BACKSPACE_EVENT: KeyboardEvent =
289283
createKeyboardEvent('keydown', BACKSPACE, nativeInput);
290284

@@ -352,12 +346,12 @@ class StandardChipList {
352346
@Component({
353347
template: `
354348
<md-input-container>
355-
<md-chip-list>
349+
<md-chip-list mdPrefix #chipList>
356350
<md-chip>Chip 1</md-chip>
357351
<md-chip>Chip 1</md-chip>
358352
<md-chip>Chip 1</md-chip>
359-
<input mdInput name="test" />
360353
</md-chip-list>
354+
<input mdInput name="test" [mdChipList]="chipList"/>
361355
</md-input-container>
362356
`
363357
})

src/lib/chips/chip-list.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ChangeDetectionStrategy,
1212
Component,
1313
ContentChildren,
14+
Directive,
1415
Input,
1516
QueryList,
1617
ViewEncapsulation,
@@ -29,6 +30,14 @@ import {
2930
} from '../core/keyboard/keycodes';
3031
import {Dir} from '../core/rtl/dir';
3132

33+
@Directive({
34+
selector: '[mdChipListContainer], [matChipListContainer]',
35+
host: {
36+
'[class.mat-chip-list-container]': 'true'
37+
}
38+
})
39+
export class MdChipListContainer {}
40+
3241
/**
3342
* A material design chips component (named ChipList for it's similarity to the List component).
3443
*
@@ -127,17 +136,13 @@ export class MdChipList implements AfterContentInit, OnDestroy {
127136
}
128137
}
129138

130-
/**
131-
* Associates an HTML input element with this chip list.
132-
*
133-
* @param inputElement The input to associate.
134-
*/
135139
@Input()
136140
get selectable(): boolean { return this._selectable; }
137141
set selectable(value: boolean) {
138142
this._selectable = coerceBooleanProperty(value);
139143
}
140144

145+
/** Associates an HTML input element with this chip list. */
141146
registerInput(inputElement: HTMLInputElement) {
142147
this._inputElement = inputElement;
143148
}

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: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10-
import {MdChipList} from './chip-list';
11-
import {MdChip, MdBasicChip} from './chip';
10+
import {MdChipList, MdChipListContainer} from './chip-list';
11+
import {MdChip} from './chip';
1212
import {MdChipInput} from './chip-input';
1313
import {MdChipRemove} from './chip-remove';
1414

@@ -19,11 +19,7 @@ export * from './chip-remove';
1919

2020
@NgModule({
2121
imports: [],
22-
exports: [MdChipList, MdChip, MdBasicChip, MdChipInput, MdChipRemove],
23-
declarations: [MdChipList, MdChip, MdBasicChip, MdChipInput, MdChipRemove]
22+
exports: [MdChipList, MdChip, MdChipInput, MdChipRemove, MdChipListContainer, MdChipRemove],
23+
declarations: [MdChipList, MdChip, MdChipInput, MdChipRemove, MdChipListContainer, MdChipRemove]
2424
})
2525
export class MdChipsModule {}
26-
27-
28-
export * from './chip-list';
29-
export * from './chip';

0 commit comments

Comments
 (0)