Skip to main content

MultiSelectTask

MultiSelectTask allows users to select one or multiple items from a list.

Creating

options := []ziva.Item{
    {Key: "item1", Name: "Item 1"},
    {Key: "item2", Name: "Item 2"},
    {Key: "item3", Name: "Item 3"},
}
task := ziva.NewMultiSelectTask("Select components", options)
Screenshot: multi-select task

Main Methods

// Settings
task.WithSelectAll("Select all")                    // add "select all" option
task.WithDefaultItems([]string{"item1", "item2"})   // default selected keys
task.WithViewport(5, true)                          // viewport window
task.WithItemsDisabled([]string{"item3"})            // disable items by key

// Get results
selected := task.GetSelected() // []string

Advanced configuration

Dependencies between items

WithDependencies lets you describe relationships between options. Each map entry uses a MultiSelectDependencyRule with actions that run when the trigger item is selected or deselected. Actions accept item keys:
  • Disable / Enable — temporarily lock or unlock other options.
  • ForceSelect — automatically mark related items (for required prerequisites).
  • ForceClear — unselect items that are no longer valid.
diagnostics := []ziva.Item{
    {Key: "logging", Name: "Logging"},
    {Key: "debug", Name: "Debug"},
    {Key: "tracing", Name: "Tracing"},
    {Key: "metrics", Name: "Metrics"},
}

rules := map[string]ziva.MultiSelectDependencyRule{
    "logging": {
        OnSelect: ziva.MultiSelectDependencyActions{
            Enable: []string{"debug"},
        },
        OnDeselect: ziva.MultiSelectDependencyActions{
            Disable:    []string{"debug"},
            ForceClear: []string{"debug", "tracing"},
        },
    },
    "debug": {
        OnSelect: ziva.MultiSelectDependencyActions{
            ForceSelect: []string{"logging"},
        },
    },
    "tracing": {
        OnSelect: ziva.MultiSelectDependencyActions{
            ForceSelect: []string{"logging"},
        },
    },
}

task := ziva.NewMultiSelectTask("Diagnostics", diagnostics).
    WithDependencies(rules).
    WithDefaultItems([]string{"logging", "metrics"})

Optional empty selection

Multi-select tasks allow confirming with no items selected by default. Call WithRequireSelection(true) if you want to enforce the classic “select at least one item” rule and show the localized hint when Enter is pressed without a choice.

Usage Example

components := []ziva.Item{
    {Key: "web", Name: "web", Description: "Web server"},
    {Key: "api", Name: "api", Description: "REST API"},
    {Key: "db", Name: "db", Description: "Database"},
}

task := ziva.NewMultiSelectTask("Components", components).
    WithSelectAll("Install all").
    WithDefaultItems([]string{"web", "api"})

queue := ziva.NewQueue("Setup")
queue.AddTasks(task)
queue.Run()

fmt.Printf("Selected: %v\n", task.GetSelected()) // keys of selected items
I