MultiSelect
MultiSelect create a dropdown list that takes more than one item and return the indices of the selected ones.
API
func MultiSelect(c *tgframe.Container, label string, items []string, conf ...*MultiSelectConf) []int
cis Parent container.labelis the label for multiselect.itemsis the list of options.confis an optional configuration, at most one.- Return the indices of the selected items, 0-indexed. nil when nothing is selected.
The result is ordered by items rather than by the order the app user picked
them in, so the same selection always reads the same way.
Nothing selected is nil, the same "nothing" Select hands back, so one check reads a single pick and a multiple one. Ranging over it is safe either way — a nil slice has no elements — so the check is only needed where the absence itself matters.
// MultiSelectConf is the configuration for the MultiSelect component.
type MultiSelectConf struct {
tgframe.Base // ID
// Default is the selection the component starts with, as indices into
// items. It is only read until the app user first touches the component.
Default []int
// MaxSelections is how many items may be selected at once. Zero, the
// default, is no limit. At the limit the frontend disables the items that
// are not selected, so the limit is never reached by a refusal.
MaxSelections int
// Placeholder is the text shown while nothing is selected.
Placeholder string
// Disabled is true if the multiselect is disabled.
Disabled bool
}
Example
items := []string{"Alpha", "Beta", "Gamma"}
selIdxes := tgcomp.MultiSelect(p.Main, "MultiSelect", items,
&tgcomp.MultiSelectConf{
Placeholder: "pick up to two",
MaxSelections: 2,
})
selItems := []string{}
for _, idx := range selIdxes {
selItems = append(selItems, items[idx])
}
tgcomp.Text(p.Main, "Values: "+strings.Join(selItems, ", "),
&tgcomp.TextConf{ID: "multiselect_result"})
Like a Select, a multiselect derives its id from its label, so two with the same label collide. Naming either of them is the way out:
tgcomp.MultiSelect(p.Main, "Pick", items)
tgcomp.MultiSelect(p.Main, "Pick", items, &tgcomp.MultiSelectConf{ID: "second_pick"})
