2020-02-14 03:43:13 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
|
|
|
|
import PropTypes from 'prop-types';
|
2020-02-25 09:32:59 +00:00
|
|
|
|
import { _n, sprintf } from '@wordpress/i18n';
|
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
|
import Label from '@woocommerce/base-components/label';
|
2020-06-05 10:00:19 +00:00
|
|
|
|
import Title from '@woocommerce/base-components/title';
|
|
|
|
|
import Panel from '@woocommerce/base-components/panel';
|
2020-02-27 18:28:36 +00:00
|
|
|
|
import classNames from 'classnames';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
|
|
|
|
import PackageOptions from './package-options';
|
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
|
|
const Package = ( {
|
|
|
|
|
className,
|
2020-03-09 11:28:26 +00:00
|
|
|
|
collapsible = false,
|
2020-02-14 03:43:13 +00:00
|
|
|
|
noResultsMessage,
|
|
|
|
|
onChange,
|
|
|
|
|
renderOption,
|
|
|
|
|
selected,
|
|
|
|
|
shippingRate,
|
|
|
|
|
showItems,
|
2020-02-27 18:28:36 +00:00
|
|
|
|
title,
|
2020-02-14 03:43:13 +00:00
|
|
|
|
} ) => {
|
2020-03-09 11:28:26 +00:00
|
|
|
|
const header = (
|
|
|
|
|
<>
|
2020-02-27 18:28:36 +00:00
|
|
|
|
{ title && (
|
2020-06-05 10:00:19 +00:00
|
|
|
|
<Title
|
2020-06-17 09:53:42 +00:00
|
|
|
|
className="wc-block-components-shipping-rates-control__package-title"
|
2020-06-05 10:00:19 +00:00
|
|
|
|
headingLevel="3"
|
|
|
|
|
>
|
2020-02-27 18:28:36 +00:00
|
|
|
|
{ title }
|
2020-06-05 10:00:19 +00:00
|
|
|
|
</Title>
|
2020-02-27 18:28:36 +00:00
|
|
|
|
) }
|
2020-02-14 03:43:13 +00:00
|
|
|
|
{ showItems && (
|
2020-06-17 09:53:42 +00:00
|
|
|
|
<ul className="wc-block-components-shipping-rates-control__package-items">
|
2020-02-25 09:32:59 +00:00
|
|
|
|
{ Object.values( shippingRate.items ).map( ( v ) => {
|
|
|
|
|
const name = decodeEntities( v.name );
|
|
|
|
|
const quantity = v.quantity;
|
|
|
|
|
return (
|
|
|
|
|
<li
|
|
|
|
|
key={ name }
|
2020-06-17 09:53:42 +00:00
|
|
|
|
className="wc-block-components-shipping-rates-control__package-item"
|
2020-02-25 09:32:59 +00:00
|
|
|
|
>
|
|
|
|
|
<Label
|
|
|
|
|
label={ `${ name } ×${ quantity }` }
|
|
|
|
|
screenReaderLabel={ sprintf(
|
|
|
|
|
// translators: %s name of the product (ie: Sunglasses), %d number of units in the current cart package
|
|
|
|
|
_n(
|
|
|
|
|
'%s (%d unit)',
|
|
|
|
|
'%s (%d units)',
|
|
|
|
|
quantity,
|
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
|
),
|
|
|
|
|
name,
|
|
|
|
|
quantity
|
|
|
|
|
) }
|
|
|
|
|
/>
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
} ) }
|
|
|
|
|
</ul>
|
2020-02-14 03:43:13 +00:00
|
|
|
|
) }
|
2020-03-09 11:28:26 +00:00
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
const body = (
|
|
|
|
|
<PackageOptions
|
|
|
|
|
className={ className }
|
|
|
|
|
noResultsMessage={ noResultsMessage }
|
|
|
|
|
onChange={ onChange }
|
|
|
|
|
options={ shippingRate.shipping_rates }
|
|
|
|
|
renderOption={ renderOption }
|
|
|
|
|
selected={ selected }
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
if ( collapsible ) {
|
|
|
|
|
return (
|
2020-06-05 10:00:19 +00:00
|
|
|
|
<Panel
|
2020-06-17 09:53:42 +00:00
|
|
|
|
className="wc-block-components-shipping-rates-control__package"
|
2020-06-12 12:07:02 +00:00
|
|
|
|
hasBorder={ true }
|
2020-03-09 11:28:26 +00:00
|
|
|
|
initialOpen={ true }
|
2020-06-05 10:00:19 +00:00
|
|
|
|
title={ header }
|
2020-03-09 11:28:26 +00:00
|
|
|
|
>
|
2020-06-05 10:00:19 +00:00
|
|
|
|
{ body }
|
|
|
|
|
</Panel>
|
2020-03-09 11:28:26 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={ classNames(
|
2020-06-17 09:53:42 +00:00
|
|
|
|
'wc-block-components-shipping-rates-control__package',
|
2020-03-09 11:28:26 +00:00
|
|
|
|
className
|
|
|
|
|
) }
|
|
|
|
|
>
|
|
|
|
|
{ header }
|
|
|
|
|
{ body }
|
2020-02-27 18:28:36 +00:00
|
|
|
|
</div>
|
2020-02-14 03:43:13 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Package.propTypes = {
|
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
|
renderOption: PropTypes.func.isRequired,
|
|
|
|
|
shippingRate: PropTypes.shape( {
|
|
|
|
|
shipping_rates: PropTypes.arrayOf( PropTypes.object ).isRequired,
|
2020-03-05 19:54:05 +00:00
|
|
|
|
items: PropTypes.arrayOf(
|
2020-02-25 09:32:59 +00:00
|
|
|
|
PropTypes.shape( {
|
|
|
|
|
name: PropTypes.string.isRequired,
|
2020-03-05 19:54:05 +00:00
|
|
|
|
key: PropTypes.string.isRequired,
|
2020-02-25 09:32:59 +00:00
|
|
|
|
quantity: PropTypes.number.isRequired,
|
|
|
|
|
} ).isRequired
|
|
|
|
|
).isRequired,
|
2020-02-14 03:43:13 +00:00
|
|
|
|
} ).isRequired,
|
|
|
|
|
className: PropTypes.string,
|
2020-03-09 11:28:26 +00:00
|
|
|
|
collapsible: PropTypes.bool,
|
2020-02-25 09:32:59 +00:00
|
|
|
|
noResultsMessage: PropTypes.string,
|
2020-02-14 03:43:13 +00:00
|
|
|
|
selected: PropTypes.string,
|
|
|
|
|
showItems: PropTypes.bool,
|
2020-02-27 18:28:36 +00:00
|
|
|
|
title: PropTypes.string,
|
2020-02-14 03:43:13 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Package;
|