-
Notifications
You must be signed in to change notification settings - Fork 219
Filter Products By Price block: don't allow to insert negative values on inputs #5123
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||||
|
@@ -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. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just reject the min inserted and default to the previous min instead of reseting max as well? this is more a UX thing and not code related but if something changes, just reset it instead of reseting two things. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My comment is probably unclear. For What do you think about this:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is great! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I rephrased it again because I just re-checked the logic better.
|
||||||||||
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, | ||||||||||
|
@@ -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; | ||||||||||
|
@@ -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; | ||||||||||
|
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; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably don't want to use this logic here given that we had issues with it, preferably we just deal with raw versions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is code is not about formatting the number, but it is just about validation. The code checks that on the right input the user can't insert a number that is minor then the number that there is in the left input. About the naming of the function, what do you think about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that |
||||||||
|
||||||||
return floatValue !== undefined && floatValue <= maxPrice; | ||||||||
}; | ||||||||
|
||||||||
export const withMinValueLimit = ( { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A doc block explaining the goal of this function would be great. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! Much better, would also add a comment to |
||||||||
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 | ||||||||
); | ||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isAllowed is rather confusing, what are we supposed to allow here? what I understood from the PR is that it doesn’t allow negative numbers, for me
allowNegative
should be sufficient.Also, do we need to pass a prop to
FormattedMonetaryAmount
just to support negative numbers? shouldn't just support them by default from what we pass tovalue
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used
isAllowed
because with the functionwithMaxValueLimit
andwithMinValueLimit
we can validate the input when the user changes the value in the input. About the naming, I choseisAllowed
to be consistent with the props of the libraryreact-number-format
.Do you mean that we should implement some custom logic to check if the value is negative? I preferred to use a ready solution that
react-number-format
giveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't understand the goal of
isAllowed
, can'tallowNegative
do that instead? My comment is mostly why we need two.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal
isAllowed
is to do an extra check on the input field:If on the right input the user write 30, on the left input the user can't write 31 (and this is the goal of the functions
withMinValueLimit
andwithMaxValueLimit
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took me sometime to understand
isAllowed
. I initially thought this is a prop you introduced, I see it's part ofreact-format-money
, Not a great naming from their side really, but I see its value now.