ForumHeader.js 3.39 KB
import React from 'react'
import {
    View,
    Text,
    Image,
    StyleSheet,
    TouchableOpacity,
    StatusBar,
    Platform,
    PixelRatio,
    Dimensions
} from 'react-native'
import {NavigationActions,SafeAreaView} from 'react-navigation'
import {observer} from 'mobx-react/native'
export const line = 1 / PixelRatio.get()
const isX=Dimensions.get("window").height>=812&&Dimensions.get("window").width===375
export 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,
		height:headerHeight,
		flex: 1,
		justifyContent:'center',
	},
	rightView: {
		flex: 1,
		paddingRight: 15,
		alignItems: 'flex-end',
	},
	titleView: {
		flex: 1,
		alignItems: 'center',
	},
	title: {
		fontSize: 18,
		color: "#000",
        fontWeight:"bold"
	},
	backIcon: {
		height: 16.5,
		width: 9,
	},
	codeIcon:{
		width:20,
		height:20
	},
	rightText:{
		fontSize:16,
		color:"#333333",
	}
})
@observer
export default class ForumHeader 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('../img/loadBack.png')}/>
				</TouchableOpacity>
			)
		}
		if (left) {
			return (
				<TouchableOpacity style={styles.leftView} onPress={() => leftPress && leftPress()}>
					{left}
				</TouchableOpacity>
			)
		}
		if(code){
			return (
				<TouchableOpacity style={styles.leftView} onPress={() => leftPress && leftPress()}>
			</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
		let marginTopSize = (Platform.OS === "ios" ? 20 : 0);
        marginTopSize = marginTopSize + XOffset;
		// debugger
		return (
			<View style={{backgroundColor:"#fafafa",marginTop:marginTopSize}}>
				<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>
		)
	}
}