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