-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix amount text field breaking when decimal separator has spaces #14057
Fix amount text field breaking when decimal separator has spaces #14057
Conversation
📲 You can test the changes from this Pull Request in WooCommerce iOS by scanning the QR code below to install the corresponding build.
|
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.
Works nicely, thanks for addressing this! I found some oddities when testing, but nothing with priority or in the scope of this PR, I've reported them separately:
- Coupons price input doesn't account for limit of decimals in store settings #14071
- Custom Amounts UI placeholder shows additional spaces if these were added to store settings #14072
I'll go ahead and merge the changes. Left a few comments, but feel free to address them when you're back from AFK 🙇
var sanitizedGroupingSeparator: String { | ||
return groupingSeparator.replacingOccurrences(of: " ", with: "") | ||
} |
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.
var sanitizedGroupingSeparator: String { | |
return groupingSeparator.replacingOccurrences(of: " ", with: "") | |
} |
This one is currently not used anywhere, so perhaps we want to remove it? If we don't, maybe we should rename it from sanitizedGroupingSeparator
to sanitizedThousandSeparator
?
The CodingKey
in the CurrencySettings
doesn't seem to follow the decoding pattern for this value: If we check the Networking.SystemStatus+Settings
it's initially decoded as case thousandSeparator = "thousand_separator"
, but at some point we started to name it "groupingSeparator" in CurrencySettings (perhaps because of the native NumberFormatter?) so perhaps makes sense to refactor the name here and there for consistency, wdyt?
@@ -51,7 +51,7 @@ struct PriceInputFormatter: UnitInputFormatter { | |||
|
|||
let formattedText = latinText | |||
// Replace any characters not in the set of 0-9 or minus symbol with the current decimal separator configured on website | |||
.replacingOccurrences(of: "[^0-9-]", with: currencySettings.decimalSeparator, options: .regularExpression) | |||
.replacingOccurrences(of: "[^0-9-]", with: currencySettings.sanitizedDecimalSeparator, options: .regularExpression) |
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.
💯
// When | ||
let input = "189.20" | ||
|
||
// Then formatting ignores spaces in store decimal separator settings | ||
XCTAssertEqual(formatter.format(input: input), "189.20") |
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.
We're not testing for space sanitization here, are we? If the input
simulates what the merchant introduces in the textfield, and then this input is sanitized within formatter.format(value)
, shouldn't we pass the value in as malformed to the formatter? (The test still succeeds)
// When | |
let input = "189.20" | |
// Then formatting ignores spaces in store decimal separator settings | |
XCTAssertEqual(formatter.format(input: input), "189.20") | |
// When | |
let input = "189. 20" | |
// Then formatting ignores spaces in store decimal separator settings | |
XCTAssertEqual(formatter.format(input: input), "189.20") |
@iamgabrielma thanks for taking care of this - reviewing, creating additional tasks, and merging! |
Closes: #14045
Description
The amount of text field breaks when the decimal separator has spaces. It breaks a few text fields - including adding custom amounts and coupons.
PriceFieldFormatter
andPriceInputFormatter
don't take into consideration that the decimal separator has spaces.Solution
As I tested, even the native number formatter ignores spaces in the decimal separator when converting numbers back to decimal. I think the best option is to ignore spaces in separators when formatting the amount in the text field. It helps avoid issues between number conversions. In labels, we can continue displaying spaces.
The solution is to use decimal and thousands separators without spaces in
PriceFieldFormatter
andPriceInputFormatter
. I also explored a possibility of removing one of these formatters but they have quite a different behavior around the edges so I left it outside the scope.Steps to reproduce
Coupons
Custom Amount
Testing information
Screenshots
Custom Amount - Decimals
Custom.Amount.-.Decimals.mp4
Coupon - Decimals
Coupon.-.Decimals.mp4
Coupon - Decimal and Thousands
Coupon.-.Decimal.and.Thousands.mp4
RELEASE-NOTES.txt
if necessary.Reviewer (or Author, in the case of optional code reviews):
Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement: