Header.js 3.22 KB
import React from 'react'
import {View, Text, Image, StyleSheet, TouchableOpacity,StatusBar,Dimensions,PixelRatio} from 'react-native'
import {NavigationActions,SafeAreaView} from 'react-navigation'
import {observer} from 'mobx-react/native'

const line = 1 / PixelRatio.get();
const isX=Dimensions.get("window").height===812&&Dimensions.get("window").width===375
const XOffset=isX?20:0

export const headerHeight = 44
const styles = StyleSheet.create({
	container: {
		height: headerHeight,
		backgroundColor: '#fafafa',
		flexDirection: 'row',
		alignItems: 'center',
		borderBottomColor:"#dddddd"
	},
	leftView: {
		paddingLeft: 15,
		flex: 1,
	},
	rightView: {
		flex: 1,
		paddingRight: 15,
		alignItems: 'flex-end',
	},
	titleView: {
		flex: 1,
		alignItems: 'center',
	},
	title: {
		fontSize: 18,
		color: "#333333"
	},
	backIcon: {
		height: 16.5,
		width: 9,
	},
	codeIcon:{
		width:20,
		height:20
	},
	rightText:{
		fontSize:16,
		color:"#333333",
	}
})
@observer
export default class Header extends React.Component {
	left() {
		const {navigation, pop, left, leftPress,backKey,popToHome,code} = this.props
		if (pop||popToHome) {
			return (
				<TouchableOpacity style={styles.leftView} onPress={() => {
					if(pop){navigation.dispatch(NavigationActions.back({key:backKey}))}
					if(popToHome){
						const resetAction = NavigationActions.reset({
							index: 0,
							key:null,
							actions: [
								NavigationActions.navigate({ routeName: 'BottomModal'})
							]
						})
						this.props.navigation.dispatch(resetAction)
					}
				}}>
					<Image style={styles.backIcon} resizeModa="contain" source={require('./imgs/back.png')}/>
				</TouchableOpacity>
			)
		}
		if (left) {
			return (
				<TouchableOpacity style={styles.leftView} onPress={() => leftPress && leftPress()}>
					{left}
				</TouchableOpacity>
			)
		}
		if(code){
			return (
				<TouchableOpacity style={styles.leftView} onPress={() => leftPress && leftPress()}>
				<Image style={styles.codeIcon} resizeModa="contain" source={require('./imgs/code.png')}/>
			</TouchableOpacity>
			)
		}
		return <View style={styles.leftView}/>
	}

	center() {
		const {center, title} = this.props
		if (title) {
			return (
				<View style={styles.titleView}>
					<Text style={styles.title}>{title}</Text>
				</View>
			)
		}
		if (center) {
			return center

		}
		return null
	}

	right() {
		const {right,rightText, rightPress} = this.props
		if (right) {
			return (
				<TouchableOpacity onPress={() => rightPress && rightPress()} style={styles.rightView}>
					{right}
				</TouchableOpacity>
			)
		}
		if(rightText){
			return(
				<TouchableOpacity onPress={() => rightPress && rightPress()} style={styles.rightView}>
					<Text style={styles.rightText}>{rightText}</Text>
				</TouchableOpacity>
			)
		}
		return <View style={styles.rightView}/>
	}

	render() {
		const border={borderBottomWidth:this.props.noborder?0:line,}
		const {offset}=this.props
		// debugger
		return (
			<View View style={{backgroundColor:"#fafafa"}}>
				<StatusBar barStyle={'default'}></StatusBar>
				<SafeAreaView style={[styles.container,border,offset&&{paddingTop:offset+XOffset,height:headerHeight+offset+XOffset}]} >
					{this.left()}
					{this.center()}
					{this.right()}
				</SafeAreaView>
			</View>
		)
	}
}