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';
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Use `Link` to create a link to another resource. It accepts a type to automatically
|
|
|
|
* create wp-admin links, wc-admin links, and external links.
|
|
|
|
*/
|
2018-06-26 14:49:42 +00:00
|
|
|
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-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The resource to link to.
|
|
|
|
*/
|
2018-07-17 18:51:56 +00:00
|
|
|
href: PropTypes.string.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Type of link. For wp-admin and wc-admin, the correct prefix is appended.
|
|
|
|
*/
|
2018-07-17 18:51:56 +00:00
|
|
|
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;
|