Skip to content

Commit 88b29cb

Browse files
committed
Now it's possible to edit the Menu order with negative values
1 parent 0f7c341 commit 88b29cb

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

WooCommerce/Classes/Tools/UnitInputFormatter/IntegerInputFormatter.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import Foundation
33
/// `UnitInputFormatter` implementation for integer number input (positive, negative, or zero)
44
///
55
struct IntegerInputFormatter: UnitInputFormatter {
6+
7+
/// The default value used by format() when the text is empty or the number formatter return a nil value
8+
///
9+
let defaultValue: String
10+
611
private let numberFormatter: NumberFormatter = {
712
let numberFormatter = NumberFormatter()
813
numberFormatter.allowsFloats = false
@@ -11,18 +16,28 @@ struct IntegerInputFormatter: UnitInputFormatter {
1116

1217
func isValid(input: String) -> Bool {
1318
guard input.isEmpty == false else {
14-
// Allows empty input to be replaced by 0.
19+
// Allows empty input to be replaced by defaultValue
1520
return true
1621
}
17-
return numberFormatter.number(from: input) != nil
22+
return numberFormatter.number(from: input) != nil || input == "-"
1823
}
1924

2025
func format(input text: String?) -> String {
2126
guard let text = text, text.isEmpty == false else {
22-
return "0"
27+
return defaultValue
28+
}
29+
30+
var formattedText = numberFormatter.number(from: text)?.stringValue ?? defaultValue
31+
32+
// The minus sign is mantained if present
33+
let minus: Character = "-"
34+
if text.first == minus {
35+
formattedText = String(minus) + formattedText.replacingOccurrences(of: "-", with: "")
2336
}
24-
25-
let formattedText = numberFormatter.number(from: text)?.stringValue ?? "0"
2637
return formattedText
2738
}
39+
40+
init(defaultValue: String = "0") {
41+
self.defaultValue = defaultValue
42+
}
2843
}

WooCommerce/Classes/ViewRelated/Products/Edit Product/Product Settings/Menu Order/ProductMenuOrderViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private extension ProductMenuOrderViewController {
146146
}
147147
}, onTextDidBeginEditing: {
148148
//TODO: Add analytics track
149-
}, inputFormatter: IntegerInputFormatter())
149+
}, inputFormatter: IntegerInputFormatter(defaultValue: ""))
150150
cell.configure(viewModel: viewModel)
151151
cell.textField.applyBodyStyle()
152152
cell.textField.keyboardType = .numbersAndPunctuation

WooCommerce/WooCommerceTests/Tools/IntegerInputFormatterTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import XCTest
33
@testable import WooCommerce
44

55
final class IntegerInputFormatterTests: XCTestCase {
6-
private let formatter = IntegerInputFormatter()
6+
private let formatter = IntegerInputFormatter(defaultValue: "0")
77

88
// MARK: test cases for `isValid(input:)`
99

0 commit comments

Comments
 (0)