Skip to content

Commit d64dde6

Browse files
committed
feat(tabs): Option switch from manual and automatic activation
1 parent 52af176 commit d64dde6

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/tabs/tab-headers.component.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ export class TabHeaders implements AfterViewInit, AfterContentInit {
9797
* @memberof Tabs
9898
*/
9999
@Input() cacheActive = false;
100+
/**
101+
* Set to 'true' to have tabs automatically activated and have their content displayed when they recieve focus.
102+
* @memberof TabHeaders
103+
*/
104+
@Input() automaticActivation: boolean;
105+
100106
/**
101107
* Gets the Unordered List element that holds the `Tab` headings from the view DOM.
102108
* @memberof TabHeaders
@@ -135,39 +141,58 @@ export class TabHeaders implements AfterViewInit, AfterContentInit {
135141
*/
136142
@HostListener("keydown", ["$event"])
137143
keyboardInput(event) {
144+
let tabsArray = Array.from<any>(this.tabs);
145+
138146
if (event.key === "Right" || event.key === "ArrowRight") {
139147
if (this.currentSelectedTab < this.allTabHeaders.length - 1) {
140148
event.preventDefault();
149+
if (this.automaticActivation) {
150+
this.selectTab(event.target, tabsArray[this.currentSelectedTab + 1], this.currentSelectedTab);
151+
}
141152
this.allTabHeaders[this.currentSelectedTab + 1].focus();
142153
} else {
143154
event.preventDefault();
155+
if (this.automaticActivation) {
156+
this.selectTab(event.target, tabsArray[0], 0);
157+
}
144158
this.allTabHeaders[0].focus();
145159
}
146160
}
147161

148162
if (event.key === "Left" || event.key === "ArrowLeft") {
149163
if (this.currentSelectedTab > 0) {
150164
event.preventDefault();
165+
if (this.automaticActivation) {
166+
this.selectTab(event.target, tabsArray[this.currentSelectedTab - 1], this.currentSelectedTab);
167+
}
151168
this.allTabHeaders[this.currentSelectedTab - 1].focus();
152169
} else {
153170
event.preventDefault();
171+
if (this.automaticActivation) {
172+
this.selectTab(event.target, tabsArray[this.allTabHeaders.length - 1], this.allTabHeaders.length);
173+
}
154174
this.allTabHeaders[this.allTabHeaders.length - 1].focus();
155175
}
156176
}
157177

158178
if (event.key === "Home") {
159179
event.preventDefault();
180+
if (this.automaticActivation) {
181+
this.selectTab(event.target, tabsArray[0], 0);
182+
}
160183
this.allTabHeaders[0].focus();
161184
}
162185

163186
if (event.key === "End") {
164187
event.preventDefault();
188+
if (this.automaticActivation) {
189+
this.selectTab(event.target, tabsArray[this.allTabHeaders.length - 1], this.allTabHeaders.length);
190+
}
165191
this.allTabHeaders[this.allTabHeaders.length - 1].focus();
166192
}
167193

168-
if (event.key === " ") {
169-
let selectedTab = Array.from<any>(this.tabInput);
170-
this.selectTab(event.target, selectedTab[this.currentSelectedTab], this.currentSelectedTab);
194+
if (event.key === " " && !this.automaticActivation) {
195+
this.selectTab(event.target, tabsArray[this.currentSelectedTab], this.currentSelectedTab);
171196
}
172197
}
173198

src/tabs/tabs.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { TabHeaders } from "./tab-headers.component";
4242
<ibm-tab-headers
4343
*ngIf="hasTabHeaders() && position === 'top'"
4444
[tabs]="tabs"
45+
[automaticActivation]="automaticActivation"
4546
[cacheActive]="cacheActive">
4647
</ibm-tab-headers>
4748
<ng-content></ng-content>
@@ -65,6 +66,12 @@ export class Tabs implements AfterContentInit {
6566
* @memberof Tabs
6667
*/
6768
@Input() cacheActive = false;
69+
/**
70+
* Set to 'true' to have tabs automatically activated and have their content displayed when they recieve focus.
71+
* @memberof Tabs
72+
*/
73+
@Input() automaticActivation = false;
74+
6875
/**
6976
* Maintains a `QueryList` of the `Tab` elements and updates if `Tab`s are added or removed.
7077
* @type {QueryList<Tab>}

src/tabs/tabs.stories.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { storiesOf, moduleMetadata } from "@storybook/angular";
2-
import { withKnobs } from "@storybook/addon-knobs/angular";
2+
import { withKnobs, boolean } from "@storybook/addon-knobs/angular";
33

44
import { TabsModule } from "../";
55

@@ -14,11 +14,14 @@ storiesOf("Tabs", module)
1414
.addDecorator(withKnobs)
1515
.add("Basic", () => ({
1616
template: `
17-
<ibm-tabs>
17+
<ibm-tabs [automaticActivation]="automaticActivation">
1818
<ibm-tab heading="one">foo</ibm-tab>
1919
<ibm-tab heading="two">bar</ibm-tab>
2020
</ibm-tabs>
21-
`
21+
`,
22+
props: {
23+
automaticActivation: boolean("automaticActivation", false)
24+
}
2225
}))
2326
.add("With template", () => ({
2427
template: `

0 commit comments

Comments
 (0)