41 lines
875 B
Swift
41 lines
875 B
Swift
import SwiftUI
|
|
|
|
public struct CaseIterablePicker<Label, Content>: View
|
|
where Content: CaseIterable,
|
|
Content: CustomStringConvertible,
|
|
Content: Hashable,
|
|
Content: Identifiable,
|
|
Content.AllCases: RandomAccessCollection,
|
|
Label: View
|
|
{
|
|
|
|
let label: () -> Label
|
|
@Binding<Content> var selection: Content
|
|
|
|
public init(
|
|
selection: Binding<Content>,
|
|
@ViewBuilder label: @escaping () -> Label
|
|
) {
|
|
self.label = label
|
|
self._selection = selection
|
|
}
|
|
|
|
public var body: some View {
|
|
Picker(selection: $selection, label: label()) {
|
|
ForEach(Content.allCases) {
|
|
Text($0.description)
|
|
.tag($0)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension CaseIterablePicker where Label == Text {
|
|
|
|
public init<S: StringProtocol>(_ title: S, selection: Binding<Content>) {
|
|
self.init(selection: selection) {
|
|
Text(title)
|
|
}
|
|
}
|
|
}
|