index.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Component } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import { CoverView, View } from '@tarojs/components'
  4. import './index.less'
  5. export default class Index extends Component {
  6. state = {
  7. selected: 0,
  8. color: '#999999',
  9. selectedColor: '#ffffff',
  10. backgroundColor: '#000000',
  11. fontSize: 22,
  12. list: [
  13. {
  14. pagePath: '/pages/category/index',
  15. text: '首页'
  16. },
  17. {
  18. pagePath: '/pages/home/home',
  19. text: '我的'
  20. }
  21. ]
  22. }
  23. switchTab(index, url) {
  24. this.setSelected(index)
  25. Taro.switchTab({ url })
  26. }
  27. setSelected (idx: number) {
  28. this.setState({
  29. selected: idx
  30. })
  31. }
  32. render() {
  33. const { list, selected, color, selectedColor, backgroundColor, fontSize } = this.state
  34. return (
  35. <CoverView className='tab-bar' style={{ backgroundColor: backgroundColor}}>
  36. <CoverView className='tab-bar-border'></CoverView>
  37. {list.map((item, index) => {
  38. return (
  39. <CoverView key={index} className='tab-bar-item' onClick={this.switchTab.bind(this, index, item.pagePath)}>
  40. <CoverView style={{ color: selected === index ? selectedColor : color, fontSize: fontSize }}>{item.text}</CoverView>
  41. <View className='line' style={{ backgroundColor: selected === index ? 'white' : 'transparent' }}></View>
  42. </CoverView>
  43. )
  44. })}
  45. </CoverView>
  46. )
  47. }
  48. }