react-router-4-compat
Version:
A wrapped version of react router 4 which compatible with react router 3
2 lines (1 loc) • 18.1 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("prop-types"),require("react-router/Route"),require("react-router/Switch"),require("react-router/matchPath"),require("react-router/Router")):"function"==typeof define&&define.amd?define(["exports","react","prop-types","react-router/Route","react-router/Switch","react-router/matchPath","react-router/Router"],e):e(t.index={},t.React,t.PropTypes,t.Route,t.Switch,t.matchPath,t.Router)}(this,function(t,e,r,n,o,a,u){"use strict";function c(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function i(){return(i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function s(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(e){c(t,e,r[e])})}return t}function p(t,e){t.prototype.__proto__=e&&e.prototype,t.__proto__=e}function l(t,e){if(null==t)return{};var r,n,o={},a=Object.keys(t);for(n=0;n<a.length;n++)r=a[n],e.indexOf(r)>=0||(o[r]=t[r]);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(t);for(n=0;n<u.length;n++)r=u[n],e.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(t,r)&&(o[r]=t[r])}return o}e=e&&e.hasOwnProperty("default")?e.default:e,r=r&&r.hasOwnProperty("default")?r.default:r,n=n&&n.hasOwnProperty("default")?n.default:n,o=o&&o.hasOwnProperty("default")?o.default:o,a=a&&a.hasOwnProperty("default")?a.default:a,u=u&&u.hasOwnProperty("default")?u.default:u;var f=function(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then};var d=function(t,e){t=t||[];for(var r=0;r<t.length;++r)if(e&&e(t[r]))return!0;return!1},h=function(t){return t?Object.keys(t).map(function(e){return[e,t[e]]}):[]},m=function(t){var e={};return(t||[]).forEach(function(t){var r=t[0],n=t[1];return e[r]=n}),e};function y(){return(y=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function v(t,e){if(null==t)return{};var r,n,o={},a=Object.keys(t);for(n=0;n<a.length;n++)r=a[n],e.indexOf(r)>=0||(o[r]=t[r]);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(t);for(n=0;n<u.length;n++)r=u[n],e.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(t,r)&&(o[r]=t[r])}return o}function g(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(e){b(t,e,r[e])})}return t}function b(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}var O=function(){return null},P=Symbol("component"),w=Symbol("job"),j=e.version?Number.parseInt(e.version.slice(0,e.version.indexOf(".")),10):0;function S(t,e,r){if(void 0===r&&(r=[]),t===e)return!0;if("object"!=typeof t||"object"!=typeof e||null===t||null===e)return!1;if(Array.isArray(t)&&Array.isArray(e)){var n=t.length;if(n!==e.length)return!1;for(var o=0;o<n;++o)if(t[o]!==e[o])return!1;return!0}var a=Object.keys(t),u=Object.keys(e);if(a.length!==u.length)return!1;for(var c=0;c<a.length;++c){var i=a[c];if(!(r.includes(i)||u.includes(i)&&t[i]===e[i]))return!1}return!0}var C=function(t){function r(e){for(var r,n=arguments.length,o=new Array(n>1?n-1:0),a=1;a<n;a++)o[a-1]=arguments[a];return(r=t.call.apply(t,[this,e].concat(o))||this).setStateSafe=function(t){r.mounted?r.setState(t):Object.assign(r.state,t)},r.updateProgress=function(t){return function(e){return r.setState(function(r){var n;return{progress:g({},r.progress,(n={},n[t]=e,n))}})}},r.loadSuccess=function(t){for(var e=null,n=0;n<t.length;++n){if(t[n][0]===P)e=t.splice(n,1)[0]}var o={resolvedProps:m(t),progress:m(t.map(function(t){return[t[0],1]})),error:null,loading:!1};if(e){var a=e;o.component=a[1]}r.setStateSafe(o)},r.state={error:null,loading:!0,resolvedProps:{},component:null,progress:m(Object.keys(e.asyncProps||{}).map(function(t){return[t,0]}))},r}var n,o,a=r.prototype;return a.componentDidMount=function(){this.mounted=!0,this.load()},a.componentWillReceiveProps=function(t){t.reloader&&t.reloader.isReload()&&(t.reloader.resetReload(),this.state.loading||this.load()),t.reloadOnUpdate&&!this.state.loading&&(null===t.reloadDependents||void 0===t.reloadDependents?S(this.props,t,["batch","errorComponent","loadingComponent","onError","delay","reloadOnUpdate","reloadDependents","reloader"])||this.load():S(this.props.reloadDependents,t.reloadDependents)||this.load())},a.componentWillUnmount=function(){this.mounted=!1},a.load=function(){var t=this,e=this.props,n=e.asyncComponent,o=e.asyncJobs,a=e.asyncProps,u=e.onError,c=e.delay;this.setStateSafe({error:null,loading:!0,progress:m(Object.keys(a||{}).map(function(t){return[t,0]}))});try{var i=(n?[[P,n]]:[]).concat((o||[]).map(function(t){return[w,t]}),h(a||{})).filter(function(t){return t[1]});if(d(i,function(t){return!("function"==typeof t[1])})){var s=new Error("The async job or async prop must be a function.");return this.setState({error:s,loading:!1}),void(u&&u(s))}var p=this.props.batch;if(i=i.map(function(e){var r=e[0];return[r,(0,e[1])("symbol"!=typeof r?t.updateProgress(r):null)]}),c>0&&i.push([w,new Promise(function(t){return setTimeout(t,c)})]),d(i,function(t){return f(t[1])})){var l=i.map(function(e){var n,o=e[0],a=e[1];if(f(a))return a.then(function(e){var n;p||"symbol"==typeof o||(t.mounted?t.setState(r.updateResolvedProps(((n={})[o]=e,n))):(t.state.resolvedProps[o]=e,t.state.progress[o]=1));return p||o!==P||t.setStateSafe({component:e}),[o,e]});p||"symbol"==typeof o||t.setState(r.updateResolvedProps(((n={})[o]=a,n)));return p||o!==P||t.setState({component:a}),[o,a]});Promise.all(l).then(function(e){t.loadSuccess(e.filter(function(t){return"symbol"!=typeof t[0]}))}).catch(function(e){try{u&&u(e)}finally{t.setStateSafe({error:e})}}).finally(function(){t.setStateSafe({loading:!1})})}else this.loadSuccess(i.filter(function(t){return"symbol"!=typeof t[0]}))}catch(s){try{u&&u(s)}finally{this.setStateSafe({loading:!1})}}},a.render=function(){var t,r,n,o=this.props,a=o.errorComponent,u=o.loadingComponent,c=o.asyncPropOpts,i=o.asyncPropsMapper,s=o.asyncComponent,p=o.children,l=o.unwrapDefault,f=this.props.syncProps,d=this.props.component,b=this.state,P=b.error,w=b.loading,S=b.resolvedProps,C=b.progress,R=i(Object.assign({},(r=function(t){return t&&{}.hasOwnProperty.call(t,"defaultProp")},void 0===(t=function(t){return t.defaultProp})&&(t=function(t){return t}),void 0===r&&(r=function(){return!0}),m(h(c).filter(function(t){var e=t[0],n=t[1];return r(n,e)}).map(function(e){var r=e[0],n=e[1];return[r,t(n,r)]}))),S));if({}.hasOwnProperty.call(f,"ref")){var E=f;n=E.ref,f=v(E,["ref"])}if({}.hasOwnProperty.call(R,"ref")){var x=R;n=x.ref,R=v(x,["ref"])}var k=R,_=(k.children,v(k,["children"]));if(w&&u!==O)return e.createElement(u,y({},this.props,_,{loading:w,progress:C}));if(P)return e.createElement(a,y({},this.props,_,{loading:w,progress:C,error:P}));if(s){if(!this.state.component)return null;d=this.state.component}if(d)return l&&(d=d.default||d),d.prototype.render&&n?e.createElement(d,y({},f||{},R,{ref:n,loading:w,progress:C})):e.createElement(d,y({},f||{},R,{loading:w,progress:C}));var D=e.Children.map(p,function(t){return e.cloneElement(t,g({},f||{},_,{loading:w,progress:C}))});return D?1===D.length?D[0]:D.length>1?j>=16?D:e.createElement("div",null," ",D," "):null:null},o=t,(n=r).prototype.__proto__=o&&o.prototype,n.__proto__=o,r}(e.PureComponent);C.updateResolvedProps=function(t){return function(e){return{resolvedProps:g({},e.resolvedProps,t),progress:g({},e.progress,m(Object.keys(t).map(function(t){return[t,1]})))}}},C.createReloader=function(t){var e=!1;return{reload:function(){e=!0,t.setState({__reloader__3yPbia72w46:{}})},isReload:function(){return e},resetReload:function(){e=!1}}};var R=r.any,E=r.bool,x=r.number,k=r.objectOf,_=r.func,D=r.oneOfType,U=r.arrayOf,T=r.element,I=r.shape;C.propTypes={batch:E,asyncJobs:U(_),asyncProps:k(_),asyncPropOpts:k(I({defaultProp:R})),asyncPropsMapper:_,syncProps:k(R),component:_,asyncComponent:_,children:D([T,U(T)]),errorComponent:_,loadingComponent:_,onError:_,delay:x,unwrapDefault:E,reloadOnUpdate:E,reloadDependents:k(R),reloader:I({reload:_,isReload:_,resetReload:_})},C.defaultProps={batch:!1,asyncJobs:[],asyncProps:{},asyncPropOpts:{},asyncPropsMapper:function(t){return t},syncProps:{},component:null,asyncComponent:null,children:null,errorComponent:function(){return null},loadingComponent:O,onError:function(){return null},delay:0,unwrapDefault:!0,reloadOnUpdate:!1,reloadDependents:null,reloader:null};"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var W,A=(function(t,e){var r,n,o,a,u,c,i,s;t.exports=(r={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,getDerivedStateFromProps:!0,mixins:!0,propTypes:!0,type:!0},n={name:!0,length:!0,prototype:!0,caller:!0,callee:!0,arguments:!0,arity:!0},o=Object.defineProperty,a=Object.getOwnPropertyNames,u=Object.getOwnPropertySymbols,c=Object.getOwnPropertyDescriptor,i=Object.getPrototypeOf,s=i&&i(Object),function t(e,p,l){if("string"!=typeof p){if(s){var f=i(p);f&&f!==s&&t(e,f,l)}var d=a(p);u&&(d=d.concat(u(p)));for(var h=0;h<d.length;++h){var m=d[h];if(!(r[m]||n[m]||l&&l[m])){var y=c(p,m);try{o(e,m,y)}catch(t){}}}return e}return e})}(W={exports:{}},W.exports),W.exports),M=function(t,e,r,n,o,a,u,c){if(!t){var i;if(void 0===e)i=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var s=[r,n,o,a,u,c],p=0;(i=new Error(e.replace(/%s/g,function(){return s[p++]}))).name="Invariant Violation"}throw i.framesToPop=1,i}},q=function(t,o){var a=(void 0===o?{}:o).withRef,u=void 0!==a&&a,c=function(r){function o(){return r.apply(this,arguments)||this}var a=o.prototype;return a.getWrappedInstance=function(){return u||M(!1),this.wrappedInstance},a.render=function(){var r=this,o=this.props,a=o.wrappedComponentRef,u=l(o,["wrappedComponentRef"]),c=a?function(t){return r.wrappedInstance=t,a(t)}:function(t){r.wrappedInstance=t};return e.createElement(n,{children:function(n){var o=n.match,a=n.location,s=n.history;return function(t){return!t.prototype||!t.prototype.render}(t)?e.createElement(t,i({},u,n,{router:s,routes:r.context.routesCompat,params:o.params,location:a})):e.createElement(t,i({},u,n,{router:s,routes:r.context.routesCompat,params:o.params,location:a,ref:c}))}})},p(o,r),o}(e.Component);return c.displayName="withRouter("+(t.displayName||t.name)+")",c.WrappedComponent=t,c.contextTypes={routesCompat:r.array},A(c,t)},N=function(t,e,r){return e||(r?function(t,e){return new Promise(function(r,n){e(t,function(t,e){t?n(t):r(e)})})}(t,r):null)},J=function(t,r,o){return void 0===o&&(o=[]),t.map(function(t){var a=t.path,u=t.component,c=t.getComponent,p=t.indexRoute,l=t.getIndexRoute,f=t.childRoutes,d=t.getChildRoutes,h=t.onEnter,m=t.exact;return u||c||h?e.createElement(n,{key:a||0,path:a,exact:!!m,children:function(n){var a=n.match,c=n.location,m=n.history,y=function(t,e,r){return s({match:t,location:e,history:r},e)}(a,c,m);if(u&&(p||!l)&&(f||!d)&&!h)return e.createElement(V,i({},n,{state:y,route:t,routeStack:o,onError:r}));var v=[];return h&&v.push(function(){return new Promise(function(t,e){h.length>=3?h(y,m.replace,function(r){r?e(r):t()}):(h(y,m.replace),t())})}),e.createElement(C,{batch:!0,onError:r,asyncJobs:v,asyncProps:{route:function(){return function(t,e){var r=e.component,n=e.getComponent,o=e.indexRoute,a=e.getIndexRoute,u=e.childRoutes,c=e.getChildRoutes;return Promise.all([N(t,r,n),N(t,o,a),N(t,u,c)]).then(function(t){var r=t[0],n=t[1],o=t[2];return s({},e,{component:r,indexRoute:n,childRoutes:o})})}(y,t)}},loadingComponent:function(){return null},reloadOnUpdate:!1},e.createElement(V,i({},n,{routeStack:o,state:y,onError:r})))}}):null}).filter(function(t){return t})},L=function(t,e){return e?e.startsWith("/")?e:("/"===t?"":t)+"/"+e:t||"/"},F=function(t,e){if(t===e)return!0;if(!t||!e)return!1;var r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(var o=0;o<r.length;++o){var a=r[o];if(t[a]!==e[a])return!1}return!0},V=function(t){function r(){return t.apply(this,arguments)||this}var u=r.prototype;return u.componentWillMount=function(){this.context.routesCompat=this.props.routeStack.concat([this.props.route]),this.context.routesUpdater(this.context.routesCompat)},u.componentWillReceiveProps=function(t,e){var r=this.props,n=r.route,o=r.state,a=r.history,u=r.onError,c=n.onChange;if(c)if(c.length>=4)new Promise(function(e,r){c(o,t.state,a.replace,function(t){t?r(t):e()})}).catch(u);else try{c(o,t.state,a.replace)}catch(t){u(t)}},u.shouldComponentUpdate=function(t,e,r){var n=t.match,o=(t.params,t.state,t.route),a=t.routeStack,u=l(t,["match","params","state","route","routeStack"]),c=this.props,i=c.match,s=(c.params,c.state,c.route),p=c.routeStack,f=l(c,["match","params","state","route","routeStack"]),d=n||{},h=d.params,m=l(d,["params"]),y=i||{},v=y.params,g=l(y,["params"]);return!(F(this.state,e)&&F(this.context,r)&&F(v,h)&&F(g,m)&&F(s,o)&&F(p,a)&&F(f,u))},u.componentWillUnmount=function(){var t=this.props,e=t.route,r=t.state,n=t.onError,o=e.onLeave;if(o)try{o(r)}catch(t){n(t)}},u.render=function(){var t=this.props,u=t.route,c=t.routeStack,p=t.state,f=t.onError,d=l(t,["route","routeStack","state","onError"]),h=u.path,m=u.component,y=u.indexRoute;if(!m)return null;var v,g,b=u.childRoutes;if(function(t,e){t=t||[];for(var r=0;r<t.length;++r)if(e&&e(t[r]))return!0;return!1}(b,function(t){return!t.components&&t.getComponents}))return e.createElement(C,{batch:!0,onError:f,loadingComponent:function(){return null},asyncProps:{route:(v=regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",Promise.all((b||[]).map(function(t){return t.components||!t.getComponents?{childRoute:t}:new Promise(function(e,r){return t.getComponents(p,function(n,o){n?r(n):e({childRoute:t,components:o})})})})).then(function(t){return s({},u,{childRoutes:t.map(function(t){return s({},t.childRoute,{components:t.components})})})}));case 1:case"end":return t.stop()}},t,this)}),g=function(){var t=this,e=arguments;return new Promise(function(r,n){var o=v.apply(t,e);function a(t,e){try{var a=o[t](e),i=a.value}catch(t){return void n(t)}a.done?r(i):Promise.resolve(i).then(u,c)}function u(t){a("next",t)}function c(t){a("throw",t)}u()})},function(){return g.apply(this,arguments)})},reloadOnUpdate:!1},e.createElement(r,i({routeStack:c,state:p,onError:f},d)));var O,P={};if(b){b=b.map(function(t){var e=t.path;return e.startsWith("/")?t:s({},t,{path:L(h,e)})});for(var w=function(t){var e=b[t],r=e.components;r&&function(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),n=1;n<e;n++)r[n-1]=arguments[n];if(0===r.length||1===r.length&&"function"==typeof r[0])return t;var o=r[r.length-1],a=(o="function"==typeof o?o:null)?r.slice(0,r.length-1):r;if(o){for(var u=function(e){var r=a[e],n=Object.keys(r).map(function(e){var n,a;return{}.hasOwnProperty.call(t,e)?((n={})[e]=o(t[e],r[e],e,t,r),n):((a={})[e]=r[e],a)});Object.assign.apply(Object,[t].concat(n))},c=0;c<a.length;++c)u(c);return t}Object.assign.apply(Object,[t].concat(a))}(P,r,function(t,r){return t?(t.push({path:e.path,component:r}),t):[{path:e.path,component:r}]})},j=0;j<b.length;++j)w(j)}var S=this.context.routesCompat;O=y?[s({},y,{path:L(h,y.path),exact:!0})].concat(b||[]):(b||[]).concat();var R=function(t,e){if(!t)return t;if(!e)return s({},t);var r=Object.keys(t).map(function(r){var n;return(n={})[r]=e(t[r],r,t),n});return Object.assign.apply(Object,[{}].concat(r))}(P,function(t){return function(r){return 1===t.length?e.createElement(n,i({},r,{path:t[0].path,component:q(t[0].component)})):e.createElement(o,null,t.map(function(t){return e.createElement(n,i({},r,{path:t.path,component:q(t.component)}))}))}}),E=O.filter(function(t){return!!a(p.location.pathname,t,p.match)});return E.length>0?e.createElement(m,i({},d,{router:p.history,params:p.match.params,location:p.location,routes:S},R),J([E[0]],f,c.concat([u]))):e.createElement(m,i({},d,{router:p.history,params:p.match.params,location:p.location,routes:S},R))},p(r,t),r}(e.Component);V.contextTypes={router:r.object};var z=r.shape({path:r.string,component:r.oneOfType([r.func,r.object]),getComponent:r.func,indexRoute:r.object,getIndexRoute:r.func,childRoutes:r.arrayOf(r.object),getChildRoutes:r.func,onLeave:r.func,onChange:r.func,exact:r.bool,strict:r.bool,sensitive:r.bool});V.defaultProps={route:{},routeStack:[],onError:function(){return null},state:{}},V.contextTypes={routesCompat:r.arrayOf(z),routesUpdater:r.func};var B=function(t){function r(e,r){var n;return(n=t.call(this,e,r)||this).routesUpdater=function(t){n.setState({routes:t})},n.state={routes:[]},n}var n=r.prototype;return n.getChildContext=function(){return{routesCompat:this.state.routes,routesUpdater:this.routesUpdater}},n.render=function(){var t=this.props,r=t.routes,n=t.history,a=t.onError;return e.createElement(u,{history:n},e.createElement(o,null,J(function(){return 0===arguments.length?[]:Array.isArray(arguments.length<=0?void 0:arguments[0])?arguments.length<=0?void 0:arguments[0]:[arguments.length<=0?void 0:arguments[0]]}(r),a,[])))},p(r,t),r}(e.Component);B.defaultProps={routes:[],onError:function(){return null}},B.childContextTypes={routesCompat:r.arrayOf(z),routesUpdater:r.func},t.Router4Compat=B,t.Route4Compat=V,t.withRouter4Compat=q,Object.defineProperty(t,"__esModule",{value:!0})});