search.js
6.93 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Created by Cassie on 2018/03/06
*/
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
Image,
TextInput,
StatusBar,
ActivityIndicator,
FlatList
} from 'react-native';
import {zoomW,zoomH} from '../../utils/getSize';
import {getHeaderPadding,xnToast,numChange,getHeaderHeight} from "../../utils/utils";
import AppService from "../../service/AppService";
const defaultIcon = require('../../img/defaultIcon.png');
const noSearch = require('../../img/noSearch.png');
export default class Search extends Component {
static navigationOptions = ({ navigation, screenProps })=>({
title:null,
headerStyle:{
elevation:0, // 去掉阴影
paddingTop:getHeaderPadding(),
height:getHeaderHeight(),
backgroundColor:'#fff',
},
headerLeft:(<View style={styles.rightTop}>
<TextInput underlineColorAndroid="transparent" returnKeyType="search" autoFocus={true} placeholder="请搜索帖子标题" style={styles.inputBox} onChangeText={(text) => {navigation.state.params.setKey(text)}} onSubmitEditing={() => {navigation.state.params.search()}}/>
<TouchableOpacity style={styles.rightIcon} onPress={navigation.state.params?navigation.state.params.back:null}>
<Text style={{fontSize:16,color:'#666'}}>取消</Text>
</TouchableOpacity>
</View>),
headerRight:null
});
constructor(props) {
super(props);
this.state = {
loading:false,
noResult:false
};
};
componentDidMount(){
//设置头部
this.props.navigation.setParams({
back:()=>{
this.props.navigation.goBack();
},
setKey:(text)=>{
this.setState({
title:text
})
},
search:()=>{
if(!this.state.title || this.state.title.length == 0){
xnToast('请输入帖子标题进行搜索~');
return;
}
this.setState({
loading:true
});
AppService.findPostList({title:this.state.title}).then(data =>{
this.setState({
loading:false
});
if(data.message){
xnToast(data.message);
return;
}
if(data.errors.length > 0) {
xnToast(data.errors[0].message);
}else{
if(data.result.length == 0){
this.setState({
noResult:true
})
}
this.setState({
list:data.result
})
}
})
}
});
};
/* 渲染列表*/
keyExtractor = (item,index) => index;
renderList = ({item,index}) => (
<TouchableOpacity style={styles.listItem} onPress={() => this.toDetail(item.id)}>
<View style={styles.itemTop}>
<Text numberOfLines={2} style={[styles.itemTitle,{width:(345/zoomW)}]}>{item.title}</Text>
</View>
<View style={styles.itemContent}>
{item.contentText.length != 0 && !item.cover && !item.video && <Text numberOfLines={2} style={styles.contentText}>{item.contentText}</Text>}
</View>
<View style={styles.itemBottom}>
<Text style={{fontSize:14,color:'#999'}}>{numChange(item.postCount)}回复</Text>
<View style={{display:'flex',flexDirection:'row',alignItems:'center'}}>
{item.threadUserAvatarUrl && <Image source={{uri:item.threadUserAvatarUrl}} style={styles.avatar} resizeMode="cover" />}
{!item.threadUserAvatarUrl && <Image source={defaultIcon} style={styles.avatar} resizeMode="cover" />}
<Text style={{fontSize:14,color:'#999'}}>{item.threadUserName}·{item.boardName}</Text>
</View>
</View>
</TouchableOpacity>
);
/*跳转到详情*/
toDetail(id){
// AppService.updateView({id:id}).then((data) => {
// if(!data.message && data.errors.length == 0){
this.props.navigation.navigate('Detail',{id:id})
// }
// })
};
render() {
return (
<View style={styles.background}>
<StatusBar barStyle="dark-content" />
{this.state.list && <FlatList keyExtractor={this.keyExtractor} data={this.state.list} renderItem={this.renderList} />}
{this.state.noResult && <View style={{width:'100%',height:'100%',display:'flex',alignItems:'center',justifyContent:'center'}}>
<Image source={noSearch} style={{marginBottom:(12/zoomH)}} resizeMode="cover" />
<Text style={{fontSize:14,color:'#999'}}>没有搜索结果~</Text>
<View style={{width:'100%',height:(100/zoomH),backgroundColor:'rgba(0,0,0,0)'}} />
</View>}
{this.state.loading && <View style={styles.loadingBg}>
<ActivityIndicator size="large" />
</View>}
</View>
);
}
}
const styles = StyleSheet.create({
background:{
width:'100%',
height:'100%',
backgroundColor:'#f2f2f2'
},
rightTop:{
display:'flex',
flexDirection:'row',
paddingLeft:(15/zoomW)
},
inputBox:{
width:(310/zoomW),
height:(28/zoomH),
backgroundColor:'#eeeff3',
borderRadius:3,
padding:0,
paddingLeft:(5/zoomW),
paddingRight:(5/zoomW)
},
rightIcon:{
width:(40/zoomW),
display:'flex',
justifyContent:'center',
alignItems:'center'
},
listItem:{
minHeight:(110/zoomH),
maxHeight:(318/zoomH),
backgroundColor:'#fff',
marginBottom:(8/zoomH),
padding:(15/zoomH),
},
itemTitle:{
color:'#000',
fontSize:16,
fontWeight:'600'
},
moreImg:{
width:(112/zoomW),
height:(112/zoomW)
},
itemContent:{
display:'flex',
flexDirection:'row',
marginTop:(15/zoomH),
marginBottom:(15/zoomH)
},
contentText:{
fontSize:14,
color:'#666'
},
itemBottom:{
display:'flex',
flexDirection:'row',
justifyContent:'space-between'
},
avatar:{
width:(20/zoomW),
height:(20/zoomW),
borderRadius:(10/zoomW),
marginRight:(6/zoomW)
},
loadingBg:{
width:'100%',
height:'100%',
position:'absolute',
display:'flex',
alignItems:'center',
justifyContent:'center'
}
});