|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { withNavigation, NavigationActions } from '@react-navigation/core'; |
| 3 | + |
| 4 | +const queryString = require('query-string'); |
| 5 | + |
| 6 | +const getTopNavigation = navigation => { |
| 7 | + const parent = navigation.dangerouslyGetParent(); |
| 8 | + if (parent) { |
| 9 | + return getTopNavigation(parent); |
| 10 | + } |
| 11 | + return navigation; |
| 12 | +}; |
| 13 | + |
| 14 | +class LinkWithNavigation extends Component { |
| 15 | + render() { |
| 16 | + const { |
| 17 | + children, |
| 18 | + params, |
| 19 | + routeName, |
| 20 | + routeKey, |
| 21 | + navigation, |
| 22 | + action, |
| 23 | + } = this.props; |
| 24 | + const topNavigation = getTopNavigation(navigation); |
| 25 | + const topRouter = topNavigation.router; |
| 26 | + const navAction = |
| 27 | + action || |
| 28 | + NavigationActions.navigate({ |
| 29 | + routeName, |
| 30 | + key: routeKey, |
| 31 | + params, |
| 32 | + }); |
| 33 | + if (!action && !routeName && !routeKey) { |
| 34 | + throw new Error( |
| 35 | + 'Must provide a routeName, routeKey, or a navigation action prop to <Link>' |
| 36 | + ); |
| 37 | + } |
| 38 | + if (action && routeKey) { |
| 39 | + throw new Error( |
| 40 | + 'Cannot specify a conflicting "routeKey" and a navigation "action" prop. Either use routeName with routeKey to specify a navigate action, or provide the specific navigation "action" prop.' |
| 41 | + ); |
| 42 | + } |
| 43 | + if (action && routeName) { |
| 44 | + throw new Error( |
| 45 | + 'Cannot specify a conflicting "routeName" and a navigation "action" prop. Either use routeName with routeKey to specify a navigate action, or provide the specific navigation "action" prop.' |
| 46 | + ); |
| 47 | + } |
| 48 | + const navActionResponse = topRouter.getStateForAction( |
| 49 | + navAction, |
| 50 | + topNavigation.state |
| 51 | + ); |
| 52 | + const nextState = |
| 53 | + navActionResponse === null ? topNavigation.state : navActionResponse; |
| 54 | + const pathAndParams = topRouter.getPathAndParamsForState(nextState); |
| 55 | + const href = Object.keys(pathAndParams.params).length |
| 56 | + ? `/${pathAndParams.path}?${queryString.stringify(pathAndParams.params)}` |
| 57 | + : `/${pathAndParams.path}`; |
| 58 | + return ( |
| 59 | + <a |
| 60 | + href={href} |
| 61 | + onClick={e => { |
| 62 | + navigation.dispatch(navAction); |
| 63 | + e.preventDefault(); |
| 64 | + }} |
| 65 | + > |
| 66 | + {children} |
| 67 | + </a> |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | +const Link = withNavigation(LinkWithNavigation); |
| 72 | + |
| 73 | +export default Link; |
0 commit comments