Skip to content

Commit c14471d

Browse files
committed
fix submenu indexing
1 parent 4662266 commit c14471d

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

src/pages/Search/SearchSelectedNarrow.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ type SearchSelectedNarrowProps = {options: Array<DropdownOption<SearchHeaderOpti
1414
function SearchSelectedNarrow({options, itemsLength}: SearchSelectedNarrowProps) {
1515
const styles = useThemeStyles();
1616
const {translate} = useLocalize();
17-
const selectedOptionIndexRef = useRef(-1);
17+
18+
// Stores an option to execute after modal closes when using deferred execution
19+
const selectedOptionRef = useRef<DropdownOption<SearchHeaderOptionValue> | null>(null);
1820

1921
const [isModalVisible, setIsModalVisible] = useState(false);
2022
const buttonRef = useRef<View>(null);
@@ -23,24 +25,25 @@ function SearchSelectedNarrow({options, itemsLength}: SearchSelectedNarrowProps)
2325
const closeMenu = () => setIsModalVisible(false);
2426

2527
const handleOnModalHide = () => {
26-
if (selectedOptionIndexRef.current === -1) {
28+
if (!selectedOptionRef.current) {
2729
return;
2830
}
2931

30-
options[selectedOptionIndexRef.current]?.onSelected?.();
32+
selectedOptionRef.current.onSelected?.();
33+
selectedOptionRef.current = null;
3134
};
3235

33-
const handleOnMenuItemPress = (option: DropdownOption<SearchHeaderOptionValue>, index: number) => {
36+
const handleOnMenuItemPress = (option: DropdownOption<SearchHeaderOptionValue>) => {
3437
if (option?.shouldCloseModalOnSelect) {
35-
selectedOptionIndexRef.current = index;
38+
selectedOptionRef.current = option;
3639
closeMenu();
3740
return;
3841
}
3942
option?.onSelected?.();
4043
};
4144

4245
const handleOnCloseMenu = () => {
43-
selectedOptionIndexRef.current = -1;
46+
selectedOptionRef.current = null;
4447
closeMenu();
4548
};
4649

@@ -61,8 +64,8 @@ function SearchSelectedNarrow({options, itemsLength}: SearchSelectedNarrowProps)
6164
isVisible={isModalVisible}
6265
onClose={handleOnCloseMenu}
6366
onModalHide={handleOnModalHide}
64-
onItemSelected={(selectedItem, index) => {
65-
handleOnMenuItemPress(selectedItem as DropdownOption<SearchHeaderOptionValue>, index);
67+
onItemSelected={(selectedItem) => {
68+
handleOnMenuItemPress(selectedItem as DropdownOption<SearchHeaderOptionValue>);
6669
}}
6770
anchorPosition={{horizontal: 0, vertical: 0}}
6871
anchorRef={buttonRef}
@@ -72,17 +75,7 @@ function SearchSelectedNarrow({options, itemsLength}: SearchSelectedNarrowProps)
7275
}}
7376
fromSidebarMediumScreen={false}
7477
shouldUseModalPaddingStyle
75-
menuItems={options.map((item, index) => ({
76-
...item,
77-
onSelected: item?.onSelected
78-
? () => {
79-
item.onSelected?.();
80-
}
81-
: () => {
82-
handleOnMenuItemPress(item, index);
83-
},
84-
shouldCallAfterModalHide: true,
85-
}))}
78+
menuItems={options}
8679
/>
8780
</View>
8881
);

0 commit comments

Comments
 (0)