App.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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} onClick={() => {
  61. this.props.history.push({
  62. pathname: '/home'
  63. })
  64. this.setState({
  65. selectedTab: 'home'
  66. })
  67. }}>
  68. {
  69. selectedTab === 'home' ?
  70. <HomeSelectedIcon/>
  71. :
  72. <HomeUnselectedIcon/>
  73. }
  74. <div className='tabbar-title'>
  75. 主页
  76. </div>
  77. </Col>
  78. </NavLink>
  79. <NavLink to="/cart">
  80. <Col className={this.chooseClassNames('cart')} span={8} onClick={() => {
  81. this.props.history.push({
  82. pathname: '/cart'
  83. })
  84. this.setState({
  85. selectedTab: 'cart'
  86. })
  87. }}>
  88. {
  89. selectedTab === 'cart' ?
  90. <CartSelectedIcon/>
  91. :
  92. <CartUnselectedIcon/>
  93. }
  94. <div className='tabbar-title'>
  95. 购物篮
  96. </div>
  97. </Col>
  98. </NavLink>
  99. <NavLink to="/my">
  100. <Col className={this.chooseClassNames('my')} span={8} onClick={() => {
  101. this.props.history.push({
  102. pathname: '/my'
  103. })
  104. this.setState({
  105. selectedTab: 'my'
  106. })
  107. }}>
  108. {
  109. selectedTab === 'my' ?
  110. <MySelectedIcon/>
  111. :
  112. <MyUnselectedIcon/>
  113. }
  114. <div className='tabbar-title'>
  115. </div>
  116. </Col>
  117. </NavLink>
  118. </Row>
  119. </div>
  120. <Switch>
  121. <Route exact path="/" component={Home}/>
  122. <Route path="/home" component={Home}/>
  123. <Route path="/cart" component={Cart}/>
  124. <Route path="/my" component={My}/>
  125. </Switch>
  126. </div>
  127. )
  128. }
  129. }
  130. export default withRouter(App)
  131. const HomeUnselectedIcon = () => (
  132. <Icon type="home" style={{fontSize: 22}}/>
  133. )
  134. const HomeSelectedIcon = () => (
  135. <Icon type="home" theme="twoTone" style={{fontSize: 22}}/>
  136. )
  137. const CartUnselectedIcon = () => (
  138. <Icon type="shopping" style={{fontSize: 22}}/>
  139. )
  140. const CartSelectedIcon = () => (
  141. <Icon type="shopping" theme="twoTone" style={{fontSize: 22}}/>
  142. )
  143. const MyUnselectedIcon = () => (
  144. <Icon type="setting" style={{fontSize: 22}}/>
  145. )
  146. const MySelectedIcon = () => (
  147. <Icon type="setting" theme="twoTone" style={{fontSize: 22}}/>
  148. )