react-router-dom.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { Router, __RouterContext, matchPath } from 'react-router';
  2. export * from 'react-router';
  3. import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
  4. import React from 'react';
  5. import { createBrowserHistory, createHashHistory, createLocation } from 'history';
  6. import PropTypes from 'prop-types';
  7. import warning from 'tiny-warning';
  8. import _extends from '@babel/runtime/helpers/esm/extends';
  9. import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/esm/objectWithoutPropertiesLoose';
  10. import invariant from 'tiny-invariant';
  11. /**
  12. * The public API for a <Router> that uses HTML5 history.
  13. */
  14. var BrowserRouter =
  15. /*#__PURE__*/
  16. function (_React$Component) {
  17. _inheritsLoose(BrowserRouter, _React$Component);
  18. function BrowserRouter() {
  19. var _this;
  20. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  21. args[_key] = arguments[_key];
  22. }
  23. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  24. _this.history = createBrowserHistory(_this.props);
  25. return _this;
  26. }
  27. var _proto = BrowserRouter.prototype;
  28. _proto.render = function render() {
  29. return React.createElement(Router, {
  30. history: this.history,
  31. children: this.props.children
  32. });
  33. };
  34. return BrowserRouter;
  35. }(React.Component);
  36. if (process.env.NODE_ENV !== "production") {
  37. BrowserRouter.propTypes = {
  38. basename: PropTypes.string,
  39. children: PropTypes.node,
  40. forceRefresh: PropTypes.bool,
  41. getUserConfirmation: PropTypes.func,
  42. keyLength: PropTypes.number
  43. };
  44. BrowserRouter.prototype.componentDidMount = function () {
  45. process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<BrowserRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { BrowserRouter as Router }`.") : void 0;
  46. };
  47. }
  48. /**
  49. * The public API for a <Router> that uses window.location.hash.
  50. */
  51. var HashRouter =
  52. /*#__PURE__*/
  53. function (_React$Component) {
  54. _inheritsLoose(HashRouter, _React$Component);
  55. function HashRouter() {
  56. var _this;
  57. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  58. args[_key] = arguments[_key];
  59. }
  60. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  61. _this.history = createHashHistory(_this.props);
  62. return _this;
  63. }
  64. var _proto = HashRouter.prototype;
  65. _proto.render = function render() {
  66. return React.createElement(Router, {
  67. history: this.history,
  68. children: this.props.children
  69. });
  70. };
  71. return HashRouter;
  72. }(React.Component);
  73. if (process.env.NODE_ENV !== "production") {
  74. HashRouter.propTypes = {
  75. basename: PropTypes.string,
  76. children: PropTypes.node,
  77. getUserConfirmation: PropTypes.func,
  78. hashType: PropTypes.oneOf(["hashbang", "noslash", "slash"])
  79. };
  80. HashRouter.prototype.componentDidMount = function () {
  81. process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<HashRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { HashRouter as Router }`.") : void 0;
  82. };
  83. }
  84. var resolveToLocation = function resolveToLocation(to, currentLocation) {
  85. return typeof to === "function" ? to(currentLocation) : to;
  86. };
  87. var normalizeToLocation = function normalizeToLocation(to, currentLocation) {
  88. return typeof to === "string" ? createLocation(to, null, null, currentLocation) : to;
  89. };
  90. var forwardRefShim = function forwardRefShim(C) {
  91. return C;
  92. };
  93. var forwardRef = React.forwardRef;
  94. if (typeof forwardRef === "undefined") {
  95. forwardRef = forwardRefShim;
  96. }
  97. function isModifiedEvent(event) {
  98. return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
  99. }
  100. var LinkAnchor = forwardRef(function (_ref, forwardedRef) {
  101. var innerRef = _ref.innerRef,
  102. navigate = _ref.navigate,
  103. _onClick = _ref.onClick,
  104. rest = _objectWithoutPropertiesLoose(_ref, ["innerRef", "navigate", "onClick"]);
  105. var target = rest.target;
  106. var props = _extends({}, rest, {
  107. onClick: function onClick(event) {
  108. try {
  109. if (_onClick) _onClick(event);
  110. } catch (ex) {
  111. event.preventDefault();
  112. throw ex;
  113. }
  114. if (!event.defaultPrevented && // onClick prevented default
  115. event.button === 0 && ( // ignore everything but left clicks
  116. !target || target === "_self") && // let browser handle "target=_blank" etc.
  117. !isModifiedEvent(event) // ignore clicks with modifier keys
  118. ) {
  119. event.preventDefault();
  120. navigate();
  121. }
  122. }
  123. }); // React 15 compat
  124. if (forwardRefShim !== forwardRef) {
  125. props.ref = forwardedRef || innerRef;
  126. } else {
  127. props.ref = innerRef;
  128. }
  129. return React.createElement("a", props);
  130. });
  131. if (process.env.NODE_ENV !== "production") {
  132. LinkAnchor.displayName = "LinkAnchor";
  133. }
  134. /**
  135. * The public API for rendering a history-aware <a>.
  136. */
  137. var Link = forwardRef(function (_ref2, forwardedRef) {
  138. var _ref2$component = _ref2.component,
  139. component = _ref2$component === void 0 ? LinkAnchor : _ref2$component,
  140. replace = _ref2.replace,
  141. to = _ref2.to,
  142. innerRef = _ref2.innerRef,
  143. rest = _objectWithoutPropertiesLoose(_ref2, ["component", "replace", "to", "innerRef"]);
  144. return React.createElement(__RouterContext.Consumer, null, function (context) {
  145. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Link> outside a <Router>") : invariant(false) : void 0;
  146. var history = context.history;
  147. var location = normalizeToLocation(resolveToLocation(to, context.location), context.location);
  148. var href = location ? history.createHref(location) : "";
  149. var props = _extends({}, rest, {
  150. href: href,
  151. navigate: function navigate() {
  152. var location = resolveToLocation(to, context.location);
  153. var method = replace ? history.replace : history.push;
  154. method(location);
  155. }
  156. }); // React 15 compat
  157. if (forwardRefShim !== forwardRef) {
  158. props.ref = forwardedRef || innerRef;
  159. } else {
  160. props.innerRef = innerRef;
  161. }
  162. return React.createElement(component, props);
  163. });
  164. });
  165. if (process.env.NODE_ENV !== "production") {
  166. var toType = PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.func]);
  167. var refType = PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.shape({
  168. current: PropTypes.any
  169. })]);
  170. Link.displayName = "Link";
  171. Link.propTypes = {
  172. innerRef: refType,
  173. onClick: PropTypes.func,
  174. replace: PropTypes.bool,
  175. target: PropTypes.string,
  176. to: toType.isRequired
  177. };
  178. }
  179. var forwardRefShim$1 = function forwardRefShim(C) {
  180. return C;
  181. };
  182. var forwardRef$1 = React.forwardRef;
  183. if (typeof forwardRef$1 === "undefined") {
  184. forwardRef$1 = forwardRefShim$1;
  185. }
  186. function joinClassnames() {
  187. for (var _len = arguments.length, classnames = new Array(_len), _key = 0; _key < _len; _key++) {
  188. classnames[_key] = arguments[_key];
  189. }
  190. return classnames.filter(function (i) {
  191. return i;
  192. }).join(" ");
  193. }
  194. /**
  195. * A <Link> wrapper that knows if it's "active" or not.
  196. */
  197. var NavLink = forwardRef$1(function (_ref, forwardedRef) {
  198. var _ref$ariaCurrent = _ref["aria-current"],
  199. ariaCurrent = _ref$ariaCurrent === void 0 ? "page" : _ref$ariaCurrent,
  200. _ref$activeClassName = _ref.activeClassName,
  201. activeClassName = _ref$activeClassName === void 0 ? "active" : _ref$activeClassName,
  202. activeStyle = _ref.activeStyle,
  203. classNameProp = _ref.className,
  204. exact = _ref.exact,
  205. isActiveProp = _ref.isActive,
  206. locationProp = _ref.location,
  207. strict = _ref.strict,
  208. styleProp = _ref.style,
  209. to = _ref.to,
  210. innerRef = _ref.innerRef,
  211. rest = _objectWithoutPropertiesLoose(_ref, ["aria-current", "activeClassName", "activeStyle", "className", "exact", "isActive", "location", "strict", "style", "to", "innerRef"]);
  212. return React.createElement(__RouterContext.Consumer, null, function (context) {
  213. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <NavLink> outside a <Router>") : invariant(false) : void 0;
  214. var currentLocation = locationProp || context.location;
  215. var toLocation = normalizeToLocation(resolveToLocation(to, currentLocation), currentLocation);
  216. var path = toLocation.pathname; // Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
  217. var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
  218. var match = escapedPath ? matchPath(currentLocation.pathname, {
  219. path: escapedPath,
  220. exact: exact,
  221. strict: strict
  222. }) : null;
  223. var isActive = !!(isActiveProp ? isActiveProp(match, currentLocation) : match);
  224. var className = isActive ? joinClassnames(classNameProp, activeClassName) : classNameProp;
  225. var style = isActive ? _extends({}, styleProp, {}, activeStyle) : styleProp;
  226. var props = _extends({
  227. "aria-current": isActive && ariaCurrent || null,
  228. className: className,
  229. style: style,
  230. to: toLocation
  231. }, rest); // React 15 compat
  232. if (forwardRefShim$1 !== forwardRef$1) {
  233. props.ref = forwardedRef || innerRef;
  234. } else {
  235. props.innerRef = innerRef;
  236. }
  237. return React.createElement(Link, props);
  238. });
  239. });
  240. if (process.env.NODE_ENV !== "production") {
  241. NavLink.displayName = "NavLink";
  242. var ariaCurrentType = PropTypes.oneOf(["page", "step", "location", "date", "time", "true"]);
  243. NavLink.propTypes = _extends({}, Link.propTypes, {
  244. "aria-current": ariaCurrentType,
  245. activeClassName: PropTypes.string,
  246. activeStyle: PropTypes.object,
  247. className: PropTypes.string,
  248. exact: PropTypes.bool,
  249. isActive: PropTypes.func,
  250. location: PropTypes.object,
  251. strict: PropTypes.bool,
  252. style: PropTypes.object
  253. });
  254. }
  255. export { BrowserRouter, HashRouter, Link, NavLink };
  256. //# sourceMappingURL=react-router-dom.js.map