Skip to content

Commit 2281357

Browse files
committed
comments addressed
1 parent 848cf2a commit 2281357

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ <h4>Input Container</h4>
5353
<md-icon mdChipRemove>cancel</md-icon>
5454
</md-chip>
5555
<input mdInput placeholder="New Contributor..."
56-
[mdChipList]="chipList" (mdChipEnd)="add($event)"
57-
[separatorKeysCodes]="separatorKeysCodes" [mdChipListAddOnBlur]="addOnBlur" />
56+
[mdChipInputFor]="chipList"
57+
[mdChipInputSeparatorKeyCodes]="separatorKeysCodes"
58+
[mdChipInputAddOnBlur]="addOnBlur"
59+
(mdChipInputTokenEnd)="add($event)" />
5860
</md-chip-list>
5961
</md-input-container>
6062

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ describe('MdChipInput', () => {
102102
@Component({
103103
template: `
104104
<md-chip-list>
105-
<input mdInput mdChipList [mdChipListAddOnBlur]="addOnBlur" [separatorKeysCodes]="separatorKeys"
106-
(mdChipEnd)="add($event)" />
105+
<input mdInput mdChipInputFor
106+
[mdChipInputAddOnBlur]="addOnBlur"
107+
[mdChipInputSeparatorKeyCodes]="separatorKeys"
108+
(mdChipInputTokenEnd)="add($event)" />
107109
</md-chip-list>
108110
`
109111
})

src/lib/chips/chip-input.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface MdChipInputEvent {
99
}
1010

1111
@Directive({
12-
selector: 'input[mdChipList], input[matChipList]',
12+
selector: 'input[mdChipInputFor], input[matChipInputFor]',
1313
host: {
1414
'class': 'mat-chip-input',
1515
'(keydown)': '_keydown($event)',
@@ -21,7 +21,7 @@ export class MdChipInput {
2121
_chipList: MdChipList;
2222

2323
/** Register input for chip list */
24-
@Input('mdChipList')
24+
@Input('mdChipInputFor')
2525
set chipList(value: MdChipList) {
2626
if (value) {
2727
this._chipList = value;
@@ -32,7 +32,7 @@ export class MdChipInput {
3232
/**
3333
* Whether or not the chipEnd event will be emitted when the input is blurred.
3434
*/
35-
@Input('mdChipListAddOnBlur')
35+
@Input('mdChipInputAddOnBlur')
3636
get addOnBlur() { return this._addOnBlur; }
3737
set addOnBlur(value) { this._addOnBlur = coerceBooleanProperty(value); }
3838
_addOnBlur: boolean = false;
@@ -43,19 +43,23 @@ export class MdChipInput {
4343
* Defaults to `[ENTER]`.
4444
*/
4545
// TODO(tinayuangao): Support Set here
46-
@Input() separatorKeysCodes: number[] = [ENTER];
46+
@Input('mdChipInputSeparatorKeyCodes') separatorKeyCodes: number[] = [ENTER];
4747

4848
/** Emitted when a chip is to be added. */
49-
@Output('mdChipEnd')
49+
@Output('mdChipInputTokenEnd')
5050
chipEnd = new EventEmitter<MdChipInputEvent>();
5151

52-
@Input('matChipList')
52+
@Input('matChipInputFor')
5353
set matChipList(value: MdChipList) { this.chipList = value; }
5454

55-
@Input('matChipListAddOnBlur')
55+
@Input('matChipInputAddOnBlur')
5656
get matAddOnBlur() { return this._addOnBlur; }
5757
set matAddOnBlur(value) { this.addOnBlur = value; }
5858

59+
@Input('matChipInputSeparatorKeyCodes')
60+
get matSeparatorKeyCodes() { return this.separatorKeyCodes; }
61+
set matSeparatorKeyCodes(v: number[]) { this.separatorKeyCodes = v; }
62+
5963
/** The native input element to which this directive is attached. */
6064
protected _inputElement: HTMLInputElement;
6165

@@ -77,7 +81,7 @@ export class MdChipInput {
7781

7882
/** Checks to see if the (chipEnd) event needs to be emitted. */
7983
_emitChipEnd(event?: KeyboardEvent) {
80-
if (!event || this.separatorKeysCodes.indexOf(event.keyCode) > -1) {
84+
if (!event || this.separatorKeyCodes.indexOf(event.keyCode) > -1) {
8185
this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value });
8286

8387
if (event) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class StandardChipList {
350350
<md-chip>Chip 1</md-chip>
351351
<md-chip>Chip 1</md-chip>
352352
<md-chip>Chip 1</md-chip>
353-
<input mdInput name="test" [mdChipList]="chipList"/>
353+
<input mdInput name="test" [mdChipInputFor]="chipList"/>
354354
</md-chip-list>
355355
</md-input-container>
356356
`

src/lib/chips/chip-list.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function _isInputEmpty(element: HTMLElement): boolean {
7373
export class MdChipList implements AfterContentInit, OnDestroy {
7474

7575
/** When a chip is destroyed, we track the index so we can focus the appropriate next chip. */
76-
protected _destroyedIndex: number = null;
76+
protected _lastDestroyedIndex: number = null;
7777

7878
/** Track which chips we're listening to for focus/destruction. */
7979
protected _chipSet: WeakMap<MdChip, boolean> = new WeakMap();
@@ -121,14 +121,14 @@ export class MdChipList implements AfterContentInit, OnDestroy {
121121

122122
// If we have 0 chips, attempt to focus an input (if available)
123123
if (chips.length == 0) {
124-
this.focusInput();
124+
this._focusInput();
125125
}
126126

127127
// Check to see if we need to update our tab index
128128
this._checkTabIndex();
129129

130130
// Check to see if we have a destroyed chip and need to refocus
131-
this._checkDestroyedFocus();
131+
this._updateFocusForDestroyedChips();
132132
});
133133
}
134134

@@ -138,6 +138,10 @@ export class MdChipList implements AfterContentInit, OnDestroy {
138138
}
139139
}
140140

141+
/**
142+
* Whether or not this chip is selectable. When a chip is not selectable,
143+
* it's selected state is always ignored.
144+
*/
141145
@Input()
142146
get selectable(): boolean { return this._selectable; }
143147
set selectable(value: boolean) {
@@ -150,20 +154,20 @@ export class MdChipList implements AfterContentInit, OnDestroy {
150154
}
151155

152156
/**
153-
* Programmatically focus the chip list. This in turn focuses the first non-disabled chip in this
154-
* chip list, or the input if available and there are 0 chips.
157+
* Focuses the the first non-disabled chip in this chip list, or the associated input when there
158+
* are no eligible chips.
155159
*/
156-
// TODO: ARIA says this should focus the first `selected` chip if any are selected.
157160
focus(event?: Event) {
161+
// TODO: ARIA says this should focus the first `selected` chip if any are selected.
158162
if (this.chips.length > 0) {
159163
this._keyManager.setFirstItemActive();
160164
} else {
161-
this.focusInput();
165+
this._focusInput();
162166
}
163167
}
164168

165169
/** Attempt to focus an input if we have one. */
166-
focusInput() {
170+
_focusInput() {
167171
if (this._inputElement) {
168172
this._inputElement.focus();
169173
}
@@ -262,7 +266,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
262266
} else if (chipIndex - 1 >= 0) {
263267
this._keyManager.setActiveItem(chipIndex - 1);
264268
}
265-
this._destroyedIndex = chipIndex;
269+
this._lastDestroyedIndex = chipIndex;
266270
}
267271

268272
this._chipSet.delete(chip);
@@ -276,17 +280,14 @@ export class MdChipList implements AfterContentInit, OnDestroy {
276280
* Checks to see if a focus chip was recently destroyed so that we can refocus the next closest
277281
* one.
278282
*/
279-
protected _checkDestroyedFocus() {
283+
protected _updateFocusForDestroyedChips() {
280284
let chipsArray = this.chips;
281285
let focusChip: MdChip;
282286

283-
if (this._destroyedIndex != null && chipsArray.length > 0) {
287+
if (this._lastDestroyedIndex != null && chipsArray.length > 0) {
284288
// Check whether the destroyed chip was the last item
285-
if (this._destroyedIndex >= chipsArray.length) {
286-
this._keyManager.setActiveItem(chipsArray.length - 1);
287-
} else if (this._destroyedIndex >= 0) {
288-
this._keyManager.setActiveItem(this._destroyedIndex);
289-
}
289+
const newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1);
290+
this._keyManager.setActiveItem(newFocusIndex);
290291

291292
// Focus the chip
292293
if (focusChip) {
@@ -295,7 +296,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
295296
}
296297

297298
// Reset our destroyed index
298-
this._destroyedIndex = null;
299+
this._lastDestroyedIndex = null;
299300
}
300301

301302
/**

0 commit comments

Comments
 (0)