66} from "@angular/core" ;
77import { Table } from "./table.component" ;
88import { getFocusElementList , tabbableSelectorIgnoreTabIndex } from "../common/tab.service" ;
9+ import { setTabIndex } from "../utils/a11y" ;
910
1011@Directive ( {
1112 selector : "[ibmDataGridFocus]"
@@ -33,25 +34,26 @@ export class DataGridFocus {
3334 if ( ! this . ibmDataGridFocus ) {
3435 return ;
3536 }
36- const rows = this . elementRef . nativeElement . closest ( "table" ) . rows ;
37- let rowIndex = Array . from ( rows ) . findIndex ( item => item === this . elementRef . nativeElement . closest ( "tr" ) ) ;
37+ const element = this . elementRef . nativeElement ;
38+ const rows = element . closest ( "table" ) . rows ;
39+ let rowIndex = Array . from ( rows ) . findIndex ( item => item === element . closest ( "tr" ) ) ;
3840
3941 switch ( event . key ) {
4042 case "Right" : // IE specific value
4143 case "ArrowRight" :
42- if ( this . elementRef . nativeElement . nextElementSibling ) {
43- const nextSibling = this . elementRef . nativeElement . nextElementSibling ;
44- Table . setTabIndex ( this . elementRef . nativeElement , - 1 ) ;
45- Table . setTabIndex ( nextSibling , 0 ) ;
44+ if ( element . nextElementSibling && element . nextElementSibling . style . width !== "0px" ) {
45+ const nextSibling = element . nextElementSibling ;
46+ setTabIndex ( element , - 1 ) ;
47+ setTabIndex ( nextSibling , 0 ) ;
4648 this . focus ( nextSibling ) ;
4749 }
4850 break ;
4951 case "Left" : // IE specific value
5052 case "ArrowLeft" :
51- if ( this . elementRef . nativeElement . previousElementSibling ) {
52- const previousSibling = this . elementRef . nativeElement . previousElementSibling ;
53- Table . setTabIndex ( this . elementRef . nativeElement , - 1 ) ;
54- Table . setTabIndex ( previousSibling , 0 ) ;
53+ if ( element . previousElementSibling ) {
54+ const previousSibling = element . previousElementSibling ;
55+ setTabIndex ( element , - 1 ) ;
56+ setTabIndex ( previousSibling , 0 ) ;
5557 this . focus ( previousSibling ) ;
5658 }
5759 break ;
@@ -60,64 +62,64 @@ export class DataGridFocus {
6062 if ( rowIndex < rows . length - 1 ) {
6163 rowIndex ++ ;
6264 const row = rows [ rowIndex ] . querySelectorAll ( "td" ) ;
63- Table . setTabIndex ( this . elementRef . nativeElement , - 1 ) ;
65+ setTabIndex ( element , - 1 ) ;
6466 if ( rows [ rowIndex ] . className === "bx--expandable-row-v2" ) {
65- Table . setTabIndex ( row [ 0 ] , 0 ) ;
67+ setTabIndex ( row [ 0 ] , 0 ) ;
6668 this . focus ( row [ 0 ] ) ;
6769 } else {
68- Table . setTabIndex ( row [ this . columnIndex ] , 0 ) ;
70+ setTabIndex ( row [ this . columnIndex ] , 0 ) ;
6971 this . focus ( row [ this . columnIndex ] ) ;
7072 }
7173 }
7274 break ;
7375 case "Up" : // IE specific value
7476 case "ArrowUp" :
7577 const headerRow = rows [ 0 ] . querySelectorAll ( "th" ) ;
76- Table . setTabIndex ( this . elementRef . nativeElement , - 1 ) ;
78+ setTabIndex ( element , - 1 ) ;
7779 if ( rowIndex === 1 && Array . from ( headerRow ) . some ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length > 0 ) ) {
78- Table . setTabIndex ( headerRow [ this . columnIndex ] , 0 ) ;
80+ setTabIndex ( headerRow [ this . columnIndex ] , 0 ) ;
7981 this . focus ( headerRow [ this . columnIndex ] ) ;
8082 } else if ( rowIndex > 1 ) {
8183 rowIndex -- ;
8284 const row = rows [ rowIndex ] . querySelectorAll ( "td" ) ;
8385 if ( rows [ rowIndex ] . className === "bx--expandable-row-v2" ) {
84- Table . setTabIndex ( row [ 0 ] , 0 ) ;
86+ setTabIndex ( row [ 0 ] , 0 ) ;
8587 this . focus ( row [ 0 ] ) ;
8688 } else {
87- Table . setTabIndex ( row [ this . columnIndex ] , 0 ) ;
89+ setTabIndex ( row [ this . columnIndex ] , 0 ) ;
8890 this . focus ( row [ this . columnIndex ] ) ;
8991 }
9092 }
9193 break ;
9294 case "Home" :
93- Table . setTabIndex ( this . elementRef . nativeElement , - 1 ) ;
95+ setTabIndex ( element , - 1 ) ;
9496 if ( event . ctrlKey ) {
9597 const headerRow = rows [ 0 ] . querySelectorAll ( "th" ) ;
9698 if ( Array . from ( headerRow ) . some ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length > 0 ) ) {
97- Table . setTabIndex ( headerRow [ 0 ] , 0 ) ;
99+ setTabIndex ( headerRow [ 0 ] , 0 ) ;
98100 this . focus ( headerRow [ 0 ] ) ;
99101 } else {
100102 const firstBodyCell = rows [ 1 ] . querySelectorAll ( "td" ) [ 0 ] ;
101- Table . setTabIndex ( firstBodyCell , 0 ) ;
103+ setTabIndex ( firstBodyCell , 0 ) ;
102104 this . focus ( firstBodyCell ) ;
103105 }
104- } else if ( this . elementRef . nativeElement . parentElement . className !== "bx--expandable-row-v2" ) {
106+ } else if ( element . parentElement . className !== "bx--expandable-row-v2" ) {
105107 const firstRowCell = rows [ rowIndex ] . querySelectorAll ( "th, td" ) [ 0 ] ;
106- Table . setTabIndex ( firstRowCell , 0 ) ;
108+ setTabIndex ( firstRowCell , 0 ) ;
107109 this . focus ( firstRowCell ) ;
108110 }
109111 break ;
110112 case "End" :
111113 const lastRow = rows [ rows . length - 1 ] . querySelectorAll ( "td" ) ;
112- Table . setTabIndex ( this . elementRef . nativeElement , - 1 ) ;
114+ setTabIndex ( element , - 1 ) ;
113115 if ( event . ctrlKey ) {
114116 this . columnIndex = lastRow . length - 1 ;
115- Table . setTabIndex ( lastRow [ this . columnIndex ] , 0 ) ;
117+ setTabIndex ( lastRow [ this . columnIndex ] , 0 ) ;
116118 this . focus ( lastRow [ this . columnIndex ] ) ;
117- } else if ( this . elementRef . nativeElement . parentElement . className !== "bx--expandable-row-v2" ) {
119+ } else if ( element . parentElement . className !== "bx--expandable-row-v2" ) {
118120 this . columnIndex = lastRow . length - 1 ;
119121 const lastRowCell = rows [ rowIndex ] . querySelectorAll ( "th, td" ) [ this . columnIndex ] ;
120- Table . setTabIndex ( lastRowCell , 0 ) ;
122+ setTabIndex ( lastRowCell , 0 ) ;
121123 this . focus ( lastRowCell ) ;
122124 }
123125 break ;
@@ -130,8 +132,8 @@ export class DataGridFocus {
130132 return ;
131133 }
132134 const focusElementList = getFocusElementList ( this . elementRef . nativeElement . closest ( "table" ) , tabbableSelectorIgnoreTabIndex ) ;
133- focusElementList . forEach ( element => Table . setTabIndex ( element , - 1 ) ) ;
134- Table . setTabIndex ( this . elementRef . nativeElement , 0 ) ;
135+ focusElementList . forEach ( element => setTabIndex ( element , - 1 ) ) ;
136+ setTabIndex ( this . elementRef . nativeElement , 0 ) ;
135137 this . focus ( this . elementRef . nativeElement ) ;
136138 }
137139}
0 commit comments