@@ -5,9 +5,12 @@ import {
55 QueryList ,
66 Output ,
77 EventEmitter ,
8- AfterViewInit
8+ AfterViewInit ,
9+ HostListener ,
10+ ElementRef
911} from "@angular/core" ;
1012import { 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