Skip to content

Commit 3e12f8d

Browse files
authored
Merge pull request #2207 from woocommerce/issue/2205-edit-the-menu-order-with-negative-values
Product Settings: Menu Order now allow negative values
2 parents 269cee6 + 248b620 commit 3e12f8d

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

WooCommerce/Classes/Tools/UnitInputFormatter/IntegerInputFormatter.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ 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 returns a nil value
8+
///
9+
let defaultValue: String
10+
11+
private let minus: Character = "-"
12+
613
private let numberFormatter: NumberFormatter = {
714
let numberFormatter = NumberFormatter()
815
numberFormatter.allowsFloats = false
@@ -11,18 +18,27 @@ struct IntegerInputFormatter: UnitInputFormatter {
1118

1219
func isValid(input: String) -> Bool {
1320
guard input.isEmpty == false else {
14-
// Allows empty input to be replaced by 0.
21+
// Allows empty input to be replaced by defaultValue
1522
return true
1623
}
17-
return numberFormatter.number(from: input) != nil
24+
return numberFormatter.number(from: input) != nil || input == String(minus)
1825
}
1926

2027
func format(input text: String?) -> String {
2128
guard let text = text, text.isEmpty == false else {
22-
return "0"
29+
return defaultValue
2330
}
2431

25-
let formattedText = numberFormatter.number(from: text)?.stringValue ?? "0"
32+
var formattedText = numberFormatter.number(from: text)?.stringValue ?? defaultValue
33+
34+
// The minus sign is maintained if present
35+
if text.first == minus {
36+
formattedText = String(minus) + formattedText.replacingOccurrences(of: "-", with: "")
37+
}
2638
return formattedText
2739
}
40+
41+
init(defaultValue: String = "0") {
42+
self.defaultValue = defaultValue
43+
}
2844
}

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: 17 additions & 2 deletions
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: "1")
77

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

@@ -37,11 +37,16 @@ final class IntegerInputFormatterTests: XCTestCase {
3737
XCTAssertTrue(formatter.isValid(input: input))
3838
}
3939

40+
func testMinusSignInputIsValid() {
41+
let input = "-"
42+
XCTAssertTrue(formatter.isValid(input: input))
43+
}
44+
4045
// MARK: test cases for `format(input:)`
4146

4247
func testFormattingEmptyInput() {
4348
let input = ""
44-
XCTAssertEqual(formatter.format(input: input), "0")
49+
XCTAssertEqual(formatter.format(input: input), "1")
4550
}
4651

4752
func testFormattingInputWithLeadingZeros() {
@@ -58,4 +63,14 @@ final class IntegerInputFormatterTests: XCTestCase {
5863
let input = "-3412424214"
5964
XCTAssertEqual(formatter.format(input: input), "-3412424214")
6065
}
66+
67+
func testFormattingMinusSignInput() {
68+
let input = "-"
69+
XCTAssertEqual(formatter.format(input: input), "-1")
70+
}
71+
72+
func testFormattingMultipleMinusSignInput() {
73+
let input = "--"
74+
XCTAssertEqual(formatter.format(input: input), "-1")
75+
}
6176
}

0 commit comments

Comments
 (0)