{"size": 855, "ext": "js", "max_stars_repo_path": "src/makeReactRouterFader.js", "max_stars_repo_name": "jcoreio/react-router-fader", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/makeReactRouterFader.js", "max_issues_repo_name": "jcoreio/react-router-fader", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/makeReactRouterFader.js", "max_forks_repo_name": "jcoreio/react-router-fader", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.75, "max_line_length": 80, "alphanum_fraction": 0.6865497076}
// @flow

import * as React from 'react'

export type Route = {
  path?: string,
}

export type RouteProps = {
  children?: React.Element<any>,
  route: Route,
  routes: Array<Route>,
}

function getKey({route, routes}: RouteProps): ?string {
  let routeFound = false
  for (let otherRoute of routes) {
    if (otherRoute === route) routeFound = true
    else if (routeFound && otherRoute.path) return otherRoute.path
  }
  return undefined
}

export default function makeReactRouterFader<FaderProps: Object>(
  Fader: React.ComponentType<FaderProps>,
  extraProps?: $Shape<FaderProps> = {}
): React.ComponentType<RouteProps> {
  const ReactRouterFader = ({children, route, routes}: RouteProps) => (
    <Fader {...extraProps}>
      {children && React.cloneElement(children, {key: getKey({route, routes})})}
    </Fader>
  )
  return ReactRouterFader
}

