Skip to content

feat(tabs): adds support for two-way bindings on selectedIndex #702

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

Merged
merged 1 commit into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/components/tabs/tab-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,31 @@ describe('MdTabGroup', () => {
checkSelectedIndex(0);

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

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

it('should support two-way binding for selectedIndex', async(() => {
let component = fixture.componentInstance;
component.selectedIndex = 0;

fixture.detectChanges();

let tabLabel = fixture.debugElement.queryAll(By.css('.md-tab-label'))[1];
tabLabel.nativeElement.click();

fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.selectedIndex).toBe(1);
});
}));

it('should cycle through tab focus with focusNextTab/focusPreviousTab functions',
fakeAsync(() => {
let testComponent = fixture.componentInstance;
Expand Down Expand Up @@ -170,7 +185,7 @@ describe('MdTabGroup', () => {
selector: 'test-app',
template: `
<md-tab-group class="tab-group"
[selectedIndex]="selectedIndex"
[(selectedIndex)]="selectedIndex"
(focusChange)="handleFocus($event)"
(selectChange)="handleSelection($event)">
<md-tab>
Expand Down
6 changes: 6 additions & 0 deletions src/components/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {MdTabContent} from './tab-content';
import {MdTabLabelWrapper} from './tab-label-wrapper';
import {MdInkBar} from './ink-bar';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';

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

/** Output to enable support for two-way binding on `selectedIndex`. */
@Output('selectedIndexChange') private get _selectedIndexChange(): Observable<number> {
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment here like

/** Output to enable support for two-way binding on `selectedIndex`. */

return this.selectChange.map(event => event.index);
}

private _onFocusChange: EventEmitter<MdTabChangeEvent> = new EventEmitter<MdTabChangeEvent>();
@Output('focusChange') get focusChange(): Observable<MdTabChangeEvent> {
return this._onFocusChange.asObservable();
Expand Down