App.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React, {Component} from 'react'
  2. import {Row, Col, Icon} from 'antd'
  3. import {Switch, Route, NavLink, withRouter} from 'react-router-dom'
  4. import classnames from 'classnames'
  5. import Home from './pages/home'
  6. import Cart from './pages/cart'
  7. import My from './pages/my'
  8. import './app.css'
  9. class App extends Component {
  10. constructor(props) {
  11. super(props)
  12. this.state = {
  13. selectedTab: 'home',
  14. tabHidden: false,
  15. page: 'detail'
  16. }
  17. }
  18. componentWillMount() {
  19. let {location} = this.props,
  20. pathname = location.pathname
  21. // 根据首次的 pathname 显示 icon 选中
  22. console.log(pathname)
  23. if (location && pathname) {
  24. this.setState({
  25. selectedTab: pathname.substr(1) === '' ? 'home' : pathname.substr(1)
  26. })
  27. }
  28. }
  29. componentWillReceiveProps(next) {
  30. let {location} = next,
  31. pathname = location.pathname,
  32. state = location.state
  33. // 有 state 的话,就隐藏 tabbar
  34. if (location && state) {
  35. this.setState({
  36. tabHidden: true
  37. })
  38. }
  39. // 根据非首次的 pathname 显示 icon 选中
  40. if (location && pathname) {
  41. this.setState({
  42. selectedTab: pathname.substr(1) === '' ? 'home' : pathname.substr(1)
  43. })
  44. }
  45. }
  46. chooseClassNames = (tabbar) => (
  47. classnames(
  48. {'tabbar-content': true},
  49. {'tabbar-inactive': this.state.selectedTab !== tabbar},
  50. {'tabbar-active': this.state.selectedTab === tabbar}
  51. )
  52. )
  53. render() {
  54. let {selectedTab, tabHidden} = this.state
  55. return (
  56. <div>
  57. <div className={classnames('tabbar', {'tarbar-hidden': tabHidden})}>
  58. <Row>
  59. <NavLink exact to="/">
  60. <Col className={this.chooseClassNames('home')} span={8}>
  61. {
  62. selectedTab === 'home' ?
  63. <HomeSelectedIcon/>
  64. :
  65. <HomeUnselectedIcon/>
  66. }
  67. <div className='tabbar-title'>
  68. 主页
  69. </div>
  70. </Col>
  71. </NavLink>
  72. <NavLink to="/cart">
  73. <Col className={this.chooseClassNames('cart')} span={8}>
  74. {
  75. selectedTab === 'cart' ?
  76. <CartSelectedIcon/>
  77. :
  78. <CartUnselectedIcon/>
  79. }
  80. <div className='tabbar-title'>
  81. 购物篮
  82. </div>
  83. </Col>
  84. </NavLink>
  85. <NavLink to="/my">
  86. <Col className={this.chooseClassNames('my')} span={8}>
  87. {
  88. selectedTab === 'my' ?
  89. <MySelectedIcon/>
  90. :
  91. <MyUnselectedIcon/>
  92. }
  93. <div className='tabbar-title'>
  94. </div>
  95. </Col>
  96. </NavLink>
  97. </Row>
  98. </div>
  99. <Switch>
  100. <Route exact path="/" component={Home}/>
  101. <Route path="/home" component={Home}/>
  102. <Route path="/cart" component={Cart}/>
  103. <Route path="/my" component={My}/>
  104. </Switch>
  105. </div>
  106. )
  107. }
  108. }
  109. export default withRouter(App)
  110. const HomeUnselectedIcon = () => (
  111. <Icon type="home" style={{fontSize: 22}}/>
  112. )
  113. const HomeSelectedIcon = () => (
  114. <Icon type="home" theme="twoTone" style={{fontSize: 22}}/>
  115. )
  116. const CartUnselectedIcon = () => (
  117. <Icon type="shopping" style={{fontSize: 22}}/>
  118. )
  119. const CartSelectedIcon = () => (
  120. <Icon type="shopping" theme="twoTone" style={{fontSize: 22}}/>
  121. )
  122. const MyUnselectedIcon = () => (
  123. <Icon type="setting" style={{fontSize: 22}}/>
  124. )
  125. const MySelectedIcon = () => (
  126. <Icon type="setting" theme="twoTone" style={{fontSize: 22}}/>
  127. )