Skip to content

Commit 8df3246

Browse files
robertmesserlejelbourn
authored andcommitted
feat(tabs): adds support for two-way bindings on selectedIndex (#702)
closes #687
1 parent 343619b commit 8df3246

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/components/tabs/tab-group.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,31 @@ describe('MdTabGroup', () => {
3939
checkSelectedIndex(0);
4040

4141
// select the second tab
42-
let tabLabel = fixture.debugElement.query(By.css('.md-tab-label:nth-of-type(2)'));
42+
let tabLabel = fixture.debugElement.queryAll(By.css('.md-tab-label'))[1];
4343
tabLabel.nativeElement.click();
4444
checkSelectedIndex(1);
4545

4646
// select the third tab
47-
tabLabel = fixture.debugElement.query(By.css('.md-tab-label:nth-of-type(3)'));
47+
tabLabel = fixture.debugElement.queryAll(By.css('.md-tab-label'))[2];
4848
tabLabel.nativeElement.click();
4949
checkSelectedIndex(2);
5050
});
5151

52+
it('should support two-way binding for selectedIndex', async(() => {
53+
let component = fixture.componentInstance;
54+
component.selectedIndex = 0;
55+
56+
fixture.detectChanges();
57+
58+
let tabLabel = fixture.debugElement.queryAll(By.css('.md-tab-label'))[1];
59+
tabLabel.nativeElement.click();
60+
61+
fixture.detectChanges();
62+
fixture.whenStable().then(() => {
63+
expect(component.selectedIndex).toBe(1);
64+
});
65+
}));
66+
5267
it('should cycle through tab focus with focusNextTab/focusPreviousTab functions',
5368
fakeAsync(() => {
5469
let testComponent = fixture.componentInstance;
@@ -170,7 +185,7 @@ describe('MdTabGroup', () => {
170185
selector: 'test-app',
171186
template: `
172187
<md-tab-group class="tab-group"
173-
[selectedIndex]="selectedIndex"
188+
[(selectedIndex)]="selectedIndex"
174189
(focusChange)="handleFocus($event)"
175190
(selectChange)="handleSelection($event)">
176191
<md-tab>

src/components/tabs/tabs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {MdTabContent} from './tab-content';
1616
import {MdTabLabelWrapper} from './tab-label-wrapper';
1717
import {MdInkBar} from './ink-bar';
1818
import {Observable} from 'rxjs/Observable';
19+
import 'rxjs/add/operator/map';
1920

2021
/** Used to generate unique ID's for each tab component */
2122
let nextId = 0;
@@ -68,6 +69,11 @@ export class MdTabGroup {
6869
return this._selectedIndex;
6970
}
7071

72+
/** Output to enable support for two-way binding on `selectedIndex`. */
73+
@Output('selectedIndexChange') private get _selectedIndexChange(): Observable<number> {
74+
return this.selectChange.map(event => event.index);
75+
}
76+
7177
private _onFocusChange: EventEmitter<MdTabChangeEvent> = new EventEmitter<MdTabChangeEvent>();
7278
@Output('focusChange') get focusChange(): Observable<MdTabChangeEvent> {
7379
return this._onFocusChange.asObservable();

0 commit comments

Comments
 (0)