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

15 changes: 13 additions & 2 deletions src/components/PopoverMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/jsx-props-no-spreading */
import lodashIsEqual from 'lodash/isEqual';
import type {ReactNode, RefObject} from 'react';
import React, {useLayoutEffect, useState} from 'react';
import React, {useEffect, useLayoutEffect, useState} from 'react';
import {StyleSheet, View} from 'react-native';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import type {ModalProps} from 'react-native-modal';
Expand Down Expand Up @@ -300,7 +300,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 @@ -314,6 +314,17 @@ function PopoverMenu({
setCurrentMenuItems(menuItems);
}, [menuItems]);

useEffect(() => {
if (isVisible) {
return;
}

// 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 to the selected item, preventing user experience disruption.
setFocusedIndex(currentMenuItemsFocusedIndex);
}, [isVisible, currentMenuItemsFocusedIndex, setFocusedIndex]);

return (
<PopoverWithMeasuredContent
anchorPosition={anchorPosition}
Expand Down
Loading