1

介绍

这几天一直在学习Taro框架技术,官方说是支持编译到多端,可是提供的都是H5、小程序案例,至于RN案例 网上都很少,经过一番深究,有了下文基于taro+react实现的自定义顶部导航栏、底部tabbar功能,支持编译到多端(h5 + 小程序 + react-native)

如下图:在H5/小程序/RN均测试通过
未标题-1.png

项目中用到的图标都是阿里iconfont字体图标,下载好后将fonts文件夹拷贝到项目目录下。
360截图20191126161217852.png

import './styles/fonts/iconfont.scss'
在h5、小程序下 这种写法即可: <Text className="iconfont icon-back"></Text>

不过为了兼容RN,只能通过Unicode方式这样写:<Text className="iconfont">&#xe84c;</Text>

如果是通过变量传递:let back = '\ue84c'   <Text>{back}</Text>

自定义导航条NavBar

360截图20191126121539728.png
在App.js配置navigationStyle,将设置为custom,就可以自定义导航栏

class App extends Component {
    config = {
        pages: 
            'pages/index/index',
            ...
        ],
        window: {
            backgroundTextStyle: 'light',
            navigationBarBackgroundColor: '#fff',
            navigationBarTitleText: 'Taro',
            navigationBarTextStyle: 'black',
            navigationStyle: 'custom'
        },
        ...
    }
    
    ...
}

在components目录下新建导航栏Navbar组件

/*
 * @desc   Taro自定义Navbar组件
 * @about  Q:282310962  wx:xy190310
 */

import Taro from '@tarojs/taro'
import { View, Text, Input, Image } from '@tarojs/components'
import classNames from "classnames";
import './index.scss'
 
export default class NavBar extends Taro.Component {
    // 默认配置
    static defaultProps = {
        isBack: false,
        leftIcon: '\ue84c',
        title: ' ',
        background: '#6190e8',
        color: '#fff',
        center: false,
        search: false,
        searchStyle: '',
        fixed: false,
        headerRight: [],
    }
    constructor(props) {
        super(props)
        this.state = {
            searchText: '',
        }
    }
    
    ...
 
    render() {
        const { isBack, leftIcon, title, background, color, center, search, searchStyle, fixed, height, headerRight } = this.props
        const { searchText } = this.state
        
        let weapp = false
        if (process.env.TARO_ENV === 'weapp') {
            weapp = true
        }
 
        return (
            <View className={classNames('taro__navbar', fixed && 'taro__navbar--fixed', fixed && weapp && 'taro__navbar-weapp--fixed')}>
                <View className={classNames('taro__navbar-wrap', fixed && 'taro__navbar-wrap--fixed', weapp && 'taro__navbar-wrap__weapp')} style={{backgroundColor: background}}>
                    {/* 返回 */}
                    <View className={classNames('taro__navbar-left__view', isBack && 'taro__navbar-left__view--isback')}>
                    {isBack &&
                        <TouchView activeOpacity={.5} onClick={this.handleNavigateBack}>
                            <View className="taro__navbar-icon__item"><Text className="iconfont taro__navbar-iconfont" style={{color: color}}>{leftIcon}</Text></View>
                        </TouchView>
                    }
                    </View>
                    
                    {/* 标题 */}
                    {!search && center && !weapp ? <View className="flex1" /> : null}
                    {search ? 
                    (
                        <View className="taro__navbar-search flex1">
                            <Input className="taro__navbar-search__input" placeholder="搜索..." onInput={this.updateInputText} style={{color: color, ...searchStyle}} />
                        </View>
                    )
                    :
                    (
                        <View className={classNames('taro__navbar-title flex1', center && !weapp && 'taro__navbar-title--center')}>
                            {title && <Text className="taro__navbar-title__text" style={{color: color}}>{title}</Text>}
                        </View>
                    )
                    }
 
                    {/* 右侧 */}
                    <View className="taro__navbar-right__view">
                    {headerRight.map((item, index) => (
                        <TouchView activeOpacity={.5} key={index} onClick={()=>item.onClick && item.onClick(searchText)}>
                            <View className="taro__navbar-icon__item">
                                {item.icon && <Text className="iconfont taro__navbar-iconfont" style={{color: color, ...item.style}}>{item.icon}</Text>}
                                {item.text && <Text className="taro__navbar-iconfont__text" style={{color: color, ...item.style}}>{item.text}</Text>}
                                {item.img && <Image className="taro__navbar-iconfont__img" src={item.img} mode='aspectFit' />}
                                {/* 圆点 */}
                                {!!item.badge && <Text className="taro__badge taro__navbar-badge">{item.badge}</Text>}
                                {!!item.dot && <Text className="taro__badge-dot taro__navbar-badge--dot"></Text>}
                            </View>
                        </TouchView>
                    ))
                    }
                    </View>
                </View>
            </View>
        );
    }
}

在页面引入组件即可:import NavBar from '@components/navbar'

支持自定义背景、颜色、左侧图标、标题居中、搜索框,右侧按钮支持图标/文字/图片,还可以设置样式,红点提示、事件处理

360截图20191126165009489.png

<NavBar title='Taro标题栏' fixed
    headerRight={[
        {icon: '\ue614', style: {color: '#e93b3d'}},
        {img: require('../../assets/taro.png'), dot: true, onClick: this.handleCallback},
        {icon: '\ue600', style: {marginRight: 10}},
    ]} 
/>

360截图20191126165041107.png
360截图20191126165115123.png

<NavBar isBack leftIcon={'\ue69f'} title='搜索栏' background='#42b983' color='#fcc' search
    searchStyle={{
        backgroundColor:'rgba(255,255,255,.6)', borderRadius: Taro.pxTransform(50), color: '#333'
    }}
    headerRight={[
        {icon: '\ue622', style: {color: '#6afff9'}},
        {icon: '\ue63a'},
    ]} 
/>

360截图20191126165139905.png

自定义Tabbar菜单

自定义tabbar也支持自定义背景、颜色、图标,点击选项事件返回索引值
未标题-2.png

import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import classNames from 'classnames'
import './index.scss'
 
export default class TabBar extends Taro.Component {
    // 默认参数配置
    static defaultProps = {
        current: 0,
        background: '#fff',
        color: '#999',
        tintColor: '#6190e8',
        fixed: false,
        onClick: () => {},
        tabList: []
    }
    constructor(props) {
        super(props)
        this.state = {
            updateCurrent: props.current
        }
    }
    ...
 
    render() {
        const { background, color, tintColor, fixed } = this.props
        const { updateCurrent } = this.state
        
        return (
            <View className={classNames('taro__tabbar', fixed && 'taro__tabbar--fixed')}>
                <View className={classNames('taro__tabbar-list', fixed && 'taro__tabbar-list--fixed')} style={{backgroundColor: background}}>
                    {this.props.tabList.map((item, index) => (
                        <View className="taro__tabbar-item taro__tabbar-item--active" key={index} onClick={this.updateTabbar.bind(this, index)}>
                            <View className="taro__tabbar-icon">
                                <Text className="iconfont taro__tabbar-iconfont" style={{color: updateCurrent == index ? tintColor : color}}>{item.icon}</Text>
                                {/* 圆点 */}
                                {!!item.badge && <Text className="taro__badge taro__tabbar-badge">{item.badge}</Text>}
                                {!!item.dot && <Text className="taro__badge-dot taro__tabbar-badge--dot"></Text>}
                            </View>
                            <Text className="taro__tabbar-title" style={{color: updateCurrent == index ? tintColor : color}}>{item.title}</Text>
                        </View>
                    ))}
                </View>
            </View>
        );
    }
}
<TabBar current={currentTabIndex} background='#f8f8f8' color='#999' tintColor='#6190e8' fixed onClick={this.handleTabbar}
    tabList={[
        {icon: '\ue627', title: '首页', badge: 8},
        {icon: '\ue61e', title: '商品'},
        {icon: '\ue605', title: '个人中心', dot: true},
    ]}
/>

到这里就基本介绍完了,大家在使用Taro开发多端项目的时候,需多注意RN下样式问题。后续也会继续分享一些taro实例。😶😶
ReactNative聊天APP实战|仿微信聊天/朋友圈/红包界面

360截图20190807103937042.jpg


xiaoyan2017
765 声望314 粉丝

web前端开发爱好者,专注于前端h5、jquery、vue、react、angular等技术研发实战项目案例。