Skip to content

Commit 7bfc37d

Browse files
committed
feat(tabs): add isActive flag on the individual tabs
* Adds the `isActive` flag on each tab, making it easier to check whether a tab is active. * Adds an `exportAs` to the tab directive to allow for it to be consumed easier in the view. Fixes #6422.
1 parent 0754320 commit 7bfc37d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
async, fakeAsync, tick, ComponentFixture, TestBed
33
} from '@angular/core/testing';
44
import {MdTabGroup, MdTabsModule, MdTabHeaderPosition} from './index';
5-
import {Component, ViewChild} from '@angular/core';
5+
import {Component, QueryList, ViewChild, ViewChildren} from '@angular/core';
66
import {NoopAnimationsModule, BrowserAnimationsModule} from '@angular/platform-browser/animations';
77
import {By} from '@angular/platform-browser';
88
import {Observable} from 'rxjs/Observable';
@@ -173,6 +173,23 @@ describe('MdTabGroup', () => {
173173
expect(testElement.querySelectorAll('.mat-ripple-element').length)
174174
.toBe(0, 'Expected no ripple to show up on label mousedown.');
175175
});
176+
177+
it('should set the isActive flag on each of the tabs', () => {
178+
fixture.detectChanges();
179+
180+
const tabs = fixture.componentInstance.tabs.toArray();
181+
182+
expect(tabs[0].isActive).toBe(false);
183+
expect(tabs[1].isActive).toBe(true);
184+
expect(tabs[2].isActive).toBe(false);
185+
186+
fixture.componentInstance.selectedIndex = 2;
187+
fixture.detectChanges();
188+
189+
expect(tabs[0].isActive).toBe(false);
190+
expect(tabs[1].isActive).toBe(false);
191+
expect(tabs[2].isActive).toBe(true);
192+
});
176193
});
177194

178195
describe('dynamic binding tabs', () => {
@@ -368,6 +385,7 @@ describe('nested MdTabGroup with enabled animations', () => {
368385
`
369386
})
370387
class SimpleTabsTestApp {
388+
@ViewChildren(MdTab) tabs: QueryList<MdTab>;
371389
selectedIndex: number = 1;
372390
focusEvent: any;
373391
selectEvent: any;

src/lib/tabs/tab-group.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export class MdTabGroup extends _MdTabGroupMixinBase implements AfterContentInit
170170
// Setup the position for each tab and optionally setup an origin on the next selected tab.
171171
this._tabs.forEach((tab: MdTab, index: number) => {
172172
tab.position = index - indexToSelect;
173+
tab.isActive = index === indexToSelect;
173174

174175
// If there is already a selected tab, then set up an origin for the next selected tab
175176
// if it doesn't have one already.

src/lib/tabs/tab.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const _MdTabMixinBase = mixinDisabled(MdTabBase);
2626
templateUrl: 'tab.html',
2727
inputs: ['disabled'],
2828
changeDetection: ChangeDetectionStrategy.OnPush,
29+
exportAs: 'mdTab',
2930
})
3031
export class MdTab extends _MdTabMixinBase implements OnInit, CanDisable, OnChanges, OnDestroy {
3132
/** Content for the tab label given by <ng-template md-tab-label>. */
@@ -56,6 +57,11 @@ export class MdTab extends _MdTabMixinBase implements OnInit, CanDisable, OnChan
5657
*/
5758
origin: number | null = null;
5859

60+
/**
61+
* Whether the tab is currently active.
62+
*/
63+
isActive = false;
64+
5965
constructor(private _viewContainerRef: ViewContainerRef) {
6066
super();
6167
}

0 commit comments

Comments
 (0)