SearchBar.js 2.92 KB
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>

		)
	}
}