All files / src CardStack.js

69.81% Statements 37/53
50% Branches 12/24
71.43% Functions 10/14
76.09% Lines 35/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                                                13x 13x 13x 13x       13x 13x           13x         13x       13x 13x 13x                 11x 11x 11x 11x 11x 11x           11x         11x         10x 10x 10x 10x 13x 10x 10x                                                                   1x 1x 1x           11x             24x 33x 24x 24x       24x                  
/* @flow */
 
import * as React from 'react'
import { BackHandler } from 'react-native'
import { matchPath, type RouterHistory } from 'react-router'
import invariant from 'invariant'
import HistoryUtils from './HistoryUtils'
import StackUtils from './StackUtils'
import RouteUtils from './RouteUtils'
import StateUtils from './StateUtils'
import type {
  Route,
  CardsRendererProps,
  NavigationState,
  Card,
} from './TypeDefinitions'
 
type Props = {
  history: RouterHistory,
  children: Array<React$Node>,
  render: (props: CardsRendererProps) => React$Element<*>,
}
 
type State = {|
  cards: Array<Card>,
  navigationState: NavigationState<>,
|}
 
class CardStack extends React.Component<Props, State> {
  unlistenHistory: ?Function = null
 
  constructor(props: Props) {
    super(props)
    const { children, history } = props
    const { entries, location } = history
    invariant(
      children || React.Children.count(children) > 0,
      'A <CardStack /> must have child elements',
    )
    const cards = StackUtils.create(children, props)
    const navigationState = StateUtils.initialize(
      cards,
      location,
      entries,
      'entries',
    )
    invariant(
      navigationState.index !== -1,
      'There is no route defined for path « %s »',
      location.pathname,
    )
    this.state = { cards, navigationState }
  }
 
  componentDidMount() {
    const { history } = this.props
    this.unlistenHistory = HistoryUtils.listen(history, this.onHistoryChange)
    BackHandler.addEventListener('hardwareBackPress', this.onNavigateBack)
  }
 
  componentWillUnmount() {
    if (this.unlistenHistory) this.unlistenHistory()
    BackHandler.removeEventListener('hardwareBackPress', this.onNavigateBack)
  }
 
  componentWillReceiveProps(nextProps: Props) {
    const { children: nextChildren, history } = nextProps
    const { cards } = this.state
    const nextCards = StackUtils.create(nextChildren, nextProps)
    Eif (nextCards && !StackUtils.shallowEqual(cards, nextCards)) {
      const { location, entries } = history
      const nextNavigationState = StateUtils.initialize(
        nextCards,
        location,
        entries,
        'entries',
      )
      invariant(
        nextNavigationState.index !== -1,
        'There is no route defined for path « %s »',
        location.pathname,
      )
      this.setState({ cards: nextCards, navigationState: nextNavigationState })
    }
  }
 
  onHistoryChange = (history: RouterHistory, nextHistory: RouterHistory) => {
    const { index } = history
    const { location, action, index: nextIndex } = nextHistory
    const { navigationState, cards } = this.state
    const currentRoute = navigationState.routes[navigationState.index]
    const nextCard = cards.find(card => matchPath(location.pathname, card))
    const nextRoute = nextCard && RouteUtils.create(nextCard, location)
    Iif (nextRoute && !RouteUtils.equal(currentRoute, nextRoute)) {
      switch (action) {
        case 'PUSH': {
          this.setState(prevState => ({
            navigationState: StateUtils.push(
              prevState.navigationState,
              nextRoute,
            ),
          }))
          break
        }
        case 'POP': {
          const n = index - nextIndex
          this.setState(prevState => ({
            navigationState: StateUtils.pop(prevState.navigationState, n),
          }))
          break
        }
        case 'REPLACE': {
          this.setState(prevState => ({
            navigationState: StateUtils.replace(
              prevState.navigationState,
              prevState.navigationState.index,
              nextRoute,
            ),
          }))
          break
        }
        default:
      }
    }
  }
 
  onNavigateBack = () => {
    Eif (this.state.navigationState.index > 0) {
      this.props.history.goBack()
      return true
    }
    return false
  }
 
  shouldComponentUpdate(nextProps: Props, nextState: State) {
    return (
      this.state.cards !== nextState.cards ||
      this.state.navigationState !== nextState.navigationState
    )
  }
 
  renderCard = (route: Route) => {
    const children = React.Children.toArray(this.props.children)
    const child = children.find(({ props }) => props.path === route.routeName)
    Iif (!child) return null
    return React.cloneElement(child, route)
  }
 
  render() {
    return this.props.render({
      ...this.state,
      onNavigateBack: this.onNavigateBack,
      renderCard: this.renderCard,
    })
  }
}
 
export default CardStack