forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditReportFieldDropdown.tsx
103 lines (86 loc) · 4.1 KB
/
EditReportFieldDropdown.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, {useCallback, useMemo} from 'react';
import {useOnyx} from 'react-native-onyx';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import type {ListItem} from '@components/SelectionList/types';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import localeCompare from '@libs/LocaleCompare';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import ONYXKEYS from '@src/ONYXKEYS';
type EditReportFieldDropdownPageComponentProps = {
/** Value of the policy report field */
fieldValue: string;
/** Key of the policy report field */
fieldKey: string;
/** ID of the policy this report field belongs to */
// eslint-disable-next-line react/no-unused-prop-types
policyID: string;
/** Options of the policy report field */
fieldOptions: string[];
/** Callback to fire when the Save button is pressed */
onSubmit: (form: Record<string, string>) => void;
};
type EditReportFieldDropdownPageProps = EditReportFieldDropdownPageComponentProps;
function EditReportFieldDropdownPage({onSubmit, fieldKey, fieldValue, fieldOptions}: EditReportFieldDropdownPageProps) {
const [recentlyUsedReportFields] = useOnyx(ONYXKEYS.RECENTLY_USED_REPORT_FIELDS);
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
const theme = useTheme();
const {translate} = useLocalize();
const recentlyUsedOptions = useMemo(() => recentlyUsedReportFields?.[fieldKey]?.sort(localeCompare) ?? [], [recentlyUsedReportFields, fieldKey]);
const itemRightSideComponent = useCallback(
(item: ListItem) => {
if (item.text === fieldValue) {
return (
<Icon
src={Expensicons.Checkmark}
fill={theme.iconSuccessFill}
/>
);
}
return null;
},
[theme.iconSuccessFill, fieldValue],
);
const [sections, headerMessage] = useMemo(() => {
const validFieldOptions = fieldOptions?.filter((option) => !!option)?.sort(localeCompare);
const {policyReportFieldOptions} = OptionsListUtils.getFilteredOptions({
searchValue: debouncedSearchValue,
selectedOptions: [
{
keyForList: fieldValue,
searchText: fieldValue,
text: fieldValue,
},
],
includeP2P: false,
canInviteUser: false,
includePolicyReportFieldOptions: true,
policyReportFieldOptions: validFieldOptions,
recentlyUsedPolicyReportFieldOptions: recentlyUsedOptions,
});
const policyReportFieldData = policyReportFieldOptions?.[0]?.data ?? [];
const header = OptionsListUtils.getHeaderMessageForNonUserList(policyReportFieldData.length > 0, debouncedSearchValue);
return [policyReportFieldOptions, header];
}, [recentlyUsedOptions, debouncedSearchValue, fieldValue, fieldOptions]);
const selectedOptionKey = useMemo(() => (sections?.[0]?.data ?? []).filter((option) => option.searchText === fieldValue)?.at(0)?.keyForList, [sections, fieldValue]);
return (
<SelectionList
textInputValue={searchValue}
textInputLabel={translate('common.search')}
sections={sections ?? []}
onSelectRow={(option) => onSubmit({[fieldKey]: !option?.text || fieldValue === option.text ? '' : option.text})}
initiallyFocusedOptionKey={selectedOptionKey ?? undefined}
onChangeText={setSearchValue}
headerMessage={headerMessage}
ListItem={RadioListItem}
isRowMultilineSupported
rightHandSideComponent={itemRightSideComponent}
/>
);
}
EditReportFieldDropdownPage.displayName = 'EditReportFieldDropdownPage';
export default EditReportFieldDropdownPage;