UNPKG

@shopgate/engage

Version:
16 lines 1.75 kB
function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import React from'react';import{RouteContext}from'@shopgate/pwa-common/context';/** * Gets the context props for the wrapped component. * @param {Object} context The context. * @param {string|null} prop The optional prop name. * @returns {Object} */var getInjectedProps=function getInjectedProps(context,prop){if(!prop){return context;}return _defineProperty({},prop,_extends({},context));};/** * Injects the route properties into the desired component. * @param {Function} WrappedComponent The react component to wrap. * @param {Object} [options={}] Options for the HOC. * @param {string} [options.prop] An optional prop name to inject the route properties. * @returns {JSX} */export function withRoute(WrappedComponent){var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};/** * The actual HOC. * @param {Object} props The component props. * @returns {JSX} */var WithRoute=function WithRoute(props){return React.createElement(RouteContext.Consumer,null,function(routeContext){return React.createElement(WrappedComponent,_extends({},getInjectedProps(routeContext,options.prop),props));});};var displayName=WrappedComponent.displayName||WrappedComponent.name||'Component';WithRoute.displayName="WithRoute(".concat(displayName,")");return WithRoute;}