Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Filter Products By Price block: don't allow to insert negative values…
Browse files Browse the repository at this point in the history
… on inputs (#5123)

* Don't allow to insert negative values on input for Filter Products By Price block #2695

Don't allow to insert negative values on input for Filter Products By Price block

* renaming util functions and add comments
  • Loading branch information
gigitux authored Jan 6, 2022
1 parent c90d6d2 commit f2a2a3e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/js/base/components/formatted-monetary-amount/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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.
Expand Down
29 changes: 29 additions & 0 deletions assets/js/base/components/price-slider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Currency, isObject } from '@woocommerce/types';
import './style.scss';
import { constrainRangeSliderValues } from './constrain-range-slider-values';
import FilterSubmitButton from '../filter-submit-button';
import { isValidMaxValue, isValidMinValue } from './utils';

export interface PriceSliderProps {
/**
Expand Down Expand Up @@ -240,9 +241,27 @@ const PriceSlider = ( {
) {
return;
}

const isMin = event.target.classList.contains(
'wc-block-price-filter__amount--min'
);

// When the user inserts in the max price input a value less or equal than the current minimum price,
// we set to 0 the minimum price.
if ( minPriceInput >= maxPriceInput ) {
const values = constrainRangeSliderValues(
[ 0, maxPriceInput ],
null,
null,
stepValue,
isMin
);
return onChange( [
parseInt( values[ 0 ], 10 ),
parseInt( values[ 1 ], 10 ),
] );
}

const values = constrainRangeSliderValues(
[ minPriceInput, maxPriceInput ],
null,
Expand Down Expand Up @@ -351,6 +370,12 @@ const PriceSlider = ( {
'Filter products by minimum price',
'woo-gutenberg-products-block'
) }
allowNegative={ false }
isAllowed={ isValidMinValue( {
minConstraint,
minorUnit: currency.minorUnit,
currentMaxValue: maxPriceInput,
} ) }
onValueChange={ ( value ) => {
if ( value === minPriceInput ) {
return;
Expand All @@ -369,6 +394,10 @@ const PriceSlider = ( {
'Filter products by maximum price',
'woo-gutenberg-products-block'
) }
isAllowed={ isValidMaxValue( {
maxConstraint,
minorUnit: currency.minorUnit,
} ) }
onValueChange={ ( value ) => {
if ( value === maxPriceInput ) {
return;
Expand Down
41 changes: 41 additions & 0 deletions assets/js/base/components/price-slider/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* External dependencies
*/
import { NumberFormatValues } from 'react-number-format';

/**
Check if that the value is minor than the max price and greater than 0.
*/
export const isValidMaxValue = ( {
maxConstraint,
minorUnit,
}: {
maxConstraint: number;
minorUnit: number;
} ) => ( { floatValue }: NumberFormatValues ): boolean => {
const maxPrice = maxConstraint / 10 ** minorUnit;

return floatValue !== undefined && floatValue <= maxPrice && floatValue > 0;
};

/**
Check if that the value is minor than the max price and greater than 0.
*/
export const isValidMinValue = ( {
minConstraint,
currentMaxValue,
minorUnit,
}: {
minConstraint: number;
currentMaxValue: number;
minorUnit: number;
} ) => ( { floatValue }: NumberFormatValues ): boolean => {
const minPrice = minConstraint / 10 ** minorUnit;
const currentMaxPrice = currentMaxValue / 10 ** minorUnit;

return (
floatValue !== undefined &&
floatValue >= minPrice &&
floatValue < currentMaxPrice
);
};

0 comments on commit f2a2a3e

Please sign in to comment.