react-router-dom.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. 'use strict';
  2. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  3. var reactRouter = require('react-router');
  4. var React = _interopDefault(require('react'));
  5. var history = require('history');
  6. var PropTypes = _interopDefault(require('prop-types'));
  7. var warning = _interopDefault(require('tiny-warning'));
  8. var invariant = _interopDefault(require('tiny-invariant'));
  9. function _extends() {
  10. _extends = Object.assign || function (target) {
  11. for (var i = 1; i < arguments.length; i++) {
  12. var source = arguments[i];
  13. for (var key in source) {
  14. if (Object.prototype.hasOwnProperty.call(source, key)) {
  15. target[key] = source[key];
  16. }
  17. }
  18. }
  19. return target;
  20. };
  21. return _extends.apply(this, arguments);
  22. }
  23. function _inheritsLoose(subClass, superClass) {
  24. subClass.prototype = Object.create(superClass.prototype);
  25. subClass.prototype.constructor = subClass;
  26. _setPrototypeOf(subClass, superClass);
  27. }
  28. function _setPrototypeOf(o, p) {
  29. _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
  30. o.__proto__ = p;
  31. return o;
  32. };
  33. return _setPrototypeOf(o, p);
  34. }
  35. function _objectWithoutPropertiesLoose(source, excluded) {
  36. if (source == null) return {};
  37. var target = {};
  38. var sourceKeys = Object.keys(source);
  39. var key, i;
  40. for (i = 0; i < sourceKeys.length; i++) {
  41. key = sourceKeys[i];
  42. if (excluded.indexOf(key) >= 0) continue;
  43. target[key] = source[key];
  44. }
  45. return target;
  46. }
  47. /**
  48. * The public API for a <Router> that uses HTML5 history.
  49. */
  50. var BrowserRouter = /*#__PURE__*/function (_React$Component) {
  51. _inheritsLoose(BrowserRouter, _React$Component);
  52. function BrowserRouter() {
  53. var _this;
  54. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  55. args[_key] = arguments[_key];
  56. }
  57. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  58. _this.history = history.createBrowserHistory(_this.props);
  59. return _this;
  60. }
  61. var _proto = BrowserRouter.prototype;
  62. _proto.render = function render() {
  63. return /*#__PURE__*/React.createElement(reactRouter.Router, {
  64. history: this.history,
  65. children: this.props.children
  66. });
  67. };
  68. return BrowserRouter;
  69. }(React.Component);
  70. {
  71. BrowserRouter.propTypes = {
  72. basename: PropTypes.string,
  73. children: PropTypes.node,
  74. forceRefresh: PropTypes.bool,
  75. getUserConfirmation: PropTypes.func,
  76. keyLength: PropTypes.number
  77. };
  78. BrowserRouter.prototype.componentDidMount = function () {
  79. warning(!this.props.history, "<BrowserRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { BrowserRouter as Router }`.") ;
  80. };
  81. }
  82. /**
  83. * The public API for a <Router> that uses window.location.hash.
  84. */
  85. var HashRouter = /*#__PURE__*/function (_React$Component) {
  86. _inheritsLoose(HashRouter, _React$Component);
  87. function HashRouter() {
  88. var _this;
  89. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  90. args[_key] = arguments[_key];
  91. }
  92. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  93. _this.history = history.createHashHistory(_this.props);
  94. return _this;
  95. }
  96. var _proto = HashRouter.prototype;
  97. _proto.render = function render() {
  98. return /*#__PURE__*/React.createElement(reactRouter.Router, {
  99. history: this.history,
  100. children: this.props.children
  101. });
  102. };
  103. return HashRouter;
  104. }(React.Component);
  105. {
  106. HashRouter.propTypes = {
  107. basename: PropTypes.string,
  108. children: PropTypes.node,
  109. getUserConfirmation: PropTypes.func,
  110. hashType: PropTypes.oneOf(["hashbang", "noslash", "slash"])
  111. };
  112. HashRouter.prototype.componentDidMount = function () {
  113. warning(!this.props.history, "<HashRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { HashRouter as Router }`.") ;
  114. };
  115. }
  116. var resolveToLocation = function resolveToLocation(to, currentLocation) {
  117. return typeof to === "function" ? to(currentLocation) : to;
  118. };
  119. var normalizeToLocation = function normalizeToLocation(to, currentLocation) {
  120. return typeof to === "string" ? history.createLocation(to, null, null, currentLocation) : to;
  121. };
  122. var forwardRefShim = function forwardRefShim(C) {
  123. return C;
  124. };
  125. var forwardRef = React.forwardRef;
  126. if (typeof forwardRef === "undefined") {
  127. forwardRef = forwardRefShim;
  128. }
  129. function isModifiedEvent(event) {
  130. return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
  131. }
  132. var LinkAnchor = forwardRef(function (_ref, forwardedRef) {
  133. var innerRef = _ref.innerRef,
  134. navigate = _ref.navigate,
  135. _onClick = _ref.onClick,
  136. rest = _objectWithoutPropertiesLoose(_ref, ["innerRef", "navigate", "onClick"]);
  137. var target = rest.target;
  138. var props = _extends({}, rest, {
  139. onClick: function onClick(event) {
  140. try {
  141. if (_onClick) _onClick(event);
  142. } catch (ex) {
  143. event.preventDefault();
  144. throw ex;
  145. }
  146. if (!event.defaultPrevented && // onClick prevented default
  147. event.button === 0 && ( // ignore everything but left clicks
  148. !target || target === "_self") && // let browser handle "target=_blank" etc.
  149. !isModifiedEvent(event) // ignore clicks with modifier keys
  150. ) {
  151. event.preventDefault();
  152. navigate();
  153. }
  154. }
  155. }); // React 15 compat
  156. if (forwardRefShim !== forwardRef) {
  157. props.ref = forwardedRef || innerRef;
  158. } else {
  159. props.ref = innerRef;
  160. }
  161. /* eslint-disable-next-line jsx-a11y/anchor-has-content */
  162. return /*#__PURE__*/React.createElement("a", props);
  163. });
  164. {
  165. LinkAnchor.displayName = "LinkAnchor";
  166. }
  167. /**
  168. * The public API for rendering a history-aware <a>.
  169. */
  170. var Link = forwardRef(function (_ref2, forwardedRef) {
  171. var _ref2$component = _ref2.component,
  172. component = _ref2$component === void 0 ? LinkAnchor : _ref2$component,
  173. replace = _ref2.replace,
  174. to = _ref2.to,
  175. innerRef = _ref2.innerRef,
  176. rest = _objectWithoutPropertiesLoose(_ref2, ["component", "replace", "to", "innerRef"]);
  177. return /*#__PURE__*/React.createElement(reactRouter.__RouterContext.Consumer, null, function (context) {
  178. !context ? invariant(false, "You should not use <Link> outside a <Router>") : void 0;
  179. var history$1 = context.history;
  180. var location = normalizeToLocation(resolveToLocation(to, context.location), context.location);
  181. var href = location ? history$1.createHref(location) : "";
  182. var props = _extends({}, rest, {
  183. href: href,
  184. navigate: function navigate() {
  185. var location = resolveToLocation(to, context.location);
  186. var isDuplicateNavigation = history.createPath(context.location) === history.createPath(normalizeToLocation(location));
  187. var method = replace || isDuplicateNavigation ? history$1.replace : history$1.push;
  188. method(location);
  189. }
  190. }); // React 15 compat
  191. if (forwardRefShim !== forwardRef) {
  192. props.ref = forwardedRef || innerRef;
  193. } else {
  194. props.innerRef = innerRef;
  195. }
  196. return /*#__PURE__*/React.createElement(component, props);
  197. });
  198. });
  199. {
  200. var toType = PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.func]);
  201. var refType = PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.shape({
  202. current: PropTypes.any
  203. })]);
  204. Link.displayName = "Link";
  205. Link.propTypes = {
  206. innerRef: refType,
  207. onClick: PropTypes.func,
  208. replace: PropTypes.bool,
  209. target: PropTypes.string,
  210. to: toType.isRequired
  211. };
  212. }
  213. var forwardRefShim$1 = function forwardRefShim(C) {
  214. return C;
  215. };
  216. var forwardRef$1 = React.forwardRef;
  217. if (typeof forwardRef$1 === "undefined") {
  218. forwardRef$1 = forwardRefShim$1;
  219. }
  220. function joinClassnames() {
  221. for (var _len = arguments.length, classnames = new Array(_len), _key = 0; _key < _len; _key++) {
  222. classnames[_key] = arguments[_key];
  223. }
  224. return classnames.filter(function (i) {
  225. return i;
  226. }).join(" ");
  227. }
  228. /**
  229. * A <Link> wrapper that knows if it's "active" or not.
  230. */
  231. var NavLink = forwardRef$1(function (_ref, forwardedRef) {
  232. var _ref$ariaCurrent = _ref["aria-current"],
  233. ariaCurrent = _ref$ariaCurrent === void 0 ? "page" : _ref$ariaCurrent,
  234. _ref$activeClassName = _ref.activeClassName,
  235. activeClassName = _ref$activeClassName === void 0 ? "active" : _ref$activeClassName,
  236. activeStyle = _ref.activeStyle,
  237. classNameProp = _ref.className,
  238. exact = _ref.exact,
  239. isActiveProp = _ref.isActive,
  240. locationProp = _ref.location,
  241. sensitive = _ref.sensitive,
  242. strict = _ref.strict,
  243. styleProp = _ref.style,
  244. to = _ref.to,
  245. innerRef = _ref.innerRef,
  246. rest = _objectWithoutPropertiesLoose(_ref, ["aria-current", "activeClassName", "activeStyle", "className", "exact", "isActive", "location", "sensitive", "strict", "style", "to", "innerRef"]);
  247. return /*#__PURE__*/React.createElement(reactRouter.__RouterContext.Consumer, null, function (context) {
  248. !context ? invariant(false, "You should not use <NavLink> outside a <Router>") : void 0;
  249. var currentLocation = locationProp || context.location;
  250. var toLocation = normalizeToLocation(resolveToLocation(to, currentLocation), currentLocation);
  251. var path = toLocation.pathname; // Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
  252. var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
  253. var match = escapedPath ? reactRouter.matchPath(currentLocation.pathname, {
  254. path: escapedPath,
  255. exact: exact,
  256. sensitive: sensitive,
  257. strict: strict
  258. }) : null;
  259. var isActive = !!(isActiveProp ? isActiveProp(match, currentLocation) : match);
  260. var className = typeof classNameProp === "function" ? classNameProp(isActive) : classNameProp;
  261. var style = typeof styleProp === "function" ? styleProp(isActive) : styleProp;
  262. if (isActive) {
  263. className = joinClassnames(className, activeClassName);
  264. style = _extends({}, style, activeStyle);
  265. }
  266. var props = _extends({
  267. "aria-current": isActive && ariaCurrent || null,
  268. className: className,
  269. style: style,
  270. to: toLocation
  271. }, rest); // React 15 compat
  272. if (forwardRefShim$1 !== forwardRef$1) {
  273. props.ref = forwardedRef || innerRef;
  274. } else {
  275. props.innerRef = innerRef;
  276. }
  277. return /*#__PURE__*/React.createElement(Link, props);
  278. });
  279. });
  280. {
  281. NavLink.displayName = "NavLink";
  282. var ariaCurrentType = PropTypes.oneOf(["page", "step", "location", "date", "time", "true", "false"]);
  283. NavLink.propTypes = _extends({}, Link.propTypes, {
  284. "aria-current": ariaCurrentType,
  285. activeClassName: PropTypes.string,
  286. activeStyle: PropTypes.object,
  287. className: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
  288. exact: PropTypes.bool,
  289. isActive: PropTypes.func,
  290. location: PropTypes.object,
  291. sensitive: PropTypes.bool,
  292. strict: PropTypes.bool,
  293. style: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
  294. });
  295. }
  296. Object.defineProperty(exports, 'MemoryRouter', {
  297. enumerable: true,
  298. get: function () {
  299. return reactRouter.MemoryRouter;
  300. }
  301. });
  302. Object.defineProperty(exports, 'Prompt', {
  303. enumerable: true,
  304. get: function () {
  305. return reactRouter.Prompt;
  306. }
  307. });
  308. Object.defineProperty(exports, 'Redirect', {
  309. enumerable: true,
  310. get: function () {
  311. return reactRouter.Redirect;
  312. }
  313. });
  314. Object.defineProperty(exports, 'Route', {
  315. enumerable: true,
  316. get: function () {
  317. return reactRouter.Route;
  318. }
  319. });
  320. Object.defineProperty(exports, 'Router', {
  321. enumerable: true,
  322. get: function () {
  323. return reactRouter.Router;
  324. }
  325. });
  326. Object.defineProperty(exports, 'StaticRouter', {
  327. enumerable: true,
  328. get: function () {
  329. return reactRouter.StaticRouter;
  330. }
  331. });
  332. Object.defineProperty(exports, 'Switch', {
  333. enumerable: true,
  334. get: function () {
  335. return reactRouter.Switch;
  336. }
  337. });
  338. Object.defineProperty(exports, 'generatePath', {
  339. enumerable: true,
  340. get: function () {
  341. return reactRouter.generatePath;
  342. }
  343. });
  344. Object.defineProperty(exports, 'matchPath', {
  345. enumerable: true,
  346. get: function () {
  347. return reactRouter.matchPath;
  348. }
  349. });
  350. Object.defineProperty(exports, 'useHistory', {
  351. enumerable: true,
  352. get: function () {
  353. return reactRouter.useHistory;
  354. }
  355. });
  356. Object.defineProperty(exports, 'useLocation', {
  357. enumerable: true,
  358. get: function () {
  359. return reactRouter.useLocation;
  360. }
  361. });
  362. Object.defineProperty(exports, 'useParams', {
  363. enumerable: true,
  364. get: function () {
  365. return reactRouter.useParams;
  366. }
  367. });
  368. Object.defineProperty(exports, 'useRouteMatch', {
  369. enumerable: true,
  370. get: function () {
  371. return reactRouter.useRouteMatch;
  372. }
  373. });
  374. Object.defineProperty(exports, 'withRouter', {
  375. enumerable: true,
  376. get: function () {
  377. return reactRouter.withRouter;
  378. }
  379. });
  380. exports.BrowserRouter = BrowserRouter;
  381. exports.HashRouter = HashRouter;
  382. exports.Link = Link;
  383. exports.NavLink = NavLink;
  384. //# sourceMappingURL=react-router-dom.js.map