diff --git a/native/components/search.react.js b/native/components/search.react.js index 96c198a9a..28bf22ebb 100644 --- a/native/components/search.react.js +++ b/native/components/search.react.js @@ -1,89 +1,107 @@ // @flow import * as React from 'react'; -import { View, TouchableOpacity, TextInput } from 'react-native'; +import { View, TouchableOpacity, TextInput, Text } from 'react-native'; import Icon from 'react-native-vector-icons/FontAwesome'; import { isLoggedIn } from 'lib/selectors/user-selectors'; import { useSelector } from '../redux/redux-utils'; import { useStyles, useColors } from '../themes/colors'; import type { ViewStyle } from '../types/styles'; type Props = {| ...React.ElementConfig, +searchText: string, +onChangeText: (searchText: string) => mixed, +containerStyle?: ViewStyle, + +active?: boolean, |}; const Search = React.forwardRef( function ForwardedSearch(props: Props, ref: React.Ref) { - const { onChangeText, searchText, containerStyle, ...rest } = props; + const { onChangeText, searchText, containerStyle, active, ...rest } = props; const clearSearch = React.useCallback(() => { onChangeText(''); }, [onChangeText]); const loggedIn = useSelector(isLoggedIn); const styles = useStyles(unboundStyles); const colors = useColors(); const prevLoggedInRef = React.useRef(); React.useEffect(() => { const prevLoggedIn = prevLoggedInRef.current; prevLoggedInRef.current = loggedIn; if (!loggedIn && prevLoggedIn) { clearSearch(); } }, [loggedIn, clearSearch]); const { listSearchIcon: iconColor } = colors; let clearSearchInputIcon = null; if (searchText) { clearSearchInputIcon = ( ); } - const textInputProps: React.ElementProps = { - style: styles.searchInput, - value: searchText, - onChangeText: onChangeText, - placeholderTextColor: iconColor, - returnKeyType: 'go', - }; + const inactive = active === false; + const usingPlaceholder = !searchText && rest.placeholder; + const inactiveTextStyle = React.useMemo( + () => + inactive && usingPlaceholder + ? [styles.searchText, { color: iconColor }] + : styles.searchText, + [inactive, usingPlaceholder, styles.searchText, iconColor], + ); + + let textNode; + if (!inactive) { + const textInputProps: React.ElementProps = { + style: styles.searchText, + value: searchText, + onChangeText: onChangeText, + placeholderTextColor: iconColor, + returnKeyType: 'go', + }; + textNode = ; + } else { + const text = usingPlaceholder ? rest.placeholder : searchText; + textNode = {text}; + } return ( - + {textNode} {clearSearchInputIcon} ); }, ); const unboundStyles = { search: { alignItems: 'center', backgroundColor: 'listSearchBackground', borderRadius: 6, flexDirection: 'row', paddingLeft: 14, paddingRight: 12, paddingVertical: 6, }, - searchInput: { + searchText: { color: 'listForegroundLabel', flex: 1, fontSize: 16, marginLeft: 8, marginVertical: 0, padding: 0, borderBottomColor: 'transparent', }, }; export default React.memo(Search);