Skip to content

Commit 231467d

Browse files
robertmesserlejelbourn
authored andcommitted
fix(tabs): adds support for async tabs (#639)
closes #574
1 parent a6e74ce commit 231467d

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

src/components/tabs/ink-bar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class MdInkBar {
2727
* @returns {string}
2828
*/
2929
private _getLeftPosition(element: HTMLElement): string {
30-
return element.offsetLeft + 'px';
30+
return element ? element.offsetLeft + 'px' : '0';
3131
}
3232

3333
/**
@@ -36,6 +36,6 @@ export class MdInkBar {
3636
* @returns {string}
3737
*/
3838
private _getElementWidth(element: HTMLElement): string {
39-
return element.offsetWidth + 'px';
39+
return element ? element.offsetWidth + 'px' : '0';
4040
}
4141
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing'
1010
import {MD_TABS_DIRECTIVES, MdTabGroup} from './tabs';
1111
import {Component} from '@angular/core';
1212
import {By} from '@angular/platform-browser';
13+
import {Observable} from 'rxjs/Observable';
1314

1415
describe('MdTabGroup', () => {
1516
let builder: TestComponentBuilder;
@@ -86,6 +87,26 @@ describe('MdTabGroup', () => {
8687
});
8788
});
8889

90+
describe('async tabs', () => {
91+
beforeEach(async(() => {
92+
builder.createAsync(AsyncTabsTestApp).then(f => fixture = f);
93+
}));
94+
95+
it('should show tabs when they are available', async(() => {
96+
let labels = fixture.debugElement.queryAll(By.css('.md-tab-label'));
97+
98+
expect(labels.length).toBe(0);
99+
100+
fixture.detectChanges();
101+
102+
fixture.whenStable().then(() => {
103+
fixture.detectChanges();
104+
labels = fixture.debugElement.queryAll(By.css('.md-tab-label'));
105+
expect(labels.length).toBe(2);
106+
});
107+
}));
108+
});
109+
89110
/**
90111
* Checks that the `selectedIndex` has been updated; checks that the label and body have the
91112
* `md-active` class
@@ -130,3 +151,30 @@ describe('MdTabGroup', () => {
130151
class SimpleTabsTestApp {
131152
selectedIndex: number = 1;
132153
}
154+
155+
@Component({
156+
selector: 'test-app',
157+
template: `
158+
<md-tab-group class="tab-group">
159+
<md-tab *ngFor="let tab of tabs | async">
160+
<template md-tab-label>{{ tab.label }}</template>
161+
<template md-tab-content>{{ tab.content }}</template>
162+
</md-tab>
163+
</md-tab-group>
164+
`,
165+
directives: [MD_TABS_DIRECTIVES]
166+
})
167+
class AsyncTabsTestApp {
168+
private _tabs = [
169+
{ label: 'one', content: 'one' },
170+
{ label: 'two', content: 'two' }
171+
];
172+
173+
tabs: Observable<any>;
174+
175+
constructor() {
176+
this.tabs = Observable.create((observer: any) => {
177+
requestAnimationFrame(() => observer.next(this._tabs));
178+
});
179+
}
180+
}

src/components/tabs/tabs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class MdTabGroup {
6464
* ViewChildren references are ready.
6565
*/
6666
private get _currentLabelWrapper(): HTMLElement {
67-
return this._labelWrappers
67+
return this._labelWrappers && this._labelWrappers.toArray()[this.selectedIndex]
6868
? this._labelWrappers.toArray()[this.selectedIndex].elementRef.nativeElement
6969
: null;
7070
}

src/demo-app/tabs/tab-group-demo.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ <h1>Tab Group Demo</h1>
1212
</template>
1313
</md-tab>
1414
</md-tab-group>
15+
16+
<h1>Async Tabs</h1>
17+
18+
<md-tab-group class="demo-tab-group">
19+
<md-tab *ngFor="let tab of asyncTabs | async">
20+
<template md-tab-label>{{tab.label}}</template>
21+
<template md-tab-content>
22+
{{tab.content}}
23+
<br>
24+
<br>
25+
<br>
26+
<md-input placeholder="Tab Label" [(ngModel)]="tab.label"></md-input>
27+
</template>
28+
</md-tab>
29+
</md-tab-group>

src/demo-app/tabs/tab-group-demo.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Component, ViewEncapsulation} from '@angular/core';
22
import {MD_TABS_DIRECTIVES} from '@angular2-material/tabs/tabs';
33
import {MdToolbar} from '@angular2-material/toolbar/toolbar';
44
import {MdInput} from '@angular2-material/input/input';
5+
import {Observable} from 'rxjs/Observable';
56

67
@Component({
78
moduleId: module.id,
@@ -17,4 +18,12 @@ export class TabsDemo {
1718
{ label: 'Tab Two', content: 'This is the body of the second tab' },
1819
{ label: 'Tab Three', content: 'This is the body of the third tab' },
1920
];
21+
asyncTabs: Observable<any>;
22+
constructor() {
23+
this.asyncTabs = Observable.create((observer: any) => {
24+
setTimeout(() => {
25+
observer.next(this.tabs);
26+
}, 1000);
27+
});
28+
}
2029
}

0 commit comments

Comments
 (0)