handle on input text change for error handling

This commit is contained in:
Ryan Zhao 2023-08-21 15:10:54 +10:00
parent ff254f674a
commit abaf3ddf89
2 changed files with 18 additions and 5 deletions

View File

@ -1,6 +1,7 @@
// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
import SwiftUI
import Combine
public struct SessionTextField: View {
@Binding var text: String
@ -31,14 +32,14 @@ public struct SessionTextField: View {
SwiftUI.TextField(
"",
text: $text
text: $text.onChange{ value in
if error?.isEmpty == false && text != value {
error = nil
}
}
)
.font(.system(size: Values.mediumFontSize))
.foregroundColor(themeColor: (error?.isEmpty == false) ? .danger : .textPrimary)
.onReceive(text.publisher, perform: { _ in
error = nil
})
}
.padding(.horizontal, Values.largeSpacing)
.frame(

View File

@ -85,3 +85,15 @@ extension View {
)
}
}
extension Binding {
public func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> {
Binding(
get: { self.wrappedValue },
set: { newValue in
handler(newValue)
self.wrappedValue = newValue
}
)
}
}