@@ -244,7 +244,13 @@ const MenuList = React.forwardRef(function MenuList(inProps, ref) {
244244 [ focusInitialTarget ] ,
245245 ) ;
246246
247- const rovingContainerProps = getContainerProps ( undefined , other . onFocus ) ;
247+ const rovingContainerProps = getContainerProps ( undefined , ( event ) => {
248+ // Reopening a kept-mounted menu starts a fresh query. Moving focus between items continues it.
249+ if ( ! contains ( event . currentTarget , event . relatedTarget ) ) {
250+ textCriteriaRef . current . keys = [ ] ;
251+ }
252+ other . onFocus ?. ( event ) ;
253+ } ) ;
248254 const handleRef = useForkRef ( listRef , rovingContainerProps . ref , ref ) ;
249255 const menuListContextValue = React . useMemo (
250256 ( ) => ( {
@@ -268,22 +274,27 @@ const MenuList = React.forwardRef(function MenuList(inProps, ref) {
268274 return ;
269275 }
270276
271- rovingContainerProps . onKeyDown ( event ) ;
277+ const criteria = textCriteriaRef . current ;
278+ if ( ! event . defaultPrevented ) {
279+ rovingContainerProps . onKeyDown ( event ) ;
280+ // Handling ArrowUp, ArrowDown, Home, or End ends the current typeahead query.
281+ if ( event . defaultPrevented ) {
282+ criteria . keys = [ ] ;
283+ }
284+ }
272285
273- if ( event . key . length === 1 ) {
274- const criteria = textCriteriaRef . current ;
286+ const currTime = performance . now ( ) ;
287+ const hasActiveQuery = criteria . keys . length > 0 && currTime - criteria . lastTime <= 500 ;
288+ // Leave a standalone Space to the list's keydown handler for option activation.
289+ if ( event . key . length === 1 && ( event . key !== ' ' || hasActiveQuery ) ) {
275290 const lowerKey = event . key . toLowerCase ( ) ;
276- const currTime = performance . now ( ) ;
277-
278- if ( criteria . keys . length > 0 ) {
279- // Reset
280- if ( currTime - criteria . lastTime > 500 ) {
281- criteria . keys = [ ] ;
282- criteria . repeating = true ;
283- criteria . previousKeyMatched = true ;
284- } else if ( criteria . repeating && lowerKey !== criteria . keys [ 0 ] ) {
285- criteria . repeating = false ;
286- }
291+
292+ if ( ! hasActiveQuery ) {
293+ criteria . keys = [ ] ;
294+ criteria . repeating = true ;
295+ criteria . previousKeyMatched = true ;
296+ } else if ( criteria . repeating && lowerKey !== criteria . keys [ 0 ] ) {
297+ criteria . repeating = false ;
287298 }
288299
289300 criteria . lastTime = currTime ;
@@ -293,14 +304,14 @@ const MenuList = React.forwardRef(function MenuList(inProps, ref) {
293304 const keepFocusOnCurrent =
294305 currentFocus && ! criteria . repeating && textCriteriaMatches ( currentFocus , criteria ) ;
295306
296- if (
307+ criteria . previousKeyMatched =
297308 criteria . previousKeyMatched &&
298309 ( keepFocusOnCurrent ||
299- focusNext ( ( item ) => isItemFocusableWithTextCriteria ( item , criteria ) ) != null )
300- ) {
310+ focusNext ( ( item ) => isItemFocusableWithTextCriteria ( item , criteria ) ) != null ) ;
311+
312+ // A space in the query belongs to typeahead even when the prefix has no match.
313+ if ( criteria . previousKeyMatched || event . key === ' ' ) {
301314 event . preventDefault ( ) ;
302- } else {
303- criteria . previousKeyMatched = false ;
304315 }
305316 }
306317
0 commit comments