File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed
react-aria-components/test Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -65,8 +65,13 @@ export function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T
65
65
66
66
// Reset focused key if that item is deleted from the collection.
67
67
useEffect ( ( ) => {
68
- if ( selectionState . focusedKey != null && ! tree . getItem ( selectionState . focusedKey ) ) {
69
- selectionState . setFocusedKey ( null ) ;
68
+ if ( selectionState . focusedKey != null ) {
69
+ let focusedItem = tree . getItem ( selectionState . focusedKey ) ;
70
+ // TODO: do we want to have the same logic as useListState/useGridState where it tries to find the nearest row?
71
+ // We could possibly special case this loader case and have it try to find the item just before it/the parent
72
+ if ( ! focusedItem || focusedItem . type === 'loader' && ! focusedItem . props . isLoading ) {
73
+ selectionState . setFocusedKey ( null ) ;
74
+ }
70
75
}
71
76
// eslint-disable-next-line react-hooks/exhaustive-deps
72
77
} , [ tree , selectionState . focusedKey ] ) ;
Original file line number Diff line number Diff line change @@ -1592,6 +1592,27 @@ describe('Tree', () => {
1592
1592
expect ( documentLoader1ParentStyles . top ) . toBe ( '900px' ) ;
1593
1593
expect ( documentLoader1ParentStyles . height ) . toBe ( '30px' ) ;
1594
1594
} ) ;
1595
+
1596
+ it ( 'should restore focus to the tree if the loader is keyboard focused when loading finishes' , async ( ) => {
1597
+ let tree = render (
1598
+ < VirtualizedLoadingSentinelTree rootIsLoading />
1599
+ ) ;
1600
+ let treeTester = testUtilUser . createTester ( 'Tree' , { root : tree . getByRole ( 'treegrid' ) } ) ;
1601
+ let rows = treeTester . rows ;
1602
+ expect ( rows ) . toHaveLength ( 8 ) ;
1603
+ let rootLoaderRow = rows [ 7 ] ;
1604
+ expect ( rootLoaderRow ) . toHaveTextContent ( 'Loading...' ) ;
1605
+
1606
+ await user . tab ( ) ;
1607
+ await user . keyboard ( '{End}' ) ;
1608
+ expect ( document . activeElement ) . toBe ( rootLoaderRow ) ;
1609
+
1610
+ tree . rerender (
1611
+ < VirtualizedLoadingSentinelTree />
1612
+ ) ;
1613
+
1614
+ expect ( document . activeElement ) . toBe ( treeTester . tree ) ;
1615
+ } ) ;
1595
1616
} ) ;
1596
1617
} ) ;
1597
1618
You can’t perform that action at this time.
0 commit comments