Skip to content

Commit 4d36ee0

Browse files
crisbetoandrewseguin
authored andcommitted
feat(tabs): add isActive flag on the individual tabs (#6424)
* 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 08a6673 commit 4d36ee0

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
@@ -1,5 +1,5 @@
11
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
2-
import {Component, ViewChild} from '@angular/core';
2+
import {Component, QueryList, ViewChild, ViewChildren} from '@angular/core';
33
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
44
import {By} from '@angular/platform-browser';
55
import {ViewportRuler} from '@angular/cdk/overlay';
@@ -169,6 +169,23 @@ describe('MdTabGroup', () => {
169169
expect(testElement.querySelectorAll('.mat-ripple-element').length)
170170
.toBe(0, 'Expected no ripple to show up on label mousedown.');
171171
});
172+
173+
it('should set the isActive flag on each of the tabs', () => {
174+
fixture.detectChanges();
175+
176+
const tabs = fixture.componentInstance.tabs.toArray();
177+
178+
expect(tabs[0].isActive).toBe(false);
179+
expect(tabs[1].isActive).toBe(true);
180+
expect(tabs[2].isActive).toBe(false);
181+
182+
fixture.componentInstance.selectedIndex = 2;
183+
fixture.detectChanges();
184+
185+
expect(tabs[0].isActive).toBe(false);
186+
expect(tabs[1].isActive).toBe(false);
187+
expect(tabs[2].isActive).toBe(true);
188+
});
172189
});
173190

174191
describe('dynamic binding tabs', () => {
@@ -364,6 +381,7 @@ describe('nested MdTabGroup with enabled animations', () => {
364381
`
365382
})
366383
class SimpleTabsTestApp {
384+
@ViewChildren(MdTab) tabs: QueryList<MdTab>;
367385
selectedIndex: number = 1;
368386
focusEvent: any;
369387
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)