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.
 
 
 
 

74 lines
2.8 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. 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; };
  3. 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; }
  4. import React from "react";
  5. import PropTypes from "prop-types";
  6. import Route from "./Route";
  7. import Link from "./Link";
  8. /**
  9. * A <Link> wrapper that knows if it's "active" or not.
  10. */
  11. var NavLink = function NavLink(_ref) {
  12. var to = _ref.to,
  13. exact = _ref.exact,
  14. strict = _ref.strict,
  15. location = _ref.location,
  16. activeClassName = _ref.activeClassName,
  17. className = _ref.className,
  18. activeStyle = _ref.activeStyle,
  19. style = _ref.style,
  20. getIsActive = _ref.isActive,
  21. ariaCurrent = _ref["aria-current"],
  22. rest = _objectWithoutProperties(_ref, ["to", "exact", "strict", "location", "activeClassName", "className", "activeStyle", "style", "isActive", "aria-current"]);
  23. var path = (typeof to === "undefined" ? "undefined" : _typeof(to)) === "object" ? to.pathname : to;
  24. // Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
  25. var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
  26. return React.createElement(Route, {
  27. path: escapedPath,
  28. exact: exact,
  29. strict: strict,
  30. location: location,
  31. children: function children(_ref2) {
  32. var location = _ref2.location,
  33. match = _ref2.match;
  34. var isActive = !!(getIsActive ? getIsActive(match, location) : match);
  35. return React.createElement(Link, _extends({
  36. to: to,
  37. className: isActive ? [className, activeClassName].filter(function (i) {
  38. return i;
  39. }).join(" ") : className,
  40. style: isActive ? _extends({}, style, activeStyle) : style,
  41. "aria-current": isActive && ariaCurrent || null
  42. }, rest));
  43. }
  44. });
  45. };
  46. NavLink.propTypes = {
  47. to: Link.propTypes.to,
  48. exact: PropTypes.bool,
  49. strict: PropTypes.bool,
  50. location: PropTypes.object,
  51. activeClassName: PropTypes.string,
  52. className: PropTypes.string,
  53. activeStyle: PropTypes.object,
  54. style: PropTypes.object,
  55. isActive: PropTypes.func,
  56. "aria-current": PropTypes.oneOf(["page", "step", "location", "date", "time", "true"])
  57. };
  58. NavLink.defaultProps = {
  59. activeClassName: "active",
  60. "aria-current": "page"
  61. };
  62. export default NavLink;