InputField.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import SwiftUI
  15. public struct InputField<Label>: View where Label: View {
  16. @Binding
  17. private var text: String
  18. private var title: String?
  19. private var label: () -> Label
  20. @Environment(\.submitHandler)
  21. var submitHandler
  22. private func submit() {
  23. if let submitHandler {
  24. submitHandler()
  25. }
  26. }
  27. public init(_ title: String? = nil, text: Binding<String>,
  28. @ViewBuilder label: @escaping () -> Label) {
  29. self.title = title
  30. _text = text
  31. self.label = label
  32. }
  33. public var body: some View {
  34. VStack(alignment: .leading) {
  35. HStack(alignment: .bottom) {
  36. VStack(alignment: .leading) {
  37. TextField(
  38. title ?? "",
  39. text: $text,
  40. axis: .vertical
  41. )
  42. .padding(.vertical, 4)
  43. .onSubmit(submit)
  44. }
  45. .padding(.horizontal, 8)
  46. .padding(.vertical, 4)
  47. .overlay {
  48. RoundedRectangle(
  49. cornerRadius: 8,
  50. style: .continuous
  51. )
  52. .stroke(Color(UIColor.systemFill), lineWidth: 1)
  53. }
  54. Button(action: submit, label: label)
  55. .padding(.bottom, 4)
  56. }
  57. }
  58. .padding(8)
  59. }
  60. }
  61. #Preview {
  62. struct Wrapper: View {
  63. @State var userInput: String = ""
  64. var body: some View {
  65. InputField("Message", text: $userInput) {
  66. Image(systemName: "arrow.up.circle.fill")
  67. .font(.title)
  68. }
  69. }
  70. }
  71. return Wrapper()
  72. }