Products pricing section code cleanup (#34560)

* Switch to formatAmount from formatCurrency

* Rename priceValidation to sanitizeAndSetPrice

* Swap order of name, value args in sanitizeAndSetPrice
This commit is contained in:
Matt Sherman 2022-09-06 10:48:50 -04:00 committed by GitHub
parent a86e387667
commit c87d3c68cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 6 deletions

View File

@ -45,7 +45,7 @@ export const PricingSection: React.FC = () => {
const context = useContext( CurrencyContext );
const { getCurrencyConfig } = context;
const { decimalSeparator } = getCurrencyConfig();
const priceValidation = ( value: string, name: string ) => {
const sanitizeAndSetPrice = ( name: string, value: string ) => {
// Build regex to strip out everything except digits, decimal point and minus sign.
const regex = new RegExp(
NUMBERS_AND_DECIMAL_SEPARATOR.replace( '%s', decimalSeparator ),
@ -134,7 +134,7 @@ export const PricingSection: React.FC = () => {
context,
} ) }
onChange={ ( value: string ) =>
priceValidation( value, 'regular_price' )
sanitizeAndSetPrice( 'regular_price', value )
}
/>
{ ! isTaxSettingsResolving && (
@ -154,7 +154,7 @@ export const PricingSection: React.FC = () => {
context,
} ) }
onChange={ ( value: string ) =>
priceValidation( value, 'sale_price' )
sanitizeAndSetPrice( 'sale_price', value )
}
/>
</div>

View File

@ -11,7 +11,7 @@ import { NUMBERS_AND_ALLOWED_CHARS } from '../constants';
type gettersProps = {
context?: {
formatCurrency: ( number: number | string ) => string;
formatAmount: ( number: number | string ) => string;
getCurrencyConfig: () => {
code: string;
symbol: string;
@ -80,7 +80,7 @@ export const getInputControlProps = ( {
if ( ! context ) {
return;
}
const { formatCurrency, getCurrencyConfig } = context;
const { formatAmount, getCurrencyConfig } = context;
const { decimalSeparator, symbol, symbolPosition, thousandSeparator } =
getCurrencyConfig();
const currencyPosition = symbolPosition.includes( 'left' )
@ -98,7 +98,7 @@ export const getInputControlProps = ( {
const currencyString =
value === undefined
? value
: formatCurrency( value ).replace( regex, '' );
: formatAmount( value ).replace( regex, '' );
return {
value: currencyString,
[ currencyPosition ]: symbol,