88} from "@angular/core" ;
99import { Table } from "./table.component" ;
1010import { getFocusElementList , tabbableSelectorIgnoreTabIndex } from "../common/tab.service" ;
11- import { setTabIndex } from "../utils/a11y" ;
1211
1312@Directive ( {
1413 selector : "[ibmDataGridFocus]"
@@ -25,6 +24,7 @@ export class DataGridFocus {
2524 get columnIndex ( ) : number {
2625 return this . _columnIndex ;
2726 }
27+
2828 @Output ( ) columnIndexChange : EventEmitter < number > = new EventEmitter ( ) ;
2929
3030 protected _columnIndex : number ;
@@ -60,8 +60,8 @@ export class DataGridFocus {
6060 if ( element . nextElementSibling && Array . from ( headerRow ) . indexOf ( element . nextElementSibling ) < headerRow . length - 1 ) {
6161 this . columnIndex ++ ;
6262 const nextSibling = element . nextElementSibling ;
63- setTabIndex ( element , - 1 ) ;
64- setTabIndex ( nextSibling , 0 ) ;
63+ Table . setTabIndex ( element , - 1 ) ;
64+ Table . setTabIndex ( nextSibling , 0 ) ;
6565 this . focus ( nextSibling ) ;
6666 }
6767 break ;
@@ -70,8 +70,8 @@ export class DataGridFocus {
7070 if ( element . previousElementSibling ) {
7171 this . columnIndex -- ;
7272 const previousSibling = element . previousElementSibling ;
73- setTabIndex ( element , - 1 ) ;
74- setTabIndex ( previousSibling , 0 ) ;
73+ Table . setTabIndex ( element , - 1 ) ;
74+ Table . setTabIndex ( previousSibling , 0 ) ;
7575 this . focus ( previousSibling ) ;
7676 }
7777 break ;
@@ -80,62 +80,68 @@ export class DataGridFocus {
8080 if ( rowIndex < rows . length - 1 ) {
8181 rowIndex ++ ;
8282 const row = rows [ rowIndex ] . querySelectorAll ( "td" ) ;
83- setTabIndex ( element , - 1 ) ;
84- if ( rows [ rowIndex ] . classList . item ( 0 ) === "bx--expandable -row-v2" ) {
85- setTabIndex ( row [ 0 ] , 0 ) ;
83+ Table . setTabIndex ( element , - 1 ) ;
84+ if ( rows [ rowIndex ] . classList . contains ( "bx--expandable-row-v2" ) && ! rows [ rowIndex ] . classList . contains ( "bx--parent -row-v2" ) ) {
85+ Table . setTabIndex ( row [ 0 ] , 0 ) ;
8686 this . focus ( row [ 0 ] ) ;
8787 } else {
88- setTabIndex ( row [ this . columnIndex ] , 0 ) ;
88+ if ( this . columnIndex > row . length - 1 ) {
89+ this . columnIndex = row . length - 1 ;
90+ }
91+ Table . setTabIndex ( row [ this . columnIndex ] , 0 ) ;
8992 this . focus ( row [ this . columnIndex ] ) ;
9093 }
9194 }
9295 break ;
9396 case "Up" : // IE specific value
9497 case "ArrowUp" :
95- if ( ( rowIndex !== 1 && Array . from ( headerRow ) . some ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length < 0 ) ) ||
98+ if ( ( rowIndex === 1 && Array . from ( headerRow ) . every ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length === 0 ) ) ||
9699 rowIndex === 0 ) {
97100 return ;
98101 }
99- setTabIndex ( element , - 1 ) ;
102+ Table . setTabIndex ( element , - 1 ) ;
100103 rowIndex -- ;
101104 const row = rows [ rowIndex ] . querySelectorAll ( "td, th" ) ;
102- if ( rows [ rowIndex ] . classList . item ( 0 ) === "bx--expandable -row-v2" ) {
103- setTabIndex ( row [ 0 ] , 0 ) ;
105+ if ( rows [ rowIndex ] . classList . contains ( "bx--expandable-row-v2" ) && ! rows [ rowIndex ] . classList . contains ( "bx--parent -row-v2" ) ) {
106+ Table . setTabIndex ( row [ 0 ] , 0 ) ;
104107 this . focus ( row [ 0 ] ) ;
105108 } else {
106- setTabIndex ( row [ this . columnIndex ] , 0 ) ;
109+ if ( this . columnIndex > row . length - 1 ) {
110+ this . columnIndex = row . length - 1 ;
111+ }
112+ Table . setTabIndex ( row [ this . columnIndex ] , 0 ) ;
107113 this . focus ( row [ this . columnIndex ] ) ;
108114 }
109115 break ;
110116 case "Home" :
111117 this . columnIndex = 0 ;
112- setTabIndex ( element , - 1 ) ;
118+ Table . setTabIndex ( element , - 1 ) ;
113119 if ( event . ctrlKey ) {
114120 if ( Array . from ( headerRow ) . some ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length > 0 ) ) {
115- setTabIndex ( headerRow [ 0 ] , 0 ) ;
121+ Table . setTabIndex ( headerRow [ 0 ] , 0 ) ;
116122 this . focus ( headerRow [ 0 ] ) ;
117123 } else {
118124 const firstBodyCell = rows [ 1 ] . querySelectorAll ( "td" ) [ 0 ] ;
119- setTabIndex ( firstBodyCell , 0 ) ;
125+ Table . setTabIndex ( firstBodyCell , 0 ) ;
120126 this . focus ( firstBodyCell ) ;
121127 }
122128 } else {
123129 const firstRowCell = rows [ rowIndex ] . querySelectorAll ( "th, td" ) [ 0 ] ;
124- setTabIndex ( firstRowCell , 0 ) ;
130+ Table . setTabIndex ( firstRowCell , 0 ) ;
125131 this . focus ( firstRowCell ) ;
126132 }
127133 break ;
128134 case "End" :
129135 const lastRow = rows [ rows . length - 1 ] . querySelectorAll ( "td" ) ;
130- setTabIndex ( element , - 1 ) ;
136+ Table . setTabIndex ( element , - 1 ) ;
131137 if ( event . ctrlKey ) {
132138 this . columnIndex = lastRow . length - 1 ;
133- setTabIndex ( lastRow [ this . columnIndex ] , 0 ) ;
139+ Table . setTabIndex ( lastRow [ this . columnIndex ] , 0 ) ;
134140 this . focus ( lastRow [ this . columnIndex ] ) ;
135141 } else {
136142 const currentRow = rows [ rowIndex ] . querySelectorAll ( "th, td" ) ;
137143 this . columnIndex = currentRow . length - 1 ;
138- setTabIndex ( currentRow [ this . columnIndex ] , 0 ) ;
144+ Table . setTabIndex ( currentRow [ this . columnIndex ] , 0 ) ;
139145 this . focus ( currentRow [ this . columnIndex ] ) ;
140146 }
141147 break ;
@@ -148,8 +154,8 @@ export class DataGridFocus {
148154 return ;
149155 }
150156 const focusElementList = getFocusElementList ( this . elementRef . nativeElement . closest ( "table" ) , tabbableSelectorIgnoreTabIndex ) ;
151- focusElementList . forEach ( element => setTabIndex ( element , - 1 ) ) ;
152- setTabIndex ( this . elementRef . nativeElement , 0 ) ;
157+ focusElementList . forEach ( element => Table . setTabIndex ( element , - 1 ) ) ;
158+ Table . setTabIndex ( this . elementRef . nativeElement , 0 ) ;
153159 this . focus ( this . elementRef . nativeElement ) ;
154160 }
155161}
0 commit comments