@@ -10,10 +10,11 @@ class KeywordsModel {
10
10
this . index = index ;
11
11
this . keys = Object . keys ( index ) ;
12
12
}
13
- getMatches ( str ) {
14
- if ( ! str ) {
13
+ getMatches ( searchString ) {
14
+ if ( ! searchString ) {
15
15
return [ ] ;
16
16
}
17
+ let str = searchString . toLowerCase ( ) ;
17
18
let useCache = false ;
18
19
if ( this . prevKey_ ) {
19
20
const occurredAt = str . indexOf ( this . prevKey_ ) ;
@@ -160,34 +161,49 @@ class CRSearchResults extends HTMLElement {
160
161
this . navigate ( event . currentTarget ) ;
161
162
}
162
163
164
+ get results ( ) {
165
+ return this . shadowRoot . querySelectorAll ( 'a' ) ;
166
+ }
167
+
163
168
get selectedResult ( ) {
164
- return this . shadowRoot . querySelectorAll ( 'a' ) [ this . _selected ] ;
169
+ return this . results [ this . _selected ] ;
170
+ }
171
+
172
+ focusSelectedResult ( ) {
173
+ if ( this . selectedResult ) {
174
+ this . selectedResult . classList . add ( 'selected' ) ;
175
+ this . selectedResult . scrollIntoView ( { block : 'center' } ) ;
176
+ }
165
177
}
166
178
167
179
focusDown ( ) {
168
180
if ( this . _selected === undefined ) {
169
181
this . _selected = 0 ;
170
182
} else {
171
- this . selectedResult . classList . remove ( 'selected' ) ;
183
+ if ( this . selectedResult ) {
184
+ this . selectedResult . classList . remove ( 'selected' ) ;
185
+ }
172
186
this . _selected = Math . min ( this . _selected + 1 , this . matches . length - 1 ) ;
173
187
}
174
188
175
- this . selectedResult . classList . add ( 'selected' ) ;
189
+ this . focusSelectedResult ( ) ;
176
190
}
177
191
178
192
focusUp ( ) {
179
193
if ( this . _selected === undefined ) {
180
194
return ;
181
195
}
182
196
183
- this . selectedResult . classList . remove ( 'selected' ) ;
197
+ if ( this . selectedResult ) {
198
+ this . selectedResult . classList . remove ( 'selected' ) ;
199
+ }
184
200
this . _selected = Math . max ( this . _selected - 1 , 0 ) ;
185
201
186
- this . selectedResult . classList . add ( 'selected' ) ;
202
+ this . focusSelectedResult ( ) ;
187
203
}
188
204
189
205
select ( ) {
190
- if ( this . _selected === undefined ) {
206
+ if ( this . _selected === undefined || this . selectedResult === undefined ) {
191
207
return ;
192
208
}
193
209
@@ -288,6 +304,11 @@ customElements.define('cr-search-control', class extends HTMLElement {
288
304
return ;
289
305
}
290
306
307
+ if ( event . code === 'Escape' ) {
308
+ this . inputElement . value = '' ;
309
+ this . inputElement . blur ( ) ;
310
+ }
311
+
291
312
const textValue = this . inputElement . value ;
292
313
293
314
if ( textValue === '' ) {
@@ -304,8 +325,8 @@ customElements.define('cr-search-control', class extends HTMLElement {
304
325
} ) ;
305
326
306
327
document . addEventListener ( 'keydown' , ( event ) => {
307
- // One of `a-zA -Z`
308
- if ( event . keyCode >= 65 && event . keyCode <= 122 ) {
328
+ // One of `a-z` or `A -Z`
329
+ if ( event . keyCode >= 65 && event . keyCode <= 90 ) {
309
330
document . querySelector ( 'cr-search-control' ) . inputElement . focus ( ) ;
310
331
}
311
332
} )
0 commit comments