List
A virtualized list of rows with keyboard selection.
Loading the live example…
import (
"fmt"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
)
type inbox struct {
list components.ListState
selected int
}
func (b *inbox) View(c *ui.Context) {
t := c.Theme()
b.list.Selected = &b.selected
ui.Column(c).Width(360).MaxWidthPercent(100).Gap(8).Children(func() {
// Only the visible rows of the 10,000 are built.
components.List(c, &b.list, 10000, func(i int) {
ui.Column(c).FillWidth().Padding(t.Space(2), t.Space(3)).Children(func() {
components.Text(c, fmt.Sprintf("Message %d", i+1)).Bold()
components.Text(c, "A preview of the message's first line").TextColor(t.TextMuted).SingleLine()
})
}).Height(260).FillWidth().Label("Messages").Radius(t.Radius).Border(1, t.Border)
components.Text(c, fmt.Sprintf("Selected message %d", b.selected+1)).TextColor(t.TextMuted)
})
}components.List builds only the visible rows, so it handles long lists. The application builds each row's content; ListState holds selection, keys and callbacks such as OnSubmit for Enter and double clicks.
API
List
func List(c *ui.Context, state *ListState, count int, row func(int)) ui.ElementList builds neutral, hover-free rows inside MyGo's non-selecting virtual list. Give it a height or fill a bounded parent. Applications own row content and spacing; builders can run during layout and must not perform application work. A nil state uses MyGo's local scrolling state, without selection.
pkg.go.devListState
ListState owns a Onevra list's selection and MyGo's virtual scroll state. Keep one state per list and do not copy it after first use. Keys must be comparable and unique. Without Key, selection identifies rows by index. Selection uses ui.Selection[any] so keys can have any comparable type. Selection, when set, controls the chosen rows; Selected optionally exposes the keyboard cursor as well. A nil Selected and Selection disables selection. KeepOffset keeps a keyed list's scroll offset as its rows move, such as when they are sorted again, while selection still follows its keys.
OnChange and OnSubmit report user selection and activation respectively; use these callbacks rather than the returned Element's Changed/Submitted. MyGo still owns virtualization, variable-height measurement and scrolling. This state intentionally does not expose table sorting, reordering or headers.
Prop
Type
Methods
func (s *ListState) Focus(c *ui.Context) boolfunc (s *ListState) FocusWithin(c *ui.Context) boolfunc (s *ListState) Focused(c *ui.Context) boolfunc (s *ListState) ScrollIntoView(row int)func (s *ListState) ScrollTo(row int, align ui.Align)func (s *ListState) ScrollToEnd()func (s *ListState) Visible() (first, last int)