Skip to content

Commit 466166b

Browse files
authored
Merge pull request #249 from youda97/contentswitcher-dap
fix(content-switcher): DAP Issues
2 parents a161635 + ac8e1d0 commit 466166b

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class ContentSwitcherOption {
1515
this._active = value;
1616
this.selectedClass = value;
1717
this.ariaSelected = value;
18+
this.tabindex = value ? "0" : "-1";
1819
}
1920

2021
get active() {
@@ -29,6 +30,7 @@ export class ContentSwitcherOption {
2930
@HostBinding("class.bx--content-switcher--selected") selectedClass = false;
3031
@HostBinding("attr.role") role = "tab";
3132
@HostBinding("attr.aria-selected") ariaSelected = false;
33+
@HostBinding("attr.tabIndex") tabindex = "-1";
3234

3335
protected _active = false;
3436

@@ -37,4 +39,16 @@ export class ContentSwitcherOption {
3739
this.active = true;
3840
this.selected.emit(true);
3941
}
42+
43+
@HostListener("focus")
44+
onFocus() {
45+
this.active = true;
46+
}
47+
48+
@HostListener("blur", ["$event"])
49+
onBlur(event) {
50+
if (event.relatedTarget) {
51+
this.active = false;
52+
}
53+
}
4054
}

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import {
55
QueryList,
66
Output,
77
EventEmitter,
8-
AfterViewInit
8+
AfterViewInit,
9+
HostListener,
10+
ElementRef
911
} from "@angular/core";
1012
import { ContentSwitcherOption } from "./content-switcher-option.directive";
13+
import { isFocusInLastItem, isFocusInFirstItem } from "./../common/tab.service";
1114

1215
/**
1316
*
@@ -31,7 +34,10 @@ export class ContentSwitcher implements AfterViewInit {
3134

3235
@ContentChildren(ContentSwitcherOption) options: QueryList<ContentSwitcherOption>;
3336

37+
constructor(protected elementRef: ElementRef) {}
38+
3439
ngAfterViewInit() {
40+
this.options.first.active = true;
3541
this.options.forEach(option => {
3642
option.selected.subscribe(_ => {
3743
const active = option;
@@ -44,4 +50,43 @@ export class ContentSwitcher implements AfterViewInit {
4450
});
4551
});
4652
}
53+
54+
@HostListener("keydown", ["$event"])
55+
hostkeys(event: KeyboardEvent) {
56+
const buttonList = Array.from<any>(this.elementRef.nativeElement.querySelectorAll(".bx--content-switcher-btn"));
57+
58+
switch (event.key) {
59+
case "Right": // IE specific value
60+
case "ArrowRight":
61+
event.preventDefault();
62+
if (!isFocusInLastItem(event, buttonList)) {
63+
const index = buttonList.findIndex(item => item === event.target);
64+
buttonList[index + 1].focus();
65+
} else {
66+
buttonList[0].focus();
67+
}
68+
break;
69+
70+
case "Left": // IE specific value
71+
case "ArrowLeft":
72+
event.preventDefault();
73+
if (!isFocusInFirstItem(event, buttonList)) {
74+
const index = buttonList.findIndex(item => item === event.target);
75+
buttonList[index - 1].focus();
76+
} else {
77+
buttonList[buttonList.length - 1].focus();
78+
}
79+
break;
80+
81+
case "Home":
82+
event.preventDefault();
83+
buttonList[0].focus();
84+
break;
85+
86+
case "End":
87+
event.preventDefault();
88+
buttonList[buttonList.length - 1].focus();
89+
break;
90+
}
91+
}
4792
}

0 commit comments

Comments
 (0)