react-router-dom.js 12 KB

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