-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathExternalLink.jsx
More file actions
46 lines (41 loc) · 1.45 KB
/
ExternalLink.jsx
File metadata and controls
46 lines (41 loc) · 1.45 KB
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
/**
* ExternalLink.jsx
* Created by Lizzie Salita 7/24/20
*/
import React from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { showModal } from 'redux/actions/modal/modalActions';
const propTypes = {
url: PropTypes.string.isRequired,
children: PropTypes.oneOfType([PropTypes.element, PropTypes.string, PropTypes.node]),
isCard: PropTypes.bool,
showIcon: PropTypes.bool
};
const ExternalLink = ({
url, children, isCard, showIcon = false
}) => {
const dispatch = useDispatch();
const redirect = () => {
dispatch(showModal(url));
};
const keyPressHandler = (e) => {
if (e.key === 'Enter') {
e.preventDefault();
dispatch(showModal(url));
}
};
if (isCard) {
if (showIcon) {
return <a className="usda-external-link__card" role="link" onClick={redirect} onKeyPress={keyPressHandler} tabIndex={0}>{children} <FontAwesomeIcon icon="external-link-alt" /></a>;
}
return <a className="usda-external-link__card" role="link" onClick={redirect} onKeyPress={keyPressHandler} tabIndex={0}>{children}</a>;
}
return (
<button className="usda-external-link" onClick={redirect}>
{children || url} <FontAwesomeIcon icon="external-link-alt" />
</button>);
};
ExternalLink.propTypes = propTypes;
export default ExternalLink;