all files / modules/ RoutingContext.js

100% Statements 85/85
82.98% Branches 39/47
100% Functions 12/12
100% Lines 26/26
3 statements, 10 branches Ignored     
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                                                                86×       104×         86×   86× 86× 168× 65×   103× 103× 103×                 103× 102×     103×           102×       86×         86×              
import invariant from 'invariant'
import React, { Component } from 'react'
import { isReactChildren } from './RouteUtils'
import getRouteParams from './getRouteParams'
 
const { array, func, object } = React.PropTypes
 
/**
 * A <RoutingContext> renders the component tree for a given router state
 * and sets the history object and the current location in context.
 */
class RoutingContext extends Component {
 
  static propTypes = {
    history: object.isRequired,
    createElement: func.isRequired,
    location: object.isRequired,
    routes: array.isRequired,
    params: object.isRequired,
    components: array.isRequired
  }
 
  static defaultProps = {
    createElement: React.createElement
  }
 
  static childContextTypes = {
    history: object.isRequired,
    location: object.isRequired
  }
 
  getChildContext() {
    const { history, location } = this.props
    return { history, location }
  }
 
  createElement(component, props) {
    return component == null ? null : this.props.createElement(component, props)
  }
 
  render() {
    const { history, location, routes, params, components } = this.props
    let element = null
 
    Eif (components) {
      element = components.reduceRight((element, components, index) => {
        if (components == null)
          return element // Don't create new children; use the grandchildren.
 
        const route = routes[index]
        const routeParams = getRouteParams(route, params)
        const props = {
          history,
          location,
          params,
          route,
          routeParams,
          routes
        }
 
        if (isReactChildren(element)) {
          props.children = element
        } else Eif (element) {
          for (let prop in element)
            Eif (element.hasOwnProperty(prop))
              props[prop] = element[prop]
        }
 
        if (typeof components === 'object') {
          const elements = {}
 
          for (let key in components)
            Eif (components.hasOwnProperty(key))
              elements[key] = this.createElement(components[key], props)
 
          return elements
        }
 
        return this.createElement(components, props)
      }, element)
    }
 
    invariant(
      element === null || element === false || React.isValidElement(element),
      'The root route must render a single element'
    )
 
    return element
  }
 
}
 
export default RoutingContext