SecureField: Fix keyboard focus when showing password

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2023-04-23 14:28:40 -04:00
parent 5232ddfc97
commit 45900d6456

View file

@ -8,16 +8,24 @@
import SwiftUI import SwiftUI
struct HybridSecureField: View { struct HybridSecureField: View {
enum Field: Hashable {
case plain
case secure
}
@Binding var text: String @Binding var text: String
@State private var showPassword = false @State private var showPassword = false
@FocusState var focusedField: Field?
var body: some View { var body: some View {
HStack { HStack {
Group { Group {
if showPassword { if showPassword {
TextField("Password", text: $text) TextField("Password", text: $text)
.focused($focusedField, equals: .plain)
} else { } else {
SecureField("Password", text: $text) SecureField("Password", text: $text)
.focused($focusedField, equals: .secure)
} }
} }
.autocorrectionDisabled(true) .autocorrectionDisabled(true)
@ -25,10 +33,12 @@ struct HybridSecureField: View {
Button { Button {
showPassword.toggle() showPassword.toggle()
focusedField = showPassword ? .plain : .secure
} label: { } label: {
Image(systemName: self.showPassword ? "eye.slash.fill" : "eye.fill") Image(systemName: showPassword ? "eye.slash.fill" : "eye.fill")
.foregroundColor(.secondary) .foregroundColor(.secondary)
} }
.buttonStyle(.borderless)
} }
} }
} }