Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chips): Add user defined tab index to chip list #6073

Merged
merged 3 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/demo-app/chips/chips-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ <h4>Stacked Chips</h4>
<code>(focus)</code> event to run custom code.
</p>

<md-chip-list class="mat-chip-list-stacked">
<md-chip-list class="mat-chip-list-stacked" [tabIndex]="-1">
<md-chip *ngFor="let aColor of availableColors"
(focus)="color = aColor.color" color="{{aColor.color}}" selected="true">
{{aColor.name}}
Expand Down
21 changes: 20 additions & 1 deletion src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ describe('MdChipList', () => {

expect(chipListInstance._tabIndex).toBe(0, 'Expected tabIndex to be reset back to 0');
}));

it(`should use user defined tabIndex`, fakeAsync(() => {
chipListInstance.tabIndex = 4;

fixture.detectChanges();

expect(chipListInstance._tabIndex)
.toBe(4, 'Expected tabIndex to be set to user defined value 4.');

chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));

expect(chipListInstance._tabIndex)
.toBe(-1, 'Expected tabIndex to be set to -1 temporarily.');

tick();

expect(chipListInstance._tabIndex).toBe(4, 'Expected tabIndex to be reset back to 4');
}));
});
});
});
Expand Down Expand Up @@ -311,7 +329,7 @@ describe('MdChipList', () => {

@Component({
template: `
<md-chip-list>
<md-chip-list [tabIndex]="tabIndex">
<div *ngFor="let i of [0,1,2,3,4]">
<div *ngIf="remove != i">
<md-chip (select)="chipSelect(i)" (deselect)="chipDeselect(i)">
Expand All @@ -327,6 +345,7 @@ class StandardChipList {
remove: number;
chipSelect: (index?: number) => void = () => {};
chipDeselect: (index?: number) => void = () => {};
tabIndex: number = 0;
}

@Component({
Expand Down
14 changes: 12 additions & 2 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class MdChipList implements AfterContentInit, OnDestroy {
/** Tab index for the chip list. */
_tabIndex = 0;

/** User defined tab index.
* When it is not null, use user defined tab index. Otherwise use _tabIndex */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting on the JsDoc is slightly off; should either be one line or formatted like

  /**
   * First line of the comment and
   * the second line of the comment
   */

_userTabIndex: number | null = null;

/** The FocusKeyManager which handles focus. */
_keyManager: FocusKeyManager;

Expand All @@ -93,7 +97,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
// it back to the first chip when the user tabs out.
this._tabOutSubscription = this._keyManager.tabOut.subscribe(() => {
this._tabIndex = -1;
setTimeout(() => this._tabIndex = 0);
setTimeout(() => this._tabIndex = this._userTabIndex || 0);
});

// Go ahead and subscribe all of the initial chips
Expand Down Expand Up @@ -138,6 +142,12 @@ export class MdChipList implements AfterContentInit, OnDestroy {
this._selectable = coerceBooleanProperty(value);
}

@Input()
set tabIndex(value: number) {
this._userTabIndex = value;
this._tabIndex = value;
}

/** Associates an HTML input element with this chip list. */
registerInput(inputElement: HTMLInputElement) {
this._inputElement = inputElement;
Expand Down Expand Up @@ -212,7 +222,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
*/
protected _updateTabIndex(): void {
// If we have 0 chips, we should not allow keyboard focus
this._tabIndex = (this.chips.length === 0 ? -1 : 0);
this._tabIndex = this._userTabIndex || (this.chips.length === 0 ? -1 : 0);
}

/**
Expand Down