Skip to content

Commit a2628ed

Browse files
committed
fix: improve paste support for PIN code input fields
- Add support for pasting multi-digit verification codes - Filter pasted content to alphanumeric characters only - Always start filling from the first field when pasting - Optimize performance by updating all fields at once - Support both numeric and alphanumeric codes (e.g., 123456, A1B2C3) Fixes the issue where pasting a 6-digit code like '123456' would only show the last digit '6' in the first field. Now all digits are correctly distributed across all input fields. This improvement benefits both SignInSMSView and SignIn2FAView since they share the same PinCodeTextField component.
1 parent 1a0d335 commit a2628ed

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Xcodes/Frontend/SignIn/PinCodeTextView.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ class PinCodeTextView: NSControl, NSTextFieldDelegate {
149149

150150
let newFieldText = field.stringValue
151151

152+
// Handle pasting multiple characters (e.g., pasting "123456" from clipboard)
153+
if newFieldText.count > 1 {
154+
// Filter to alphanumeric characters only
155+
let validCharacters = newFieldText.filter { $0.isLetter || $0.isNumber }
156+
157+
// Always start from the first field and clear previous content
158+
var newCode = Array(repeating: Character?.none, count: numberOfDigits)
159+
for (offset, character) in validCharacters.enumerated() {
160+
if offset < numberOfDigits {
161+
newCode[offset] = character
162+
}
163+
}
164+
165+
// Update all fields at once to avoid triggering didSet multiple times
166+
code = newCode
167+
168+
// Move focus to next empty field or the last field if all are filled
169+
let nextEmptyIndex = code.firstIndex(where: { $0 == nil }) ?? numberOfDigits - 1
170+
if nextEmptyIndex < characterViews.count {
171+
window?.makeFirstResponder(characterViews[nextEmptyIndex])
172+
} else {
173+
resignFirstResponder()
174+
}
175+
176+
return
177+
}
178+
179+
// Handle single character input
152180
let lastCharacter: Character?
153181
if newFieldText.isEmpty {
154182
lastCharacter = nil

0 commit comments

Comments
 (0)