-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathSearchRouterInput.tsx
133 lines (113 loc) · 4.8 KB
/
SearchRouterInput.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
import React, {useState} from 'react';
import type {ReactNode, RefObject} from 'react';
import {View} from 'react-native';
import type {StyleProp, ViewStyle} from 'react-native';
import FormHelpMessage from '@components/FormHelpMessage';
import type {SelectionListHandle} from '@components/SelectionList/types';
import TextInput from '@components/TextInput';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import variables from '@styles/variables';
import CONST from '@src/CONST';
type SearchRouterInputProps = {
/** Value of TextInput */
value: string;
/** Setter to TextInput value */
setValue: (searchTerm: string) => void;
/** Callback to update search in SearchRouter */
updateSearch: (searchTerm: string) => void;
/** Callback invoked when the user submits the input */
onSubmit?: () => void;
/** SearchRouterList ref for managing TextInput and SearchRouterList focus */
routerListRef?: 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;
/** Whether the input should be focused */
autoFocus?: boolean;
/** 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;
};
function SearchRouterInput({
value,
setValue,
updateSearch,
onSubmit = () => {},
routerListRef,
isFullWidth,
disabled = false,
shouldShowOfflineMessage = false,
autoFocus = true,
wrapperStyle,
wrapperFocusedStyle,
outerWrapperStyle,
rightComponent,
isSearchingForReports,
}: SearchRouterInputProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [isFocused, setIsFocused] = useState<boolean>(false);
const {isOffline} = useNetwork();
const offlineMessage: string = isOffline && shouldShowOfflineMessage ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';
const onChangeText = (text: string) => {
setValue(text);
updateSearch(text);
};
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}>
<TextInput
testID="search-router-text-input"
value={value}
onChangeText={onChangeText}
autoFocus={autoFocus}
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}
onSubmitEditing={onSubmit}
shouldUseDisabledStyles={false}
textInputContainerStyles={[styles.borderNone, styles.pb0]}
inputStyle={[inputWidth, styles.pl3, styles.pr3]}
onFocus={() => {
setIsFocused(true);
routerListRef?.current?.updateExternalTextInputFocus(true);
}}
onBlur={() => {
setIsFocused(false);
routerListRef?.current?.updateExternalTextInputFocus(false);
}}
isLoading={!!isSearchingForReports}
/>
</View>
{rightComponent && <View style={styles.pr3}>{rightComponent}</View>}
</View>
<FormHelpMessage
style={styles.ph3}
isError={false}
message={offlineMessage}
/>
</View>
);
}
SearchRouterInput.displayName = 'SearchRouterInput';
export default SearchRouterInput;