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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
import React, {useEffect, useState} from 'react';
import {
Keyboard,
NativeSyntheticEvent,
StyleSheet,
Text,
TextInput,
TextInputProps,
TextInputSubmitEditingEventData,
TouchableOpacity,
View,
} from 'react-native';
import {normalize} from 'react-native-elements';
import Animated, {interpolate} from 'react-native-reanimated';
import Icon from 'react-native-vector-icons/Feather';
import {useSelector} from 'react-redux';
import {RootState} from '../../store/rootReducer';
import {getSearchSuggestions, SCREEN_HEIGHT} from '../../utils';
const AnimatedIcon = Animated.createAnimatedComponent(Icon);
interface SearchBarProps extends TextInputProps {
onCancel: () => void;
top: Animated.Value<number>;
searching: boolean;
}
const SearchBar: React.FC<SearchBarProps> = ({
onFocus,
onBlur,
onChangeText,
value,
onCancel,
searching,
top,
}) => {
const handleSubmit = (
e: NativeSyntheticEvent<TextInputSubmitEditingEventData>,
) => {
e.preventDefault();
Keyboard.dismiss();
};
const {university} = useSelector((state: RootState) => state.user.profile);
const DEFAULT_PLACEHOLDER: string = 'Search';
// the list of suggestions to cycle through. TODO: get this from the backend
const SEARCH_SUGGESTIONS: string[] = getSearchSuggestions(university);
/*
* index & id of current placeholder, used in selecting next placeholder. -1
* indicates DEFAULT_PLACEHOLDER. TODO: make it appear more random by tracking
* last 3-5 ids & use longer list of placeholders
*/
const [placeholderId, setPlaceholderId] = useState<number>(-1);
// the current placeholder
const [placeholder, setPlaceholder] = useState<string>(DEFAULT_PLACEHOLDER);
/*
* Utility function that generates a random integer in [0, xCeil).
*
* @param xCeil - the exclusive ceiling (getRandomInt(2) => 0 or 1, not 2)
* @returns a random integer in the range [0, xCeil)
*/
const getRandomInt = (xCeil: number): number => {
return Math.floor(Math.random() * Math.floor(xCeil));
};
/*
* Handler for `placeholderChangeInterval` that sets the next placeholderId.
*/
const updatePlaceholder = () => {
let nextId: number = getRandomInt(SEARCH_SUGGESTIONS.length);
while (nextId === placeholderId) {
nextId = getRandomInt(SEARCH_SUGGESTIONS.length);
}
// TODO: FIGURE OUT WHY CHANGES IN placeholderId ARE NOT REFLECTED HERE
// my thought: the value is set when the function is defined, so it keeps
// its inital value of -1 forever.
setPlaceholderId(nextId);
};
/*
* Update `placeholder` when `placeholderId` is updated by the interval handler.
*/
useEffect(() => {
if (placeholderId === -1) {
setPlaceholder(DEFAULT_PLACEHOLDER);
return;
}
setPlaceholder(
DEFAULT_PLACEHOLDER.concat(` '${SEARCH_SUGGESTIONS[placeholderId]}'`),
);
}, [placeholderId]);
/*
* Sets the interval when the user begins searching and clears it when the user is done.
*/
useEffect(() => {
if (!searching) {
return;
}
updatePlaceholder();
const updateInterval = setInterval(() => {
updatePlaceholder();
}, 4000);
return () => {
clearInterval(updateInterval);
setPlaceholderId(-1);
};
}, [searching]);
/*
* Animated nodes used in search bar activation animation.
*/
const marginRight: Animated.Node<number> = interpolate(top, {
inputRange: [-SCREEN_HEIGHT, 0],
outputRange: [0, 58],
});
const opacity: Animated.Node<number> = interpolate(top, {
inputRange: [-SCREEN_HEIGHT, 0],
outputRange: [0, 1],
});
return (
<View style={styles.container}>
<Animated.View style={styles.inputContainer}>
<AnimatedIcon
name="search"
color={'#7E7E7E'}
size={25}
style={styles.searchIcon}
/>
<TextInput
style={[styles.input]}
placeholderTextColor={'#828282'}
onSubmitEditing={handleSubmit}
clearButtonMode="while-editing"
autoCapitalize="none"
autoCorrect={false}
{...{placeholder, value, onChangeText, onFocus, onBlur}}
/>
</Animated.View>
<Animated.View style={{marginRight, opacity}}>
<TouchableOpacity style={styles.cancelButton} onPress={onCancel}>
<Text style={styles.cancelText}>Cancel</Text>
</TouchableOpacity>
</Animated.View>
</View>
);
};
const styles = StyleSheet.create({
container: {
height: 40,
paddingHorizontal: 20,
flexDirection: 'row',
},
inputContainer: {
flexGrow: 1,
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 8,
borderRadius: 20,
backgroundColor: '#F0F0F0',
},
searchIcon: {
marginRight: 8,
},
input: {
flex: 1,
fontSize: 16,
color: '#000',
letterSpacing: normalize(0.5),
},
cancelButton: {
height: '100%',
position: 'absolute',
justifyContent: 'center',
paddingHorizontal: 8,
},
cancelText: {
color: '#818181',
fontWeight: '500',
},
});
export default SearchBar;
|