Radio Group Supports Generic Type of Value for SwiftUI
File ➜ Swift Packages ➜ Add Package Dependancy..
.package(url: "https://github.com/Changemin/RadioGroup", from: "1.1.0")
@State var value: T = 0
var options: [RadioOption<T>] = [
RadioOption(label: "Option 1", value: 1),
RadioOption(label: "Option 2", value: 2),
RadioOption(label: "Option 3", value: 3)
]
RadioGroup(options: options, value: $value)
options
: Array of the available optionslabel
: Label displays beyond the buttonvalue
: Actually value of the optionYou can change variable type
T
into any type you want. (even custom enum)
RadioGroup(options: options, value: $value)
.accentColor(color: Color)
color
: accentColor of the buttonimport RadioGroup
struct ContentView: View {
@State var value: Int = 0
var options: [RadioOption<Int>] = [
RadioOption(label: "Option 1", value: 1),
RadioOption(label: "Option 2", value: 2),
RadioOption(label: "Option 3", value: 3)
]
var body: some View {
VStack {
RadioGroup(options: options, value: $value)
}
}
}
import RadioGroup
struct ContentView: View {
@State var value: Float = 0
var options: [RadioOption<Float>] = [
RadioOption(label: "Option 1(1.1)", value: 1.1),
RadioOption(label: "Option 2(1.2)", value: 1.2),
RadioOption(label: "Option 3(1.3)", value: 1.3)
]
var body: some View {
VStack {
RadioGroup(options: options, value: $value)
.accentColor(.orange)
}
}
}
import RadioGroup
enum MyEnum {
case case1
case case2
case case3
}
struct ContentView: View {
@State var value: MyEnum = .case1
var options: [RadioOption<MyEnum>] = [
RadioOption(label: "MyEnum : case1", value: .case1),
RadioOption(label: "MyEnum : case1", value: .case2),
RadioOption(label: "MyEnum : case1", value: .case3)
]
var body: some View {
RadioGroup(options: options, value: $value)
}
}
RadioGroup is available under the MIT license. See the LICENSE
file for more info.
link |
Stars: 4 |
Last commit: 3 years ago |
Successfully removed 'CM' prefix from package
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics