From 3c479669c3036fa3a382050840b2857ed34562b5 Mon Sep 17 00:00:00 2001 From: adahy344 Date: Tue, 3 Oct 2023 15:31:42 +0200 Subject: [PATCH] Fixed useAutocomplete nextFocus to account for disabled first and last entries --- .../src/useAutocomplete/useAutocomplete.js | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/mui-base/src/useAutocomplete/useAutocomplete.js b/packages/mui-base/src/useAutocomplete/useAutocomplete.js index f81827e684d00f..f69a498b23b991 100644 --- a/packages/mui-base/src/useAutocomplete/useAutocomplete.js +++ b/packages/mui-base/src/useAutocomplete/useAutocomplete.js @@ -300,22 +300,23 @@ export function useAutocomplete(props) { let nextFocus = index; while (true) { - // Out of range - if ( - (direction === 'next' && nextFocus === filteredOptions.length) || - (direction === 'previous' && nextFocus === -1) - ) { - return -1; - } - const option = listboxRef.current.querySelector(`[data-option-index="${nextFocus}"]`); - // Same logic as MenuList.js const nextFocusDisabled = disabledItemsFocusable ? false : !option || option.disabled || option.getAttribute('aria-disabled') === 'true'; - if ((option && !option.hasAttribute('tabindex')) || nextFocusDisabled) { + if (direction === 'next' && nextFocus === filteredOptions.length) { + if (!nextFocusDisabled) { + return -1; + } + nextFocus = 0; + } else if (direction === 'previous' && nextFocus === -1) { + if (!nextFocusDisabled) { + return -1; + } + nextFocus = filteredOptions.length; + } else if ((option && !option.hasAttribute('tabindex')) || nextFocusDisabled) { // Move to the next element. nextFocus += direction === 'next' ? 1 : -1; } else {