woocommerce/plugins/woocommerce-blocks/assets/js/base/components/formatted-monetary-amount/index.tsx

114 lines
2.8 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import NumberFormat from 'react-number-format';
import type {
NumberFormatValues,
NumberFormatProps,
} from 'react-number-format';
import classNames from 'classnames';
import type { ReactElement } from 'react';
import type { Currency } from '@woocommerce/types';
/**
* Internal dependencies
*/
import './style.scss';
interface FormattedMonetaryAmountProps
extends Omit< NumberFormatProps, 'onValueChange' > {
className?: string;
displayType?: NumberFormatProps[ 'displayType' ];
allowNegative?: boolean;
isAllowed?: ( formattedValue: NumberFormatValues ) => boolean;
value: number | string; // Value of money amount.
currency: Currency | Record< string, never >; // Currency configuration object.
onValueChange?: ( unit: number ) => void; // Function to call when value changes.
Convert product-elements/price to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/7683) * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * Apply review feedback * Outsource supports section * bot: update checkstyle.xml * Resolve merge conflicts * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * bot: update checkstyle.xml * Empty-Commit * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * Resolve merge conflicts * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Apply review feedback * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * Empty-Commit * bot: update checkstyle.xml * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * bot: update checkstyle.xml * Empty checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Use BlockAttributes from @wordpress/blocks * Fix TS error * Fix TS errors * Fix TS error Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-12-23 15:30:10 +00:00
style?: React.CSSProperties | undefined;
renderText?: ( value: string ) => JSX.Element;
}
/**
* Formats currency data into the expected format for NumberFormat.
*/
const currencyToNumberFormat = (
currency: FormattedMonetaryAmountProps[ 'currency' ]
) => {
return {
Convert product-elements/price to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/7683) * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * Apply review feedback * Outsource supports section * bot: update checkstyle.xml * Resolve merge conflicts * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * bot: update checkstyle.xml * Empty-Commit * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * Resolve merge conflicts * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Apply review feedback * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * Empty-Commit * bot: update checkstyle.xml * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * bot: update checkstyle.xml * Empty checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Use BlockAttributes from @wordpress/blocks * Fix TS error * Fix TS errors * Fix TS error Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-12-23 15:30:10 +00:00
thousandSeparator: currency?.thousandSeparator,
decimalSeparator: currency?.decimalSeparator,
decimalScale: currency?.minorUnit,
fixedDecimalScale: true,
Convert product-elements/price to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/7683) * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * Apply review feedback * Outsource supports section * bot: update checkstyle.xml * Resolve merge conflicts * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * bot: update checkstyle.xml * Empty-Commit * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * Resolve merge conflicts * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Apply review feedback * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * Empty-Commit * bot: update checkstyle.xml * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * bot: update checkstyle.xml * Empty checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Use BlockAttributes from @wordpress/blocks * Fix TS error * Fix TS errors * Fix TS error Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-12-23 15:30:10 +00:00
prefix: currency?.prefix,
suffix: currency?.suffix,
isNumericString: true,
};
};
Convert product-elements/price to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/7683) * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * Apply review feedback * Outsource supports section * bot: update checkstyle.xml * Resolve merge conflicts * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * bot: update checkstyle.xml * Empty-Commit * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * Resolve merge conflicts * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Apply review feedback * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * Empty-Commit * bot: update checkstyle.xml * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * bot: update checkstyle.xml * Empty checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Use BlockAttributes from @wordpress/blocks * Fix TS error * Fix TS errors * Fix TS error Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-12-23 15:30:10 +00:00
type CustomFormattedMonetaryAmountProps = Omit<
FormattedMonetaryAmountProps,
'currency'
> & {
currency: Currency | Record< string, never >;
};
/**
* FormattedMonetaryAmount component.
*
* Takes a price and returns a formatted price using the NumberFormat component.
*/
const FormattedMonetaryAmount = ( {
className,
value: rawValue,
currency,
onValueChange,
displayType = 'text',
...props
Convert product-elements/price to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/7683) * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * Apply review feedback * Outsource supports section * bot: update checkstyle.xml * Resolve merge conflicts * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * bot: update checkstyle.xml * Empty-Commit * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * Resolve merge conflicts * Convert product-element/price to TypeScript * Apply feedback from woocommerce/woocommerce-blocks#7095 to this PR * Export block due to Cross-Sells dependency * Apply review feedback * Update assets/js/atomic/blocks/product-elements/price/edit.tsx Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error in cart-cross-sells-product.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS error regarding min_amount and max_amount * Empty-Commit * bot: update checkstyle.xml * Fix TS problems in product-elements/price/block.tsx * bot: update checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * bot: update checkstyle.xml * bot: update checkstyle.xml * Empty checkstyle.xml * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Solve TS errors * bot: update checkstyle.xml * Use BlockAttributes from @wordpress/blocks * Fix TS error * Fix TS errors * Fix TS error Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-12-23 15:30:10 +00:00
}: CustomFormattedMonetaryAmountProps ): ReactElement | null => {
const value =
typeof rawValue === 'string' ? parseInt( rawValue, 10 ) : rawValue;
if ( ! Number.isFinite( value ) ) {
return null;
}
const priceValue = value / 10 ** currency.minorUnit;
if ( ! Number.isFinite( priceValue ) ) {
return null;
}
const classes = classNames(
'wc-block-formatted-money-amount',
'wc-block-components-formatted-money-amount',
className
);
const numberFormatProps = {
...props,
...currencyToNumberFormat( currency ),
value: undefined,
currency: undefined,
onValueChange: undefined,
};
// Wrapper for NumberFormat onValueChange which handles subunit conversion.
const onValueChangeWrapper = onValueChange
? ( values: NumberFormatValues ) => {
const minorUnitValue = +values.value * 10 ** currency.minorUnit;
onValueChange( minorUnitValue );
}
: () => void 0;
return (
<NumberFormat
className={ classes }
displayType={ displayType }
{ ...numberFormatProps }
value={ priceValue }
onValueChange={ onValueChangeWrapper }
/>
);
};
export default FormattedMonetaryAmount;