1 | import { getPathFromState, NavigationContainerRefContext, NavigationHelpersContext } from '@react-navigation/core';
|
2 | import * as React from 'react';
|
3 | import { Platform } from 'react-native';
|
4 | import LinkingContext from './LinkingContext';
|
5 | import useLinkTo from './useLinkTo';
|
6 |
|
7 | const getStateFromParams = params => {
|
8 | if (params !== null && params !== void 0 && params.state) {
|
9 | return params.state;
|
10 | }
|
11 |
|
12 | if (params !== null && params !== void 0 && params.screen) {
|
13 | return {
|
14 | routes: [{
|
15 | name: params.screen,
|
16 | params: params.params,
|
17 |
|
18 | state: params.screen ? getStateFromParams(params.params) : undefined
|
19 | }]
|
20 | };
|
21 | }
|
22 |
|
23 | return undefined;
|
24 | };
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export default function useLinkProps(_ref) {
|
34 | var _options$getPathFromS;
|
35 |
|
36 | let {
|
37 | to,
|
38 | action
|
39 | } = _ref;
|
40 | const root = React.useContext(NavigationContainerRefContext);
|
41 | const navigation = React.useContext(NavigationHelpersContext);
|
42 | const {
|
43 | options
|
44 | } = React.useContext(LinkingContext);
|
45 | const linkTo = useLinkTo();
|
46 |
|
47 | const onPress = e => {
|
48 | var _e$currentTarget;
|
49 |
|
50 | let shouldHandle = false;
|
51 |
|
52 | if (Platform.OS !== 'web' || !e) {
|
53 | shouldHandle = e ? !e.defaultPrevented : true;
|
54 | } else if (!e.defaultPrevented &&
|
55 |
|
56 | !(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && (
|
57 |
|
58 | e.button == null || e.button === 0) &&
|
59 |
|
60 | [undefined, null, '', 'self'].includes((_e$currentTarget = e.currentTarget) === null || _e$currentTarget === void 0 ? void 0 : _e$currentTarget.target)
|
61 | ) {
|
62 | e.preventDefault();
|
63 | shouldHandle = true;
|
64 | }
|
65 |
|
66 | if (shouldHandle) {
|
67 | if (action) {
|
68 | if (navigation) {
|
69 | navigation.dispatch(action);
|
70 | } else if (root) {
|
71 | root.dispatch(action);
|
72 | } else {
|
73 | throw new Error("Couldn't find a navigation object. Is your component inside NavigationContainer?");
|
74 | }
|
75 | } else {
|
76 | linkTo(to);
|
77 | }
|
78 | }
|
79 | };
|
80 |
|
81 | const getPathFromStateHelper = (_options$getPathFromS = options === null || options === void 0 ? void 0 : options.getPathFromState) !== null && _options$getPathFromS !== void 0 ? _options$getPathFromS : getPathFromState;
|
82 | const href = typeof to === 'string' ? to : getPathFromStateHelper({
|
83 | routes: [{
|
84 | name: to.screen,
|
85 |
|
86 | params: to.params,
|
87 |
|
88 | state: getStateFromParams(to.params)
|
89 | }]
|
90 | }, options === null || options === void 0 ? void 0 : options.config);
|
91 | return {
|
92 | href,
|
93 | accessibilityRole: 'link',
|
94 | onPress
|
95 | };
|
96 | }
|
97 |
|
\ | No newline at end of file |