11import isEmpty from 'lodash/isEmpty' ;
2- import React , { memo , useCallback , useMemo , useState } from 'react' ;
2+ import React , { memo , useCallback , useMemo , useRef , useState } from 'react' ;
33import { View } from 'react-native' ;
44import Button from '@components/Button' ;
55import { usePersonalDetails } from '@components/OnyxListItemProvider' ;
66import { useOptionsList } from '@components/OptionListContextProvider' ;
77import SelectionList from '@components/SelectionList' ;
88import UserSelectionListItem from '@components/SelectionList/Search/UserSelectionListItem' ;
9+ import type { SelectionListHandle } from '@components/SelectionList/types' ;
910import useLocalize from '@hooks/useLocalize' ;
1011import useOnyx from '@hooks/useOnyx' ;
1112import useResponsiveLayout from '@hooks/useResponsiveLayout' ;
@@ -43,6 +44,7 @@ type UserSelectPopupProps = {
4344} ;
4445
4546function 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