index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. import React, {Component} from 'react';
  3. import {createReactNavigationReduxMiddleware, createReduxBoundAddListener} from 'react-navigation-redux-helpers';
  4. import {connect} from 'react-redux';
  5. export const navigatorPieces = (BareNavigator, name = "root") => {
  6. const middleware = createReactNavigationReduxMiddleware(name, x => x);
  7. const addListener = createReduxBoundAddListener(name);
  8. const reducer = (state, action) => BareNavigator.router.getStateForAction(action, state);
  9. class Navigator extends Component {
  10. render () {
  11. const {dispatch, navigationState} = this.props;
  12. return <BareNavigator
  13. navigation={{dispatch, state: navigationState, addListener}}
  14. />;
  15. }
  16. }
  17. return {Navigator, reducer, middleware};
  18. };
  19. export const connectNavigator = selectorFn => connect(state => ({navigationState: selectorFn(state)}));
  20. import {NavigationActions, StackActions} from 'react-navigation';
  21. const {navigate, back} = NavigationActions;
  22. const {reset} = StackActions;
  23. export const backButtonHandler = ({dispatch, getState}) => {
  24. if (getState().index === 0) return false;
  25. dispatch(back());
  26. return true;
  27. };
  28. export const navigationHelpers = {
  29. topOfNavigationStack: ({routes}) => routes[routes.length - 1] || {},
  30. resetRoutes: routeNames => reset({
  31. actions: routeNames.map(routeName => navigate({routeName})),
  32. index: routeNames.length - 1
  33. }),
  34. isRoute: expected => ({routeName}) => routeName === expected,
  35. isTopRoute: expected => state => isRoute(expected)(topOfNavigationStack(state)),
  36. goBackFrom: routeName => ({dispatch, getState}) => {
  37. if (isTopRoute(routeName)(getState())) {
  38. dispatch(back());
  39. }
  40. },
  41. };