-
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 all commits
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 |
---|---|---|
@@ -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; | ||
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 && 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 | ||
); | ||
}; |
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.