22 Directive ,
33 Input ,
44 ElementRef ,
5- HostListener
5+ HostListener ,
6+ Output ,
7+ EventEmitter
68} from "@angular/core" ;
79import { Table } from "./table.component" ;
810import { getFocusElementList , tabbableSelectorIgnoreTabIndex } from "../common/tab.service" ;
@@ -11,21 +13,32 @@ import { setTabIndex } from "../utils/a11y";
1113@Directive ( {
1214 selector : "[ibmDataGridFocus]"
1315} )
14-
1516export class DataGridFocus {
1617 @Input ( ) ibmDataGridFocus : boolean ;
17- @Input ( ) columnIndex : number ;
18+ @Input ( ) set columnIndex ( value : number ) {
19+ const shouldEmit = value !== this . _columnIndex ;
20+ this . _columnIndex = value ;
21+ if ( shouldEmit ) {
22+ this . columnIndexChange . emit ( value ) ;
23+ }
24+ }
25+ get columnIndex ( ) : number {
26+ return this . _columnIndex ;
27+ }
28+ @Output ( ) columnIndexChange : EventEmitter < number > = new EventEmitter ( ) ;
1829
19- constructor ( private elementRef : ElementRef ) { }
30+ protected _columnIndex : number ;
2031
21- focus ( focusElement ) {
22- const focusElementList = getFocusElementList ( focusElement , tabbableSelectorIgnoreTabIndex ) ;
23- if ( focusElement . firstElementChild && focusElement . firstElementChild . classList . contains ( "bx--table-sort-v2" ) ) {
32+ constructor ( protected elementRef : ElementRef ) { }
33+
34+ focus ( element ) {
35+ const focusElementList = getFocusElementList ( element , tabbableSelectorIgnoreTabIndex ) ;
36+ if ( element . firstElementChild && element . firstElementChild . classList . contains ( "bx--table-sort-v2" ) ) {
2437 focusElementList [ 1 ] . focus ( ) ;
2538 } else if ( focusElementList . length > 0 ) {
2639 focusElementList [ 0 ] . focus ( ) ;
2740 } else {
28- focusElement . focus ( ) ;
41+ element . focus ( ) ;
2942 }
3043 }
3144
@@ -36,12 +49,16 @@ export class DataGridFocus {
3649 }
3750 const element = this . elementRef . nativeElement ;
3851 const rows = element . closest ( "table" ) . rows ;
39- let rowIndex = Array . from ( rows ) . findIndex ( item => item === element . closest ( "tr" ) ) ;
52+ const closestTr = element . closest ( "tr" ) ;
53+ let rowIndex = Array . from ( rows ) . indexOf ( closestTr ) ;
54+
55+ const headerRow = rows [ 0 ] . querySelectorAll ( "th" ) ;
4056
4157 switch ( event . key ) {
4258 case "Right" : // IE specific value
4359 case "ArrowRight" :
44- if ( element . nextElementSibling && element . nextElementSibling . style . width !== "0px" ) {
60+ if ( element . nextElementSibling && Array . from ( headerRow ) . indexOf ( element . nextElementSibling ) < headerRow . length - 1 ) {
61+ this . columnIndex ++ ;
4562 const nextSibling = element . nextElementSibling ;
4663 setTabIndex ( element , - 1 ) ;
4764 setTabIndex ( nextSibling , 0 ) ;
@@ -51,6 +68,7 @@ export class DataGridFocus {
5168 case "Left" : // IE specific value
5269 case "ArrowLeft" :
5370 if ( element . previousElementSibling ) {
71+ this . columnIndex -- ;
5472 const previousSibling = element . previousElementSibling ;
5573 setTabIndex ( element , - 1 ) ;
5674 setTabIndex ( previousSibling , 0 ) ;
@@ -63,7 +81,7 @@ export class DataGridFocus {
6381 rowIndex ++ ;
6482 const row = rows [ rowIndex ] . querySelectorAll ( "td" ) ;
6583 setTabIndex ( element , - 1 ) ;
66- if ( rows [ rowIndex ] . className === "bx--expandable-row-v2" ) {
84+ if ( rows [ rowIndex ] . classList . item ( 0 ) === "bx--expandable-row-v2" ) {
6785 setTabIndex ( row [ 0 ] , 0 ) ;
6886 this . focus ( row [ 0 ] ) ;
6987 } else {
@@ -74,27 +92,25 @@ export class DataGridFocus {
7492 break ;
7593 case "Up" : // IE specific value
7694 case "ArrowUp" :
77- const headerRow = rows [ 0 ] . querySelectorAll ( "th" ) ;
95+ if ( ( rowIndex !== 1 && Array . from ( headerRow ) . some ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length < 0 ) ) ||
96+ rowIndex === 0 ) {
97+ return ;
98+ }
7899 setTabIndex ( element , - 1 ) ;
79- if ( rowIndex === 1 && Array . from ( headerRow ) . some ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length > 0 ) ) {
80- setTabIndex ( headerRow [ this . columnIndex ] , 0 ) ;
81- this . focus ( headerRow [ this . columnIndex ] ) ;
82- } else if ( rowIndex > 1 ) {
83- rowIndex -- ;
84- const row = rows [ rowIndex ] . querySelectorAll ( "td" ) ;
85- if ( rows [ rowIndex ] . className === "bx--expandable-row-v2" ) {
86- setTabIndex ( row [ 0 ] , 0 ) ;
87- this . focus ( row [ 0 ] ) ;
88- } else {
89- setTabIndex ( row [ this . columnIndex ] , 0 ) ;
90- this . focus ( row [ this . columnIndex ] ) ;
91- }
100+ rowIndex -- ;
101+ const row = rows [ rowIndex ] . querySelectorAll ( "td, th" ) ;
102+ if ( rows [ rowIndex ] . classList . item ( 0 ) === "bx--expandable-row-v2" ) {
103+ setTabIndex ( row [ 0 ] , 0 ) ;
104+ this . focus ( row [ 0 ] ) ;
105+ } else {
106+ setTabIndex ( row [ this . columnIndex ] , 0 ) ;
107+ this . focus ( row [ this . columnIndex ] ) ;
92108 }
93109 break ;
94110 case "Home" :
111+ this . columnIndex = 0 ;
95112 setTabIndex ( element , - 1 ) ;
96113 if ( event . ctrlKey ) {
97- const headerRow = rows [ 0 ] . querySelectorAll ( "th" ) ;
98114 if ( Array . from ( headerRow ) . some ( th => getFocusElementList ( th , tabbableSelectorIgnoreTabIndex ) . length > 0 ) ) {
99115 setTabIndex ( headerRow [ 0 ] , 0 ) ;
100116 this . focus ( headerRow [ 0 ] ) ;
@@ -103,7 +119,7 @@ export class DataGridFocus {
103119 setTabIndex ( firstBodyCell , 0 ) ;
104120 this . focus ( firstBodyCell ) ;
105121 }
106- } else if ( element . parentElement . className !== "bx--expandable-row-v2" ) {
122+ } else {
107123 const firstRowCell = rows [ rowIndex ] . querySelectorAll ( "th, td" ) [ 0 ] ;
108124 setTabIndex ( firstRowCell , 0 ) ;
109125 this . focus ( firstRowCell ) ;
@@ -116,11 +132,11 @@ export class DataGridFocus {
116132 this . columnIndex = lastRow . length - 1 ;
117133 setTabIndex ( lastRow [ this . columnIndex ] , 0 ) ;
118134 this . focus ( lastRow [ this . columnIndex ] ) ;
119- } else if ( element . parentElement . className !== "bx--expandable-row-v2" ) {
120- this . columnIndex = lastRow . length - 1 ;
121- const lastRowCell = rows [ rowIndex ] . querySelectorAll ( "th, td" ) [ this . columnIndex ] ;
122- setTabIndex ( lastRowCell , 0 ) ;
123- this . focus ( lastRowCell ) ;
135+ } else {
136+ const currentRow = rows [ rowIndex ] . querySelectorAll ( "th, td" ) ;
137+ this . columnIndex = currentRow . length - 1 ;
138+ setTabIndex ( currentRow [ this . columnIndex ] , 0 ) ;
139+ this . focus ( currentRow [ this . columnIndex ] ) ;
124140 }
125141 break ;
126142 }
0 commit comments