unpkg/public/react-router-dom@4.3.1/es/NavLink.js

74 lines
2.8 KiB
JavaScript

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; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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; }
import React from "react";
import PropTypes from "prop-types";
import Route from "./Route";
import Link from "./Link";
/**
* A <Link> wrapper that knows if it's "active" or not.
*/
var NavLink = function NavLink(_ref) {
var to = _ref.to,
exact = _ref.exact,
strict = _ref.strict,
location = _ref.location,
activeClassName = _ref.activeClassName,
className = _ref.className,
activeStyle = _ref.activeStyle,
style = _ref.style,
getIsActive = _ref.isActive,
ariaCurrent = _ref["aria-current"],
rest = _objectWithoutProperties(_ref, ["to", "exact", "strict", "location", "activeClassName", "className", "activeStyle", "style", "isActive", "aria-current"]);
var path = (typeof to === "undefined" ? "undefined" : _typeof(to)) === "object" ? to.pathname : to;
// Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
return React.createElement(Route, {
path: escapedPath,
exact: exact,
strict: strict,
location: location,
children: function children(_ref2) {
var location = _ref2.location,
match = _ref2.match;
var isActive = !!(getIsActive ? getIsActive(match, location) : match);
return React.createElement(Link, _extends({
to: to,
className: isActive ? [className, activeClassName].filter(function (i) {
return i;
}).join(" ") : className,
style: isActive ? _extends({}, style, activeStyle) : style,
"aria-current": isActive && ariaCurrent || null
}, rest));
}
});
};
NavLink.propTypes = {
to: Link.propTypes.to,
exact: PropTypes.bool,
strict: PropTypes.bool,
location: PropTypes.object,
activeClassName: PropTypes.string,
className: PropTypes.string,
activeStyle: PropTypes.object,
style: PropTypes.object,
isActive: PropTypes.func,
"aria-current": PropTypes.oneOf(["page", "step", "location", "date", "time", "true"])
};
NavLink.defaultProps = {
activeClassName: "active",
"aria-current": "page"
};
export default NavLink;