react-router-dom.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. return React.createElement("a", props);
  159. });
  160. {
  161. LinkAnchor.displayName = "LinkAnchor";
  162. }
  163. /**
  164. * The public API for rendering a history-aware <a>.
  165. */
  166. var Link = forwardRef(function (_ref2, forwardedRef) {
  167. var _ref2$component = _ref2.component,
  168. component = _ref2$component === void 0 ? LinkAnchor : _ref2$component,
  169. replace = _ref2.replace,
  170. to = _ref2.to,
  171. innerRef = _ref2.innerRef,
  172. rest = _objectWithoutPropertiesLoose(_ref2, ["component", "replace", "to", "innerRef"]);
  173. return React.createElement(reactRouter.__RouterContext.Consumer, null, function (context) {
  174. !context ? invariant(false, "You should not use <Link> outside a <Router>") : void 0;
  175. var history = context.history;
  176. var location = normalizeToLocation(resolveToLocation(to, context.location), context.location);
  177. var href = location ? history.createHref(location) : "";
  178. var props = _extends({}, rest, {
  179. href: href,
  180. navigate: function navigate() {
  181. var location = resolveToLocation(to, context.location);
  182. var method = replace ? history.replace : history.push;
  183. method(location);
  184. }
  185. }); // React 15 compat
  186. if (forwardRefShim !== forwardRef) {
  187. props.ref = forwardedRef || innerRef;
  188. } else {
  189. props.innerRef = innerRef;
  190. }
  191. return React.createElement(component, props);
  192. });
  193. });
  194. {
  195. var toType = PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.func]);
  196. var refType = PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.shape({
  197. current: PropTypes.any
  198. })]);
  199. Link.displayName = "Link";
  200. Link.propTypes = {
  201. innerRef: refType,
  202. onClick: PropTypes.func,
  203. replace: PropTypes.bool,
  204. target: PropTypes.string,
  205. to: toType.isRequired
  206. };
  207. }
  208. var forwardRefShim$1 = function forwardRefShim(C) {
  209. return C;
  210. };
  211. var forwardRef$1 = React.forwardRef;
  212. if (typeof forwardRef$1 === "undefined") {
  213. forwardRef$1 = forwardRefShim$1;
  214. }
  215. function joinClassnames() {
  216. for (var _len = arguments.length, classnames = new Array(_len), _key = 0; _key < _len; _key++) {
  217. classnames[_key] = arguments[_key];
  218. }
  219. return classnames.filter(function (i) {
  220. return i;
  221. }).join(" ");
  222. }
  223. /**
  224. * A <Link> wrapper that knows if it's "active" or not.
  225. */
  226. var NavLink = forwardRef$1(function (_ref, forwardedRef) {
  227. var _ref$ariaCurrent = _ref["aria-current"],
  228. ariaCurrent = _ref$ariaCurrent === void 0 ? "page" : _ref$ariaCurrent,
  229. _ref$activeClassName = _ref.activeClassName,
  230. activeClassName = _ref$activeClassName === void 0 ? "active" : _ref$activeClassName,
  231. activeStyle = _ref.activeStyle,
  232. classNameProp = _ref.className,
  233. exact = _ref.exact,
  234. isActiveProp = _ref.isActive,
  235. locationProp = _ref.location,
  236. strict = _ref.strict,
  237. styleProp = _ref.style,
  238. to = _ref.to,
  239. innerRef = _ref.innerRef,
  240. rest = _objectWithoutPropertiesLoose(_ref, ["aria-current", "activeClassName", "activeStyle", "className", "exact", "isActive", "location", "strict", "style", "to", "innerRef"]);
  241. return React.createElement(reactRouter.__RouterContext.Consumer, null, function (context) {
  242. !context ? invariant(false, "You should not use <NavLink> outside a <Router>") : void 0;
  243. var currentLocation = locationProp || context.location;
  244. var toLocation = normalizeToLocation(resolveToLocation(to, currentLocation), currentLocation);
  245. var path = toLocation.pathname; // Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
  246. var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
  247. var match = escapedPath ? reactRouter.matchPath(currentLocation.pathname, {
  248. path: escapedPath,
  249. exact: exact,
  250. strict: strict
  251. }) : null;
  252. var isActive = !!(isActiveProp ? isActiveProp(match, currentLocation) : match);
  253. var className = isActive ? joinClassnames(classNameProp, activeClassName) : classNameProp;
  254. var style = isActive ? _extends({}, styleProp, {}, activeStyle) : styleProp;
  255. var props = _extends({
  256. "aria-current": isActive && ariaCurrent || null,
  257. className: className,
  258. style: style,
  259. to: toLocation
  260. }, rest); // React 15 compat
  261. if (forwardRefShim$1 !== forwardRef$1) {
  262. props.ref = forwardedRef || innerRef;
  263. } else {
  264. props.innerRef = innerRef;
  265. }
  266. return React.createElement(Link, props);
  267. });
  268. });
  269. {
  270. NavLink.displayName = "NavLink";
  271. var ariaCurrentType = PropTypes.oneOf(["page", "step", "location", "date", "time", "true"]);
  272. NavLink.propTypes = _extends({}, Link.propTypes, {
  273. "aria-current": ariaCurrentType,
  274. activeClassName: PropTypes.string,
  275. activeStyle: PropTypes.object,
  276. className: PropTypes.string,
  277. exact: PropTypes.bool,
  278. isActive: PropTypes.func,
  279. location: PropTypes.object,
  280. strict: PropTypes.bool,
  281. style: PropTypes.object
  282. });
  283. }
  284. Object.keys(reactRouter).forEach(function (k) {
  285. if (k !== 'default') Object.defineProperty(exports, k, {
  286. enumerable: true,
  287. get: function () {
  288. return reactRouter[k];
  289. }
  290. });
  291. });
  292. exports.BrowserRouter = BrowserRouter;
  293. exports.HashRouter = HashRouter;
  294. exports.Link = Link;
  295. exports.NavLink = NavLink;
  296. //# sourceMappingURL=react-router-dom.js.map