SearchBar.js
2.92 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
import React from "react"
import {
View,
StyleSheet,
Text,
Image,
LayoutAnimation,
TextInput,
TouchableWithoutFeedback,
Dimensions,
TouchableOpacity,
Platform
} from "react-native"
import {observer} from 'mobx-react/native'
import {observable} from 'mobx'
import Toast from "react-native-simple-toast";
import {ownerUserId} from "../service/rpc";
const width = Dimensions.get('window').width
export const isAndroid=Platform.OS==="android"
const styles = StyleSheet.create({
container: {
flex: 1,
},
searchBar: {
height: 50,
backgroundColor: '#f0eff4',
padding: 7.5,
flexDirection:'row'
},
searchContent: {
flex: 1,
backgroundColor: 'white',
borderRadius: 4,
paddingHorizontal: 7.5
},
searchInput: {
flex: 1,
fontSize: 15,
padding: 0,
},
searchPosition: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
position: 'absolute',
top: isAndroid?3:10
},
searchIcon: {
height: 18,
width: 18,
marginRight: 2,
},
button: {
width: 50,
borderRadius: 4,
alignItems: 'center',
justifyContent: 'center',
paddingLeft:10,
},
buttonText: {
color: 'black',
fontSize: 15,
}
})
@observer
export default class SearchBar extends React.Component {
@observable
focused = false
@observable
showButton = false
onFocus = () => {
const conf = {
duration: 300,
update: {
type: "linear",
},
}
LayoutAnimation.configureNext(conf)
this.showButton=true
this.focused = true
}
onBlur = () => {
const conf = {
duration: 300,
update: {
type: "linear",
},
}
LayoutAnimation.configureNext(conf)
this.showButton=false
this.focused = false
}
onPress=()=>{
if(!this.props.noButton){
if(this.props.logic.text.length===0){
Toast.show("请输入内容")
return
}
const {navigation,logic}=this.props
navigation.navigate("TaskList", {ownerUserId,keyword:logic.text,title:logic.text})
this.props.logic.text=""
}
}
render() {
const position = {left: this.focused ? 10 : width / 2 - 30}
const {logic,} = this.props
return (
<View style={styles.searchBar}>
<TouchableWithoutFeedback onPress={() => this.input.focus()}>
<View style={styles.searchContent}>
<View style={[styles.searchPosition, position]}>
<Image source={require('./imgs/search.png')} style={styles.searchIcon} resizeMode='contain'/>
<TextInput
style={styles.searchInput}
placeholder="搜索"
onFocus={this.onFocus}
onBlur={this.onBlur}
ref={v => this.input = v}
value={logic.text}
onChangeText={v => {logic.text=v}}
underlineColorAndroid={"transparent"}
onSubmitEditing={this.onPress}
returnKeyType={"search"}
/>
</View>
</View>
</TouchableWithoutFeedback>
{!this.props.noButton&&this.showButton&&<TouchableOpacity style={styles.button} onPress={this.onPress}>
<Text style={styles.buttonText}>搜索</Text>
</TouchableOpacity>}
</View>
)
}
}