NavBar.js 4.09 KB
import React, {} from 'react';
import {Alert, Image, Platform, StatusBar, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import {width} from '../../utils/getSize';
import back from './back.png';
import PropTypes from 'prop-types';
export const shadowOpt = {
  width: width,
  height: 0,
  color: "#000",
  border: 3,
  radius: 0,
  opacity: 0.15,
  x: 0,
  y: 4.5,
}
const styles = StyleSheet.create({
  container: {
    height: Platform.OS === 'ios' ? 44 : 44,
    // marginTop: Platform.OS === 'ios' ? 20 : 0,
    backgroundColor: '#ffffff',
    flexDirection: 'row',
  },
  renderLeft: {
    flex: 1,

  },
  renderCenter: {
    flex: 3,
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'column',
  },
  renderSearch: {
    paddingLeft: 50,
    flex: 4,
    justifyContent: 'center',
    paddingRight: 10,
  },
  renderRight: {
    flex: 1,
  },
  slideContainer: {
    flexDirection: 'row',
    flex: 1,
    alignItems: 'center',
    paddingLeft: 15
  },
  backImg: {
    width: 16,
    height: 16,
  },
  title: {
    fontSize: 16,
    fontWeight: '700',
    color: '#000'
  },
  shadow: {
    borderBottomWidth:1,
    borderBottomColor:'#dddddd'
  }
});

export default class NavBar extends React.Component {
  static contextTypes = {
    navigator: PropTypes.object,
  };

  static defaultProps = {
    opacity: false,
    renderBack: false,
    style: null,
    search: false,
  };

  static propTypes = {
    renderBack: PropTypes.bool,
    renderLeft: PropTypes.func,
    renderSearch: PropTypes.func,
    title: PropTypes.string,
    opacity: PropTypes.bool,
    search: PropTypes.bool,
    removeAnimation: PropTypes.func,
    noShadow: PropTypes.bool,
    messageBack:PropTypes.string
  };

  back = () => {
    if (this.props.messageBack){
      Alert.alert(
          '注意',
          this.props.messageBack,
          [
            {text: '取消', onPress: () => console.log('OK Pressed!')},
            {text: '确定', onPress: () => {
                this.props.removeAnimation && this.props.removeAnimation()
                this.props.nav.goBack();
              }},
          ]
      )

    }else{
      this.props.removeAnimation && this.props.removeAnimation()
      this.props.nav.goBack();
    }

  };

  renderLeft() {
    const {renderBack, opacity, styleBack} = this.props;
    if (renderBack) {
      return (
        <TouchableOpacity
          style={styles.slideContainer}
          onPress={this.back}
        >
          <Image
            source={back}
            style={styles.backImg}
            resizeMode="contain"
          />
        </TouchableOpacity>
      )
    }
    return this.props.renderLeft ? this.props.renderLeft() : null;
  }

  renderSearch() {
    if (this.props.search) {
      return this.props.renderSearch();
    }
    return null;
  }

  renderRight() {
    if (this.props.renderRight) {
      return this.props.renderRight();
    }
    return null;
  }

  render() {
    const {style, opacity, search, styleTitle, noShadow} = this.props;
    const mainStyle = [styles.container,{width}, style, opacity ? {backgroundColor: 'transparent'} : null, !noShadow && styles.shadow];
    if (search) {
      return (
        <View >
          <View style={mainStyle}>
            <StatusBar
              barStyle={opacity ? 'default' : 'light-content'}
            />
            <View style={styles.renderLeft}>
              {this.renderLeft()}
            </View>
            <View style={styles.renderSearch}>
              {this.renderSearch()}
            </View>
          </View>
        </View>
      )
    }
    return (
      <View >
        <View style={mainStyle}>
          <StatusBar
            barStyle={opacity ? 'default' : 'light-content'}
          />
          <View style={styles.renderLeft}>
            {this.renderLeft()}
          </View>
          <View style={styles.renderCenter}>
            {this.props.title && <Text style={styles.title}>{this.props.title}</Text>}
            {this.props.children}
          </View>
          <View style={styles.renderRight}>
            {this.renderRight()}
          </View>
        </View>
      </View>
    )
  }
}