2018-06-26 14:49:42 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Link as RouterLink } from 'react-router-dom';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getAdminLink } from 'lib/nav-utils';
|
|
|
|
|
|
|
|
class Link extends Component {
|
|
|
|
render() {
|
2018-07-17 18:51:56 +00:00
|
|
|
const { children, href, type, ...props } = this.props;
|
|
|
|
if ( this.context.router && 'wc-admin' === type ) {
|
2018-06-26 14:49:42 +00:00
|
|
|
return (
|
2018-07-17 18:51:56 +00:00
|
|
|
<RouterLink to={ href } { ...props }>
|
2018-06-26 14:49:42 +00:00
|
|
|
{ children }
|
|
|
|
</RouterLink>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:51:56 +00:00
|
|
|
let path;
|
|
|
|
if ( 'wp-admin' === type ) {
|
|
|
|
path = getAdminLink( href );
|
|
|
|
} else if ( 'external' === type ) {
|
|
|
|
path = href;
|
|
|
|
} else {
|
|
|
|
path = getAdminLink( 'admin.php?page=wc-admin#' + href );
|
|
|
|
}
|
|
|
|
|
2018-06-26 14:49:42 +00:00
|
|
|
return (
|
|
|
|
<a href={ path } { ...props }>
|
|
|
|
{ children }
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Link.propTypes = {
|
2018-07-17 18:51:56 +00:00
|
|
|
href: PropTypes.string.isRequired,
|
|
|
|
type: PropTypes.oneOf( [ 'wp-admin', 'wc-admin', 'external' ] ).isRequired,
|
2018-06-26 14:49:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Link.defaultProps = {
|
2018-07-17 18:51:56 +00:00
|
|
|
type: 'wc-admin',
|
2018-06-26 14:49:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Link.contextTypes = {
|
|
|
|
router: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Link;
|