@shopgate/engage
Version:
Shopgate's ENGAGE library.
6 lines • 3.19 kB
JavaScript
import React,{useContext,useMemo}from'react';import PropTypes from'prop-types';import{css}from'glamor';import classNames from'classnames';import{I18n,SurroundPortals}from'@shopgate/engage/components';import{themeConfig}from'@shopgate/engage';import{PRODUCT_UNIT_QUANTITY_PICKER,ProductContext}from'@shopgate/engage/product';import{hasNewServices}from'@shopgate/engage/core/helpers';import{withCurrentProduct}from'@shopgate/engage/core/hocs';import{useWidgetSettings}from'@shopgate/engage/core/hooks';import UnitQuantityPicker from"./UnitQuantityPicker";import connect from"./ProductUnitQuantityPicker.connector";import{small,big}from"./styles";var variables=themeConfig.variables;var styles={root:css({padding:variables.gap.big}),title:css({fontSize:'1rem',fontWeight:500,marginBottom:'0.5rem'}).toString()};/**
* Renders the quantity picker enriched with current product data.
* @param {Object} props Props
* @returns {JSX}
*/var ProductUnitQuantityPicker=function ProductUnitQuantityPicker(_ref){var children=_ref.children,className=_ref.className,classes=_ref.classes,product=_ref.product,disabled=_ref.disabled,stockInfo=_ref.stockInfo,size=_ref.size,quantityLabel=_ref.quantityLabel,hideHeadline=_ref.hideHeadline;var _useWidgetSettings=useWidgetSettings('@shopgate/engage/product/components/UnitQuantityPicker'),_useWidgetSettings$sh=_useWidgetSettings.show,show=_useWidgetSettings$sh===void 0?hasNewServices():_useWidgetSettings$sh;var _useContext=useContext(ProductContext),quantity=_useContext.quantity,setQuantity=_useContext.setQuantity;var _useMemo=useMemo(function(){var min;var max;if((stockInfo===null||stockInfo===void 0?void 0:stockInfo.minOrderQuantity)>0){min=stockInfo.minOrderQuantity;}if((stockInfo===null||stockInfo===void 0?void 0:stockInfo.maxOrderQuantity)>0){max=stockInfo.maxOrderQuantity;}return{minValue:min,maxValue:max};},[stockInfo]),minValue=_useMemo.minValue,maxValue=_useMemo.maxValue;if(!product||!show){return null;}var unit=product.unit,hasCatchWeight=product.hasCatchWeight;var hasUnitWithDecimals=unit&&hasCatchWeight||false;return React.createElement(SurroundPortals,{portalName:PRODUCT_UNIT_QUANTITY_PICKER},React.createElement("div",{className:classNames(styles.root,className)},React.createElement("div",null,!hideHeadline&&React.createElement("div",{className:styles.title},React.createElement(I18n.Text,{string:"product.sections.quantity"})),React.createElement(UnitQuantityPicker// eslint-disable-next-line no-nested-ternary
,{className:(classes===null||classes===void 0?void 0:classes.picker)?classes.picker:hasUnitWithDecimals?big:small,unit:hasUnitWithDecimals?unit:null,maxDecimals:hasUnitWithDecimals?2:0,incrementStep:hasUnitWithDecimals?0.25:1,decrementStep:hasUnitWithDecimals?0.25:1,onChange:setQuantity,value:quantity,disabled:disabled,minValue:minValue,maxValue:maxValue,size:size,quantityLabel:quantityLabel})),children&&React.createElement("div",null,children)));};ProductUnitQuantityPicker.defaultProps={disabled:false,product:null,size:undefined,stockInfo:null,children:null,className:null,quantityLabel:null,classes:{picker:null},hideHeadline:false};export default withCurrentProduct(connect(ProductUnitQuantityPicker));