Skip to content

Commit 1ee8054

Browse files
authored
Merge pull request Expensify#64712 from abzokhattab/clear-search-input-after-selecting-a-member-in-report-from-modal
Clear search input after selecting a member in the "From" report modal
2 parents f342ff1 + 0332ef0 commit 1ee8054

1 file changed

Lines changed: 35 additions & 25 deletions

File tree

src/components/Search/FilterDropdowns/UserSelectPopup.tsx

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import isEmpty from 'lodash/isEmpty';
2-
import React, {memo, useCallback, useMemo, useState} from 'react';
2+
import React, {memo, useCallback, useMemo, useRef, useState} from 'react';
33
import {View} from 'react-native';
44
import Button from '@components/Button';
55
import {usePersonalDetails} from '@components/OnyxListItemProvider';
66
import {useOptionsList} from '@components/OptionListContextProvider';
77
import SelectionList from '@components/SelectionList';
88
import UserSelectionListItem from '@components/SelectionList/Search/UserSelectionListItem';
9+
import type {SelectionListHandle} from '@components/SelectionList/types';
910
import useLocalize from '@hooks/useLocalize';
1011
import useOnyx from '@hooks/useOnyx';
1112
import useResponsiveLayout from '@hooks/useResponsiveLayout';
@@ -43,6 +44,7 @@ type UserSelectPopupProps = {
4344
};
4445

4546
function UserSelectPopup({value, closeOverlay, onChange}: UserSelectPopupProps) {
47+
const selectionListRef = useRef<SelectionListHandle | null>(null);
4648
const styles = useThemeStyles();
4749
const {translate} = useLocalize();
4850
const {options} = useOptionsList();
@@ -100,31 +102,38 @@ function UserSelectPopup({value, closeOverlay, onChange}: UserSelectPopupProps)
100102
}, [optionsList, cleanSearchTerm]);
101103

102104
const listData = useMemo(() => {
103-
const personalDetailList = filteredOptions.personalDetails
104-
.map((participant) => ({
105-
...participant,
106-
isSelected: selectedAccountIDs.has(participant.accountID),
107-
}))
108-
.sort((a, b) => {
109-
// Put the current user at the top of the list
110-
if (a.accountID === accountID) {
111-
return -1;
112-
}
113-
if (b.accountID === accountID) {
114-
return 1;
115-
}
116-
return 0;
117-
});
118-
119-
const recentReportsList = filteredOptions.recentReports.map((report) => {
120-
const isSelected = selectedOptions.some((selectedOption) => selectedOption.reportID === report.reportID);
121-
return {
122-
...report,
123-
isSelected,
124-
};
105+
const personalDetailList = filteredOptions.personalDetails.map((participant) => ({
106+
...participant,
107+
isSelected: selectedAccountIDs.has(participant.accountID),
108+
}));
109+
110+
const recentReportsList = filteredOptions.recentReports.map((report) => ({
111+
...report,
112+
isSelected: selectedOptions.some((opt) => opt.reportID === report.reportID),
113+
}));
114+
115+
const combined = [...personalDetailList, ...recentReportsList];
116+
117+
combined.sort((a, b) => {
118+
// selected items first
119+
if (a.isSelected && !b.isSelected) {
120+
return -1;
121+
}
122+
if (!a.isSelected && b.isSelected) {
123+
return 1;
124+
}
125+
126+
// Put the current user at the top of the list
127+
if (a.accountID === accountID) {
128+
return -1;
129+
}
130+
if (b.accountID === accountID) {
131+
return 1;
132+
}
133+
return 0;
125134
});
126135

127-
return [...personalDetailList, ...recentReportsList];
136+
return combined;
128137
}, [filteredOptions, selectedOptions, accountID, selectedAccountIDs]);
129138

130139
const {sections, headerMessage} = useMemo(() => {
@@ -150,6 +159,7 @@ function UserSelectPopup({value, closeOverlay, onChange}: UserSelectPopupProps)
150159
const isSelected = selectedOptions.some((selected) => optionsMatch(selected, option));
151160

152161
setSelectedOptions((prev) => (isSelected ? prev.filter((selected) => !optionsMatch(selected, option)) : [...prev, getSelectedOptionData(option)]));
162+
selectionListRef?.current?.scrollToIndex(0, true);
153163
},
154164
[selectedOptions],
155165
);
@@ -171,9 +181,9 @@ function UserSelectPopup({value, closeOverlay, onChange}: UserSelectPopupProps)
171181
return (
172182
<View style={[styles.getUserSelectionListPopoverHeight(dataLength || 1, windowHeight, shouldUseNarrowLayout)]}>
173183
<SelectionList
184+
ref={selectionListRef}
174185
canSelectMultiple
175186
textInputAutoFocus={shouldFocusInputOnScreenFocus}
176-
shouldClearInputOnSelect={false}
177187
headerMessage={headerMessage}
178188
sections={sections}
179189
ListItem={UserSelectionListItem}

0 commit comments

Comments
 (0)