-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathSearchAutocompleteInput.tsx
233 lines (197 loc) · 9.22 KB
/
SearchAutocompleteInput.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import type {ForwardedRef, ReactNode, RefObject} from 'react';
import React, {forwardRef, useCallback, useEffect, useLayoutEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import type {StyleProp, TextInputProps, ViewStyle} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import {useSharedValue} from 'react-native-reanimated';
import FormHelpMessage from '@components/FormHelpMessage';
import type {SelectionListHandle} from '@components/SelectionList/types';
import TextInput from '@components/TextInput';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import useActiveWorkspace from '@hooks/useActiveWorkspace';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import {parseFSAttributes} from '@libs/Fullstory';
import runOnLiveMarkdownRuntime from '@libs/runOnLiveMarkdownRuntime';
import {getAutocompleteCategories, getAutocompleteTags, parseForLiveMarkdown} from '@libs/SearchAutocompleteUtils';
import handleKeyPress from '@libs/SearchInputOnKeyPress';
import shouldDelayFocus from '@libs/shouldDelayFocus';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {SubstitutionMap} from './SearchRouter/getQueryWithSubstitutions';
type SearchAutocompleteInputProps = {
/** Value of TextInput */
value: string;
/** Callback to update search in SearchRouter */
onSearchQueryChange: (searchTerm: string) => void;
/** Callback invoked when the user submits the input */
onSubmit?: () => void;
/** SearchAutocompleteList ref for managing TextInput and SearchAutocompleteList focus */
autocompleteListRef?: RefObject<SelectionListHandle>;
/** Whether the input is full width */
isFullWidth: boolean;
/** Whether the input is disabled */
disabled?: boolean;
/** Whether the offline message should be shown */
shouldShowOfflineMessage?: boolean;
/** Callback to call when the input gets focus */
onFocus?: () => void;
/** Callback to call when the input gets blur */
onBlur?: () => void;
/** Any additional styles to apply */
wrapperStyle?: StyleProp<ViewStyle>;
/** Any additional styles to apply when input is focused */
wrapperFocusedStyle?: StyleProp<ViewStyle>;
/** Any additional styles to apply to text input along with FormHelperMessage */
outerWrapperStyle?: StyleProp<ViewStyle>;
/** Component to be displayed on the right */
rightComponent?: ReactNode;
/** Whether the search reports API call is running */
isSearchingForReports?: boolean;
/** Map of autocomplete suggestions. Required for highlighting to work properly */
substitutionMap: SubstitutionMap;
} & Pick<TextInputProps, 'caretHidden' | 'autoFocus' | 'selection'>;
function SearchAutocompleteInput(
{
value,
onSearchQueryChange,
onSubmit = () => {},
autocompleteListRef,
isFullWidth,
disabled = false,
shouldShowOfflineMessage = false,
autoFocus = true,
onFocus,
onBlur,
caretHidden = false,
wrapperStyle,
wrapperFocusedStyle,
outerWrapperStyle,
rightComponent,
isSearchingForReports,
selection,
substitutionMap,
}: SearchAutocompleteInputProps,
ref: ForwardedRef<BaseTextInputRef>,
) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [isFocused, setIsFocused] = useState<boolean>(false);
const {isOffline} = useNetwork();
const {activeWorkspaceID} = useActiveWorkspace();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST);
const currencyAutocompleteList = Object.keys(currencyList ?? {});
const currencySharedValue = useSharedValue(currencyAutocompleteList);
const [allPolicyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES);
const categoryAutocompleteList = useMemo(() => {
return getAutocompleteCategories(allPolicyCategories, activeWorkspaceID);
}, [activeWorkspaceID, allPolicyCategories]);
const categorySharedValue = useSharedValue(categoryAutocompleteList);
const [allPoliciesTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS);
const tagAutocompleteList = useMemo(() => {
return getAutocompleteTags(allPoliciesTags, activeWorkspaceID);
}, [activeWorkspaceID, allPoliciesTags]);
const tagSharedValue = useSharedValue(tagAutocompleteList);
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
const emailList = Object.keys(loginList ?? {});
const emailListSharedValue = useSharedValue(emailList);
const offlineMessage: string = isOffline && shouldShowOfflineMessage ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';
useEffect(() => {
runOnLiveMarkdownRuntime(() => {
'worklet';
emailListSharedValue.set(emailList);
})();
}, [emailList, emailListSharedValue]);
useEffect(() => {
runOnLiveMarkdownRuntime(() => {
'worklet';
currencySharedValue.set(currencyAutocompleteList);
})();
}, [currencyAutocompleteList, currencySharedValue]);
useEffect(() => {
runOnLiveMarkdownRuntime(() => {
'worklet';
categorySharedValue.set(categoryAutocompleteList);
})();
}, [categorySharedValue, categoryAutocompleteList]);
useEffect(() => {
runOnLiveMarkdownRuntime(() => {
'worklet';
tagSharedValue.set(tagAutocompleteList);
});
}, [tagSharedValue, tagAutocompleteList]);
const parser = useCallback(
(input: string) => {
'worklet';
return parseForLiveMarkdown(input, currentUserPersonalDetails.displayName ?? '', substitutionMap, emailListSharedValue, currencySharedValue, categorySharedValue, tagSharedValue);
},
[currentUserPersonalDetails.displayName, substitutionMap, currencySharedValue, categorySharedValue, tagSharedValue, emailListSharedValue],
);
const inputWidth = isFullWidth ? styles.w100 : {width: variables.popoverWidth};
// Parse Fullstory attributes on initial render
useLayoutEffect(parseFSAttributes, []);
return (
<View style={[outerWrapperStyle]}>
<View style={[styles.flexRow, styles.alignItemsCenter, wrapperStyle ?? styles.searchRouterTextInputContainer, isFocused && wrapperFocusedStyle]}>
<View
style={styles.flex1}
fsClass={CONST.FULL_STORY.UNMASK}
testID={CONST.FULL_STORY.UNMASK}
>
<TextInput
testID="search-autocomplete-text-input"
value={value}
onChangeText={onSearchQueryChange}
autoFocus={autoFocus}
shouldDelayFocus={shouldDelayFocus}
caretHidden={caretHidden}
loadingSpinnerStyle={[styles.mt0, styles.mr2]}
role={CONST.ROLE.PRESENTATION}
placeholder={translate('search.searchPlaceholder')}
autoCapitalize="none"
autoCorrect={false}
spellCheck={false}
enterKeyHint="search"
accessibilityLabel={translate('search.searchPlaceholder')}
disabled={disabled}
maxLength={CONST.SEARCH_QUERY_LIMIT}
onSubmitEditing={onSubmit}
shouldUseDisabledStyles={false}
textInputContainerStyles={[styles.borderNone, styles.pb0, styles.pr3]}
inputStyle={[inputWidth, styles.pl3, styles.pr3]}
onFocus={() => {
setIsFocused(true);
autocompleteListRef?.current?.updateExternalTextInputFocus(true);
onFocus?.();
}}
onBlur={() => {
setIsFocused(false);
autocompleteListRef?.current?.updateExternalTextInputFocus(false);
onBlur?.();
}}
isLoading={!!isSearchingForReports}
ref={ref}
onKeyPress={handleKeyPress(onSubmit)}
type="markdown"
multiline={false}
parser={parser}
selection={selection}
/>
</View>
{!!rightComponent && <View style={styles.pr3}>{rightComponent}</View>}
</View>
<FormHelpMessage
style={styles.ph3}
isError={false}
message={offlineMessage}
/>
</View>
);
}
SearchAutocompleteInput.displayName = 'SearchAutocompleteInput';
export type {SearchAutocompleteInputProps};
export default forwardRef(SearchAutocompleteInput);