Skip to content

Commit

Permalink
Merge pull request #10503 from parasharrajat/fix-link-native
Browse files Browse the repository at this point in the history
Fix the native link renderer
  • Loading branch information
marcaaron authored Sep 13, 2022
2 parents 2b6016d + 8ba409a commit c614dfc
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 108 deletions.
107 changes: 0 additions & 107 deletions src/components/AnchorForCommentsOnly.js

This file was deleted.

69 changes: 69 additions & 0 deletions src/components/AnchorForCommentsOnly/BaseAnchorForCommentsOnly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import _ from 'underscore';
import React from 'react';
import {StyleSheet} from 'react-native';
import lodashGet from 'lodash/get';
import Str from 'expensify-common/lib/str';
import Text from '../Text';
import PressableWithSecondaryInteraction from '../PressableWithSecondaryInteraction';
import * as ReportActionContextMenu from '../../pages/home/report/ContextMenu/ReportActionContextMenu';
import * as ContextMenuActions from '../../pages/home/report/ContextMenu/ContextMenuActions';
import Tooltip from '../Tooltip';
import canUseTouchScreen from '../../libs/canUseTouchscreen';
import styles from '../../styles/styles';
import withWindowDimensions from '../withWindowDimensions';
import {propTypes, defaultProps} from './anchorForCommentsOnlyPropTypes';

/*
* This is a default anchor component for regular links.
*/
const BaseAnchorForCommentsOnly = (props) => {
let linkRef;
const rest = _.omit(props, _.keys(propTypes));
const linkProps = {};
if (_.isFunction(props.onPress)) {
linkProps.onPress = props.onPress;
} else {
linkProps.href = props.href;
}
const defaultTextStyle = canUseTouchScreen() || props.isSmallScreenWidth ? {} : styles.userSelectText;

return (
<PressableWithSecondaryInteraction
inline
onSecondaryInteraction={
(event) => {
ReportActionContextMenu.showContextMenu(
Str.isValidEmail(props.displayName) ? ContextMenuActions.CONTEXT_MENU_TYPES.EMAIL : ContextMenuActions.CONTEXT_MENU_TYPES.LINK,
event,
props.href,
lodashGet(linkRef, 'current'),
);
}
}
>
<Tooltip text={Str.isValidEmail(props.displayName) ? '' : props.href}>
<Text
ref={el => linkRef = el}
style={StyleSheet.flatten([props.style, defaultTextStyle])}
accessibilityRole="link"
hrefAttrs={{
rel: props.rel,
target: props.target,
}}
// eslint-disable-next-line react/jsx-props-no-spreading
{...linkProps}
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{props.children}
</Text>
</Tooltip>
</PressableWithSecondaryInteraction>
);
};

BaseAnchorForCommentsOnly.propTypes = propTypes;
BaseAnchorForCommentsOnly.defaultProps = defaultProps;
BaseAnchorForCommentsOnly.displayName = 'BaseAnchorForCommentsOnly';

export default withWindowDimensions(BaseAnchorForCommentsOnly);
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import PropTypes from 'prop-types';
import stylePropTypes from '../../styles/stylePropTypes';
import {windowDimensionsPropTypes} from '../withWindowDimensions';

const propTypes = {
/** The URL to open */
href: PropTypes.string,

/** What headers to send to the linked page (usually noopener and noreferrer)
This is unused in native, but is here for parity with web */
rel: PropTypes.string,

/** Used to determine where to open a link ("_blank" is passed for a new tab)
This is unused in native, but is here for parity with web */
target: PropTypes.string,

/** Any children to display */
children: PropTypes.node,

/** Anchor text of URLs or emails. */
displayName: PropTypes.string,

/** Any additional styles to apply */
style: stylePropTypes,

/** Press handler for the link, when not passed, default href is used to create a link like behaviour */
onPress: PropTypes.func,

...windowDimensionsPropTypes,
};

const defaultProps = {
href: '',
rel: '',
target: '',
children: null,
style: {},
displayName: '',
onPress: undefined,
};

export {propTypes, defaultProps};
11 changes: 11 additions & 0 deletions src/components/AnchorForCommentsOnly/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import * as anchorForCommentsOnlyPropTypes from './anchorForCommentsOnlyPropTypes';
import BaseAnchorForCommentsOnly from './BaseAnchorForCommentsOnly';

// eslint-disable-next-line react/jsx-props-no-spreading
const AnchorForCommentsOnly = props => <BaseAnchorForCommentsOnly {...props} />;
AnchorForCommentsOnly.propTypes = anchorForCommentsOnlyPropTypes.propTypes;
AnchorForCommentsOnly.defaultProps = anchorForCommentsOnlyPropTypes.defaultProps;
AnchorForCommentsOnly.displayName = 'AnchorForCommentsOnly';

export default AnchorForCommentsOnly;
20 changes: 20 additions & 0 deletions src/components/AnchorForCommentsOnly/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import {Linking} from 'react-native';
import _ from 'underscore';

import * as anchorForCommentsOnlyPropTypes from './anchorForCommentsOnlyPropTypes';
import BaseAnchorForCommentsOnly from './BaseAnchorForCommentsOnly';

// eslint-disable-next-line react/jsx-props-no-spreading
const AnchorForCommentsOnly = (props) => {
const onPress = () => (_.isFunction(props.onPress) ? props.onPress() : Linking.openURL(props.href));

// eslint-disable-next-line react/jsx-props-no-spreading
return <BaseAnchorForCommentsOnly {...props} onPress={onPress} />;
};

AnchorForCommentsOnly.propTypes = anchorForCommentsOnlyPropTypes.propTypes;
AnchorForCommentsOnly.defaultProps = anchorForCommentsOnlyPropTypes.defaultProps;
AnchorForCommentsOnly.displayName = 'AnchorForCommentsOnly';

export default AnchorForCommentsOnly;
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const AnchorRenderer = (props) => {
displayName={displayName}

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

0 comments on commit c614dfc

Please sign in to comment.