@shopgate/engage
Version:
Shopgate's ENGAGE library.
16 lines • 1.92 kB
JavaScript
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,{useContext}from'react';import{ThemeContext}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 current Product Context information into the desired component.
* @param {Function} WrappedComponent The react component to wrap.
* @param {Object} [options] A optional prop name to inject the current product properties.
* @param {string} [options.prop] An optional prop name to inject the current product properties.
* @returns {JSX}
*/export function withCurrentProduct(WrappedComponent){var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};/**
* The actual HOC.
* @param {Object} props The component props.
* @returns {JSX}
*/function WithCurrentProduct(props){var _useContext=useContext(ThemeContext),ProductContext=_useContext.contexts.ProductContext;var productProps=useContext(ProductContext);return React.createElement(WrappedComponent,_extends({},getInjectedProps(productProps,options.prop),props));}var displayName=WrappedComponent.displayName||WrappedComponent.name||'Component';WithCurrentProduct.displayName="WithCurrentProduct(".concat(displayName,")");return WithCurrentProduct;}