react-router-dom.js 11 KB

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