@@ -158,42 +158,28 @@ export default {
158158 } ,
159159
160160 handleTypeahead ( e ) {
161- if ( ! this . isMenuVisible ( ) ) return
161+ if ( ! this . isMenuVisible ( ) || e . key . length !== 1 || ! / [ a - z A - Z 0 - 9 ] / . test ( e . key ) ) return
162162
163- // Check if it's a printable character (A-Z, a-z, 0-9)
164- if ( e . key . length === 1 && / [ a - z A - Z 0 - 9 ] / . test ( e . key ) ) {
165- e . preventDefault ( )
163+ e . preventDefault ( )
166164
167- const searchChar = e . key . toLowerCase ( )
168- const items = this . getEnabledMenuItems ( )
165+ const searchChar = e . key . toLowerCase ( )
166+ const items = this . getEnabledMenuItems ( )
167+ const matchingItems = Array . from ( items ) . filter ( item =>
168+ item . textContent . trim ( ) . toLowerCase ( ) . startsWith ( searchChar )
169+ )
169170
170- // Find all items that start with the typed character
171- const matchingItems = [ ]
172- for ( let item of items ) {
173- const itemText = item . textContent . trim ( ) . toLowerCase ( )
174- if ( itemText . startsWith ( searchChar ) ) {
175- matchingItems . push ( item )
176- }
177- }
171+ if ( matchingItems . length === 0 ) return
178172
179- if ( matchingItems . length === 0 ) return
180-
181- // Check if currently focused item matches the search character
182- const currentFocused = this . el . querySelector ( SELECTORS . FOCUSED_MENUITEM )
183- const shouldCycle = currentFocused &&
184- currentFocused . textContent . trim ( ) . toLowerCase ( ) . startsWith ( searchChar ) &&
185- matchingItems . includes ( currentFocused )
186-
187- if ( shouldCycle ) {
188- // Find the current item's index in matching items and cycle to next
189- const currentIndex = matchingItems . indexOf ( currentFocused )
190- const nextIndex = ( currentIndex + 1 ) % matchingItems . length
191- this . setFocus ( matchingItems [ nextIndex ] )
192- } else {
193- // Focus the first matching item
194- this . setFocus ( matchingItems [ 0 ] )
195- }
196- }
173+ const currentFocused = this . el . querySelector ( SELECTORS . FOCUSED_MENUITEM )
174+ const currentIndex = currentFocused && matchingItems . includes ( currentFocused )
175+ ? matchingItems . indexOf ( currentFocused )
176+ : - 1
177+
178+ const nextIndex = currentIndex >= 0 && currentIndex < matchingItems . length - 1
179+ ? currentIndex + 1
180+ : 0
181+
182+ this . setFocus ( matchingItems [ nextIndex ] )
197183 } ,
198184
199185 handleClose ( ) {
0 commit comments