|
1 | 1 | /* eslint-disable import/no-extraneous-dependencies */ |
2 | 2 | // 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'; |
4 | 4 | import PropTypes from 'prop-types'; |
5 | 5 | import { action } from '@storybook/addon-actions'; |
6 | 6 |
|
7 | | -const onLinkClick = action('onLinkClick'); |
| 7 | +const fireClickAction = action('onLinkClick'); |
8 | 8 |
|
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 | + }; |
11 | 15 |
|
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 | + ); |
19 | 21 | } |
20 | 22 |
|
21 | 23 | StoryLinkWrapper.propTypes = { |
| 24 | + // eslint-disable-next-line react/forbid-prop-types |
| 25 | + children: PropTypes.any.isRequired, |
22 | 26 | href: PropTypes.string.isRequired, |
23 | | - passHref: PropTypes.bool, |
24 | | - children: PropTypes.node.isRequired, |
| 27 | + onClick: PropTypes.func, |
25 | 28 | }; |
26 | 29 |
|
27 | 30 | StoryLinkWrapper.defaultProps = { |
28 | | - passHref: false, |
| 31 | + onClick: () => {}, |
29 | 32 | }; |
0 commit comments