react-router-dom.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 } 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. /* eslint-disable-next-line jsx-a11y/anchor-has-content */
  130. return React.createElement("a", props);
  131. });
  132. if (process.env.NODE_ENV !== "production") {
  133. LinkAnchor.displayName = "LinkAnchor";
  134. }
  135. /**
  136. * The public API for rendering a history-aware <a>.
  137. */
  138. var Link = forwardRef(function (_ref2, forwardedRef) {
  139. var _ref2$component = _ref2.component,
  140. component = _ref2$component === void 0 ? LinkAnchor : _ref2$component,
  141. replace = _ref2.replace,
  142. to = _ref2.to,
  143. innerRef = _ref2.innerRef,
  144. rest = _objectWithoutPropertiesLoose(_ref2, ["component", "replace", "to", "innerRef"]);
  145. return React.createElement(__RouterContext.Consumer, null, function (context) {
  146. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Link> outside a <Router>") : invariant(false) : void 0;
  147. var history = context.history;
  148. var location = normalizeToLocation(resolveToLocation(to, context.location), context.location);
  149. var href = location ? history.createHref(location) : "";
  150. var props = _extends({}, rest, {
  151. href: href,
  152. navigate: function navigate() {
  153. var location = resolveToLocation(to, context.location);
  154. var method = replace ? history.replace : history.push;
  155. method(location);
  156. }
  157. }); // React 15 compat
  158. if (forwardRefShim !== forwardRef) {
  159. props.ref = forwardedRef || innerRef;
  160. } else {
  161. props.innerRef = innerRef;
  162. }
  163. return React.createElement(component, props);
  164. });
  165. });
  166. if (process.env.NODE_ENV !== "production") {
  167. var toType = PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.func]);
  168. var refType = PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.shape({
  169. current: PropTypes.any
  170. })]);
  171. Link.displayName = "Link";
  172. Link.propTypes = {
  173. innerRef: refType,
  174. onClick: PropTypes.func,
  175. replace: PropTypes.bool,
  176. target: PropTypes.string,
  177. to: toType.isRequired
  178. };
  179. }
  180. var forwardRefShim$1 = function forwardRefShim(C) {
  181. return C;
  182. };
  183. var forwardRef$1 = React.forwardRef;
  184. if (typeof forwardRef$1 === "undefined") {
  185. forwardRef$1 = forwardRefShim$1;
  186. }
  187. function joinClassnames() {
  188. for (var _len = arguments.length, classnames = new Array(_len), _key = 0; _key < _len; _key++) {
  189. classnames[_key] = arguments[_key];
  190. }
  191. return classnames.filter(function (i) {
  192. return i;
  193. }).join(" ");
  194. }
  195. /**
  196. * A <Link> wrapper that knows if it's "active" or not.
  197. */
  198. var NavLink = forwardRef$1(function (_ref, forwardedRef) {
  199. var _ref$ariaCurrent = _ref["aria-current"],
  200. ariaCurrent = _ref$ariaCurrent === void 0 ? "page" : _ref$ariaCurrent,
  201. _ref$activeClassName = _ref.activeClassName,
  202. activeClassName = _ref$activeClassName === void 0 ? "active" : _ref$activeClassName,
  203. activeStyle = _ref.activeStyle,
  204. classNameProp = _ref.className,
  205. exact = _ref.exact,
  206. isActiveProp = _ref.isActive,
  207. locationProp = _ref.location,
  208. sensitive = _ref.sensitive,
  209. strict = _ref.strict,
  210. styleProp = _ref.style,
  211. to = _ref.to,
  212. innerRef = _ref.innerRef,
  213. rest = _objectWithoutPropertiesLoose(_ref, ["aria-current", "activeClassName", "activeStyle", "className", "exact", "isActive", "location", "sensitive", "strict", "style", "to", "innerRef"]);
  214. return React.createElement(__RouterContext.Consumer, null, function (context) {
  215. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <NavLink> outside a <Router>") : invariant(false) : void 0;
  216. var currentLocation = locationProp || context.location;
  217. var toLocation = normalizeToLocation(resolveToLocation(to, currentLocation), currentLocation);
  218. var path = toLocation.pathname; // Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
  219. var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
  220. var match = escapedPath ? matchPath(currentLocation.pathname, {
  221. path: escapedPath,
  222. exact: exact,
  223. sensitive: sensitive,
  224. strict: strict
  225. }) : null;
  226. var isActive = !!(isActiveProp ? isActiveProp(match, currentLocation) : match);
  227. var className = isActive ? joinClassnames(classNameProp, activeClassName) : classNameProp;
  228. var style = isActive ? _extends({}, styleProp, {}, activeStyle) : styleProp;
  229. var props = _extends({
  230. "aria-current": isActive && ariaCurrent || null,
  231. className: className,
  232. style: style,
  233. to: toLocation
  234. }, rest); // React 15 compat
  235. if (forwardRefShim$1 !== forwardRef$1) {
  236. props.ref = forwardedRef || innerRef;
  237. } else {
  238. props.innerRef = innerRef;
  239. }
  240. return React.createElement(Link, props);
  241. });
  242. });
  243. if (process.env.NODE_ENV !== "production") {
  244. NavLink.displayName = "NavLink";
  245. var ariaCurrentType = PropTypes.oneOf(["page", "step", "location", "date", "time", "true"]);
  246. NavLink.propTypes = _extends({}, Link.propTypes, {
  247. "aria-current": ariaCurrentType,
  248. activeClassName: PropTypes.string,
  249. activeStyle: PropTypes.object,
  250. className: PropTypes.string,
  251. exact: PropTypes.bool,
  252. isActive: PropTypes.func,
  253. location: PropTypes.object,
  254. sensitive: PropTypes.bool,
  255. strict: PropTypes.bool,
  256. style: PropTypes.object
  257. });
  258. }
  259. export { BrowserRouter, HashRouter, Link, NavLink };
  260. //# sourceMappingURL=react-router-dom.js.map