Skip to content

MultiSelect

KeyAction
/ kMove focus up
/ jMove focus down
SpaceToggle focused option on/off

Enter is reserved for form submission. Toggle selections with Space.

MultiSelect implements FormField. The bound value type is []string (option values). SetFieldValue accepts either []string (matched by Value) or []int (matched by index).

MultiSelect preview

MultiSelect allows multiple option selection. It implements MultiValueProvider[string].

Constructor func NewMultiSelect(name string) *MultiSelect

Usage

ms := components.NewMultiSelect("tags").
SetLabel("Tags").
SetOptionsWithValues([]components.SelectOption{
{Label: "Backend", Value: "be"},
{Label: "Frontend", Value: "fe"},
{Label: "Infra", Value: "infra"},
})
_ = ms.SetSelectedValues([]string{"be", "infra"})
ms.SetOnChange(func(e *components.ChangeEvent[[]components.SelectOption]) {
fmt.Println("count:", len(e.NewValue))
})
fmt.Println(len(ms.Values()))

Methods

Method Signature Description
Clear func (m *MultiSelect) Clear() Clear deselects all options.
ClearField func (m *MultiSelect) ClearField() ClearField resets the selection. Implements FormField.
FieldValue func (m *MultiSelect) FieldValue() any FieldValue returns the selected values as an any ([]string). Implements FormField.
GetFieldHeight func (m *MultiSelect) GetFieldHeight() int GetFieldHeight returns the preferred height for this field.
GetName func (m *MultiSelect) GetName() string GetName returns the field name.
HasValue func (m *MultiSelect) HasValue() bool HasValue returns true if at least one option is selected.
SelectedIndices func (m *MultiSelect) SelectedIndices() []int SelectedIndices returns all selected indices (sorted).
SelectedOptions func (m *MultiSelect) SelectedOptions() []SelectOption SelectedOptions returns all selected options.
SetExpanded func (m *MultiSelect) SetExpanded(expanded bool) *MultiSelect SetExpanded sets whether the dropdown list is open.
SetFieldValue func (m *MultiSelect) SetFieldValue(value any) error SetFieldValue sets the multi-select's value from an any. Accepts []string (matched against option values) or []int (option indices). Implements FormField.
SetLabel func (m *MultiSelect) SetLabel(label string) *MultiSelect SetLabel sets the field label.
SetOnChange func (m *MultiSelect) SetOnChange(handler ChangeHandler[[]SelectOption]) *MultiSelect SetOnChange sets the change handler (new API).
SetOptions func (m *MultiSelect) SetOptions(options []string) *MultiSelect SetOptions sets the available options.
SetOptionsWithValues func (m *MultiSelect) SetOptionsWithValues(options []SelectOption) *MultiSelect SetOptionsWithValues sets options with custom values.
SetSelected func (m *MultiSelect) SetSelected(indices []int) *MultiSelect SetSelected sets the selected indices.
SetSelectedIndices func (m *MultiSelect) SetSelectedIndices(indices []int) error SetSelectedIndices sets the selected indices, returning an error if any index is out of range.
SetSelectedValues func (m *MultiSelect) SetSelectedValues(values []string) error SetSelectedValues sets the selected options by value.
Values func (m *MultiSelect) Values() []string Values returns all selected value strings. This method is part of the MultiValueProvider interface.

Types

type ChangeHandler ChangeHandler handles value changes
type SelectOption struct SelectOption represents an option in a select dropdown.
FieldTypeDescription
Label string
Value string