2019-12-18 11:29:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-04-08 13:47:19 +00:00
|
|
|
import NumberFormat from 'react-number-format';
|
|
|
|
import type {
|
2021-03-09 10:55:24 +00:00
|
|
|
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 { ReactElement } from 'react';
|
2021-11-26 17:03:12 +00:00
|
|
|
import type { Currency } from '@woocommerce/types';
|
2020-01-14 20:52:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2019-12-18 11:29:20 +00:00
|
|
|
|
2021-12-15 16:54:49 +00:00
|
|
|
interface FormattedMonetaryAmountProps
|
|
|
|
extends Omit< NumberFormatProps, 'onValueChange' > {
|
2021-03-09 10:55:24 +00:00
|
|
|
className?: string;
|
|
|
|
displayType?: NumberFormatProps[ 'displayType' ];
|
2022-01-06 14:30:34 +00:00
|
|
|
allowNegative?: boolean;
|
|
|
|
isAllowed?: ( formattedValue: NumberFormatValues ) => boolean;
|
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.
|
2021-11-26 17:03:12 +00:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
renderText?: ( value: string ) => JSX.Element;
|
2021-03-09 10:55:24 +00:00
|
|
|
}
|
|
|
|
|
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 ) => {
|
2021-12-15 16:54:49 +00:00
|
|
|
const minorUnitValue = +values.value * 10 ** currency.minorUnit;
|
2019-12-18 11:29:20 +00:00
|
|
|
onValueChange( minorUnitValue );
|
|
|
|
}
|
2021-12-15 16:54:49 +00:00
|
|
|
: () => void 0;
|
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;
|