Skip to content

Commit c175059

Browse files
authored
Merge pull request #475 from cal-smith/issue#450
fix(content-switcher): change after checked error and documentation
2 parents f3108a0 + e1bfb92 commit c175059

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/content-switcher/content-switcher-option.directive.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
selector: "[ibmContentOption]"
1212
})
1313
export class ContentSwitcherOption {
14+
/**
15+
* Used to activate the option. Only one option may be `active` at a time
16+
*/
1417
@Input() set active (value: boolean) {
1518
this._active = value;
1619
this.selectedClass = value;
@@ -22,8 +25,16 @@ export class ContentSwitcherOption {
2225
return this._active;
2326
}
2427

28+
/**
29+
* Internal name for the option.
30+
* Should be something that identifies the option to the application.
31+
* Accessible from the `ContentSwitcher` `selected` emitter
32+
*/
2533
@Input() name = "option";
2634

35+
/**
36+
* Emits when the option is selected.
37+
*/
2738
@Output() selected = new EventEmitter();
2839

2940
@HostBinding("class") switcherClass = "bx--content-switcher-btn";

src/content-switcher/content-switcher.component.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ import { ContentSwitcherOption } from "./content-switcher-option.directive";
1313
import { isFocusInLastItem, isFocusInFirstItem } from "./../common/tab.service";
1414

1515
/**
16+
* The content switcher can be used for toggling between distinct options.
17+
* Similar to tabs, but without an associated content panel
1618
*
17-
*
19+
* ```html
20+
* <ibm-content-switcher (selected)="selected($event)">
21+
* <button ibmContentOption>First section</button>
22+
* <button ibmContentOption>Second section</button>
23+
* <button ibmContentOption>Third section</button>
24+
* </ibm-content-switcher>
25+
* ```
1826
*/
1927
@Component({
2028
selector: "ibm-content-switcher",
@@ -28,16 +36,27 @@ import { isFocusInLastItem, isFocusInFirstItem } from "./../common/tab.service";
2836
`
2937
})
3038
export class ContentSwitcher implements AfterViewInit {
39+
/**
40+
* aria-label for the content switcher. Should be set to something descriptive
41+
*/
3142
@Input() label = "content switcher";
3243

44+
/**
45+
* Emits the activated `ContentSwitcherOption`
46+
*/
3347
@Output() selected = new EventEmitter();
3448

3549
@ContentChildren(ContentSwitcherOption) options: QueryList<ContentSwitcherOption>;
3650

3751
constructor(protected elementRef: ElementRef) {}
3852

3953
ngAfterViewInit() {
40-
this.options.first.active = true;
54+
const firstActive = this.options.find(option => option.active);
55+
// delay setting active until the DOM has settled
56+
if (!firstActive) {
57+
setTimeout(() => this.options.first.active = true);
58+
}
59+
// subscribe to each item, emit when one is selected, and reset the active states
4160
this.options.forEach(option => {
4261
option.selected.subscribe(_ => {
4362
const active = option;
@@ -53,7 +72,7 @@ export class ContentSwitcher implements AfterViewInit {
5372

5473
@HostListener("keydown", ["$event"])
5574
hostkeys(event: KeyboardEvent) {
56-
const buttonList = Array.from<any>(this.elementRef.nativeElement.querySelectorAll(".bx--content-switcher-btn"));
75+
const buttonList = Array.from<any>(this.elementRef.nativeElement.querySelectorAll("[ibmContentOption]"));
5776

5877
switch (event.key) {
5978
case "Right": // IE specific value

0 commit comments

Comments
 (0)