Skip to content

Commit 08d8072

Browse files
authored
Merge pull request #53330 from bernhardoj/fix/50224-search-type-popover-has-2-items-highlighted
Fix search type popover has 2 items highlighted
2 parents 35e4761 + 11ba377 commit 08d8072

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/components/PopoverMenu.tsx

+17-3
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ const renderWithConditionalWrapper = (shouldUseScrollView: boolean, contentConta
140140
return <>{children}</>;
141141
};
142142

143+
function getSelectedItemIndex(menuItems: PopoverMenuItem[]) {
144+
return menuItems.findIndex((option) => option.isSelected);
145+
}
146+
143147
function PopoverMenu({
144148
menuItems,
145149
onItemSelected,
@@ -178,7 +182,7 @@ function PopoverMenu({
178182
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
179183
const {isSmallScreenWidth} = useResponsiveLayout();
180184
const [currentMenuItems, setCurrentMenuItems] = useState(menuItems);
181-
const currentMenuItemsFocusedIndex = currentMenuItems?.findIndex((option) => option.isSelected);
185+
const currentMenuItemsFocusedIndex = getSelectedItemIndex(currentMenuItems);
182186
const [enteredSubMenuIndexes, setEnteredSubMenuIndexes] = useState<readonly number[]>(CONST.EMPTY_ARRAY);
183187
const {windowHeight} = useWindowDimensions();
184188

@@ -304,7 +308,7 @@ function PopoverMenu({
304308
);
305309

306310
const onModalHide = () => {
307-
setFocusedIndex(-1);
311+
setFocusedIndex(currentMenuItemsFocusedIndex);
308312
};
309313

310314
// When the menu items are changed, we want to reset the sub-menu to make sure
@@ -316,7 +320,17 @@ function PopoverMenu({
316320
}
317321
setEnteredSubMenuIndexes(CONST.EMPTY_ARRAY);
318322
setCurrentMenuItems(menuItems);
319-
}, [menuItems]);
323+
324+
// Update the focused item to match the selected item, but only when the popover is not visible.
325+
// This ensures that if the popover is visible, highlight from the keyboard navigation is not overridden
326+
// by external updates.
327+
if (isVisible) {
328+
return;
329+
}
330+
setFocusedIndex(getSelectedItemIndex(menuItems));
331+
332+
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
333+
}, [menuItems, setFocusedIndex]);
320334

321335
return (
322336
<PopoverWithMeasuredContent

0 commit comments

Comments
 (0)