Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/SelectionList/components/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function TextInput({
}: TextInputProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {label, value, onChangeText, errorText, headerMessage, hint, disableAutoFocus, placeholder, maxLength, inputMode, ref: optionsRef, style} = options ?? {};
const {label, value, onChangeText, errorText, headerMessage, hint, disableAutoFocus, placeholder, maxLength, inputMode, ref: optionsRef, style, disableAutoCorrect} = options ?? {};
const resultsFound = headerMessage !== translate('common.noResultsFound');
const noData = dataLength === 0 && !showLoadingPlaceholder;
const shouldShowHeaderMessage = !!headerMessage && (!isLoadingNewOptions || resultsFound || noData);
Expand Down Expand Up @@ -135,6 +135,7 @@ function TextInput({
testID="selection-list-text-input"
errorText={errorText}
shouldInterceptSwipe={false}
autoCorrect={!disableAutoCorrect}
/>
</View>
{shouldShowHeaderMessage && (
Expand Down
3 changes: 3 additions & 0 deletions src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ type TextInputOptions = {
/** Whether the text input autofocus should be disabled */
disableAutoFocus?: boolean;

/** Whether the text input auto correct should be disabled */
disableAutoCorrect?: boolean;

/** Styles for the text input */
style?: {
/** Styles for the text input container */
Expand Down
167 changes: 71 additions & 96 deletions src/pages/iou/request/MoneyRequestAttendeeSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import reportsSelector from '@selectors/Attributes';
import {deepEqual} from 'fast-equals';
import React, {memo, useCallback, useEffect, useMemo} from 'react';
import React, {memo, useEffect} from 'react';
import type {GestureResponderEvent} from 'react-native';
import Button from '@components/Button';
import EmptySelectionListContent from '@components/EmptySelectionListContent';
import FormHelpMessage from '@components/FormHelpMessage';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
// eslint-disable-next-line no-restricted-imports
import SelectionList from '@components/SelectionListWithSections';
import InviteMemberListItem from '@components/SelectionListWithSections/InviteMemberListItem';
import type {SectionListDataType} from '@components/SelectionListWithSections/types';
import InviteMemberListItem from '@components/SelectionList/ListItem/InviteMemberListItem';
import SelectionListWithSections from '@components/SelectionList/SelectionListWithSections';
import type {Section} from '@components/SelectionList/SelectionListWithSections/types';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
Expand Down Expand Up @@ -37,6 +36,7 @@ import type {IOUAction, IOUType} from '@src/CONST';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Attendee} from '@src/types/onyx/IOU';
import getEmptyArray from '@src/types/utils/getEmptyArray';
import SafeString from '@src/utils/SafeString';

type MoneyRequestAttendeesSelectorProps = {
Expand Down Expand Up @@ -73,27 +73,19 @@ function MoneyRequestAttendeeSelector({attendees = [], onFinish, onAttendeesAdde
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const currentUserEmail = currentUserPersonalDetails.email ?? '';
const currentUserAccountID = currentUserPersonalDetails.accountID;
Comment thread
zfurtak marked this conversation as resolved.
Resolved
const isPaidGroupPolicy = isPaidGroupPolicyFn(policy);
Comment thread
zfurtak marked this conversation as resolved.
Resolved
const recentAttendeeLists = getFilteredRecentAttendees(personalDetails, attendees, recentAttendees ?? [], currentUserEmail, currentUserAccountID);
Comment thread
zfurtak marked this conversation as resolved.
Resolved

const isPaidGroupPolicy = useMemo(() => isPaidGroupPolicyFn(policy), [policy]);

const recentAttendeeLists = useMemo(
() => getFilteredRecentAttendees(personalDetails, attendees, recentAttendees ?? [], currentUserEmail, currentUserAccountID),
[personalDetails, attendees, recentAttendees, currentUserEmail, currentUserAccountID],
);
const initialSelectedOptions = useMemo(
() =>
attendees.map((attendee) => ({
...attendee,
reportID: CONST.DEFAULT_NUMBER_ID.toString(),
keyForList: `${attendee.accountID ?? (attendee.email || attendee.displayName)}`,
selected: true,
// Use || to fall back to displayName for name-only attendees (empty email)
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
login: attendee.email || attendee.displayName,
...getPersonalDetailByEmail(attendee.email),
})),
[attendees],
);
const initialSelectedOptions = attendees.map((attendee) => ({
...attendee,
reportID: CONST.DEFAULT_NUMBER_ID.toString(),
keyForList: String(attendee.accountID) ?? (attendee.email || attendee.displayName),
selected: true,
// Use || to fall back to displayName for name-only attendees (empty email)
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
login: attendee.email || attendee.displayName,
...getPersonalDetailByEmail(attendee.email),
}));

const {searchTerm, debouncedSearchTerm, setSearchTerm, availableOptions, selectedOptions, toggleSelection, areOptionsInitialized, onListEndReached} = useSearchSelector({
selectionMode: CONST.SEARCH_SELECTOR.SELECTION_MODE_MULTI,
Expand Down Expand Up @@ -134,11 +126,10 @@ function MoneyRequestAttendeeSelector({attendees = [], onFinish, onAttendeesAdde
searchInServer(debouncedSearchTerm.trim());
}, [debouncedSearchTerm]);
Comment thread
zfurtak marked this conversation as resolved.
Resolved

const orderedAvailableOptions = useMemo(() => {
if (!isPaidGroupPolicy || !areOptionsInitialized) {
return availableOptions;
}

let orderedAvailableOptions;
if (!isPaidGroupPolicy || !areOptionsInitialized) {
orderedAvailableOptions = availableOptions;
} else {
const orderedOptions = orderOptions(
{
recentReports: availableOptions.recentReports,
Expand All @@ -152,31 +143,27 @@ function MoneyRequestAttendeeSelector({attendees = [], onFinish, onAttendeesAdde
preferRecentExpenseReports: action === CONST.IOU.ACTION.CREATE,
Comment thread
zfurtak marked this conversation as resolved.
Resolved
},
);

return {
orderedAvailableOptions = {
...availableOptions,
recentReports: orderedOptions.recentReports,
Comment thread
zfurtak marked this conversation as resolved.
Resolved
personalDetails: orderedOptions.personalDetails,
workspaceChats: orderedOptions.workspaceChats,
Comment thread
zfurtak marked this conversation as resolved.
Resolved
};
}, [availableOptions, isPaidGroupPolicy, areOptionsInitialized, searchTerm, action]);
}

const shouldShowErrorMessage = selectedOptions.length < 1;

const handleConfirmSelection = useCallback(
(_keyEvent?: GestureResponderEvent | KeyboardEvent, option?: OptionData) => {
if (shouldShowErrorMessage || (!selectedOptions.length && !option)) {
return;
}
const handleConfirmSelection = (_keyEvent?: GestureResponderEvent | KeyboardEvent, option?: OptionData) => {
if (shouldShowErrorMessage || (!selectedOptions.length && !option)) {
return;
}

Comment thread
zfurtak marked this conversation as resolved.
Resolved
onFinish(CONST.IOU.TYPE.SUBMIT);
},
[shouldShowErrorMessage, onFinish, selectedOptions.length],
);
onFinish(CONST.IOU.TYPE.SUBMIT);
};

const showLoadingPlaceholder = useMemo(() => !areOptionsInitialized || !didScreenTransitionEnd, [areOptionsInitialized, didScreenTransitionEnd]);
const showLoadingPlaceholder = !areOptionsInitialized || !didScreenTransitionEnd;

const footerContent = useMemo(() => {
const getFooterContent = () => {
if (!shouldShowErrorMessage && !selectedOptions.length) {
return;
}
Expand All @@ -200,17 +187,16 @@ function MoneyRequestAttendeeSelector({attendees = [], onFinish, onAttendeesAdde
/>
</>
);
}, [handleConfirmSelection, selectedOptions.length, shouldShowErrorMessage, styles, translate]);

/**
* Returns the sections needed for the OptionsSelector
*/
const [sections, header] = useMemo(() => {
const newSections: Array<SectionListDataType<OptionData>> = [];
if (!areOptionsInitialized || !didScreenTransitionEnd) {
return [newSections, ''];
}
};
const footerContent = getFooterContent();

let sections: Array<Section<OptionData>>;
let header: string;
if (!areOptionsInitialized || !didScreenTransitionEnd) {
sections = [];
header = '';
} else {
const newSections: Array<Section<OptionData>> = [];
const cleanSearchTerm = searchTerm.trim().toLowerCase();
const formatResults = formatSectionsFromSearchTerm(
cleanSearchTerm,
Expand All @@ -226,20 +212,23 @@ function MoneyRequestAttendeeSelector({attendees = [], onFinish, onAttendeesAdde

newSections.push({
...formatResults.section,
sectionIndex: 0,
data: formatResults.section.data as OptionData[],
});

if (orderedAvailableOptions.recentReports.length > 0) {
newSections.push({
title: translate('common.recents'),
data: orderedAvailableOptions.recentReports,
sectionIndex: 1,
});
}

if (orderedAvailableOptions.personalDetails.length > 0) {
newSections.push({
title: translate('common.contacts'),
data: orderedAvailableOptions.personalDetails,
sectionIndex: 2,
});
}

Expand All @@ -263,67 +252,53 @@ function MoneyRequestAttendeeSelector({attendees = [], onFinish, onAttendeesAdde
? getPolicyExpenseReportOption(participant, currentUserAccountID, personalDetails, reportAttributesDerived)
: getParticipantsOption(participant, personalDetails);
}) as OptionData[],
shouldShow: true,
sectionIndex: 3,
});
}

const headerMessage = getHeaderMessage(
header = getHeaderMessage(
formatResults.section.data.length + (orderedAvailableOptions.personalDetails ?? []).length + (orderedAvailableOptions.recentReports ?? []).length !== 0,
!!orderedAvailableOptions?.userToInvite,
cleanSearchTerm,
countryCode,
attendees.some((attendee) => getPersonalDetailSearchTerms(attendee, currentUserAccountID).join(' ').toLowerCase().includes(cleanSearchTerm)),
);
sections = newSections;
}

return [newSections, headerMessage];
}, [
areOptionsInitialized,
didScreenTransitionEnd,
searchTerm,
initialSelectedOptions,
attendees,
orderedAvailableOptions.recentReports,
orderedAvailableOptions.personalDetails,
orderedAvailableOptions.userToInvite,
personalDetails,
reportAttributesDerived,
loginList,
countryCode,
translate,
currentUserAccountID,
currentUserEmail,
]);
const optionLength = !areOptionsInitialized ? 0 : sections.reduce((acc, section) => acc + (section.data?.length ?? 0), 0);
Comment thread
zfurtak marked this conversation as resolved.
Resolved

const optionLength = useMemo(() => {
if (!areOptionsInitialized) {
return 0;
}
return sections.reduce((acc, section) => acc + (section.data?.length ?? 0), 0);
}, [areOptionsInitialized, sections]);
const shouldShowListEmptyContent = optionLength === 0 && !showLoadingPlaceholder;
Comment thread
zfurtak marked this conversation as resolved.
Resolved

const shouldShowListEmptyContent = useMemo(() => optionLength === 0 && !showLoadingPlaceholder, [optionLength, showLoadingPlaceholder]);
const textInputOptions = {
Comment thread
zfurtak marked this conversation as resolved.
Resolved
label: translate('selectionList.nameEmailOrPhoneNumber'),
hint: offlineMessage,
value: searchTerm,
onChangeText: setSearchTerm,
headerMessage: header,
disableAutoCorrect: true,
};

return (
<SelectionList
onConfirm={handleConfirmSelection}
sections={areOptionsInitialized ? sections : CONST.EMPTY_ARRAY}
<SelectionListWithSections
sections={areOptionsInitialized ? sections : getEmptyArray<Section<OptionData>>()}
ListItem={InviteMemberListItem}
textInputValue={searchTerm}
textInputLabel={translate('selectionList.nameEmailOrPhoneNumber')}
textInputHint={offlineMessage}
onChangeText={setSearchTerm}
shouldPreventDefaultFocusOnSelectRow={!canUseTouchScreen()}
onSelectRow={toggleSelection}
shouldSingleExecuteRowSelect
textInputOptions={textInputOptions}
confirmButtonOptions={{
onConfirm: handleConfirmSelection,
}}
footerContent={footerContent}
autoCorrect={false}
listEmptyContent={<EmptySelectionListContent contentType={iouType} />}
headerMessage={header}
showLoadingPlaceholder={showLoadingPlaceholder}
canSelectMultiple
isLoadingNewOptions={!!isSearchingForReports}
shouldShowListEmptyContent={shouldShowListEmptyContent}
showLoadingPlaceholder={showLoadingPlaceholder}
showListEmptyContent={shouldShowListEmptyContent}
listEmptyContent={<EmptySelectionListContent contentType={iouType} />}
shouldPreventDefaultFocusOnSelectRow={!canUseTouchScreen()}
onEndReached={onListEndReached}
disableMaintainingScrollPosition
shouldSingleExecuteRowSelect
shouldShowTextInput
canSelectMultiple
/>
);
}
Expand Down
Loading