Przeglądaj źródła

refactor: change TagBarView to use an @FocusState and onChange listener instead on onSubmit
chore: re-indent

Sam Jaffe 3 tygodni temu
rodzic
commit
80c4405e6e
1 zmienionych plików z 15 dodań i 12 usunięć
  1. 15 12
      Todos/View/TagBarView.swift

+ 15 - 12
Todos/View/TagBarView.swift

@@ -10,22 +10,25 @@ import SwiftUI
 struct TagBarView: View {
   @Binding var tags: [Tag]
   @State private var active: String = ""
+  @FocusState private var isFocused: Bool
 
-    var body: some View {
-      HStack {
-        ForEach($tags) { tag in
-          TextField("", text: tag.id)
-            .onSubmit {
-              tags.removeAll(where: { $0.id.isEmpty })
-            }
-        }
-        TextField("Tag", text: $active)
-          .onSubmit {
-            tags.append(Tag(id: active))
-            active = ""
+  var body: some View {
+    HStack {
+      ForEach($tags) { tag in
+        TextField("", text: tag.id)
+          .focused($isFocused)
+          .onChange(of: isFocused) {
+            tags.removeAll(where: { $0.id.isEmpty })
           }
       }
+      .scaledToFit()
+      TextField("Tag", text: $active)
+        .onSubmit {
+          tags.append(Tag(id: active))
+          active = ""
+        }
     }
+  }
 }
 
 #Preview {