-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHomeRoute.tsx
114 lines (99 loc) · 2.85 KB
/
HomeRoute.tsx
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
import * as React from 'react';
import { StyleSheet, View, Text, FlatList } from 'react-native';
import { RouteContentProps, RouteViewEvents } from 'react-native-ios-navigator';
import { RouteItem } from './RouteItem';
import { RouteItems } from './Constants';
import * as Colors from '../../constants/Colors';
type HomeRouteState = {
searchBarText?: string;
};
export class HomeRoute extends React.PureComponent<RouteContentProps, HomeRouteState> {
static styles = StyleSheet.create({
rootContentContainer: {
paddingBottom: 100,
},
rootContainerEmpty: {
flex: 1,
marginTop: 15,
alignItems: 'center',
},
titleEmptyText: {
fontSize: 18,
fontWeight: '600',
color: Colors.PURPLE[300],
},
});
constructor(props: RouteContentProps){
super(props);
this.state = {
searchBarText: null,
};
};
// @ts-ignore
_handleOnPressItem = async (routeKey: string) => {
const navigation = this.props.navigation;
await navigation.push({routeKey});
};
_renderItem = ({item, index}: {
item: typeof RouteItems[number];
index: number;
}) => {
return (
<RouteItem
key={item.routeKey}
title={item.title}
routeKey={item.routeKey}
desc={item.desc}
index={index}
onPress={this._handleOnPressItem}
/>
);
};
render(){
const { styles } = HomeRoute;
const { searchBarText } = this.state;
const hasSearchBarText = searchBarText != null;
const searchBarTextLowerCase = searchBarText?.toLowerCase();
const items = (hasSearchBarText
// true - filter `RouteItems` that matches `searchBarText`
? RouteItems.filter(item => (
item.routeKey
.toLocaleLowerCase()
.includes(searchBarTextLowerCase)
|| item.title
.toLocaleLowerCase()
.includes(searchBarTextLowerCase)
|| item.desc
?.toLocaleLowerCase()
.includes(searchBarTextLowerCase)
))
// false - no filter
: RouteItems
);
return (
<React.Fragment>
<RouteViewEvents
onUpdateSearchResults={({nativeEvent}) => {
this.setState({ searchBarText: nativeEvent.text });
}}
onSearchBarSearchButtonClicked={({nativeEvent}) => {
this.setState({ searchBarText: nativeEvent.text });
}}
/>
<FlatList
contentContainerStyle={styles.rootContentContainer}
data={items}
keyExtractor={(item) => item.routeKey}
renderItem={this._renderItem}
ListEmptyComponent={(
<View style={styles.rootContainerEmpty}>
<Text style={styles.titleEmptyText}>
{'Nothing to Show 😔'}
</Text>
</View>
)}
/>
</React.Fragment>
);
};
};