forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandablePicker.swift
More file actions
57 lines (51 loc) · 1.54 KB
/
ExpandablePicker.swift
File metadata and controls
57 lines (51 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// ExpandablePicker.swift
// LoopKitUI
//
// Created by Anna Quinlan on 8/13/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import SwiftUI
public protocol Labeled {
var label: String { get }
}
public struct ExpandablePicker<SelectionType: Hashable & Labeled>: View {
@State var pickerShouldExpand = false
var selectedValue: Binding<SelectionType>
let label: String
let items: [SelectionType]
public init (
with items: [SelectionType],
selectedValue: Binding<SelectionType>,
label: String = ""
) {
self.items = items
self.selectedValue = selectedValue
self.label = label
}
public var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack(alignment: .center) {
Text(label)
Spacer()
Text(selectedValue.wrappedValue.label)
.foregroundColor(.gray)
}
.padding(.vertical, 5)
.frame(minWidth: 0, maxWidth: .infinity).onTapGesture {
self.pickerShouldExpand.toggle()
}
if pickerShouldExpand {
HStack(alignment: .center) {
Picker(selection: selectedValue, label: Text("")) {
ForEach(items, id: \.self) { item in
Text(item.label)
}
}
.pickerStyle(.wheel)
.labelsHidden()
}
}
}
}
}