Skip to content

Commit

Permalink
Move the cursor to the end of the text after pasting
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisNikolic committed Oct 17, 2024
1 parent a73cb8c commit a368694
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public final class PrimerCardNumberFieldView: PrimerTextFieldView {
}

primerTextField.text = newText.withoutWhiteSpace.separate(every: 4, with: " ")

// Detect pasting of the card number
if string.count > 1 && (primerTextField.internalText?.count ?? 0) > 1 {
if let text = primerTextField.internalText {

// Get the position of the last character in the string
if let endPosition = primerTextField.position(from: primerTextField.beginningOfDocument, offset: text.count) {
DispatchQueue.main.async {
// Create a UITextRange from the endPosition to endPosition (for placing the cursor at the end)
primerTextField.selectedTextRange = primerTextField.textRange(from: endPosition, to: endPosition)
}
}
}
}

return false
}
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/Primer/UI/PrimerCardNumberFieldViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,26 @@ final class PrimerCardNumberFieldViewTests: XCTestCase {

waitForExpectations(timeout: 2.0)
}

func testCursorMovesToEndAfterPasting() throws {
view.text = ""

// Simulate pasting a card number
_ = view.textField(view.textField,
shouldChangeCharactersIn: NSRange(location: 0, length: 0),
replacementString: "4242424242424242")

// Add a small delay to ensure the cursor movement code has executed
let expectation = XCTestExpectation(description: "Wait for cursor to move")

DispatchQueue.main.async {
// Check if the cursor is at the end of the text
let expectedPosition = self.view.textField.position(from: self.view.textField.endOfDocument, offset: 0)
XCTAssertEqual(self.view.textField.selectedTextRange?.start, expectedPosition)

expectation.fulfill()
}

wait(for: [expectation], timeout: 0.3)
}
}

0 comments on commit a368694

Please sign in to comment.