2019-12-18 11:29:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-03-09 10:55:24 +00:00
|
|
|
import NumberFormat, {
|
|
|
|
NumberFormatValues,
|
|
|
|
NumberFormatProps,
|
|
|
|
} from 'react-number-format';
|
2020-01-14 20:52:42 +00:00
|
|
|
import classNames from 'classnames';
|
2021-03-09 10:55:24 +00:00
|
|
|
import type { Currency } from '@woocommerce/price-format';
|
|
|
|
import type { ReactElement } from 'react';
|
2020-01-14 20:52:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2019-12-18 11:29:20 +00:00
|
|
|
|
2021-03-09 10:55:24 +00:00
|
|
|
interface FormattedMonetaryAmountProps {
|
|
|
|
className?: string;
|
|
|
|
displayType?: NumberFormatProps[ 'displayType' ];
|
2021-03-15 15:03:47 +00:00
|
|
|
value: number | string; // Value of money amount.
|
2021-03-09 10:55:24 +00:00
|
|
|
currency: Currency | Record< string, never >; // Currency configuration object.
|
|
|
|
onValueChange?: ( unit: number ) => void; // Function to call when value changes.
|
|
|
|
}
|
|
|
|
|
2019-12-18 11:29:20 +00:00
|
|
|
/**
|
|
|
|
* Formats currency data into the expected format for NumberFormat.
|
|
|
|
*/
|
2021-03-09 10:55:24 +00:00
|
|
|
const currencyToNumberFormat = (
|
|
|
|
currency: FormattedMonetaryAmountProps[ 'currency' ]
|
|
|
|
) => {
|
2019-12-18 11:29:20 +00:00
|
|
|
return {
|
|
|
|
thousandSeparator: currency.thousandSeparator,
|
|
|
|
decimalSeparator: currency.decimalSeparator,
|
|
|
|
decimalScale: currency.minorUnit,
|
2020-01-08 20:10:29 +00:00
|
|
|
fixedDecimalScale: true,
|
2019-12-18 11:29:20 +00:00
|
|
|
prefix: currency.prefix,
|
|
|
|
suffix: currency.suffix,
|
|
|
|
isNumericString: true,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-03-09 10:55:24 +00:00
|
|
|
* FormattedMonetaryAmount component.
|
2019-12-18 11:29:20 +00:00
|
|
|
*
|
|
|
|
* Takes a price and returns a formatted price using the NumberFormat component.
|
|
|
|
*/
|
|
|
|
const FormattedMonetaryAmount = ( {
|
2021-03-09 10:55:24 +00:00
|
|
|
className,
|
2021-03-15 15:03:47 +00:00
|
|
|
value: rawValue,
|
2019-12-18 11:29:20 +00:00
|
|
|
currency,
|
2021-03-09 10:55:24 +00:00
|
|
|
onValueChange,
|
2021-03-05 14:03:48 +00:00
|
|
|
displayType = 'text',
|
2019-12-18 11:29:20 +00:00
|
|
|
...props
|
2021-03-09 10:55:24 +00:00
|
|
|
}: FormattedMonetaryAmountProps ): ReactElement | null => {
|
2021-03-15 15:03:47 +00:00
|
|
|
const value =
|
|
|
|
typeof rawValue === 'string' ? parseInt( rawValue, 10 ) : rawValue;
|
|
|
|
|
2021-03-05 14:03:48 +00:00
|
|
|
if ( ! Number.isFinite( value ) ) {
|
2020-05-01 09:59:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-12-18 11:29:20 +00:00
|
|
|
const priceValue = value / 10 ** currency.minorUnit;
|
|
|
|
|
2020-05-01 09:59:27 +00:00
|
|
|
if ( ! Number.isFinite( priceValue ) ) {
|
2019-12-18 11:29:20 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:38:31 +00:00
|
|
|
const classes = classNames(
|
|
|
|
'wc-block-formatted-money-amount',
|
|
|
|
'wc-block-components-formatted-money-amount',
|
|
|
|
className
|
|
|
|
);
|
2019-12-18 11:29:20 +00:00
|
|
|
const numberFormatProps = {
|
|
|
|
...props,
|
|
|
|
...currencyToNumberFormat( currency ),
|
|
|
|
value: undefined,
|
|
|
|
currency: undefined,
|
|
|
|
onValueChange: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wrapper for NumberFormat onValueChange which handles subunit conversion.
|
|
|
|
const onValueChangeWrapper = onValueChange
|
2021-03-09 10:55:24 +00:00
|
|
|
? ( values: NumberFormatValues ) => {
|
|
|
|
const minorUnitValue =
|
|
|
|
( ( values.value as unknown ) as number ) *
|
|
|
|
10 ** currency.minorUnit;
|
2019-12-18 11:29:20 +00:00
|
|
|
onValueChange( minorUnitValue );
|
|
|
|
}
|
2021-03-09 10:55:24 +00:00
|
|
|
: () => {
|
|
|
|
/* not used */
|
|
|
|
};
|
2019-12-18 11:29:20 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<NumberFormat
|
2020-01-14 20:52:42 +00:00
|
|
|
className={ classes }
|
2021-03-09 10:55:24 +00:00
|
|
|
displayType={ displayType }
|
2019-12-18 11:29:20 +00:00
|
|
|
{ ...numberFormatProps }
|
|
|
|
value={ priceValue }
|
|
|
|
onValueChange={ onValueChangeWrapper }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormattedMonetaryAmount;
|