forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSearchAutocompleteInput.tsx
165 lines (143 loc) · 6.51 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
import type {ForwardedRef, ReactNode, RefObject} from 'react';
import React, {forwardRef, useState} from 'react';
import {View} from 'react-native';
import type {StyleProp, TextInputProps, ViewStyle} from 'react-native';
import {useOnyx} from 'react-native-onyx';
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 useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import {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';
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;
} & 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,
}: SearchAutocompleteInputProps,
ref: ForwardedRef<BaseTextInputRef>,
) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [isFocused, setIsFocused] = useState<boolean>(false);
const {isOffline} = useNetwork();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
const emailList = Object.keys(loginList ?? {});
const offlineMessage: string = isOffline && shouldShowOfflineMessage ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';
const inputWidth = isFullWidth ? styles.w100 : {width: variables.popoverWidth};
return (
<View style={[outerWrapperStyle]}>
<View style={[styles.flexRow, styles.alignItemsCenter, wrapperStyle ?? styles.searchRouterTextInputContainer, isFocused && wrapperFocusedStyle]}>
<View
style={styles.flex1}
fsClass="fs-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={(input: string) => {
'worklet';
return parseForLiveMarkdown(input, emailList, currentUserPersonalDetails.displayName ?? '');
}}
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);