@@ -18,19 +18,22 @@ const Main = ({
18
18
story,
19
19
updateStory,
20
20
hotkeys,
21
+ search,
22
+ setSearch,
21
23
} : {
22
24
stories : string [ ] ;
23
25
story : string ;
24
26
updateStory : UpdateStory ;
25
27
hotkeys : boolean ;
28
+ search : string ;
29
+ setSearch : React . Dispatch < React . SetStateAction < string > > ;
26
30
} ) => {
27
- const [ search , setSearch ] = React . useState ( "" ) ;
28
31
const [ width , setWidth ] = React . useState (
29
32
getSettings ( ) . sidebarWidth || DEFAULT_WIDTH ,
30
33
) ;
31
34
const [ resizeActive , setResizeActive ] = React . useState ( false ) ;
32
35
const handleRef = React . useRef < HTMLDivElement > ( null ) ;
33
- const searchEl = React . useRef ( null ) ;
36
+ const searchEl = React . useRef < HTMLInputElement > ( null ) ;
34
37
const treeRoot = React . useRef < HTMLUListElement | null > ( null ) ;
35
38
36
39
React . useEffect ( ( ) => {
@@ -79,11 +82,10 @@ const Main = ({
79
82
} ;
80
83
} , [ resizeActive , setResizeActive , setWidth , handleRef . current ] ) ;
81
84
82
- useHotkeys (
83
- config . hotkeys . search ,
84
- ( ) => ( searchEl . current as any as HTMLInputElement ) . focus ( ) ,
85
- { preventDefault : true , enabled : hotkeys } ,
86
- ) ;
85
+ useHotkeys ( config . hotkeys . search , ( ) => searchEl . current ?. focus ( ) , {
86
+ preventDefault : true ,
87
+ enabled : hotkeys ,
88
+ } ) ;
87
89
const canonicalSearch = search
88
90
. toLocaleLowerCase ( )
89
91
. replace ( new RegExp ( "\\s+" , "g" ) , "-" ) ;
@@ -92,6 +94,14 @@ const Main = ({
92
94
story . includes ( canonicalSearch ) ,
93
95
) ;
94
96
97
+ const onKeyDown : React . KeyboardEventHandler < HTMLInputElement > = ( e ) => {
98
+ if ( e . key === "ArrowDown" && treeRoot . current ?. firstChild ) {
99
+ const firstLiElement = treeRoot . current . firstChild as HTMLLIElement ;
100
+
101
+ firstLiElement . focus ( ) ;
102
+ }
103
+ } ;
104
+
95
105
return (
96
106
< >
97
107
< div
@@ -125,11 +135,7 @@ const Main = ({
125
135
aria-label = "Search stories"
126
136
value = { search }
127
137
ref = { searchEl }
128
- onKeyDown = { ( e ) => {
129
- if ( e . key === "ArrowDown" ) {
130
- ( treeRoot as any ) . current . firstChild . focus ( ) ;
131
- }
132
- } }
138
+ onKeyDown = { onKeyDown }
133
139
onChange = { ( e : React . ChangeEvent < HTMLInputElement > ) =>
134
140
setSearch ( e . target . value )
135
141
}
0 commit comments