You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
4.2 KiB

  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. import React from "react";
  7. import PropTypes from "prop-types";
  8. import invariant from "invariant";
  9. import { createLocation } from "history";
  10. var isModifiedEvent = function isModifiedEvent(event) {
  11. return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
  12. };
  13. /**
  14. * The public API for rendering a history-aware <a>.
  15. */
  16. var Link = function (_React$Component) {
  17. _inherits(Link, _React$Component);
  18. function Link() {
  19. var _temp, _this, _ret;
  20. _classCallCheck(this, Link);
  21. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  22. args[_key] = arguments[_key];
  23. }
  24. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleClick = function (event) {
  25. if (_this.props.onClick) _this.props.onClick(event);
  26. if (!event.defaultPrevented && // onClick prevented default
  27. event.button === 0 && // ignore everything but left clicks
  28. !_this.props.target && // let browser handle "target=_blank" etc.
  29. !isModifiedEvent(event) // ignore clicks with modifier keys
  30. ) {
  31. event.preventDefault();
  32. var history = _this.context.router.history;
  33. var _this$props = _this.props,
  34. replace = _this$props.replace,
  35. to = _this$props.to;
  36. if (replace) {
  37. history.replace(to);
  38. } else {
  39. history.push(to);
  40. }
  41. }
  42. }, _temp), _possibleConstructorReturn(_this, _ret);
  43. }
  44. Link.prototype.render = function render() {
  45. var _props = this.props,
  46. replace = _props.replace,
  47. to = _props.to,
  48. innerRef = _props.innerRef,
  49. props = _objectWithoutProperties(_props, ["replace", "to", "innerRef"]); // eslint-disable-line no-unused-vars
  50. invariant(this.context.router, "You should not use <Link> outside a <Router>");
  51. invariant(to !== undefined, 'You must specify the "to" property');
  52. var history = this.context.router.history;
  53. var location = typeof to === "string" ? createLocation(to, null, null, history.location) : to;
  54. var href = history.createHref(location);
  55. return React.createElement("a", _extends({}, props, { onClick: this.handleClick, href: href, ref: innerRef }));
  56. };
  57. return Link;
  58. }(React.Component);
  59. Link.propTypes = {
  60. onClick: PropTypes.func,
  61. target: PropTypes.string,
  62. replace: PropTypes.bool,
  63. to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
  64. innerRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func])
  65. };
  66. Link.defaultProps = {
  67. replace: false
  68. };
  69. Link.contextTypes = {
  70. router: PropTypes.shape({
  71. history: PropTypes.shape({
  72. push: PropTypes.func.isRequired,
  73. replace: PropTypes.func.isRequired,
  74. createHref: PropTypes.func.isRequired
  75. }).isRequired
  76. }).isRequired
  77. };
  78. export default Link;