Rocket.Chat.ReactNative/app/containers/markdown/Link.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
2020-02-14 13:39:42 +00:00
import { Text, Clipboard } from 'react-native';
import styles from './styles';
2019-12-04 16:39:53 +00:00
import { themes } from '../../constants/colors';
import openLink from '../../utils/openLink';
2020-02-14 13:39:42 +00:00
import { LISTENER } from '../Toast';
import EventEmitter from '../../utils/events';
import I18n from '../../i18n';
const Link = React.memo(({
2020-02-28 16:18:03 +00:00
children, link, theme
}) => {
const handlePress = () => {
if (!link) {
return;
}
2019-12-04 16:39:53 +00:00
openLink(link, theme);
};
const childLength = React.Children.toArray(children).filter(o => o).length;
2020-02-14 13:39:42 +00:00
const onLongPress = () => {
Clipboard.setString(link);
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
};
// if you have a [](https://rocket.chat) render https://rocket.chat
return (
<Text
2020-02-28 16:18:03 +00:00
onPress={handlePress}
onLongPress={onLongPress}
style={{ ...styles.link, color: themes[theme].actionTintColor }}
>
{ childLength !== 0 ? children : link }
</Text>
);
});
Link.propTypes = {
children: PropTypes.node,
link: PropTypes.string,
2020-02-28 16:18:03 +00:00
theme: PropTypes.string
};
export default Link;