Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix search type popover has 2 items highlighted #53330

20 changes: 17 additions & 3 deletions src/components/PopoverMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ const renderWithConditionalWrapper = (shouldUseScrollView: boolean, contentConta
return <>{children}</>;
};

function getSelectedItemIndex(menuItems: PopoverMenuItem[]) {
return menuItems.findIndex((option) => option.isSelected);
}

function PopoverMenu({
menuItems,
onItemSelected,
Expand Down Expand Up @@ -174,7 +178,7 @@ function PopoverMenu({
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const [currentMenuItems, setCurrentMenuItems] = useState(menuItems);
const currentMenuItemsFocusedIndex = currentMenuItems?.findIndex((option) => option.isSelected);
const currentMenuItemsFocusedIndex = getSelectedItemIndex(currentMenuItems);
const [enteredSubMenuIndexes, setEnteredSubMenuIndexes] = useState<readonly number[]>(CONST.EMPTY_ARRAY);
const {windowHeight} = useWindowDimensions();

Expand Down Expand Up @@ -300,7 +304,7 @@ function PopoverMenu({
);

const onModalHide = () => {
setFocusedIndex(-1);
setFocusedIndex(currentMenuItemsFocusedIndex);
};

// When the menu items are changed, we want to reset the sub-menu to make sure
Expand All @@ -312,7 +316,17 @@ function PopoverMenu({
}
setEnteredSubMenuIndexes(CONST.EMPTY_ARRAY);
setCurrentMenuItems(menuItems);
}, [menuItems]);

// Update the focused item to match the selected item, but only when the popover is not visible.
// This ensures that if the popover is visible, highlight from the keyboard navigation is not overridden
// by external updates.
if (isVisible) {
return;
}
setFocusedIndex(getSelectedItemIndex(menuItems));

// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [menuItems, setFocusedIndex]);

return (
<PopoverWithMeasuredContent
Expand Down
Loading