-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10503 from parasharrajat/fix-link-native
Fix the native link renderer
- Loading branch information
Showing
6 changed files
with
143 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
src/components/AnchorForCommentsOnly/BaseAnchorForCommentsOnly.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
42 changes: 42 additions & 0 deletions
42
src/components/AnchorForCommentsOnly/anchorForCommentsOnlyPropTypes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters