2021-03-05 14:03:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { isValidElement } from '@wordpress/element';
|
|
|
|
import type { ReactElement, ReactNode } from 'react';
|
2023-11-20 12:53:41 +00:00
|
|
|
import type { Currency } from '@woocommerce/types';
|
2021-03-05 14:03:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2023-11-20 12:53:41 +00:00
|
|
|
import FormattedMonetaryAmount from '../../formatted-monetary-amount';
|
2021-03-05 14:03:48 +00:00
|
|
|
|
2021-12-21 17:08:57 +00:00
|
|
|
export interface TotalsItemProps {
|
2023-11-20 12:53:41 +00:00
|
|
|
className?: string | undefined;
|
2021-03-05 14:03:48 +00:00
|
|
|
currency: Currency;
|
|
|
|
label: string;
|
|
|
|
// Value may be a number, or react node. Numbers are passed to FormattedMonetaryAmount.
|
|
|
|
value: number | ReactNode;
|
|
|
|
description?: ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TotalsItemValue = ( {
|
|
|
|
value,
|
|
|
|
currency,
|
|
|
|
}: Partial< TotalsItemProps > ): ReactElement | null => {
|
|
|
|
if ( isValidElement( value ) ) {
|
2024-03-20 10:42:05 +00:00
|
|
|
return (
|
|
|
|
<div className="wc-block-components-totals-item__value">
|
|
|
|
{ value }
|
|
|
|
</div>
|
|
|
|
);
|
2021-03-05 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Number.isFinite( value ) ? (
|
|
|
|
<FormattedMonetaryAmount
|
|
|
|
className="wc-block-components-totals-item__value"
|
|
|
|
currency={ currency || {} }
|
|
|
|
value={ value as number }
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const TotalsItem = ( {
|
|
|
|
className,
|
|
|
|
currency,
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
description,
|
|
|
|
}: TotalsItemProps ): ReactElement => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-totals-item',
|
|
|
|
className
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<span className="wc-block-components-totals-item__label">
|
|
|
|
{ label }
|
|
|
|
</span>
|
|
|
|
<TotalsItemValue value={ value } currency={ currency } />
|
|
|
|
<div className="wc-block-components-totals-item__description">
|
|
|
|
{ description }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TotalsItem;
|