Skip to content

Commit 0fd32a9

Browse files
committed
Sync and rebase & fix test
1 parent 8ef5a0c commit 0fd32a9

File tree

11 files changed

+127
-163
lines changed

11 files changed

+127
-163
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,39 @@ <h4>Input Container</h4>
4545
<code>&lt;md-input-container&gt;</code>.
4646
</p>
4747

48-
<md-input-container [floatPlaceholder]="people.length > 0 ? 'always' : 'auto'">
49-
<md-chip-list mdPrefix #chipList>
48+
49+
<md-input-container>
50+
<md-chip-list mdPrefix #chipList1>
5051
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
5152
[removable]="removable" (remove)="remove(person)">
5253
{{person.name}}
5354
<md-icon mdChipRemove>cancel</md-icon>
5455
</md-chip>
5556
</md-chip-list>
56-
<input mdInput mdChipInput placeholder="New Contributor..."
57-
[mdChipInputFor]="chipList"
57+
<input mdInput placeholder="New Contributor..."
58+
[mdChipInputFor]="chipList1"
59+
[mdChipInputSeparatorKeyCodes]="separatorKeysCodes"
60+
[mdChipInputAddOnBlur]="addOnBlur"
61+
(mdChipInputTokenEnd)="add($event)" />
62+
</md-input-container>
63+
64+
65+
66+
<p>
67+
You can also put <code>&lt;md-input-container&gt;</code> outside of an <code>md-chip-list</code>.
68+
With <code>mdChipInput</code> the input work with chip-list
69+
</p>
70+
71+
<md-chip-list #chipList2>
72+
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
73+
[removable]="removable" (remove)="remove(person)">
74+
{{person.name}}
75+
<md-icon mdChipRemove>cancel</md-icon>
76+
</md-chip>
77+
</md-chip-list>
78+
<md-input-container>
79+
<input mdInput placeholder="New Contributor..."
80+
[mdChipInputFor]="chipList2"
5881
[mdChipInputSeparatorKeyCodes]="separatorKeysCodes"
5982
[mdChipInputAddOnBlur]="addOnBlur"
6083
(mdChipInputTokenEnd)="add($event)" />

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {MdChipsModule} from './index';
33
import {Component, DebugElement} from '@angular/core';
44
import {MdChipInput, MdChipInputEvent} from './chip-input';
55
import {By} from '@angular/platform-browser';
6-
import {Dir} from '../core/rtl/dir';
6+
import {Directionality} from '../core';
77
import {createKeyboardEvent} from '../core/testing/event-objects';
88

99
import {ENTER, COMMA} from '../core/keyboard/keycodes';
@@ -22,7 +22,7 @@ describe('MdChipInput', () => {
2222
imports: [MdChipsModule],
2323
declarations: [TestChipInput],
2424
providers: [{
25-
provide: Dir, useFactory: () => {
25+
provide: Directionality, useFactory: () => {
2626
return {value: dir.toLowerCase()};
2727
}
2828
}]
@@ -101,18 +101,18 @@ describe('MdChipInput', () => {
101101

102102
@Component({
103103
template: `
104-
<md-chip-list>
105-
<input mdInput mdChipInputFor
104+
<md-chip-list #chipList>
105+
</md-chip-list>
106+
<input mdInput [mdChipInputFor]="chipList"
106107
[mdChipInputAddOnBlur]="addOnBlur"
107108
[mdChipInputSeparatorKeyCodes]="separatorKeys"
108109
(mdChipInputTokenEnd)="add($event)" />
109-
</md-chip-list>
110110
`
111111
})
112112
class TestChipInput {
113113
addOnBlur: boolean = false;
114114
separatorKeys: number[] = [ENTER];
115115

116-
add(event: MdChipInputEvent) {
116+
add(_: MdChipInputEvent) {
117117
}
118118
}

src/lib/chips/chip-input.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
19
import {Directive, Output, EventEmitter, ElementRef, Input} from '@angular/core';
2-
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
3-
import {ENTER, BACKSPACE} from '../core/keyboard/keycodes';
10+
import {coerceBooleanProperty} from '@angular/cdk';
11+
import {ENTER} from '../core/keyboard/keycodes';
412
import {MdChipList} from './chip-list';
513

614
export interface MdChipInputEvent {
@@ -9,7 +17,7 @@ export interface MdChipInputEvent {
917
}
1018

1119
@Directive({
12-
selector: 'input[mdChipInput], input[matChipInput]',
20+
selector: 'input[mdChipInputFor], input[matChipInputFor]',
1321
host: {
1422
'class': 'mat-chip-input',
1523
'(keydown)': '_keydown($event)',
@@ -81,8 +89,8 @@ export class MdChipInput {
8189

8290
/** Checks to see if the (chipEnd) event needs to be emitted. */
8391
_emitChipEnd(event?: KeyboardEvent) {
84-
if (!this._inputElement.value && !!event && event.keyCode == BACKSPACE) {
85-
this._chipList.chips.last.focus();
92+
if (!this._inputElement.value && !!event) {
93+
this._chipList._keydown(event);
8694
}
8795
if (!event || this.separatorKeyCodes.indexOf(event.keyCode) > -1) {
8896
this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value });

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {createKeyboardEvent} from '@angular/cdk/testing';
99

1010
import {MdInputModule} from '../input/index';
1111
import {LEFT_ARROW, RIGHT_ARROW, BACKSPACE, DELETE, SPACE, TAB} from '../core/keyboard/keycodes';
12-
import {Dir} from '../core/rtl/dir';
12+
import {Directionality} from '../core';
1313

1414
describe('MdChipList', () => {
1515
let fixture: ComponentFixture<any>;
@@ -29,7 +29,7 @@ describe('MdChipList', () => {
2929
StandardChipList, InputContainerChipList
3030
],
3131
providers: [{
32-
provide: Dir, useFactory: () => {
32+
provide: Directionality, useFactory: () => {
3333
return {value: dir.toLowerCase()};
3434
}
3535
}]
@@ -83,20 +83,6 @@ describe('MdChipList', () => {
8383
expect(manager.activeItemIndex).toBe(lastIndex);
8484
});
8585

86-
87-
it('should focus the previous item', () => {
88-
// Focus the last item by fake updating the _hasFocus state for unit tests.
89-
lastItem._hasFocus = true;
90-
91-
// Destroy the last item
92-
testComponent.remove = lastIndex;
93-
94-
lastItem.focus();
95-
fixture.detectChanges();
96-
97-
expect(manager.activeItemIndex).toBe(lastIndex);
98-
});
99-
10086
describe('on chip destroy', () => {
10187
it('should focus the next item', () => {
10288
let array = chips.toArray();
@@ -268,7 +254,7 @@ describe('MdChipList', () => {
268254

269255
// Focus the input
270256
nativeInput.focus();
271-
expect(manager.activeItemIndex).toBeFalsy();
257+
expect(manager.activeItemIndex).toBe(-1);
272258

273259
// Press the DELETE key
274260
chipListInstance._keydown(DELETE_EVENT);
@@ -285,7 +271,7 @@ describe('MdChipList', () => {
285271

286272
// Focus the input
287273
nativeInput.focus();
288-
expect(manager.activeItemIndex).toBeFalsy();
274+
expect(manager.activeItemIndex).toBe(-1);
289275

290276
// Press the BACKSPACE key
291277
chipListInstance._keydown(BACKSPACE_EVENT);
@@ -351,8 +337,8 @@ class StandardChipList {
351337
<md-chip>Chip 1</md-chip>
352338
<md-chip>Chip 1</md-chip>
353339
<md-chip>Chip 1</md-chip>
354-
<input mdInput name="test" [mdChipInputFor]="chipList"/>
355340
</md-chip-list>
341+
<input mdInput name="test" [mdChipInputFor]="chipList"/>
356342
</md-input-container>
357343
`
358344
})

src/lib/chips/chip-list.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import {
1111
ChangeDetectionStrategy,
1212
Component,
1313
ContentChildren,
14-
Directive,
1514
Input,
1615
QueryList,
1716
ViewEncapsulation,
1817
OnDestroy,
18+
Optional,
1919
ElementRef,
2020
Renderer2,
2121
} from '@angular/core';
@@ -24,7 +24,8 @@ import {MdChip} from './chip';
2424
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
2525
import {SPACE, LEFT_ARROW, RIGHT_ARROW} from '../core/keyboard/keycodes';
2626
import {Subscription} from 'rxjs/Subscription';
27-
import {coerceBooleanProperty} from '@angular/cdk';
27+
import {coerceBooleanProperty, Directionality} from '@angular/cdk';
28+
import {Subscription} from 'rxjs/Subscription';
2829

2930
/** Utility to check if an input element has no value. */
3031
function _isInputEmpty(element: HTMLElement): boolean {
@@ -56,7 +57,7 @@ function _isInputEmpty(element: HTMLElement): boolean {
5657
'role': 'listbox',
5758
'class': 'mat-chip-list',
5859

59-
'(focus)': 'focus($event)',
60+
'(focus)': 'focus()',
6061
'(keydown)': '_keydown($event)'
6162
},
6263
queries: {
@@ -69,7 +70,7 @@ function _isInputEmpty(element: HTMLElement): boolean {
6970
export class MdChipList implements AfterContentInit, OnDestroy {
7071

7172
/** When a chip is destroyed, we track the index so we can focus the appropriate next chip. */
72-
protected _lastDestroyedIndex: number = null;
73+
protected _lastDestroyedIndex: number|null = null;
7374

7475
/** Track which chips we're listening to for focus/destruction. */
7576
protected _chipSet: WeakMap<MdChip, boolean> = new WeakMap();
@@ -92,7 +93,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
9293
chips: QueryList<MdChip>;
9394

9495
constructor(protected _renderer: Renderer2, protected _elementRef: ElementRef,
95-
protected _dir: Dir) {
96+
@Optional() private _dir: Directionality) {
9697
}
9798

9899
ngAfterContentInit(): void {
@@ -153,7 +154,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
153154
* Focuses the the first non-disabled chip in this chip list, or the associated input when there
154155
* are no eligible chips.
155156
*/
156-
focus(event?: Event) {
157+
focus() {
157158
// TODO: ARIA says this should focus the first `selected` chip if any are selected.
158159
if (this.chips.length > 0) {
159160
this._keyManager.setFirstItemActive();
@@ -176,7 +177,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
176177
let code = event.keyCode;
177178
let target = event.target as HTMLElement;
178179
let isInputEmpty = _isInputEmpty(target);
179-
let isRtl = this._dir.value == 'rtl';
180+
let isRtl = this._dir && this._dir.value == 'rtl';
180181

181182
let isPrevKey = (code == (isRtl ? RIGHT_ARROW : LEFT_ARROW));
182183
let isNextKey = (code == (isRtl ? LEFT_ARROW : RIGHT_ARROW));
@@ -254,15 +255,19 @@ export class MdChipList implements AfterContentInit, OnDestroy {
254255
// On destroy, remove the item from our list, and setup our destroyed focus check
255256
chip.destroy.subscribe(() => {
256257
let chipIndex: number = this.chips.toArray().indexOf(chip);
257-
258-
if (this._isValidIndex(chipIndex) && chip._hasFocus) {
259-
// Check whether the chip is the last item
260-
if (chipIndex < this.chips.length - 1) {
261-
this._keyManager.setActiveItem(chipIndex);
262-
} else if (chipIndex - 1 >= 0) {
263-
this._keyManager.setActiveItem(chipIndex - 1);
258+
if (this._isValidIndex(chipIndex)) {
259+
if (chip._hasFocus) {
260+
// Check whether the chip is the last item
261+
if (chipIndex < this.chips.length - 1) {
262+
this._keyManager.setActiveItem(chipIndex);
263+
} else if (chipIndex - 1 >= 0) {
264+
this._keyManager.setActiveItem(chipIndex - 1);
265+
}
264266
}
265-
this._lastDestroyedIndex = chipIndex;
267+
if (this._keyManager.activeItemIndex == chipIndex) {
268+
this._lastDestroyedIndex = chipIndex;
269+
}
270+
266271
}
267272

268273
this._chipSet.delete(chip);
@@ -278,12 +283,12 @@ export class MdChipList implements AfterContentInit, OnDestroy {
278283
*/
279284
protected _updateFocusForDestroyedChips() {
280285
let chipsArray = this.chips;
281-
let focusChip: MdChip;
282286

283287
if (this._lastDestroyedIndex != null && chipsArray.length > 0) {
284288
// Check whether the destroyed chip was the last item
285289
const newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1);
286290
this._keyManager.setActiveItem(newFocusIndex);
291+
let focusChip = this._keyManager.activeItem;
287292

288293
// Focus the chip
289294
if (focusChip) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ describe('Chip Remove', () => {
3131

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

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

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

4242
testChip.removable = true;
4343
fixture.detectChanges();
@@ -50,7 +50,7 @@ describe('Chip Remove', () => {
5050
});
5151

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

5555
testChip.removable = true;
5656
fixture.detectChanges();

src/lib/chips/chip.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {By} from '@angular/platform-browser';
44
import {MdChipList, MdChip, MdChipEvent, MdChipsModule} from './index';
55
import {SPACE, DELETE, BACKSPACE} from '../core/keyboard/keycodes';
66
import {createKeyboardEvent} from '../core/testing/event-objects';
7-
import {Dir} from '../core/rtl/dir';
7+
import {Directionality} from '../core';
88

99
describe('Chips', () => {
1010
let fixture: ComponentFixture<any>;
@@ -22,7 +22,7 @@ describe('Chips', () => {
2222
BasicChip, SingleChip
2323
],
2424
providers: [{
25-
provide: Dir, useFactory: () => {
25+
provide: Directionality, useFactory: () => {
2626
return {value: dir};
2727
}
2828
}]
@@ -64,7 +64,7 @@ describe('Chips', () => {
6464
chipDebugElement = fixture.debugElement.query(By.directive(MdChip));
6565
chipListNativeElement = fixture.debugElement.query(By.directive(MdChipList)).nativeElement;
6666
chipNativeElement = chipDebugElement.nativeElement;
67-
chipInstance = chipDebugElement.componentInstance;
67+
chipInstance = chipDebugElement.injector.get(MdChip);
6868
testComponent = fixture.debugElement.componentInstance;
6969

7070
document.body.appendChild(chipNativeElement);

0 commit comments

Comments
 (0)