Skip to content

Commit daff4af

Browse files
committed
Updated tab switching logic
1 parent 58de5eb commit daff4af

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

components/tabs/tab.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class SuiTab {
1414
}
1515
}
1616

17+
public index:number;
18+
1719
private _content:SuiTabContent;
1820

1921
public set content(content:SuiTabContent) {
@@ -27,7 +29,10 @@ export class SuiTab {
2729
private _stateObserver: Observer<SuiTab>;
2830

2931
constructor() {
30-
this.stateChanged$ = new Observable<SuiTab>((observer:any) => this._stateObserver = observer);
32+
this.stateChanged$ = new Observable<SuiTab>((observer:any) => {
33+
this._stateObserver = observer;
34+
this._stateObserver.next(this);
35+
});
3136
}
3237

3338
private _isActive:boolean = false;
@@ -39,7 +44,9 @@ export class SuiTab {
3944
public set isActive(value:boolean) {
4045
var change = this._isActive != value;
4146
this._isActive = value;
42-
this._content.isActive = value;
47+
if (this._content) {
48+
this._content.isActive = value;
49+
}
4350
this.stateObserverNext(change);
4451
this.isActiveChange.emit(this._isActive);
4552

@@ -62,7 +69,7 @@ export class SuiTab {
6269
}
6370

6471
private stateObserverNext(change:boolean) {
65-
if (change) {
72+
if (change && this._stateObserver) {
6673
this._stateObserver.next(this);
6774
}
6875
}

components/tabs/tabset.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class SuiTabset implements AfterContentInit {
4141
throw new Error("You cannot have no tabs!");
4242
}
4343
//For every content header we have managed to find,
44-
this._loadedTabs.forEach((t:SuiTab) => {
44+
this._loadedTabs.forEach((t:SuiTab, i:number) => {
4545
//Assuming they have an associated content
4646
if (!t.content) {
4747
//Link the content header with the content with the same ID excluding ones that are already linked
@@ -56,6 +56,8 @@ export class SuiTabset implements AfterContentInit {
5656
t.content = possibleContents.pop();
5757
}
5858

59+
t.index = i;
60+
5961
//Next observe the content's state to catch any changes made anywhere
6062
t.stateChanged$.subscribe((t:SuiTab) => this.tabStateChanged(t));
6163
});
@@ -81,8 +83,8 @@ export class SuiTabset implements AfterContentInit {
8183
this._activeTab = tab;
8284
}
8385
//Otherwise check there are no active tabs
84-
else if (this._activeTab && !this._loadedTabs.filter((tH:SuiTab) => tH.isActive).length) {
85-
this.activateClosestTab(tab);
86+
else if (this._activeTab && !this._loadedTabs.find((tH:SuiTab) => tH.isActive)) {
87+
this.activateClosestTab(this._activeTab);
8688
}
8789

8890
//Check if the content is now disabled, and if so if is currently active
@@ -114,13 +116,14 @@ export class SuiTabset implements AfterContentInit {
114116
private activateClosestTab(tab:SuiTab) {
115117
//Grab a list of all of the loaded tabs that aren't disabled (excluding the one we are leaving)
116118
var availableTabs = this._loadedTabs
117-
.filter((tH:SuiTab) => !tH.isDisabled || tH == tab);
119+
.filter((tH:SuiTab) => !tH.isDisabled);
118120

119-
var tabIndex = availableTabs
120-
.findIndex((tH:SuiTab) => tH == tab);
121+
var tabIndex = tab.index;
121122

122-
//Go to the previous content, unless it is the 1st content.
123-
tabIndex += (tabIndex ? -1 : 1);
123+
// Only change the tabIndex if we have to.
124+
if (tabIndex >= availableTabs.length) {
125+
tabIndex = availableTabs.length - 1;
126+
}
124127

125128
availableTabs[tabIndex].isActive = true;
126129
//This if we just activated a disabled content, not to worry as it will bubble through

demo/app/pages/tabs/tabs.page.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class TabsPage {
9292
<sui-tabset>
9393
<div class="ui top attached tabular menu">
9494
<a class="item" suiTabHeader="static">Static</a>
95-
<a class="item" *ngFor="let tab of tabs; let i = index" [suiTabHeader]="i">{{ tab.header }}</a>
95+
<a class="item" *ngFor="let tab of tabs; let i = index" [suiTabHeader]="i" [(isActive)]="active[i]">{{ tab.header }}</a>
9696
</div>
9797
<div class="ui bottom attached segment" *ngFor="let tab of tabs; let i = index" [suiTabContent]="i">{{ tab.content }}</div>
9898
<div class="ui bottom attached segment" suiTabContent="static">
@@ -149,18 +149,21 @@ export class TabExampleProperties {
149149
template: new TabsPage().exampleDynamicTemplate
150150
})
151151
export class TabExampleDynamic {
152+
public active:boolean[] = [];
152153
public tabs = [
153154
{ header: "1st", content: "Dynamic content" },
154155
{ header: "2nd", content: "More content" },
155156
{ header: "3rd", content: "Even more content" }
156157
];
157158
public addTab() {
159+
this.active.push(true);
158160
this.tabs.push({
159161
header: "New",
160162
content: "Another dynamic tab"
161163
});
162164
};
163165
public removeTab() {
166+
this.active.pop();
164167
this.tabs.pop();
165168
}
166169
}

0 commit comments

Comments
 (0)