UNPKG

@vve/react-router

Version:

react router for react-helper & with keep-alive

73 lines (72 loc) 2.72 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["innerRef", "replace", "to", "children", "linkExtendable"]; import React from 'react'; import { createLocation } from 'history'; import PropTypes from 'prop-types'; import RouterContext from "./router-context"; function isModifiedEvent(event) { return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey); } /** * The public API for rendering a history-aware <a>. */ class Link extends React.Component { handleClick(event, context, linkExtendable, to) { var _this$props = this.props, onClick = _this$props.onClick, target = _this$props.target, replace = _this$props.replace; if (onClick) { onClick(event); } var nextUrl = linkExtendable && context.linkExtendable ? context.genNextUrl(to) : to; if (!event.defaultPrevented // onClick prevented default && event.button === 0 // ignore everything but left clicks && (!target || target === '_self') // let browser handle "target=_blank" etc. && !isModifiedEvent(event) // ignore clicks with modifier keys ) { event.preventDefault(); var method = replace ? context.history.replace : context.history.push; method(nextUrl); } } render() { var _this$props2 = this.props, innerRef = _this$props2.innerRef, replace = _this$props2.replace, to = _this$props2.to, children = _this$props2.children, linkExtendable = _this$props2.linkExtendable, rest = _objectWithoutProperties(_this$props2, _excluded); return /*#__PURE__*/React.createElement(RouterContext.Consumer, null, context => { if (!context) { throw new Error('You should not use <Link> outside a <Router>'); } var location = typeof to === 'string' ? createLocation(to, null, null, context.location) : to; var href = location ? context.history.createHref(location) : ''; return /*#__PURE__*/React.createElement("a", _extends({}, rest, { onClick: event => this.handleClick(event, context, linkExtendable, to), href: href, ref: innerRef }), children); }); } } Link.defaultProps = { linkExtendable: true }; var toType = PropTypes.oneOfType([PropTypes.string, PropTypes.object]); var innerRefType = PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.shape({ current: PropTypes.any })]); Link.propTypes = { innerRef: innerRefType, onClick: PropTypes.func, replace: PropTypes.bool, target: PropTypes.string, to: toType.isRequired, linkExtendable: PropTypes.bool }; export default Link; //# sourceMappingURL=link.js.map