2018-08-31 18:13:30 +00:00
|
|
|
import React from 'react';
|
2022-01-17 16:10:39 +00:00
|
|
|
import { NativeSyntheticEvent, StyleSheet, Text, TextInputFocusEventData, TextInputProps, View } from 'react-native';
|
2019-07-29 16:33:28 +00:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import TextInput from '../presentation/TextInput';
|
2018-08-31 18:13:30 +00:00
|
|
|
import I18n from '../i18n';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../lib/Icons';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../views/Styles';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2019-07-29 16:33:28 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
flex: 1
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
|
|
|
searchBox: {
|
|
|
|
alignItems: 'center',
|
|
|
|
borderRadius: 10,
|
|
|
|
flexDirection: 'row',
|
|
|
|
fontSize: 17,
|
|
|
|
height: 36,
|
|
|
|
margin: 16,
|
|
|
|
marginVertical: 10,
|
2019-07-29 16:33:28 +00:00
|
|
|
paddingHorizontal: 10,
|
|
|
|
flex: 1
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
|
|
|
input: {
|
|
|
|
flex: 1,
|
|
|
|
fontSize: 17,
|
|
|
|
marginLeft: 8,
|
|
|
|
paddingTop: 0,
|
2019-03-29 19:36:07 +00:00
|
|
|
paddingBottom: 0,
|
|
|
|
...sharedStyles.textRegular
|
2019-07-29 16:33:28 +00:00
|
|
|
},
|
|
|
|
cancel: {
|
2020-02-13 19:24:39 +00:00
|
|
|
marginRight: 15
|
2019-07-29 16:33:28 +00:00
|
|
|
},
|
|
|
|
cancelText: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 17
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface ISearchBox {
|
2022-01-17 16:10:39 +00:00
|
|
|
value?: string;
|
2021-12-03 19:27:57 +00:00
|
|
|
onChangeText: TextInputProps['onChangeText'];
|
2022-01-17 16:10:39 +00:00
|
|
|
onSubmitEditing?: () => void;
|
|
|
|
hasCancel?: boolean;
|
|
|
|
onCancelPress?: Function;
|
|
|
|
theme?: string;
|
|
|
|
inputRef?: React.Ref<unknown>;
|
2021-09-13 20:41:05 +00:00
|
|
|
testID?: string;
|
2022-01-17 16:10:39 +00:00
|
|
|
onFocus?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const CancelButton = (onCancelPress: Function, theme: string) => (
|
2019-07-29 16:33:28 +00:00
|
|
|
<Touchable onPress={onCancelPress} style={styles.cancel}>
|
2020-07-06 20:56:28 +00:00
|
|
|
<Text style={[styles.cancelText, { color: themes[theme].headerTintColor }]}>{I18n.t('Cancel')}</Text>
|
2019-07-29 16:33:28 +00:00
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
|
|
|
|
const SearchBox = ({
|
2021-09-13 20:41:05 +00:00
|
|
|
onChangeText,
|
|
|
|
onSubmitEditing,
|
|
|
|
testID,
|
|
|
|
hasCancel,
|
|
|
|
onCancelPress,
|
|
|
|
inputRef,
|
|
|
|
theme,
|
|
|
|
...props
|
|
|
|
}: ISearchBox) => (
|
2019-12-04 16:39:53 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.container,
|
2022-01-17 16:10:39 +00:00
|
|
|
{ backgroundColor: isIOS ? themes[theme!].headerBackground : themes[theme!].headerSecondaryBackground }
|
2021-09-13 20:41:05 +00:00
|
|
|
]}>
|
2022-01-17 16:10:39 +00:00
|
|
|
<View style={[styles.searchBox, { backgroundColor: themes[theme!].searchboxBackground }]}>
|
|
|
|
<CustomIcon name='search' size={14} color={themes[theme!].auxiliaryText} />
|
2018-08-31 18:13:30 +00:00
|
|
|
<TextInput
|
2019-11-25 20:01:17 +00:00
|
|
|
ref={inputRef}
|
2018-08-31 18:13:30 +00:00
|
|
|
autoCapitalize='none'
|
|
|
|
autoCorrect={false}
|
|
|
|
blurOnSubmit
|
|
|
|
clearButtonMode='while-editing'
|
|
|
|
placeholder={I18n.t('Search')}
|
|
|
|
returnKeyType='search'
|
|
|
|
style={styles.input}
|
|
|
|
testID={testID}
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
onChangeText={onChangeText}
|
2019-06-10 16:22:35 +00:00
|
|
|
onSubmitEditing={onSubmitEditing}
|
2022-01-17 16:10:39 +00:00
|
|
|
theme={theme!}
|
2019-07-29 16:33:28 +00:00
|
|
|
{...props}
|
2018-08-31 18:13:30 +00:00
|
|
|
/>
|
|
|
|
</View>
|
2022-01-17 16:10:39 +00:00
|
|
|
{hasCancel ? CancelButton(onCancelPress!, theme!) : null}
|
2018-08-31 18:13:30 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default withTheme(SearchBox);
|