Could you please help me with clear button code, that doesn't work properly?
I have a TextField, which stores an input of type Double, and unfortunately the classic solution of including additional modifier is not working.
Here is my code:
import SwiftUI
struct ContentView: View {
@State private var amount: Double = 10
@FocusState private var isFocused: Bool
var body: some View {
NavigationView {
Form {
Section {
TextField("Amount to pay", value: $amount, format: .currency(code: "USD"))
.keyboardType(.decimalPad)
.focused($isFocused)
.modifier(TextFieldClearButton(amount: $amount, focus: $isFocused))
}
}
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("Done") {
isFocused = false
}
}
}
}
}
struct TextFieldClearButton: ViewModifier {
@Binding var amount: Double
@FocusState.Binding var focus: Bool
func body(content: Content) -> some View {
HStack {
content
if amount != 0.0 {
Button(
action: {
self.amount = 0.0
if focus == false {
focus = true
}
},
label: {
Image(systemName: "delete.left")
.foregroundColor(Color(UIColor.opaqueSeparator))
}
)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Looks like the view is not updated when I am calling the modifier, so I can see only default value. How can I fix this?