-
Notifications
You must be signed in to change notification settings - Fork 12
/
RNNSearchBar.js
167 lines (147 loc) · 4.22 KB
/
RNNSearchBar.js
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
/**
* @author Luke Brandon Farrell
* @description Search bar component for iOS and Android.
*/
/* NPM - Node Package Manage */
import React from "react";
import { Platform, Animated, StyleSheet } from "react-native";
import PropTypes from "prop-types";
import { SearchBar as ElementsSearchBar } from "react-native-elements";
import { Navigation } from "react-native-navigation";
export class RNNSearchBar extends React.Component {
/**
* [ Built-in React method. ]
*
* Executes when the component is initialized
*
* @param {object} props
*
*/
constructor(props) {
super(props);
/** Component State */
this.state = {
searchBarTopPadding: new Animated.Value(0),
searchBarFocused: false
};
/** Component Bindings */
this.searchBarFocused = this.searchBarFocused.bind(this);
this.searchBarBlurred = this.searchBarBlurred.bind(this);
}
/**
* [ Built-in React method. ]
*
* Allows us to render JSX to the screen
*/
render() {
/** Styles */
const { searchContainerStyle } = styles;
/** Props */
const { search, placeholder, onSearchButtonPress, onCancel, onClear, onChangeText } = this.props;
/** State */
const { searchBarTopPadding } = this.state;
return (
<Animated.View
style={[searchContainerStyle, { paddingTop: searchBarTopPadding }]}
>
<ElementsSearchBar
value={search}
platform={Platform.OS}
placeholder={placeholder}
cancelIcon={{ type: "font-awesome", name: "chevron-left" }}
inputContainerStyle={
Platform.OS === "ios" ? { backgroundColor: "#efefef" } : null
}
containerStyle={
Platform.OS === "ios" ? { backgroundColor: "#FFFFFF" } : null
}
lightTheme={true}
autoCapitalize="none"
autoCorrect={false}
spellCheck={false}
returnKeyType="search"
onChangeText={value => onChangeText(value)}
onBlur={this.searchBarBlurred}
onFocus={this.searchBarFocused}
onSubmitEditing={() => {
if (onSearchButtonPress) onSearchButtonPress(search);
}}
onCancel={onCancel}
onClear={onClear}
/>
</Animated.View>
);
}
/**
* Sets the search as focused and evokes any side effects
*/
searchBarFocused() {
this.setState({ searchBarFocused: true });
if (Platform.OS === "ios") {
const { statusBarHeight } = this.props;
Animated.timing(this.state.searchBarTopPadding, {
toValue: statusBarHeight,
duration: 200
}).start();
Navigation.mergeOptions(this.props.componentId, {
topBar: {
visible: false,
animate: true
}
});
Navigation.mergeOptions(this.props.componentId, {
statusBar: { style: "dark" }
});
}
if (this.props.onFocus) this.props.onFocus();
}
/**
* Sets the search as blurred and evokes any side effects
*/
searchBarBlurred() {
this.setState({ searchBarFocused: false });
if (Platform.OS === "ios") {
Animated.timing(this.state.searchBarTopPadding, {
toValue: 0,
duration: 200
}).start();
Navigation.mergeOptions(this.props.componentId, {
topBar: {
visible: true,
animate: true
}
});
Navigation.mergeOptions(this.props.componentId, {
statusBar: { style: "light" }
});
}
if (this.props.onBlur) this.props.onBlur();
}
}
RNNSearchBar.defaultProps = {
placeholder: "search"
};
RNNSearchBar.propTypes = {
search: PropTypes.string,
componentId: PropTypes.string.isRequired,
statusBarHeight: PropTypes.number.isRequired,
placeholder: PropTypes.string,
onChangeText: PropTypes.func,
onSearchButtonPress: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
onClear: PropTypes.func,
onCancel: PropTypes.func
};
/** -------------------------------------------- */
/** Component Styling */
/** -------------------------------------------- */
const styles = StyleSheet.create({
searchContainerStyle: {
flex: 1,
paddingLeft: 4,
paddingRight: 4,
backgroundColor: "#FFFFFF",
zIndex: 999
}
});