Skip to content

Commit c614dfc

Browse files
authored
Merge pull request #10503 from parasharrajat/fix-link-native
Fix the native link renderer
2 parents 2b6016d + 8ba409a commit c614dfc

File tree

6 files changed

+143
-108
lines changed

6 files changed

+143
-108
lines changed

src/components/AnchorForCommentsOnly.js

Lines changed: 0 additions & 107 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import _ from 'underscore';
2+
import React from 'react';
3+
import {StyleSheet} from 'react-native';
4+
import lodashGet from 'lodash/get';
5+
import Str from 'expensify-common/lib/str';
6+
import Text from '../Text';
7+
import PressableWithSecondaryInteraction from '../PressableWithSecondaryInteraction';
8+
import * as ReportActionContextMenu from '../../pages/home/report/ContextMenu/ReportActionContextMenu';
9+
import * as ContextMenuActions from '../../pages/home/report/ContextMenu/ContextMenuActions';
10+
import Tooltip from '../Tooltip';
11+
import canUseTouchScreen from '../../libs/canUseTouchscreen';
12+
import styles from '../../styles/styles';
13+
import withWindowDimensions from '../withWindowDimensions';
14+
import {propTypes, defaultProps} from './anchorForCommentsOnlyPropTypes';
15+
16+
/*
17+
* This is a default anchor component for regular links.
18+
*/
19+
const BaseAnchorForCommentsOnly = (props) => {
20+
let linkRef;
21+
const rest = _.omit(props, _.keys(propTypes));
22+
const linkProps = {};
23+
if (_.isFunction(props.onPress)) {
24+
linkProps.onPress = props.onPress;
25+
} else {
26+
linkProps.href = props.href;
27+
}
28+
const defaultTextStyle = canUseTouchScreen() || props.isSmallScreenWidth ? {} : styles.userSelectText;
29+
30+
return (
31+
<PressableWithSecondaryInteraction
32+
inline
33+
onSecondaryInteraction={
34+
(event) => {
35+
ReportActionContextMenu.showContextMenu(
36+
Str.isValidEmail(props.displayName) ? ContextMenuActions.CONTEXT_MENU_TYPES.EMAIL : ContextMenuActions.CONTEXT_MENU_TYPES.LINK,
37+
event,
38+
props.href,
39+
lodashGet(linkRef, 'current'),
40+
);
41+
}
42+
}
43+
>
44+
<Tooltip text={Str.isValidEmail(props.displayName) ? '' : props.href}>
45+
<Text
46+
ref={el => linkRef = el}
47+
style={StyleSheet.flatten([props.style, defaultTextStyle])}
48+
accessibilityRole="link"
49+
hrefAttrs={{
50+
rel: props.rel,
51+
target: props.target,
52+
}}
53+
// eslint-disable-next-line react/jsx-props-no-spreading
54+
{...linkProps}
55+
// eslint-disable-next-line react/jsx-props-no-spreading
56+
{...rest}
57+
>
58+
{props.children}
59+
</Text>
60+
</Tooltip>
61+
</PressableWithSecondaryInteraction>
62+
);
63+
};
64+
65+
BaseAnchorForCommentsOnly.propTypes = propTypes;
66+
BaseAnchorForCommentsOnly.defaultProps = defaultProps;
67+
BaseAnchorForCommentsOnly.displayName = 'BaseAnchorForCommentsOnly';
68+
69+
export default withWindowDimensions(BaseAnchorForCommentsOnly);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import PropTypes from 'prop-types';
2+
import stylePropTypes from '../../styles/stylePropTypes';
3+
import {windowDimensionsPropTypes} from '../withWindowDimensions';
4+
5+
const propTypes = {
6+
/** The URL to open */
7+
href: PropTypes.string,
8+
9+
/** What headers to send to the linked page (usually noopener and noreferrer)
10+
This is unused in native, but is here for parity with web */
11+
rel: PropTypes.string,
12+
13+
/** Used to determine where to open a link ("_blank" is passed for a new tab)
14+
This is unused in native, but is here for parity with web */
15+
target: PropTypes.string,
16+
17+
/** Any children to display */
18+
children: PropTypes.node,
19+
20+
/** Anchor text of URLs or emails. */
21+
displayName: PropTypes.string,
22+
23+
/** Any additional styles to apply */
24+
style: stylePropTypes,
25+
26+
/** Press handler for the link, when not passed, default href is used to create a link like behaviour */
27+
onPress: PropTypes.func,
28+
29+
...windowDimensionsPropTypes,
30+
};
31+
32+
const defaultProps = {
33+
href: '',
34+
rel: '',
35+
target: '',
36+
children: null,
37+
style: {},
38+
displayName: '',
39+
onPress: undefined,
40+
};
41+
42+
export {propTypes, defaultProps};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import * as anchorForCommentsOnlyPropTypes from './anchorForCommentsOnlyPropTypes';
3+
import BaseAnchorForCommentsOnly from './BaseAnchorForCommentsOnly';
4+
5+
// eslint-disable-next-line react/jsx-props-no-spreading
6+
const AnchorForCommentsOnly = props => <BaseAnchorForCommentsOnly {...props} />;
7+
AnchorForCommentsOnly.propTypes = anchorForCommentsOnlyPropTypes.propTypes;
8+
AnchorForCommentsOnly.defaultProps = anchorForCommentsOnlyPropTypes.defaultProps;
9+
AnchorForCommentsOnly.displayName = 'AnchorForCommentsOnly';
10+
11+
export default AnchorForCommentsOnly;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import {Linking} from 'react-native';
3+
import _ from 'underscore';
4+
5+
import * as anchorForCommentsOnlyPropTypes from './anchorForCommentsOnlyPropTypes';
6+
import BaseAnchorForCommentsOnly from './BaseAnchorForCommentsOnly';
7+
8+
// eslint-disable-next-line react/jsx-props-no-spreading
9+
const AnchorForCommentsOnly = (props) => {
10+
const onPress = () => (_.isFunction(props.onPress) ? props.onPress() : Linking.openURL(props.href));
11+
12+
// eslint-disable-next-line react/jsx-props-no-spreading
13+
return <BaseAnchorForCommentsOnly {...props} onPress={onPress} />;
14+
};
15+
16+
AnchorForCommentsOnly.propTypes = anchorForCommentsOnlyPropTypes.propTypes;
17+
AnchorForCommentsOnly.defaultProps = anchorForCommentsOnlyPropTypes.defaultProps;
18+
AnchorForCommentsOnly.displayName = 'AnchorForCommentsOnly';
19+
20+
export default AnchorForCommentsOnly;

src/components/HTMLEngineProvider/HTMLRenderers/AnchorRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const AnchorRenderer = (props) => {
8585
displayName={displayName}
8686

8787
// Only pass the press handler for internal links, for public links fallback to default link handling
88-
onPress={internalNewExpensifyPath || internalExpensifyPath ? navigateToLink : undefined}
88+
onPress={(internalNewExpensifyPath || internalExpensifyPath) ? navigateToLink : undefined}
8989
>
9090
<TNodeChildrenRenderer tnode={props.tnode} />
9191
</AnchorForCommentsOnly>

0 commit comments

Comments
 (0)