UNPKG

@commercelayer/react-components

Version:
2 lines 1.2 kB
"use client"; import{jsx as _jsx}from"react/jsx-runtime";import{useState,useEffect,useContext}from"react";import getAmount from"../../utils/getAmount";import ShippingMethodChildrenContext from"../../context/ShippingMethodChildrenContext";import Parent from"../utils/Parent";export function ShippingMethodPrice(props){const{base="price_amount",type="for_shipment",format="formatted",labelFreeOver="Free",labelExternal="Price estimates after the shipping method selection",...p}=props,{shippingMethod}=useContext(ShippingMethodChildrenContext),[price,setPrice]=useState(""),[priceCents,setPriceCents]=useState(0),scheme=shippingMethod?.scheme;useEffect(()=>{if(shippingMethod){const p2=getAmount({base,type,format,obj:shippingMethod});setPrice(p2);const pCents=getAmount({base:"price_amount",type:"for_shipment",format:"cents",obj:shippingMethod});setPriceCents(pCents)}return()=>{setPrice(""),setPriceCents(0)}},[shippingMethod]);const parentProps={price,scheme,...p},finalPrice=priceCents===0?labelFreeOver:price;return props.children?_jsx(Parent,{...parentProps,children:props.children}):_jsx("span",{...p,children:scheme==="external"?labelExternal:finalPrice})}export default ShippingMethodPrice;