自定义顶部导航栏-搜索、菜单功能

react native 自定义顶部导航菜单


在用rn开发过程中,需要统一定制顶部导航栏,由于每个页面都要写,避免麻烦,因此执自行封装了该导航工具。详细代码如下
使用方式:
在页面中引用该组件,并通过以下方式调用:
<TopView
title={‘标题’}
rightIcon={‘search2’} //搜索按钮的字体图标
rightMenueIcon={‘menu’} //菜单字体图标
search={(text)=>{}} //点击搜索功能后回调
menueOnPress={()=>{}}>//菜单点击回调

‘use strict’
import {
View,
Text,
StyleSheet,
TouchableOpacity,
TextInput
} from ‘react-native’;

import React, { Component } from ‘react’;
import {VectorIcon,Actions} from ‘c2-mobile’

class TopView extends Component {

self = this;

constructor(props) {
    super(props);

    this._onPressedRightIcon = this._onPressedRightIcon.bind(this);
    this._onPressedRightMenueIcon = this._onPressedRightMenueIcon.bind(this);
    this._onPressedSearch = this._onPressedSearch.bind(this);

    this.state = {
        title:this.props.title ? this.props.title : '',
        hideBack:this.props.hideBack ? this.props.hideBack : false,
        leftIcon:this.props.leftIcon ? this.props.leftIcon : 'angle_left',
        rightIcon:this.props.rightIcon ? this.props.rightIcon : '',
        rightText:this.props.rightText ? this.props.rightText : '',
        rightColor:this.props.rightColor ? this.props.rightColor : '#3D3D3D',
        color:this.props.color ? this.props.color : '#3D3D3D',
        rightOnPressed:this.props.rightOnPressed ? this.props.rightOnPressed : ()=>{},
        showSearchView:this.props.showSearchView ? this.props.showSearchView : false,
        search:this.props.search ? this.props.search : ()=>{},
        bottomView:this.props.bottom ? this.props.bottom : (<View></View>),
        hideLine: this.props.hideLine ? this.props.hideLine : false,
        searchInput:'',
        rightMenueIcon:this.props.rightMenueIcon ? this.props.rightMenueIcon : ''
    }
}

_onPressedSearch(){
    this.setState({
        showSearchView:false
    });
    if(this.props.search){
        this.props.search(this.state.searchInput);
    }
}

_onPressedRightIcon(){
    this.setState({
        showSearchView:true
    });
}

_onPressedRightMenueIcon(){
    if(this.props.menueOnPress){
        this.props.menueOnPress();
    }
}

renderSearchView(){
    return (
        <View style={styles.searchView}>
        {/* selectionColor='#E0E0E0' */}
            <TextInput 
                style={{flex:1,color:'#E0E0E0',padding:0}}
                underlineColorAndroid='transparent'
                defaultValue={this.state.searchInput}
                placeholder ={'请输入搜索条件'}
                onChangeText={(text)=> this.setState({searchInput:text})}>
            </TextInput>
            <TouchableOpacity onPress={this._onPressedSearch.bind(this)}  style={{paddingLeft:10,paddingRight:10}}>
                <VectorIcon name={'android-search'} style={{ fontSize: 25, color:'#E0E0E0',marginLeft:10}} />
            </TouchableOpacity>
        </View>
    );
}

renderRightView(){
    return (
        <View style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}>
                    <Text style={{ flex: 1, textAlign: 'center', fontSize: 18, color: this.state.color, marginRight: 10, }} numberOfLines={1}>{this.state.title}</Text>
                    {this.state.rightIcon == '' ? (<View></View>) : 
                        <TouchableOpacity onPress={this._onPressedRightIcon.bind(this)} style={{paddingLeft:10,paddingRight:10}}>
                            <VectorIcon name={this.state.rightIcon} style={{ fontSize: 25, color:this.state.rightColor}} />
                        </TouchableOpacity>
                    }
                    {
                        this.state.rightMenueIcon ? (
                            <TouchableOpacity onPress={this._onPressedRightMenueIcon.bind(this)} style={{paddingLeft:10,paddingRight:10}}>
                                <VectorIcon name={this.state.rightMenueIcon} style={{ fontSize: 25, color:this.state.rightColor}} />
                            </TouchableOpacity>
                        ) : null
                    }
                    {this.state.rightText == '' ? (<View></View>) : 
                        <TouchableOpacity style={{paddingLeft:10,paddingRight:10}}><Text style={{ fontSize: 18, color: this.state.color }} >{this.state.rightText}</Text></TouchableOpacity>
                    }
                    </View>
    );
}

render() {
    return (
        <View style={{backgroundColor: '#fff',zIndex:999}}>
                            {/* 头部标题 */}
            <View style={styles.topView}>
                <View style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}>
                    {this.state.hideBack ? null : (
                    <TouchableOpacity onPress={()=> Actions.pop()} style={{padding:5}}>
                        <VectorIcon name={this.state.leftIcon} style={{ fontSize: 25, color: '#E0E0E0' }} />
                    </TouchableOpacity>)}
                    {this.state.showSearchView ? 
                    this.renderSearchView(): this.renderRightView()
                    }
                </View>
            </View>
            {this.state.bottomView}
            {this.state.hideLine ? null : <View style={{height:1,backgroundColor:'#dedede'}}></View>}
            
        </View>
    )
}

}

let styles = StyleSheet.create({
topView: {
height: 60,
backgroundColor: ‘#fff’,
flexDirection: ‘row’,
paddingLeft: 20,
paddingRight: 20,
paddingTop: 10,
paddingBottom: 10,
alignSelf: ‘center’
},
searchView:{
flex:1,
flexDirection:‘row’,
paddingLeft:10,
// paddingRight:10,
paddingTop:7,
paddingBottom:7,
backgroundColor:’#fff’,
justifyContent:‘center’,
alignItems:‘center’,
borderColor:’#E0E0E0’,
alignSelf:‘center’,
marginLeft:20,
marginRight:10,
borderRadius:3,
borderWidth:1
}
})

module.exports = TopView;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值