I'm trying to create a list of TextFields base on an array of strings, when the return key is pressed on the keyboard, I want to create a new TextField and focus it with the new @FocusState SwiftUI attribut
I've build a view like so:
struct ContentView: View {
@State var list: [String] = [""]
@FocusState private var focusField: Int?
var body: some View {
List {
ForEach($list.indices, id: \.self) { idx in
TextField("placeholder", text: $list[idx])
.focused($focusField, equals: idx)
.submitLabel(.next)
.onSubmit {
list.insert("", at: idx + 1)
focusNextField(from: idx)
}
}
}
}
private func focusNextField(from index: Int) {
focusField = index + 1
}
}
The issue is in the focusNextField(from index: Int) method if I print focusField after focusField = index + 1 it give me the same value as index. Any idea how could I solve this?