Skip to content

Commit

Permalink
[Experimental] Product Filter: Price (Beta) block: Prevent invalid pr…
Browse files Browse the repository at this point in the history
…ice range selection in Product Filter Price block (#45403)

* Prevent invalid price range selection in Product Filter Price block

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
2 people authored and Konamiman committed Mar 13, 2024
1 parent facd3ce commit 4fd386b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { store, getContext } from '@woocommerce/interactivity';
import { store, getContext, getElement } from '@woocommerce/interactivity';
import { formatPrice, getCurrency } from '@woocommerce/price-format';
import { HTMLElementEvent } from '@woocommerce/types';

Expand Down Expand Up @@ -70,17 +70,28 @@ store< PriceFilterStore >( 'woocommerce/product-filter-price', {
type === 'min'
? Math.min(
Number.isNaN( value ) ? minRange : value,
maxRange - 1
maxPrice
)
: minPrice;
const currentMaxPrice =
type === 'max'
? Math.max(
Number.isNaN( value ) ? maxRange : value,
minRange + 1
minPrice
)
: maxPrice;

// In some occasions the input element is updated with the incorrect value.
// By using the element that triggered the event, we can ensure the correct value is used for the input.
const element = getElement();
if ( type === 'min' ) {
element.ref.value = currentMinPrice;
}

if ( type === 'max' ) {
element.ref.value = currentMaxPrice;
}

context.minPrice = currentMinPrice;
context.maxPrice = currentMaxPrice;

Expand Down
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix an issue where shoppers could select invalid price ranges in the Product Filter: Price (Beta) block

0 comments on commit 4fd386b

Please sign in to comment.