Swiftpack.co - changemin/RadioGroup as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by changemin.
changemin/RadioGroup v1.1.0
🗳 Radio Group Supports Generic Type of Value for SwiftUI
⭐️ 4
🕓 3 years ago
iOS macOS
.package(url: "https://github.com/changemin/RadioGroup.git", from: "v1.1.0")

Project logo

🗳 SwiftUI RadioGroup 🗳

License Release


Radio Group Supports Generic Type of Value for SwiftUI

🏁 Getting Started

Requirements

  • Xcode 11+
  • SwiftUI
  • iOS 14+
  • macOS 10.15+

Installaion

Swift Package Manager(SPM)

File ➜ Swift Packages ➜ Add Package Dependancy..
.package(url: "https://github.com/Changemin/RadioGroup", from: "1.1.0")

🎈Usage

@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 options
  • label : Label displays beyond the button
  • value : Actually value of the option

You can change variable type T into any type you want. (even custom enum)

🛠 Custom Modifiers

RadioGroup(options: options, value: $value)
    .accentColor(color: Color)
  • color : accentColor of the button

Example

👶 Simple

import 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)
        }
    }
}

Result

🎨 Accent Color, Other value type

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)
        }
    }
}

Result

📚 Advanced, Custom type(enum)

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)
    }
}

Result

📜 License

RadioGroup is available under the MIT license. See the LICENSE file for more info.

✅ Todos

  • ☐ design variation
  • ☐ more modifiers
  • ☐ custom font

✍️ Author

GitHub

link
Stars: 4
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Release Notes

Version 1.1.0
3 years ago

Successfully removed 'CM' prefix from package

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics