Skip to content

Commit

Permalink
Merge pull request #53330 from bernhardoj/fix/50224-search-type-popov…
Browse files Browse the repository at this point in the history
…er-has-2-items-highlighted

Fix search type popover has 2 items highlighted
  • Loading branch information
cristipaval authored Dec 4, 2024
2 parents 35e4761 + 11ba377 commit 08d8072
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/components/PopoverMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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 @@ -178,7 +182,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 @@ -304,7 +308,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 @@ -316,7 +320,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

0 comments on commit 08d8072

Please sign in to comment.