Skip to content

Commit 9eb9ddf

Browse files
tinayuangaokara
authored andcommitted
feat(chips): add user defined tab index to chip list (#6073)
1 parent 154bb55 commit 9eb9ddf

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ <h4>Stacked Chips</h4>
102102
<code>(focus)</code> event to run custom code.
103103
</p>
104104

105-
<md-chip-list class="mat-chip-list-stacked">
105+
<md-chip-list class="mat-chip-list-stacked" [tabIndex]="-1">
106106
<md-chip *ngFor="let aColor of availableColors"
107107
(focus)="color = aColor.color" color="{{aColor.color}}" selected="true">
108108
{{aColor.name}}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ describe('MdChipList', () => {
230230

231231
expect(chipListInstance._tabIndex).toBe(0, 'Expected tabIndex to be reset back to 0');
232232
}));
233+
234+
it(`should use user defined tabIndex`, fakeAsync(() => {
235+
chipListInstance.tabIndex = 4;
236+
237+
fixture.detectChanges();
238+
239+
expect(chipListInstance._tabIndex)
240+
.toBe(4, 'Expected tabIndex to be set to user defined value 4.');
241+
242+
chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));
243+
244+
expect(chipListInstance._tabIndex)
245+
.toBe(-1, 'Expected tabIndex to be set to -1 temporarily.');
246+
247+
tick();
248+
249+
expect(chipListInstance._tabIndex).toBe(4, 'Expected tabIndex to be reset back to 4');
250+
}));
233251
});
234252
});
235253
});
@@ -312,7 +330,7 @@ describe('MdChipList', () => {
312330

313331
@Component({
314332
template: `
315-
<md-chip-list>
333+
<md-chip-list [tabIndex]="tabIndex">
316334
<div *ngFor="let i of [0,1,2,3,4]">
317335
<div *ngIf="remove != i">
318336
<md-chip (select)="chipSelect(i)" (deselect)="chipDeselect(i)">
@@ -328,6 +346,7 @@ class StandardChipList {
328346
remove: number;
329347
chipSelect: (index?: number) => void = () => {};
330348
chipDeselect: (index?: number) => void = () => {};
349+
tabIndex: number = 0;
331350
}
332351

333352
@Component({

src/lib/chips/chip-list.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ export class MdChipList implements AfterContentInit, OnDestroy {
7676
/** Tab index for the chip list. */
7777
_tabIndex = 0;
7878

79+
/**
80+
* User defined tab index.
81+
* When it is not null, use user defined tab index. Otherwise use _tabIndex
82+
*/
83+
_userTabIndex: number | null = null;
84+
7985
/** The FocusKeyManager which handles focus. */
8086
_keyManager: FocusKeyManager<MdChip>;
8187

@@ -93,7 +99,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
9399
// it back to the first chip when the user tabs out.
94100
this._tabOutSubscription = this._keyManager.tabOut.subscribe(() => {
95101
this._tabIndex = -1;
96-
setTimeout(() => this._tabIndex = 0);
102+
setTimeout(() => this._tabIndex = this._userTabIndex || 0);
97103
});
98104

99105
// Go ahead and subscribe all of the initial chips
@@ -138,6 +144,12 @@ export class MdChipList implements AfterContentInit, OnDestroy {
138144
this._selectable = coerceBooleanProperty(value);
139145
}
140146

147+
@Input()
148+
set tabIndex(value: number) {
149+
this._userTabIndex = value;
150+
this._tabIndex = value;
151+
}
152+
141153
/** Associates an HTML input element with this chip list. */
142154
registerInput(inputElement: HTMLInputElement) {
143155
this._inputElement = inputElement;
@@ -212,7 +224,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
212224
*/
213225
protected _updateTabIndex(): void {
214226
// If we have 0 chips, we should not allow keyboard focus
215-
this._tabIndex = (this.chips.length === 0 ? -1 : 0);
227+
this._tabIndex = this._userTabIndex || (this.chips.length === 0 ? -1 : 0);
216228
}
217229

218230
/**

0 commit comments

Comments
 (0)