Skip to content

Commit 954fe36

Browse files
committed
make the label required on a link. however, it can be an empty string if the desire is to only show an icon
1 parent bbb4b4f commit 954fe36

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/components/Link/Link.jsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,39 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
33
import PropTypes from 'prop-types'
44
import './link.css'
55

6-
const Link = ({ addClass, href, icon, label, style, ...props }) => (
7-
<a
8-
href={href}
9-
className={`link ${addClass}`}
10-
style={style}
11-
{...props}
12-
>
13-
{icon && (
14-
<FontAwesomeIcon icon={icon} className={`small ${label.length > 0 && 'me-2'}`} />
15-
)}
16-
{label}
17-
</a>
18-
)
6+
const Link = ({ addClass, href, icon, label, style, ...props }) => {
7+
return (
8+
<a
9+
href={href}
10+
className={`link ${addClass}`}
11+
style={style}
12+
{...props}
13+
>
14+
{icon && (
15+
<FontAwesomeIcon icon={icon} className={`small ${label.length && 'me-2'}`} />
16+
)}
17+
{label && label}
18+
</a>
19+
)
20+
}
1921

22+
/**
23+
* there isn't an easy way to require either the icon OR the label. there are additional packages we could install that have the
24+
* capability, but that would bloat the package unnecessarily. instead, we're requiring that the label be passed. the overwhelming
25+
* majority of instances where a link is used, will want a label anyway. however, in the event a label isn't desired, passing the
26+
* value as an empty string will satisfy prop types, while still only showing an icon.
27+
*/
2028
Link.propTypes = {
2129
addClass: PropTypes.string,
2230
href: PropTypes.string.isRequired,
2331
icon: PropTypes.string,
24-
label: PropTypes.string,
32+
label: PropTypes.string.isRequired,
2533
style: PropTypes.shape({}),
2634
}
2735

2836
Link.defaultProps = {
2937
addClass: '',
3038
icon: '',
31-
label: '',
3239
style: {},
3340
}
3441

src/components/Link/Link.stories.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ Default.args = {
2121
export const Alternate = Template.bind({})
2222
Alternate.args = {
2323
href: '',
24+
icon: 'fa-envelope',
2425
label: 'No underline!',
2526
style: {
2627
textDecoration: 'none',
2728
color: '#AB1289',
2829
},
29-
icon: 'fa-envelope',
30+
}
31+
32+
export const NoLabel = Template.bind({})
33+
NoLabel.args = {
34+
href: '',
35+
icon: 'fa-download',
36+
label: '',
37+
style: {
38+
textDecoration: 'none',
39+
color: '#AS8908',
40+
},
3041
}

0 commit comments

Comments
 (0)