TextFieldRow.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // TextFieldRow.swift
  3. // LoopKitUI
  4. //
  5. // Created by Noah Brauner on 7/31/23.
  6. // Copyright © 2023 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public struct TextFieldRow: View {
  10. @Binding private var text: String
  11. @Binding private var isFocused: Bool
  12. let title: String
  13. let placeholder: String
  14. public init(text: Binding<String>, isFocused: Binding<Bool>, title: String, placeholder: String) {
  15. self._text = text
  16. self._isFocused = isFocused
  17. self.title = title
  18. self.placeholder = placeholder
  19. }
  20. public var body: some View {
  21. HStack {
  22. Text(title)
  23. .foregroundColor(.primary)
  24. Spacer()
  25. RowTextField(text: $text, isFocused: $isFocused) {
  26. $0.textAlignment = .right
  27. $0.placeholder = placeholder
  28. $0.font = .preferredFont(forTextStyle: .body)
  29. }
  30. .onTapGesture {
  31. // so that row does not lose focus on cursor move
  32. if !isFocused {
  33. rowTapped()
  34. }
  35. }
  36. }
  37. .accessibilityElement(children: .combine)
  38. .onTapGesture {
  39. rowTapped()
  40. }
  41. }
  42. private func rowTapped() {
  43. withAnimation {
  44. isFocused.toggle()
  45. }
  46. }
  47. }