Repository URL to install this package:
|
Version:
1.2.12 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Link = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactRouterDom = require("react-router-dom");
var _exotic = require("exotic");
var _types = require("../view-container/types");
var _jsxFileName = "link-container/index.js";
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
const IS_TEST = process.env.NODE_ENV === 'test';
const isValueExternal = to => to.includes('http') || to.includes('mailto') || to.includes('tel');
/**
* @see https://github.com/ReactTraining/react-router/issues/2051
*/
class Link extends _react.default.PureComponent {
/**
* href, url, to, address
*/
render() {
let _props = this.props,
to = _props.to,
href = _props.href,
url = _props.url,
link = _props.link,
onClick = _props.onClick,
text = _props.text,
children = _props.children,
className = _props.className,
isExternal = _props.isExternal,
missingLinkTag = _props.missingLinkTag,
target = _props.target,
nofollow = _props.nofollow,
shouldNotFollow = _props.shouldNotFollow,
remainingProps = _objectWithoutProperties(_props, ["to", "href", "url", "link", "onClick", "text", "children", "className", "isExternal", "missingLinkTag", "target", "nofollow", "shouldNotFollow"]);
to = to || href || url || link || onClick; // <a text="eh">
// <a children="eh">
// <a>{'eh'}</a>
// when passing text in via children
if ((0, _exotic.isNil)(text)) {
// || to
text = children || null;
}
if (to && missingLinkTag === true) {
const Tag = missingLinkTag;
return _react.default.createElement(Tag, {
__source: {
fileName: _jsxFileName,
lineNumber: 131
},
__self: this
}, to);
}
if (process.env.NODE_ENV !== 'production') {
if (!to) {
throw new Error('/MISSING-LINK' + JSON.stringify(this.props));
}
} // if (isNil(to)) {
// to = href || url
// }
// check if external
if (isValueExternal(to)) {
isExternal = true;
}
const attributes = _objectSpread({}, remainingProps, {
target,
className,
children: text
});
if (nofollow === true || shouldNotFollow === true) {
attributes.rel = 'no-follow';
}
if ((0, _exotic.isTrue)(isExternal) || IS_TEST) {
attributes.href = to;
return _react.default.createElement("a", _extends({}, attributes, {
__source: {
fileName: _jsxFileName,
lineNumber: 161
},
__self: this
}));
} else {
attributes.to = {
pathname: to
};
}
const view = _react.default.createElement(_reactRouterDom.NavLink, _extends({}, attributes, {
__source: {
fileName: _jsxFileName,
lineNumber: 169
},
__self: this
}));
return view; // <Link to={to}>{text}</Link>, <a href="">{text}</a>
// if (isRelative(url))
// if (isAbsolute(url))
// if (routes.includes(url))
}
}
exports.Link = Link;
Object.defineProperty(Link, "propTypes", {
configurable: true,
enumerable: true,
writable: true,
value: {
/**
* @type {String || Object || Array || URL}
*/
to: _types.any,
/**
* @type {String || Object}
*/
className: _types.any,
/**
* @type {String || Object}
*/
activeClassName: _types.any,
// === label
/**
* @alias text
*/
children: _types.any,
/**
* @alias label
* @alias title
*/
text: _types.string,
/**
* @optional
*/
isExternal: _types.boolean,
/**
* @description can set the prefix for the url
*/
urlBase: _types.string,
/**
* @description open in a new tab/window/same window
*/
target: _types.string,
/**
* === synonymous, use oneOf, not multiple
*/
href: _types.string,
link: _types.string,
url: _types.string,
onClick: _types.func,
/**
* @type {Function | Object | String}
*/
styles: _types.string,
/**
* @version 0 - was boolean
* @version 1 - is string or node
*/
missingLinkTag: (0, _types.oneOfType)([_types.string, _types.node]),
/**
* @example <Link nofollow />
*/
nofollow: _types.boolean,
/**
* @alias shouldNotBeFollowedByRobots
* @example <Link shouldNotFollow />
*/
shouldNotFollow: _types.boolean // eslint-disable-next-line
}
});
var _default = Link;
exports.default = _default;