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

Fix/react version backwards compatibility #5148

Merged
merged 9 commits into from
Jan 9, 2019
50 changes: 50 additions & 0 deletions addons/links/src/react/components/RoutedLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import PropTypes from 'prop-types';
import React from 'react';

// NOTE: this is a copy of `lib/components/src/navigation/RoutedLink.js`.
// It's duplicated here because that copy has an explicit dependency on
// React 16.3+, which breaks older versions of React running in the preview.
// The proper DRY solution is to create a new package that doesn't depend
// on a specific react version. However, that's a heavy-handed solution for
// one trivial file.

const LEFT_BUTTON = 0;
// Cmd/Ctrl/Shift/Alt + Click should trigger default browser behaviour. Same applies to non-left clicks
const isPlainLeftClick = e =>
e.button === LEFT_BUTTON && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey;

export default class RoutedLink extends React.Component {
onClick = e => {
const { onClick } = this.props;

if (isPlainLeftClick(e)) {
e.preventDefault();
onClick(e);
}
};

render() {
const { href, children, onClick, className, style } = this.props;
const props = onClick
? { href, className, style, onClick: this.onClick }
: { href, className, style };
return <a {...props}>{children}</a>;
}
}

RoutedLink.defaultProps = {
onClick: null,
href: '#',
children: null,
className: undefined,
style: undefined,
};

RoutedLink.propTypes = {
onClick: PropTypes.func,
href: PropTypes.string,
children: PropTypes.node,
className: PropTypes.string,
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.object,
};
2 changes: 1 addition & 1 deletion addons/links/src/react/components/link.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';

import { RoutedLink } from '@storybook/components';
import RoutedLink from './RoutedLink';
import { openLink, hrefTo } from '../../preview';

export default class LinkTo extends PureComponent {
Expand Down