@@ -124,6 +124,7 @@ interface KeyHandlerOptions<TData> {
124
124
multiSelect ?: boolean ;
125
125
reorder ?: { updateOrder ?: ( moveIndexes : number [ ] , toIndex : number ) => void ; getRowId ?: ( row : TData ) => string } ;
126
126
lazyLoadChildren ?: ( row : Row < TData > ) => void ;
127
+ resetSelectionOnTab ?: boolean ;
127
128
}
128
129
129
130
interface TableKeyboardHandlerProps < TData > {
@@ -141,16 +142,17 @@ export const useTableKeyHandler = <TData,>({ table, data, options }: TableKeyboa
141
142
const actions : Record < string , ( ) => void > = {
142
143
ArrowUp : ( ) => handleArrowKeyUpDown ( event , - 1 ) ,
143
144
ArrowDown : ( ) => handleArrowKeyUpDown ( event , 1 ) ,
144
- ArrowLeft : ( ) => toggleExpand ( table . getSelectedRowModel ( ) . flatRows [ 0 ] , true , options ?. lazyLoadChildren ) ,
145
- ArrowRight : ( ) => toggleExpand ( table . getSelectedRowModel ( ) . flatRows [ 0 ] , false , options ?. lazyLoadChildren ) ,
146
- Tab : ( ) => table . resetRowSelection ( ) ,
145
+ ArrowLeft : ( ) => toggleExpand ( false , table . getSelectedRowModel ( ) . flatRows [ 0 ] , options ?. lazyLoadChildren ) ,
146
+ ArrowRight : ( ) => toggleExpand ( true , table . getSelectedRowModel ( ) . flatRows [ 0 ] , options ?. lazyLoadChildren ) ,
147
+ Tab : ( ) => options ?. resetSelectionOnTab && table . resetRowSelection ( ) ,
147
148
Enter : ( ) => onEnterAction ?.( table . getSelectedRowModel ( ) . flatRows [ 0 ] )
148
149
} ;
149
150
150
151
actions [ event . key ] ?.( ) ;
151
152
} ;
152
153
153
154
const handleArrowKeyUpDown = ( event : React . KeyboardEvent < HTMLTableElement > , direction : - 1 | 1 ) => {
155
+ event . preventDefault ( ) ;
154
156
const { multiSelect = false , reorder } = options || { } ;
155
157
const allRows = table . getRowModel ( ) . rows ;
156
158
const selectedRows = table . getSelectedRowModel ( ) . flatRows ;
@@ -175,6 +177,7 @@ export const useTableKeyHandler = <TData,>({ table, data, options }: TableKeyboa
175
177
table . resetRowSelection ( ) ;
176
178
allRows [ newReorderIndex ] . toggleSelected ( ) ;
177
179
setRootIndex ( newReorderIndex ) ;
180
+ event . currentTarget . rows [ newReorderIndex ] . scrollIntoView ( { block : 'center' } ) ;
178
181
}
179
182
} ;
180
183
@@ -208,7 +211,10 @@ export const useTableKeyHandler = <TData,>({ table, data, options }: TableKeyboa
208
211
return { handleKeyDown } ;
209
212
} ;
210
213
211
- const toggleExpand = < TData , > ( row : Row < TData > , expand : boolean , loadChildren ?: ( row : Row < TData > ) => void ) => {
214
+ const toggleExpand = < TData , > ( expand : boolean , row ?: Row < TData > , loadChildren ?: ( row : Row < TData > ) => void ) => {
215
+ if ( row === undefined || ! row . getCanExpand ( ) ) {
216
+ return ;
217
+ }
212
218
if ( expand && ! row . getIsExpanded ( ) ) {
213
219
loadChildren ?.( row ) ;
214
220
row . toggleExpanded ( ) ;
0 commit comments