頂部導(dǎo)航組件TabBar

頂部導(dǎo)航組件


IMG_3827.PNG

調(diào)用示例:

<TabBar activeKey={this.state.activeKey}
onChange={this.tabChange.bind(this)}> //點擊事件里要更改stata.activeKey來觸發(fā)組件更新
  <TabBar.Item title={`頁面1`} tabKey={'page1'}>
  //tab頁1
  </TabBar.Item>
<TabBar.Item title={`頁面2`} tabKey={'page2'}>
  //tab頁2
</TabBar.Item>
</TabBar>

選擇事件:

tabChange({next: tabKey}){
  this.setState({activeKey: tabKey});
}

具體實現(xiàn)

export default class TabBar extends PureComponent {
  static Item = Item //子組件
  constructor(props) {
    super(props);
    //存儲tab頁id以及坐標,用于實現(xiàn)動畫觸發(fā)
    const map = new Map();
    props.children.forEach((child, index) => {
      //默認頁面寬度為750,tabbar長度為80
      const length = 750 / props.children.length;
      const position = index * length + length / 2 - 42
      map.set(child.props.tabKey, position);
    })
    this.state = {
      map
    }
  }

  componentDidMount() {
    const left_new = this.state.map.get(this.props.activeKey)
    const dom = this.refs.border
    //初始動畫渲染
    doTransition(dom, {transform: `translateX(${left_new + 80})`})
  }

  changeSelect(key) {//選擇事件
    //獲取點擊tab頁的坐標
    const left_new = this.state.map.get(key)
    const dom = this.refs.border
    //平移底部border
    doTransition(dom, {transform: `translateX(${left_new + 80})`}, null, () => {
    //回調(diào)
      this.props.onChange && this.props.onChange({next: key})
    })
  }

  getChild() {//獲取子tab頁
    //根據(jù)tab頁id獲取頁面內(nèi)容
    let child = this.props.children.find(child => {
    return child.props.tabKey == this.props.activeKey
  })



}

頁面內(nèi)容渲染

render(){
  let child = this.getChild()
  return (
    <View style={[style.container,
      this.props.full ? {flex: 1} : '',
      this.props.style ? this.props.style : '']}>
      <View style={style.borderContainer}>
        <View style={style.body}>
          //渲染導(dǎo)航
          {this.props.children.map(obj => {
            const flag = obj.props.tabKey == this.props.activeKey
            return (
              <TouchableHighlight 
                onPress={this.changeSelect.bind(this, obj.props.tabKey)} 
                style={style.tab}>
              <View style={{flex: 1}}/>//平分頁面占比
              <View style={{flexDirection: "row"}}>
                <Text style={flag ?
                  style.tabTextFocus 
                  : 
                  style.tabText}>{obj.props.title}
                  </Text>
                {obj.props.nextTitle ? 
                  <Text style={obj.props.nextTitle === "(有)" ?
                  style.yesText 
                    : 
                    style.noText}>{obj.props.nextTitle}</Text> 
                  : null}
              </View>
              <View style={style.num}>
                <Text style={flag ?
                  style.numFocus : style.numText}>
                  {!obj.props.total || 
                  (obj.props.total == 0 && !this.props.showZero) ? 
                    '' :
                    obj.props.total > 9999 ? 
                      `(9999+)` : `(${obj.props.total})`}
                </Text>
              </View>
            </TouchableHighlight>)
          })}
        </View>
        //底部border
        <View style={style.bottom}>
          <View ref={`border`} style={[style.bottom_border]}/>
        </View>
      </View>
      //渲染根據(jù)tabkey得到的子組件
      {child}
    </View>)

}

子組件實現(xiàn):

import {createElement, PureComponent, render} from 'rax';
import {View, Text} from 'nuke';
import style from "./style.less"
export default class TabBarItem extends PureComponent{
    constructor(props){
        super(props);
    }
    render(){
        return(
            <View style={style.children}>
                {this.props.children}
            </View>
        )
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末厕妖,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子耙替,更是在濱河造成了極大的恐慌,老刑警劉巖沐寺,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件林艘,死亡現(xiàn)場離奇詭異,居然都是意外死亡混坞,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門钢坦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來究孕,“玉大人,你說我怎么就攤上這事爹凹〕睿” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵禾酱,是天一觀的道長微酬。 經(jīng)常有香客問我,道長颤陶,這世上最難降的妖魔是什么颗管? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮滓走,結(jié)果婚禮上垦江,老公的妹妹穿的比我還像新娘。我一直安慰自己搅方,他們只是感情好比吭,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布绽族。 她就那樣靜靜地躺著,像睡著了一般衩藤。 火紅的嫁衣襯著肌膚如雪吧慢。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天赏表,我揣著相機與錄音娄蔼,去河邊找鬼。 笑死底哗,一個胖子當著我的面吹牛岁诉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播跋选,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涕癣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了前标?” 一聲冷哼從身側(cè)響起坠韩,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎炼列,沒想到半個月后只搁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡俭尖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年氢惋,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片稽犁。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡焰望,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出已亥,到底是詐尸還是另有隱情熊赖,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布虑椎,位于F島的核電站震鹉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏捆姜。R本人自食惡果不足惜传趾,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望娇未。 院中可真熱鬧墨缘,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蝶棋,卻和暖如春卸亮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背玩裙。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工兼贸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人吃溅。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓溶诞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親决侈。 傳聞我的和親對象是個殘疾皇子螺垢,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354