TagBarView.swift 832 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // TagBarView.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import SwiftUI
  8. struct TagBarView: View {
  9. @Binding var tags: [Tag]
  10. @State private var active: String = ""
  11. @FocusState private var isFocused: Bool
  12. var body: some View {
  13. HStack {
  14. ForEach($tags) { tag in
  15. TextField("", text: tag.id)
  16. .focused($isFocused)
  17. .onChange(of: isFocused) {
  18. tags.removeAll(where: { $0.id.isEmpty })
  19. }
  20. }
  21. .scaledToFit()
  22. TextField("Tag", text: $active)
  23. .onSubmit {
  24. if !tags.contains(where: { $0.id.caseInsensitiveCompare(active) == .orderedSame }) {
  25. tags.append(Tag(id: active))
  26. }
  27. active = ""
  28. }
  29. }
  30. }
  31. }
  32. #Preview {
  33. @Previewable @State var tags = Array<Tag>()
  34. TagBarView(tags: $tags)
  35. }