-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TextLink.js
80 lines (68 loc) · 1.85 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
import _ from 'underscore';
import React from 'react';
import PropTypes from 'prop-types';
import {Linking} from 'react-native';
import Text from './Text';
import styles from '../styles/styles';
import stylePropTypes from '../styles/stylePropTypes';
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,
};
const defaultProps = {
href: undefined,
style: [],
onPress: undefined,
onMouseDown: undefined,
};
const TextLink = (props) => {
const additionalStyles = _.isArray(props.style) ? props.style : [props.style];
/**
* @param {Event} event
*/
const openLink = (event) => {
event.preventDefault();
if (props.onPress) {
props.onPress();
return;
}
Linking.openURL(props.href);
};
/**
* @param {Event} event
*/
const openLinkIfEnterKeyPressed = (event) => {
if (event.key !== 'Enter') {
return;
}
openLink(event);
};
return (
<Text
style={[styles.link, ...additionalStyles]}
accessibilityRole="link"
href={props.href}
onPress={openLink}
onMouseDown={props.onMouseDown}
onKeyDown={openLinkIfEnterKeyPressed}
>
{props.children}
</Text>
);
};
TextLink.defaultProps = defaultProps;
TextLink.propTypes = propTypes;
TextLink.displayName = 'TextLink';
export default TextLink;