Created
September 5, 2020 06:20
-
-
Save elmetal/e43cddded45dea1e3d826e961b7a6170 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
protocol PickerEnum: Hashable, Identifiable, CaseIterable where AllCases: RandomAccessCollection { | |
var displayName: String { get } | |
} | |
struct ListPicker<Selections>: View where Selections: PickerEnum { | |
@Binding var selection: Selections | |
var body: some View { | |
ForEach(Selections.allCases, id: \.self) { sel in | |
ListPickerRow(text: sel.displayName, | |
value: selection, | |
selected: sel == selection) | |
.contentShape(Rectangle()) | |
.onTapGesture { | |
selection = sel | |
} | |
} | |
} | |
} | |
fileprivate struct ListPickerRow<Value>: View where Value: Hashable { | |
let text: String | |
let value: Value | |
var selected: Bool | |
private let systemImageName = "checkmark" | |
var body: some View { | |
HStack { | |
Text(text) | |
Spacer() | |
if selected { | |
Image(systemName: systemImageName) | |
} | |
} | |
} | |
} | |
struct ListPicker_Previews: PreviewProvider { | |
enum Hoge: String, PickerEnum { | |
var displayName: String { self.rawValue } | |
var id: String { self.rawValue } | |
case a | |
case b | |
} | |
static var previews: some View { | |
Form { | |
ListPicker(selection: .constant(Hoge.a)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment