This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
/
index.js
134 lines (118 loc) · 3.58 KB
/
index.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
// @flow
import React, { Component, type ComponentType } from 'react';
import { withTheme } from 'styled-components';
import { View, StyleSheet, Dimensions, TextInput } from 'react-native';
import { TabViewAnimated, SceneMap, TabBar } from 'react-native-tab-view';
import { TabLabel, SearchView } from './style';
import { Constants } from 'expo';
import SearchInput from '../../components/SearchInput';
import ThreadsSearchView from './ThreadsSearchView';
import CommunitiesSearchView from './CommunitiesSearchView';
import PeopleSearchView from './PeopleSearchView';
const initialLayout = {
height: 0,
width: Dimensions.get('window').width,
};
type TabView = {
key: string,
title: string,
};
type State = {
searchString: ?string,
index: number,
routes: Array<TabView>,
};
type Props = {
theme: Object,
};
class Search extends Component<Props, State> {
searchInput: ComponentType<TextInput>;
state = {
searchString: '',
index: 0,
routes: [
{ key: 'threads', title: 'Threads' },
{ key: 'communities', title: 'Communities' },
{ key: 'people', title: 'People' },
],
};
renderThreadsRoute = () => {
const { index, routes } = this.state;
if (routes[index].key !== 'threads') return <SearchView />;
if (!this.state.searchString) return <SearchView />;
return <ThreadsSearchView queryString={this.state.searchString} />;
};
renderCommunitiesRoute = () => {
const { index, routes } = this.state;
if (routes[index].key !== 'communities') return <SearchView />;
if (!this.state.searchString) return <SearchView />;
return <CommunitiesSearchView queryString={this.state.searchString} />;
};
renderPeopleRoute = () => {
const { index, routes } = this.state;
if (routes[index].key !== 'people') return <SearchView />;
if (!this.state.searchString) return <SearchView />;
return <PeopleSearchView queryString={this.state.searchString} />;
};
handleIndexChange = (index: number) => this.setState({ index });
renderTabs = (props: any) => {
const active = this.state.index === props.navigationState.index;
return (
<TabBar
indicatorStyle={{
backgroundColor: '#000',
height: StyleSheet.hairlineWidth,
}}
renderLabel={props => (
<TabLabel active={active}>{props.route.title}</TabLabel>
)}
style={{
backgroundColor: '#fff',
paddingTop: Constants.statusBarHeight,
}}
useNativeDriver
{...props}
/>
);
};
handleChange = (e: any) => {
this.setState({ searchString: e.nativeEvent.text });
};
renderHeader = (props: any) => {
const Tabs = () => this.renderTabs(props);
return (
<View>
<SearchInput
autoFocus={true}
blurOnSubmit={true}
placeholder={`Search for ${this.state.routes[
this.state.index
].title.toLowerCase()}`}
returnKeyType={'search'}
clearButtonMode={'always'}
onEndEditing={this.handleChange}
onSubmitEditing={this.handleChange}
/>
<Tabs />
</View>
);
};
render() {
const renderScene = SceneMap({
threads: this.renderThreadsRoute,
communities: this.renderCommunitiesRoute,
people: this.renderPeopleRoute,
});
return (
<TabViewAnimated
navigationState={this.state}
renderScene={renderScene}
renderHeader={this.renderHeader}
onIndexChange={this.handleIndexChange}
initialLayout={initialLayout}
useNativeDriver
/>
);
}
}
export default withTheme(Search);