@@ -41,6 +41,9 @@ const ItemLink = styled(Link)`
41
41
display: block;
42
42
padding: ${ ( props ) => props . theme . spacing . xs } ;
43
43
text-decoration: none;
44
+ &:focus {
45
+ background-color: ${ ( props ) => props . theme . colors . input . hover } ;
46
+ }
44
47
45
48
width: 100%;
46
49
` ;
@@ -93,21 +96,39 @@ export const Search = ({ descriptors }: SearchParams) => {
93
96
}
94
97
} , [ searchTerm ] ) ;
95
98
99
+ function keyPressHandler ( event : React . KeyboardEvent , index : number ) {
100
+ if ( event . key === "ArrowDown" && index + 1 < ( results ?. length ?? 0 ) ) {
101
+ document . getElementById ( `SearchResultItem_Link_${ index + 1 } ` ) ?. focus ( ) ;
102
+ event . preventDefault ( ) ;
103
+ } else if ( event . key === "ArrowUp" && index >= 0 ) {
104
+ const target = document . getElementById ( index === 0 ? "Search_Input" : `SearchResultItem_Link_${ index - 1 } ` ) ;
105
+ target ?. focus ( ) ;
106
+ event . preventDefault ( ) ;
107
+ }
108
+ }
109
+
96
110
return (
97
111
< SearchContainer >
98
112
< input
113
+ id = "Search_Input"
99
114
onChange = { ( event ) => {
100
115
setSearchTerm ( event . target . value ) ;
101
116
} }
102
117
value = { searchTerm }
118
+ onKeyDown = { ( event ) => keyPressHandler ( event , - 1 ) }
103
119
/>
104
120
{ searchTerm && results && (
105
121
< SearchResultContainer >
106
122
{ results . total === 0 && "No results found" }
107
123
{ results . total > 0 &&
108
124
results . map ( ( result : Result , ind : number ) => (
109
125
< SearchResultItem key = { `${ result . obj . name } _${ ind } ` } >
110
- < ItemLink to = { `${ result . obj . url } ` } onClick = { ( event ) => setSearchTerm ( "" ) } >
126
+ < ItemLink
127
+ id = { `SearchResultItem_Link_${ ind } ` }
128
+ to = { `${ result . obj . url } ` }
129
+ onClick = { ( ) => setSearchTerm ( "" ) }
130
+ onKeyDown = { ( event ) => keyPressHandler ( event , ind ) }
131
+ >
111
132
< Match result = { result } />
112
133
</ ItemLink >
113
134
</ SearchResultItem >
0 commit comments