forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
120 lines (99 loc) · 4.46 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, {useMemo, useState} from 'react';
import {useOnyx} from 'react-native-onyx';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import type * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PolicyTag, PolicyTags} from '@src/types/onyx';
import type {PendingAction} from '@src/types/onyx/OnyxCommon';
type SelectedTagOption = {
name: string;
enabled: boolean;
isSelected?: boolean;
accountID: number | undefined;
pendingAction?: PendingAction;
};
type TagPickerProps = {
/** The policyID we are getting tags for */
// It's used in withOnyx HOC.
// eslint-disable-next-line react/no-unused-prop-types
policyID: string;
/** The selected tag of the expense */
selectedTag: string;
/** The name of tag list we are getting tags for */
tagListName: string;
/** Callback to submit the selected tag */
onSubmit: (selectedTag: Partial<ReportUtils.OptionData>) => void;
/** Should show the selected option that is disabled? */
shouldShowDisabledAndSelectedOption?: boolean;
/** Indicates which tag list index was selected */
tagListIndex: number;
};
function TagPicker({selectedTag, tagListName, policyID, tagListIndex, shouldShowDisabledAndSelectedOption = false, onSubmit}: TagPickerProps) {
const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`);
const [policyRecentlyUsedTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`);
const styles = useThemeStyles();
const {translate} = useLocalize();
const [searchValue, setSearchValue] = useState('');
const policyRecentlyUsedTagsList = useMemo(() => policyRecentlyUsedTags?.[tagListName] ?? [], [policyRecentlyUsedTags, tagListName]);
const policyTagList = PolicyUtils.getTagList(policyTags, tagListIndex);
const policyTagsCount = PolicyUtils.getCountOfEnabledTagsOfList(policyTagList.tags);
const isTagsCountBelowThreshold = policyTagsCount < CONST.TAG_LIST_THRESHOLD;
const shouldShowTextInput = !isTagsCountBelowThreshold;
const selectedOptions: SelectedTagOption[] = useMemo(() => {
if (!selectedTag) {
return [];
}
return [
{
name: selectedTag,
enabled: true,
accountID: undefined,
},
];
}, [selectedTag]);
const enabledTags: PolicyTags | Array<PolicyTag | SelectedTagOption> = useMemo(() => {
if (!shouldShowDisabledAndSelectedOption) {
return policyTagList.tags;
}
const selectedNames = selectedOptions.map((s) => s.name);
return [...selectedOptions, ...Object.values(policyTagList.tags).filter((policyTag) => policyTag.enabled && !selectedNames.includes(policyTag.name))];
}, [selectedOptions, policyTagList, shouldShowDisabledAndSelectedOption]);
const sections = useMemo(
() =>
OptionsListUtils.getFilteredOptions({
searchValue,
selectedOptions,
includeP2P: false,
includeTags: true,
tags: enabledTags,
recentlyUsedTags: policyRecentlyUsedTagsList,
canInviteUser: false,
}).tagOptions,
[searchValue, enabledTags, selectedOptions, policyRecentlyUsedTagsList],
);
const headerMessage = OptionsListUtils.getHeaderMessageForNonUserList((sections?.at(0)?.data?.length ?? 0) > 0, searchValue);
const selectedOptionKey = sections.at(0)?.data?.filter((policyTag) => policyTag.searchText === selectedTag)?.[0]?.keyForList;
return (
<SelectionList
ListItem={RadioListItem}
sectionTitleStyles={styles.mt5}
sections={sections}
textInputValue={searchValue}
headerMessage={headerMessage}
textInputLabel={shouldShowTextInput ? translate('common.search') : undefined}
isRowMultilineSupported
initiallyFocusedOptionKey={selectedOptionKey}
onChangeText={setSearchValue}
onSelectRow={onSubmit}
/>
);
}
TagPicker.displayName = 'TagPicker';
export default TagPicker;
export type {SelectedTagOption};