-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TextLink.js
93 lines (80 loc) · 2.41 KB
/
TextLink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import _ from 'underscore';
import React from 'react';
import PropTypes from 'prop-types';
import Text from './Text';
import styles from '../styles/styles';
import stylePropTypes from '../styles/stylePropTypes';
import CONST from '../CONST';
import * as Link from '../libs/actions/Link';
import refPropTypes from './refPropTypes';
const propTypes = {
/** Link to open in new tab */
href: PropTypes.string,
/** Text content child */
children: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.object]).isRequired,
/** Additional style props */
style: stylePropTypes,
/** Overwrites the default link behavior with a custom callback */
onPress: PropTypes.func,
/** Callback that is called when mousedown is triggered */
onMouseDown: PropTypes.func,
/** A ref to forward to text */
forwardedRef: refPropTypes,
};
const defaultProps = {
forwardedRef: undefined,
href: undefined,
style: [],
onPress: undefined,
onMouseDown: (event) => event.preventDefault(),
};
function TextLink(props) {
const rest = _.omit(props, _.keys(propTypes));
const additionalStyles = _.isArray(props.style) ? props.style : [props.style];
/**
* @param {Event} event
*/
const openLink = (event) => {
event.preventDefault();
if (props.onPress) {
props.onPress();
return;
}
Link.openExternalLink(props.href);
};
/**
* @param {Event} event
*/
const openLinkIfEnterKeyPressed = (event) => {
if (event.key !== 'Enter') {
return;
}
openLink(event);
};
return (
<Text
style={[styles.link, ...additionalStyles]}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.LINK}
href={props.href}
onPress={openLink}
onMouseDown={props.onMouseDown}
onKeyDown={openLinkIfEnterKeyPressed}
ref={props.forwardedRef}
suppressHighlighting
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{props.children}
</Text>
);
}
TextLink.defaultProps = defaultProps;
TextLink.propTypes = propTypes;
TextLink.displayName = 'TextLink';
export default React.forwardRef((props, ref) => (
<TextLink
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));