/** * External dependencies */ import classNames from 'classnames'; import { _n, sprintf } from '@wordpress/i18n'; import { decodeEntities } from '@wordpress/html-entities'; import type { ReactElement } from 'react'; import { Panel } from '@woocommerce/blocks-checkout'; import Label from '@woocommerce/base-components/label'; import { useShippingData, useStoreEvents, } from '@woocommerce/base-context/hooks'; import { sanitizeHTML } from '@woocommerce/utils'; /** * Internal dependencies */ import PackageRates from './package-rates'; import type { PackageProps } from './types'; import './style.scss'; export const ShippingRatesControlPackage = ( { packageId, className = '', noResultsMessage, renderOption, packageData, collapsible, showItems, }: PackageProps ): ReactElement => { const { selectShippingRate } = useShippingData(); const { dispatchCheckoutEvent } = useStoreEvents(); const multiplePackages = document.querySelectorAll( '.wc-block-components-shipping-rates-control__package' ).length > 1; // If showItems is not set, we check if we have multiple packages. // We sometimes don't want to show items even if we have multiple packages. const shouldShowItems = showItems ?? multiplePackages; // If collapsible is not set, we check if we have multiple packages. // We sometimes don't want to collapse even if we have multiple packages. const shouldBeCollapsible = collapsible ?? multiplePackages; const header = ( <> { ( shouldBeCollapsible || shouldShowItems ) && (
) } { shouldShowItems && ( ) } ); const packageRatesProps = { className, noResultsMessage, rates: packageData.shipping_rates, onSelectRate: ( newShippingRateId: string ) => { selectShippingRate( newShippingRateId, packageId ); dispatchCheckoutEvent( 'set-selected-shipping-rate', { shippingRateId: newShippingRateId, } ); }, selectedRate: packageData.shipping_rates.find( ( rate ) => rate.selected ), renderOption, }; if ( shouldBeCollapsible ) { return ( ); } return (
{ header }
); }; export default ShippingRatesControlPackage;