2018-08-31 18:13:30 +00:00
|
|
|
import React from 'react';
|
2022-03-21 19:23:59 +00:00
|
|
|
import { StyleSheet, Text, TextInput as RNTextInput, 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
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2018-08-31 18:13:30 +00:00
|
|
|
import I18n from '../i18n';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from './CustomIcon';
|
2022-05-18 19:17:42 +00:00
|
|
|
import TextInput from './TextInput';
|
2022-03-21 19:23:59 +00:00
|
|
|
import { useTheme } from '../theme';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
2022-03-21 19:23:59 +00:00
|
|
|
import sharedStyles from '../views/Styles';
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-21 19:23:59 +00:00
|
|
|
interface ISearchBox extends TextInputProps {
|
2022-01-17 16:10:39 +00:00
|
|
|
value?: string;
|
|
|
|
hasCancel?: boolean;
|
2022-03-29 20:06:50 +00:00
|
|
|
onCancelPress?: () => void;
|
2022-02-07 15:18:37 +00:00
|
|
|
inputRef?: React.Ref<RNTextInput>;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
const CancelButton = ({ onCancelPress }: { onCancelPress?: () => void }) => {
|
2022-03-21 19:23:59 +00:00
|
|
|
const { theme } = useTheme();
|
|
|
|
return (
|
|
|
|
<Touchable onPress={onCancelPress} style={styles.cancel}>
|
|
|
|
<Text style={[styles.cancelText, { color: themes[theme].headerTintColor }]}>{I18n.t('Cancel')}</Text>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
};
|
2019-07-29 16:33:28 +00:00
|
|
|
|
2022-03-21 19:23:59 +00:00
|
|
|
const SearchBox = ({ hasCancel, onCancelPress, inputRef, ...props }: ISearchBox): React.ReactElement => {
|
|
|
|
const { theme } = useTheme();
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.container,
|
|
|
|
{ backgroundColor: isIOS ? themes[theme].headerBackground : themes[theme].headerSecondaryBackground }
|
|
|
|
]}>
|
|
|
|
<View style={[styles.searchBox, { backgroundColor: themes[theme].searchboxBackground }]}>
|
|
|
|
<CustomIcon name='search' size={14} color={themes[theme].auxiliaryText} />
|
|
|
|
<TextInput
|
|
|
|
ref={inputRef}
|
|
|
|
autoCapitalize='none'
|
|
|
|
autoCorrect={false}
|
|
|
|
blurOnSubmit
|
|
|
|
clearButtonMode='while-editing'
|
|
|
|
placeholder={I18n.t('Search')}
|
|
|
|
returnKeyType='search'
|
|
|
|
style={styles.input}
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
theme={theme}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
</View>
|
2022-03-29 20:06:50 +00:00
|
|
|
{hasCancel && onCancelPress ? <CancelButton onCancelPress={onCancelPress} /> : null}
|
2018-08-31 18:13:30 +00:00
|
|
|
</View>
|
2022-03-21 19:23:59 +00:00
|
|
|
);
|
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2022-03-21 19:23:59 +00:00
|
|
|
export default SearchBox;
|