UNPKG

4.39 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _lucidRouter = require('lucid-router');
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19function _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; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23function _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; }
24
25function _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; }
26
27function isLeftClickEvent(e) {
28 return e.button === 0;
29}
30
31function isModifiedEvent(e) {
32 return Boolean(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
33}
34
35var Link = function (_React$Component) {
36 _inherits(Link, _React$Component);
37
38 function Link() {
39 var _Object$getPrototypeO;
40
41 var _temp, _this, _ret;
42
43 _classCallCheck(this, Link);
44
45 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
46 args[_key] = arguments[_key];
47 }
48
49 return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Link)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.onClick = function (e) {
50 var _this$props = _this.props;
51 var onClick = _this$props.onClick;
52 var target = _this$props.target;
53
54 var allowTransition = true;
55
56 if (onClick instanceof Function) onClick(e);
57
58 if (isModifiedEvent(e) || !isLeftClickEvent(e)) return;
59
60 if (e.defaultPrevented === true) allowTransition = false;
61
62 if (target) {
63 if (!allowTransition) e.preventDefault();
64
65 return;
66 }
67
68 e.preventDefault();
69
70 if (allowTransition) {
71 var _this$props2 = _this.props;
72 var to = _this$props2.to;
73 var params = _this$props2.params;
74 var href = _this$props2.href;
75
76 if (to) (0, _lucidRouter.navigateToRoute)(to, params, e);else (0, _lucidRouter.navigate)(href, e);
77 }
78 }, _temp), _possibleConstructorReturn(_this, _ret);
79 }
80
81 _createClass(Link, [{
82 key: 'render',
83 value: function render() {
84 var _props = this.props;
85 var to = _props.to;
86 var params = _props.params;
87 var href = _props.href;
88 var children = _props.children;
89
90 var props = _objectWithoutProperties(_props, ['to', 'params', 'href', 'children']);
91
92 var linkTo = to ? (0, _lucidRouter.pathFor)(to, params) : href;
93 return _react2.default.createElement(
94 'a',
95 _extends({ href: linkTo, onClick: this._onClick }, props),
96 children
97 );
98 }
99 }]);
100
101 return Link;
102}(_react2.default.Component);
103
104exports.default = Link;