Skip to content

Radio

The radio component allows users to select option(s) from a predefined list.

Map Ordering

Unsorted Maps

Go does not provide a built-in data structure for maintaining insertion order into maps.

Iridium has it's own ordered_map collection to solve this issue. This means you're required to use or satisfy our ordered_map struct/interface, to populate a list of radio options with a particular order. If you don't care about order, you can pass in a simple map[string]string as well.

Common Methods

For a list of common field component methods, see here.

Options

You can specify your radio options via:

go
// static
FormRadio("beverages").
    Options(beveragesMap())

// callback
FormRadio("beverages").
    OptionsFn(
        func(ctx FieldContext) collections.IOrderedMap[string,string] {
            return beveragesMap()
        }
    )

// Example of creating an in order map
func beveragesMap() collections.IOrderedMap[string, string]{
    pairs := []collections.Pair[string, string]{
        {Key: "coffee", Value: "Coffee"},
        {Key: "tea", Value: "tea"},
        {Key: "kombucha", Value: "Kombucha"},
    }
    return collections.NewOrderedMapFromPairs(pairs)
}

Options Random

If you don't care about order, you can also provide a map[string]string for your options

go
// static
FormRadio("beverages").
    OptionsRandom(map[string]string{
        "coffee": "Coffee",
        "tea" : "Tea",
        "kombucha": "Kombucha",
    })

// callback
FormRadio("beverages").
    OptionsRandomFn(
        func(ctx FieldContext) map[string]string {
            return map[string]string{
                "coffee": "Coffee",
                "tea" : "Tea",
                "kombucha": "Kombucha",
            }
        }
    )

Description

You can provide descriptions for each of your radio elements using the Description or DescriptionFn methods.

You need to match the description key to the key of your option for it to render.

go
// static
FormRadio("beverages").
    OptionsRandom(map[string]string{
        "coffee": "Coffee",
        "tea" : "Tea",
        "kombucha": "Kombucha",
    }).
    // static
    Descriptions(map[string]string{
        "coffee": "Delicious.",
        "tea": "Soothing.",
        "kombucha": "Great for gut health.",
    }).
    // callback
    DescriptionsFn(
        func (ctx FieldContext) map[string]string {
            if model, err := ctx.GetModel(); err == nil {
                return map[string]string {
                    "coffee": "A great choice for you " + ctx.Model.Name + "."
                }
            }
            return map[string]string {
                "coffee": "A great choice."
            }
            
        }
    )

Grouped

You can 'group' radio options, by placing them inside their own cards. This is a purely cosmetic choice.

go
// static
FormRadio("beverage").
    Grouped()

// callback
FormRadio("beverages").
    GroupedFn(
        func (ctx FieldContext) bool {
            return false
        }
    )

Released under the MIT License.