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.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. exports.__esModule = true;
  3. 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; };
  4. var _react = require("react");
  5. var _react2 = _interopRequireDefault(_react);
  6. var _propTypes = require("prop-types");
  7. var _propTypes2 = _interopRequireDefault(_propTypes);
  8. var _invariant = require("invariant");
  9. var _invariant2 = _interopRequireDefault(_invariant);
  10. var _history = require("history");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. 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; }
  13. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  14. 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; }
  15. 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; }
  16. var isModifiedEvent = function isModifiedEvent(event) {
  17. return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
  18. };
  19. /**
  20. * The public API for rendering a history-aware <a>.
  21. */
  22. var Link = function (_React$Component) {
  23. _inherits(Link, _React$Component);
  24. function Link() {
  25. var _temp, _this, _ret;
  26. _classCallCheck(this, Link);
  27. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  28. args[_key] = arguments[_key];
  29. }
  30. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleClick = function (event) {
  31. if (_this.props.onClick) _this.props.onClick(event);
  32. if (!event.defaultPrevented && // onClick prevented default
  33. event.button === 0 && // ignore everything but left clicks
  34. !_this.props.target && // let browser handle "target=_blank" etc.
  35. !isModifiedEvent(event) // ignore clicks with modifier keys
  36. ) {
  37. event.preventDefault();
  38. var history = _this.context.router.history;
  39. var _this$props = _this.props,
  40. replace = _this$props.replace,
  41. to = _this$props.to;
  42. if (replace) {
  43. history.replace(to);
  44. } else {
  45. history.push(to);
  46. }
  47. }
  48. }, _temp), _possibleConstructorReturn(_this, _ret);
  49. }
  50. Link.prototype.render = function render() {
  51. var _props = this.props,
  52. replace = _props.replace,
  53. to = _props.to,
  54. innerRef = _props.innerRef,
  55. props = _objectWithoutProperties(_props, ["replace", "to", "innerRef"]); // eslint-disable-line no-unused-vars
  56. (0, _invariant2.default)(this.context.router, "You should not use <Link> outside a <Router>");
  57. (0, _invariant2.default)(to !== undefined, 'You must specify the "to" property');
  58. var history = this.context.router.history;
  59. var location = typeof to === "string" ? (0, _history.createLocation)(to, null, null, history.location) : to;
  60. var href = history.createHref(location);
  61. return _react2.default.createElement("a", _extends({}, props, { onClick: this.handleClick, href: href, ref: innerRef }));
  62. };
  63. return Link;
  64. }(_react2.default.Component);
  65. Link.propTypes = {
  66. onClick: _propTypes2.default.func,
  67. target: _propTypes2.default.string,
  68. replace: _propTypes2.default.bool,
  69. to: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object]).isRequired,
  70. innerRef: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func])
  71. };
  72. Link.defaultProps = {
  73. replace: false
  74. };
  75. Link.contextTypes = {
  76. router: _propTypes2.default.shape({
  77. history: _propTypes2.default.shape({
  78. push: _propTypes2.default.func.isRequired,
  79. replace: _propTypes2.default.func.isRequired,
  80. createHref: _propTypes2.default.func.isRequired
  81. }).isRequired
  82. }).isRequired
  83. };
  84. exports.default = Link;