Skip to content

Commit 9ada621

Browse files
committed
[native] Add active prop to Search component
Summary: In the following diff I create a separate rendering path for the search component in `ChatThreadList` when it's not active versus when it is active. I needed a way to detect when the search became active so I could switch from rendering the inline (inactive) `TextInput` to the fixed (active) one. I first tried wrapping the inactive `TextInput` in a `TouchableWithoutFeedback` that could capture touches, but the `TextInput` had priority priority. I then tried just using the `TextInput`'s `onFocus`, but that caused weird keyboard effects... the keyboard would appear for the inline `TextInput`, then disappear and reappear for the fixed one. This diff introduces an `active` prop, and when it is set to `false` we will render a `Text` instead of a `TextInput. Test Plan: Tested in combination D1083 Reviewers: palys-swm Subscribers: KatPo, Adrian, atul Differential Revision: https://phabricator.ashoat.com/D1082
1 parent 3b3fc65 commit 9ada621

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

native/components/search.react.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22

33
import * as React from 'react';
4-
import { View, TouchableOpacity, TextInput } from 'react-native';
4+
import { View, TouchableOpacity, TextInput, Text } from 'react-native';
55
import Icon from 'react-native-vector-icons/FontAwesome';
66

77
import { isLoggedIn } from 'lib/selectors/user-selectors';
@@ -15,10 +15,11 @@ type Props = {|
1515
+searchText: string,
1616
+onChangeText: (searchText: string) => mixed,
1717
+containerStyle?: ViewStyle,
18+
+active?: boolean,
1819
|};
1920
const Search = React.forwardRef<Props, typeof TextInput>(
2021
function ForwardedSearch(props: Props, ref: React.Ref<typeof TextInput>) {
21-
const { onChangeText, searchText, containerStyle, ...rest } = props;
22+
const { onChangeText, searchText, containerStyle, active, ...rest } = props;
2223

2324
const clearSearch = React.useCallback(() => {
2425
onChangeText('');
@@ -47,18 +48,35 @@ const Search = React.forwardRef<Props, typeof TextInput>(
4748
);
4849
}
4950

50-
const textInputProps: React.ElementProps<typeof TextInput> = {
51-
style: styles.searchInput,
52-
value: searchText,
53-
onChangeText: onChangeText,
54-
placeholderTextColor: iconColor,
55-
returnKeyType: 'go',
56-
};
51+
const inactive = active === false;
52+
const usingPlaceholder = !searchText && rest.placeholder;
53+
const inactiveTextStyle = React.useMemo(
54+
() =>
55+
inactive && usingPlaceholder
56+
? [styles.searchText, { color: iconColor }]
57+
: styles.searchText,
58+
[inactive, usingPlaceholder, styles.searchText, iconColor],
59+
);
60+
61+
let textNode;
62+
if (!inactive) {
63+
const textInputProps: React.ElementProps<typeof TextInput> = {
64+
style: styles.searchText,
65+
value: searchText,
66+
onChangeText: onChangeText,
67+
placeholderTextColor: iconColor,
68+
returnKeyType: 'go',
69+
};
70+
textNode = <TextInput {...textInputProps} {...rest} ref={ref} />;
71+
} else {
72+
const text = usingPlaceholder ? rest.placeholder : searchText;
73+
textNode = <Text style={inactiveTextStyle}>{text}</Text>;
74+
}
5775

5876
return (
5977
<View style={[styles.search, containerStyle]}>
6078
<Icon name="search" size={18} color={iconColor} />
61-
<TextInput {...textInputProps} {...rest} ref={ref} />
79+
{textNode}
6280
{clearSearchInputIcon}
6381
</View>
6482
);
@@ -75,7 +93,7 @@ const unboundStyles = {
7593
paddingRight: 12,
7694
paddingVertical: 6,
7795
},
78-
searchInput: {
96+
searchText: {
7997
color: 'listForegroundLabel',
8098
flex: 1,
8199
fontSize: 16,

0 commit comments

Comments
 (0)