-
Notifications
You must be signed in to change notification settings - Fork 6
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: Move the cursor to the end of the text after pasting the card number #1024
fix: Move the cursor to the end of the text after pasting the card number #1024
Conversation
if !newText.withoutWhiteSpace.isNumeric && !string.isEmpty { return false } | ||
|
||
primerTextField.internalText = newText |
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.
Setting text
below will also overwrite internalText
so this isn't necessary
// Move the cursor to the last character of the text after pasting using Dispatch | ||
DispatchQueue.main.async { | ||
if let endPosition = primerTextField.position(from: primerTextField.endOfDocument, offset: 0) { | ||
primerTextField.selectedTextRange = primerTextField.textRange(from: endPosition, to: endPosition) |
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.
Won't this also move the cursor to the end if the user edits the middle of the card string? e.g. if they missed a few numbers
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.
true. hmmm...
6b0a0d7
to
29b46dd
Compare
if let endPosition = primerTextField.position(from: primerTextField.beginningOfDocument, offset: text.count) { | ||
// Delaying it for 0.01 seconds because setting the selectedTextRange | ||
// before `shouldChangeCharactersIn:range` returns does not apply the value. | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: { |
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.
If this is already on main queue (which it should be) can you just dispatch to main without the delay? That should make it happen as soon as this function exits
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.
true, thanks!
// before `shouldChangeCharactersIn:range` returns does not apply the value. | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: { | ||
// Create a UITextRange from the endPosition to endPosition (for placing the cursor at the end) | ||
primerTextField.selectedTextRange = primerTextField.textRange(from: endPosition, to: endPosition) |
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.
If the user pastes into the middle of the string what happens?
It's an edge case but I think having it move the cursor to the end of the text doesn't feel right. Could we move the cursor to the end of the inserted range instead?
// Add a small delay to ensure the cursor movement code has executed | ||
let expectation = XCTestExpectation(description: "Wait for cursor to move") | ||
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { |
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.
If you switch to just a straight main dispatch above, you can also do that here
29b46dd
to
25bcb57
Compare
25bcb57
to
3a4b11e
Compare
Quality Gate passedIssues Measures |
Description
CHKT-4055