Skip to Content
Onevra

Select

Chooses a value from a drop-down list, optionally with a search field.

Loading the live example…

select.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type sizeChoice struct {
	size string
}

func (s *sizeChoice) View(c *ui.Context) {
	sel := components.Select(c, &s.size)
	sel.Trigger.Label("Size").Children(func() {
		components.Text(c, s.size).SingleLine()
	})
	sel.Popup(func(panel ui.Element) {
		for _, size := range []string{"Small", "Medium", "Large"} {
			sel.Item(size).Label(size).Children(func() {
				components.Text(c, size).SingleLine()
			})
		}
	})
}
Source

components.Select returns parts: style the Trigger, then build the options in Popup with Item. The application owns the option text, so items can hold any content.

Searchable

SearchField adds a search input at the top of the popup. The application owns the query and builds only the items that match.

Loading the live example…

select_search.go
import (
	"strings"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

var timezones = []string{
	"America/New_York", "America/Los_Angeles", "Asia/Shanghai", "Asia/Tokyo",
	"Europe/Berlin", "Europe/Lisbon", "Europe/London", "Pacific/Auckland",
}

type timezone struct {
	zone, query string
}

func (z *timezone) View(c *ui.Context) {
	t := c.Theme()
	sel := components.Select(c, &z.zone)
	sel.Trigger.Label("Time zone").Children(func() {
		components.Text(c, z.zone).SingleLine()
	})
	sel.Popup(func(panel ui.Element) {
		panel.MinWidth(t.Rem(16))
		sel.SearchField(&z.query).Label("Search time zones").Placeholder("Search")
		matches := 0
		for _, zone := range timezones {
			if !strings.Contains(strings.ToLower(zone), strings.ToLower(z.query)) {
				continue
			}
			matches++
			sel.Item(zone).Label(zone).Children(func() {
				components.Text(c, zone).SingleLine()
			})
		}
		if matches == 0 {
			components.Text(c, "No time zones match").Padding(t.Space(2)).TextColor(t.TextMuted)
		}
	})
}
Source

Native menu

components.NativeSelect opens the operating system's menu instead, from a list of values and labels. Disabled options stay visible.

Loading the live example…

native_select.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type exportFormat struct {
	format string
}

func (e *exportFormat) View(c *ui.Context) {
	components.NativeSelect(c, &e.format, []components.NativeSelectOption[string]{
		{Value: "png", Label: "PNG image"},
		{Value: "jpg", Label: "JPEG image"},
		{Value: "pdf", Label: "PDF document"},
		{Value: "svg", Label: "SVG (coming soon)", Disabled: true},
	}).Trigger.Label("Export format")
}
Source

Browsers have no native menu, so this example shows MyGo's HTML menu in its place.

API

Select

func Select[T comparable](c *ui.Context, selected *T) *SelectParts[T]

Select creates a drop-down choosing *selected from options the caller builds in Popup with Item. The trigger, popup and items have Onevra styling:

sel := components.Select(c, &choice)
sel.Trigger.Children(func() { components.Text(c, string(choice)).SingleLine() })
sel.Popup(func(panel ui.Element) {
	for _, opt := range options {
		item := sel.Item(opt)
		item.Children(func() { components.Text(c, string(opt)).SingleLine() })
	}
})

The trigger's Changed reports a new choice.

pkg.go.dev

SelectParts

SelectParts composes MyGo's select behavior with a themed popup and an optional search field. Applications own the query, filtering and option text. Embedded MyGo parts retain trigger, item and open-state behavior.

Prop

Type

Methods
func (s *SelectParts[T]) Item(value T) ui.Element

Item creates a themed option with the same neutral selected face as SidebarItem. Unselected options use a weaker neutral face while hovered. The caller supplies its label and children.

func (s *SelectParts[T]) Popup(build func(panel ui.Element)) ui.Element

Popup shows a themed, shadowed panel while the select is open. The callback can override its appearance and build a SearchField followed by filtered Items. It returns an absent Element when closed.

func (s *SelectParts[T]) SearchField(query *string) ui.Element

SearchField adds an optional search input inside Popup. Give it an accessible Label and localized Placeholder. Build only matching Items after it; an empty result message and query reset policy belong to the caller. The highlight moves to the first enabled result when filtering removes its option. Down or Up returns focus to the trigger, where MyGo handles option navigation and selection.

pkg.go.dev

NativeSelect

func NativeSelect[T comparable](c *ui.Context, selected *T, options []NativeSelectOption[T]) *NativeSelectParts

NativeSelect chooses a value through MyGo's operating-system menu. The trigger uses Onevra styling, not an OS select widget. It displays the selected option's label (empty if absent); callers may replace its Children and should supply an accessible Label. Enter, Space and Down open the focused trigger's menu. An empty option list disables the trigger. Keep option order stable while open.

pkg.go.dev

NativeSelectOption

NativeSelectOption associates an application value with localized menu text. Values should be unique. Disabled options remain visible but cannot be chosen.

Prop

Type

pkg.go.dev
Edit this page on GitHub

On this page