TagBarView.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // TagBarView.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import SwiftUI
  8. import SwiftData
  9. struct TagBarView: View {
  10. @Binding var tags: [Tag]
  11. @AppStorage(UserDefaultsKeys.UrlHints) var allHints = CodableArray<URLHint>()
  12. @State private var active: String = ""
  13. @FocusState private var isFocused: Bool
  14. var body: some View {
  15. HStack {
  16. ForEach($tags) { tag in
  17. let url = allHints.filter({ $0.matches(tag.wrappedValue) })
  18. .first?.url(tag.wrappedValue)
  19. if url != nil {
  20. Link(destination: url!) {
  21. Label("", systemImage: "link")
  22. }
  23. .padding(.leading, -8)
  24. .padding(.trailing, -10)
  25. }
  26. TextField("", text: tag.id)
  27. .focused($isFocused)
  28. .onChange(of: isFocused) {
  29. tags.removeAll(where: { $0.id.isEmpty })
  30. }
  31. }
  32. .scaledToFit()
  33. TextField("Tag", text: $active)
  34. .onSubmit {
  35. if !active.isEmpty && !tags.contains(where: { $0.like(active) }) {
  36. tags.append(Tag(id: active))
  37. }
  38. active = ""
  39. }
  40. }
  41. }
  42. }
  43. #Preview {
  44. @Previewable @State var tags = Array<Tag>()
  45. @Previewable @State var allHints = CodableArray([
  46. URLHint(prefix: "RPD:", replacement: "http://localhost/")
  47. ])
  48. TagBarView(tags: $tags)
  49. }