@@ -24,6 +24,16 @@ const getFirstVisibleCell = (target, currentlyFocusedCell, noData) => {
24
24
}
25
25
} ;
26
26
27
+ function recursiveSubComponentElementSearch ( element ) {
28
+ if ( ! element . parentElement ) {
29
+ return null ;
30
+ }
31
+ if ( element ?. parentElement . dataset . subcomponent ) {
32
+ return element . parentElement ;
33
+ }
34
+ return recursiveSubComponentElementSearch ( element . parentElement ) ;
35
+ }
36
+
27
37
const findParentCell = ( target ) => {
28
38
if ( target === undefined || target === null ) return ;
29
39
if (
@@ -45,6 +55,10 @@ const setFocus = (currentlyFocusedCell, nextElement) => {
45
55
}
46
56
} ;
47
57
58
+ const navigateFromActiveSubCompItem = ( currentlyFocusedCell , e ) => {
59
+ setFocus ( currentlyFocusedCell , recursiveSubComponentElementSearch ( e . target ) ) ;
60
+ } ;
61
+
48
62
const useGetTableProps = ( tableProps , { instance : { webComponentsReactProperties, data, columns } } ) => {
49
63
const { showOverlay, tableRef } = webComponentsReactProperties ;
50
64
const currentlyFocusedCell = useRef < HTMLDivElement > ( null ) ;
@@ -80,7 +94,13 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
80
94
81
95
const onTableFocus = useCallback (
82
96
( e ) => {
83
- if ( e . target . dataset ?. emptyRowCell === 'true' ) {
97
+ if ( e . target . dataset ?. emptyRowCell === 'true' || e . target . dataset . subcomponentActiveElement ) {
98
+ return ;
99
+ }
100
+ if ( e . target . dataset . subcomponent ) {
101
+ e . target . tabIndex = 0 ;
102
+ e . target . focus ( ) ;
103
+ currentlyFocusedCell . current = e . target ;
84
104
return ;
85
105
}
86
106
const isFirstCellAvailable = e . target . querySelector ( 'div[data-column-index="0"][data-row-index="1"]' ) ;
@@ -121,6 +141,7 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
121
141
122
142
const onKeyboardNavigation = useCallback (
123
143
( e ) => {
144
+ const isActiveItemInSubComponent = e . target . dataset . subcomponentActiveElement ;
124
145
// check if target is cell and if so proceed from there
125
146
if (
126
147
! currentlyFocusedCell . current &&
@@ -129,14 +150,18 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
129
150
currentlyFocusedCell . current = e . target ;
130
151
}
131
152
if ( currentlyFocusedCell . current ) {
132
- const columnIndex = parseInt ( currentlyFocusedCell . current . dataset . columnIndex , 10 ) ;
133
- const rowIndex = parseInt ( currentlyFocusedCell . current . dataset . rowIndex , 10 ) ;
153
+ const columnIndex = parseInt ( currentlyFocusedCell . current . dataset . columnIndex ?? '0' , 10 ) ;
154
+ const rowIndex = parseInt (
155
+ currentlyFocusedCell . current . dataset . rowIndex ?? currentlyFocusedCell . current . dataset . subcomponentRowIndex ,
156
+ 10
157
+ ) ;
134
158
switch ( e . key ) {
135
159
case 'End' : {
136
160
e . preventDefault ( ) ;
137
161
const visibleColumns : HTMLDivElement [ ] = tableRef . current . querySelector (
138
162
`div[data-component-name="AnalyticalTableHeaderRow"]`
139
163
) . children ;
164
+
140
165
const lastVisibleColumn = Array . from ( visibleColumns )
141
166
. slice ( 0 )
142
167
. reduceRight ( ( _ , cur , index , arr ) => {
@@ -149,7 +174,7 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
149
174
} , 0 ) ;
150
175
151
176
const newElement = tableRef . current . querySelector (
152
- `div[data-visible-column-index="${ lastVisibleColumn + 1 } "][data-row-index="${ rowIndex } "]`
177
+ `div[data-visible-column-index="${ lastVisibleColumn } "][data-row-index="${ rowIndex } "]`
153
178
) ;
154
179
setFocus ( currentlyFocusedCell , newElement ) ;
155
180
break ;
@@ -196,6 +221,10 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
196
221
}
197
222
case 'ArrowRight' : {
198
223
e . preventDefault ( ) ;
224
+ if ( isActiveItemInSubComponent ) {
225
+ navigateFromActiveSubCompItem ( currentlyFocusedCell , e ) ;
226
+ return ;
227
+ }
199
228
const newElement = tableRef . current . querySelector (
200
229
`div[data-column-index="${ columnIndex + 1 } "][data-row-index="${ rowIndex } "]`
201
230
) ;
@@ -208,6 +237,10 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
208
237
}
209
238
case 'ArrowLeft' : {
210
239
e . preventDefault ( ) ;
240
+ if ( isActiveItemInSubComponent ) {
241
+ navigateFromActiveSubCompItem ( currentlyFocusedCell , e ) ;
242
+ return ;
243
+ }
211
244
const newElement = tableRef . current . querySelector (
212
245
`div[data-column-index="${ columnIndex - 1 } "][data-row-index="${ rowIndex } "]`
213
246
) ;
@@ -220,6 +253,10 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
220
253
}
221
254
case 'ArrowDown' : {
222
255
e . preventDefault ( ) ;
256
+ if ( isActiveItemInSubComponent ) {
257
+ navigateFromActiveSubCompItem ( currentlyFocusedCell , e ) ;
258
+ return ;
259
+ }
223
260
const parent = currentlyFocusedCell . current . parentElement as HTMLDivElement ;
224
261
const firstChildOfParent = parent ?. children ?. [ 0 ] as HTMLDivElement ;
225
262
const hasSubcomponent = firstChildOfParent ?. dataset ?. subcomponent ;
@@ -235,35 +272,27 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
235
272
currentlyFocusedCell . current = firstChildOfParent ;
236
273
} else if ( newElement ) {
237
274
setFocus ( currentlyFocusedCell , newElement ) ;
238
- } else if ( e . target . dataset . subcomponent ) {
239
- const nextElementToSubComp = tableRef . current . querySelector (
240
- `div[data-column-index="${ parseInt ( e . target . dataset . columnIndexSub ) } "][data-row-index="${
241
- parseInt ( e . target . dataset . rowIndexSub ) + 1
242
- } "]`
243
- ) ;
244
- setFocus ( currentlyFocusedCell , nextElementToSubComp ) ;
245
275
}
246
276
break ;
247
277
}
248
278
case 'ArrowUp' : {
249
279
e . preventDefault ( ) ;
280
+ if ( isActiveItemInSubComponent ) {
281
+ navigateFromActiveSubCompItem ( currentlyFocusedCell , e ) ;
282
+ return ;
283
+ }
284
+ let prevRowIndex = rowIndex - 1 ;
285
+ const isSubComponent = e . target . dataset . subcomponent ;
286
+ if ( isSubComponent ) {
287
+ prevRowIndex ++ ;
288
+ }
250
289
const previousRowCell = tableRef . current . querySelector (
251
- `div[data-column-index="${ columnIndex } "][data-row-index="${ rowIndex - 1 } "]`
290
+ `div[data-column-index="${ columnIndex } "][data-row-index="${ prevRowIndex } "]`
252
291
) ;
253
292
const firstChildPrevRow = previousRowCell ?. parentElement . children [ 0 ] as HTMLDivElement ;
254
293
const hasSubcomponent = firstChildPrevRow ?. dataset ?. subcomponent ;
255
294
256
- if ( currentlyFocusedCell . current ?. dataset ?. subcomponent ) {
257
- currentlyFocusedCell . current . tabIndex = - 1 ;
258
- const newElement = tableRef . current . querySelector (
259
- `div[data-column-index="${ parseInt ( e . target . dataset . columnIndexSub ) } "][data-row-index="${ parseInt (
260
- e . target . dataset . rowIndexSub
261
- ) } "]`
262
- ) ;
263
- newElement . tabIndex = 0 ;
264
- newElement . focus ( ) ;
265
- currentlyFocusedCell . current = newElement ;
266
- } else if ( hasSubcomponent ) {
295
+ if ( hasSubcomponent && ! isSubComponent ) {
267
296
currentlyFocusedCell . current . tabIndex = - 1 ;
268
297
firstChildPrevRow . dataset . rowIndexSub = `${ rowIndex - 1 } ` ;
269
298
firstChildPrevRow . dataset . columnIndexSub = `${ columnIndex } ` ;
0 commit comments