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

Commit

Permalink
Don't allow to insert negative values on input for Filter Products By…
Browse files Browse the repository at this point in the history
… Price block #2695

Don't allow to insert negative values on input for Filter Products By Price block
  • Loading branch information
gigitux committed Nov 10, 2021
1 parent 1146617 commit b51cf86
Show file tree
Hide file tree
Showing 3 changed files with 66 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 @@ -17,6 +17,8 @@ import './style.scss';
interface FormattedMonetaryAmountProps {
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.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isObject } from '@woocommerce/types';
import './style.scss';
import { constrainRangeSliderValues } from './constrain-range-slider-values';
import FilterSubmitButton from '../filter-submit-button';
import { withMaxValueLimit, withMinValueLimit } from './utils';

/**
* Price slider component.
Expand Down Expand Up @@ -212,9 +213,27 @@ const PriceSlider = ( {
) {
return;
}

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

// When user insert in the minimum price input a value greater than the max price,
// we update the value of the slider to 0 - max.
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 @@ -322,6 +341,12 @@ const PriceSlider = ( {
'Filter products by minimum price',
'woo-gutenberg-products-block'
) }
allowNegative={ false }
isAllowed={ withMinValueLimit( {
minConstraint,
minorUnit: currency.minorUnit,
currentMaxValue: maxPriceInput,
} ) }
onValueChange={ ( value ) => {
if ( value === minPriceInput ) {
return;
Expand All @@ -340,6 +365,10 @@ const PriceSlider = ( {
'Filter products by maximum price',
'woo-gutenberg-products-block'
) }
isAllowed={ withMaxValueLimit( {
maxConstraint,
minorUnit: currency.minorUnit,
} ) }
onValueChange={ ( value ) => {
if ( value === maxPriceInput ) {
return;
Expand Down
35 changes: 35 additions & 0 deletions assets/js/base/components/price-slider/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* External dependencies
*/
import { NumberFormatValues } from 'react-number-format';

export const withMaxValueLimit = ( {
maxConstraint,
minorUnit,
}: {
maxConstraint: number;
minorUnit: number;
} ) => ( { floatValue }: NumberFormatValues ): boolean => {
const maxPrice = maxConstraint / 10 ** minorUnit;

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

export const withMinValueLimit = ( {
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 b51cf86

Please sign in to comment.