ExpandablePicker.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // ExpandablePicker.swift
  3. // LoopKitUI
  4. //
  5. // Created by Anna Quinlan on 8/13/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public struct ExpandablePicker: View {
  10. @State var pickerShouldExpand = false
  11. var pickerIndex: Binding<Int>
  12. let label: String
  13. let items: [String]
  14. public init (
  15. with items: [String],
  16. pickerIndex: Binding<Int>,
  17. label: String = ""
  18. ) {
  19. self.items = items
  20. self.pickerIndex = pickerIndex
  21. self.label = label
  22. }
  23. public var body: some View {
  24. VStack(alignment: .leading, spacing: 0) {
  25. HStack(alignment: .center) {
  26. Text(label)
  27. Spacer()
  28. Text(items[pickerIndex.wrappedValue])
  29. .foregroundColor(.gray)
  30. }
  31. .padding(.vertical, 5)
  32. .frame(minWidth: 0, maxWidth: .infinity).onTapGesture {
  33. self.pickerShouldExpand.toggle()
  34. }
  35. if pickerShouldExpand {
  36. HStack(alignment: .center) {
  37. Picker(selection: self.pickerIndex, label: Text("")) {
  38. ForEach(0 ..< self.items.count) {
  39. Text(self.items[$0])
  40. }
  41. }
  42. .pickerStyle(WheelPickerStyle())
  43. .labelsHidden()
  44. }
  45. }
  46. }
  47. }
  48. }