Skip to content

Commit

Permalink
Merge pull request #398 from Kyle-Ye/feature/autocomplete
Browse files Browse the repository at this point in the history
Add PinCodeTextField autocomplete support
  • Loading branch information
MattKiazyk authored Nov 22, 2023
2 parents b8c6a7c + fc35950 commit 330f590
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
17 changes: 13 additions & 4 deletions Xcodes/Frontend/SignIn/PinCodeTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ struct PinCodeTextField: NSViewRepresentable {

@Binding var code: String
let numberOfDigits: Int
let complete: (String) -> Void

func makeNSView(context: Context) -> NSViewType {
let view = PinCodeTextView(numberOfDigits: numberOfDigits, itemSpacing: 10)
view.codeDidChange = { c in code = c }
view.codeDidChange = { c in code = c }
view.codeDidComplete = { complete($0) }
return view
}

Expand All @@ -29,8 +31,9 @@ struct PinCodeTextField_Previews: PreviewProvider {
struct PreviewContainer: View {
@State private var code = "1234567890"
var body: some View {
PinCodeTextField(code: $code, numberOfDigits: 11)
.padding()
PinCodeTextField(code: $code, numberOfDigits: 11) {
print("Input is complete \($0)")
}.padding()
}
}

Expand All @@ -52,10 +55,16 @@ class PinCodeTextView: NSControl, NSTextFieldDelegate {
handler(String(code.compactMap { $0 }))
}
updateText()

if code.compactMap({ $0 }).count == numberOfDigits,
let handler = codeDidComplete {
handler(String(code.compactMap { $0 }))
}
}
}
var codeDidChange: ((String) -> Void)? = nil

var codeDidComplete: ((String) -> Void)? = nil

private let numberOfDigits: Int
private let stackView: NSStackView = .init(frame: .zero)
private var characterViews: [PinCodeCharacterTextField] = []
Expand Down
4 changes: 3 additions & 1 deletion Xcodes/Frontend/SignIn/SignIn2FAView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ struct SignIn2FAView: View {

HStack {
Spacer()
PinCodeTextField(code: $code, numberOfDigits: authOptions.securityCode.length)
PinCodeTextField(code: $code, numberOfDigits: authOptions.securityCode.length) {
appState.submitSecurityCode(.device(code: $0), sessionData: sessionData)
}
Spacer()
}
.padding()
Expand Down
4 changes: 3 additions & 1 deletion Xcodes/Frontend/SignIn/SignInSMSView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ struct SignInSMSView: View {

HStack {
Spacer()
PinCodeTextField(code: $code, numberOfDigits: authOptions.securityCode.length)
PinCodeTextField(code: $code, numberOfDigits: authOptions.securityCode.length) {
appState.submitSecurityCode(.sms(code: $0, phoneNumberId: trustedPhoneNumber.id), sessionData: sessionData)
}
Spacer()
}
.padding()
Expand Down

0 comments on commit 330f590

Please sign in to comment.