@commercelayer/react-components
Version:
The Official Commerce Layer React Components
2 lines • 939 B
JavaScript
"use client";
import{jsx as _jsx}from"react/jsx-runtime";import{useState,useEffect}from"react";import Parent from"../utils/Parent";import getLineItemsCount from"../../utils/getLineItemsCount";import LineItemContext from"../../context/LineItemContext";import useCustomContext from"../../utils/hooks/useCustomContext";export function LineItemsCount(props){const{children,typeAccepted,...p}=props,{lineItems}=useCustomContext({context:LineItemContext,contextComponentName:"LineItemsContainer",currentComponentName:"LineItemsCount",key:"lineItems"}),[quantity,setQuantity]=useState(0);useEffect(()=>{if(lineItems&&lineItems.length>0){const qty=getLineItemsCount({lineItems:lineItems||[],typeAccepted});setQuantity(qty)}return()=>{setQuantity(0)}},[lineItems,typeAccepted]);const parentProps={quantity,typeAccepted,...p};return children?_jsx(Parent,{...parentProps,children}):_jsx("span",{...p,children:quantity})}export default LineItemsCount;