-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathTextLink.js
61 lines (53 loc) · 1.58 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
import _ from 'underscore';
import React from 'react';
import PropTypes from 'prop-types';
import {Pressable, 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,
};
const defaultProps = {
href: '',
style: [],
onPress: undefined,
};
const TextLink = (props) => {
const additionalStyles = _.isArray(props.style) ? props.style : [props.style];
return (
<Pressable
onPress={(e) => {
e.preventDefault();
if (props.onPress) {
props.onPress();
return;
}
Linking.openURL(props.href);
}}
accessibilityRole="link"
href={props.href}
>
{({hovered, pressed}) => (
<Text style={[styles.link, (hovered || pressed) ? styles.linkHovered : undefined, ...additionalStyles]}>
{props.children}
</Text>
)}
</Pressable>
);
};
TextLink.defaultProps = defaultProps;
TextLink.propTypes = propTypes;
TextLink.displayName = 'TextLink';
export default TextLink;