Skip to content

Commit c6b86f5

Browse files
author
Dominic Nguyen
authored
Apply link styles to LinkWrapper (#6)
Apply link styles to LinkWrapper
2 parents 718eee5 + 270a39b commit c6b86f5

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

src/components/Link.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ export function Link({ isButton, withArrow, containsIcon, LinkWrapper, children,
146146
);
147147

148148
if (LinkWrapper) {
149-
return <LinkWrapper {...props}>{content}</LinkWrapper>;
149+
const StyledLinkWrapper = LinkA.withComponent(LinkWrapper);
150+
return <StyledLinkWrapper {...props}>{content}</StyledLinkWrapper>;
150151
}
151152
if (isButton) {
152153
return <LinkButton {...props}>{content}</LinkButton>;

src/components/Link.stories.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { storiesOf } from '@storybook/react';
33
import { action } from '@storybook/addon-actions';
44

5+
import { Button } from './Button';
56
import { Icon } from './Icon';
67
import { Link } from './Link';
78
import { StoryLinkWrapper } from './StoryLinkWrapper';
@@ -53,5 +54,9 @@ storiesOf('Design System|Link', module)
5354
<Link LinkWrapper={StoryLinkWrapper} href="http://storybook.js.org">
5455
has a LinkWrapper like GatsbyLink or NextLink
5556
</Link>
57+
<br />
58+
<Link LinkWrapper={StoryLinkWrapper} href="http://storybook.js.org">
59+
<Button>has a LinkWrapper like GatsbyLink or NextLink and a Button child</Button>
60+
</Link>
5661
</div>
5762
));

src/components/StoryLinkWrapper.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
/* eslint-disable import/no-extraneous-dependencies */
22
// This is allows us to test whether the link works via the actions addon
3-
import React, { Children } from 'react';
3+
import React from 'react';
44
import PropTypes from 'prop-types';
55
import { action } from '@storybook/addon-actions';
66

7-
const onLinkClick = action('onLinkClick');
7+
const fireClickAction = action('onLinkClick');
88

9-
export function StoryLinkWrapper({ href, passHref, children }) {
10-
const child = Children.only(children);
9+
export function StoryLinkWrapper({ children, href, onClick, ...rest }) {
10+
const modifiedOnClick = event => {
11+
event.preventDefault();
12+
onClick();
13+
fireClickAction(href);
14+
};
1115

12-
return React.cloneElement(child, {
13-
href: passHref && href,
14-
onClick: e => {
15-
e.preventDefault();
16-
onLinkClick(href);
17-
},
18-
});
16+
return (
17+
<a href={href} {...rest} onClick={modifiedOnClick}>
18+
{children}
19+
</a>
20+
);
1921
}
2022

2123
StoryLinkWrapper.propTypes = {
24+
// eslint-disable-next-line react/forbid-prop-types
25+
children: PropTypes.any.isRequired,
2226
href: PropTypes.string.isRequired,
23-
passHref: PropTypes.bool,
24-
children: PropTypes.node.isRequired,
27+
onClick: PropTypes.func,
2528
};
2629

2730
StoryLinkWrapper.defaultProps = {
28-
passHref: false,
31+
onClick: () => {},
2932
};

0 commit comments

Comments
 (0)