Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply link styles to LinkWrapper #6

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export function Link({ isButton, withArrow, containsIcon, LinkWrapper, children,
);

if (LinkWrapper) {
return <LinkWrapper {...props}>{content}</LinkWrapper>;
const StyledLinkWrapper = LinkA.withComponent(LinkWrapper);
return <StyledLinkWrapper {...props}>{content}</StyledLinkWrapper>;
}
if (isButton) {
return <LinkButton {...props}>{content}</LinkButton>;
Expand Down
5 changes: 5 additions & 0 deletions src/components/Link.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';

import { Button } from './Button';
import { Icon } from './Icon';
import { Link } from './Link';
import { StoryLinkWrapper } from './StoryLinkWrapper';
Expand Down Expand Up @@ -53,5 +54,9 @@ storiesOf('Design System|Link', module)
<Link LinkWrapper={StoryLinkWrapper} href="http://storybook.js.org">
has a LinkWrapper like GatsbyLink or NextLink
</Link>
<br />
<Link LinkWrapper={StoryLinkWrapper} href="http://storybook.js.org">
<Button>has a LinkWrapper like GatsbyLink or NextLink and a Button child</Button>
</Link>
</div>
));
31 changes: 17 additions & 14 deletions src/components/StoryLinkWrapper.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
/* eslint-disable import/no-extraneous-dependencies */
// This is allows us to test whether the link works via the actions addon
import React, { Children } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import { action } from '@storybook/addon-actions';

const onLinkClick = action('onLinkClick');
const fireClickAction = action('onLinkClick');

export function StoryLinkWrapper({ href, passHref, children }) {
const child = Children.only(children);
export function StoryLinkWrapper({ children, href, onClick, ...rest }) {
const modifiedOnClick = event => {
event.preventDefault();
onClick();
fireClickAction(href);
};

return React.cloneElement(child, {
href: passHref && href,
onClick: e => {
e.preventDefault();
onLinkClick(href);
},
});
return (
<a href={href} {...rest} onClick={modifiedOnClick}>
{children}
</a>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this to better simulate what a GatsbyLink (or React Router link for that matter) would return

);
}

StoryLinkWrapper.propTypes = {
// eslint-disable-next-line react/forbid-prop-types
children: PropTypes.any.isRequired,
href: PropTypes.string.isRequired,
passHref: PropTypes.bool,
children: PropTypes.node.isRequired,
onClick: PropTypes.func,
};

StoryLinkWrapper.defaultProps = {
passHref: false,
onClick: () => {},
};