-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Data API
The objective of this page is to define a Data API that could be used by all widget that need to handle data like Select, Radio, Checkbox and Table.
The Table widget has not been designed / implemented yet, but some discussion have been done with regard to the Select widget and are summarized in the next sections.
The DataItem interface that embeds the fmt.Stringer
interface should allow to handle complex types and at the same time allow to use the String
method to handle labels. It also provides the opportunity to hook in to be informed of change events so that widgets can update accordingly.
type DataItem interface {
String() string
AddListener(func(DataItem))
}
The DataMap interface is like a DataItem except that it has many items each with a name. It extends DataItem so that anything returning an item can also return a map. The change listener is called when an item (or multiple) within the map is changed.
type DataMap interface {
DataItem
Get(string) DataItem
AddListener(func(DataMap))
}
The DataSource interface defines an interface that returns multiple DataItem
s you can consider it like
[]DataItem
except that it can support lazy loading and advanced features like paging.
The change listener is notified if the number if items in the source changes - an addition or deletion - but not if items within it change.
type DataSource interface {
Count() int
Get(int) DataItem
AddListener(func(DataSource))
}
The Get(int)
from DataSource
could also return a DataMap
, if the widget you have built expects it.
Future extensions such as PagedDataSource may be added in the future.
We can add helper functions such as the creation of a data from string or data source from a slice.
func NewDataItem(Stringer) DataItem {}
func NewDataItemFromString(string) DataItem {}
func NewDataMapFromMap([string]DataItem) DataMap {}
func NewDataSourceFromSlice([]DataItem) DataSource
func NewDataSourceFromStrings([]string) DataSource
The introduction of the DataItem and DatSource interfaces will break the current API for the Radio and Checkbox widgets.
The break is related to the type changes for the Selected
, Options
and OnChanged
fields.
package widget
import "fmt"
type Select struct {
baseWidget
Selected DataItem
Options DataSource
OnChanged func(DataItem) `json:"-"`
hovered bool
popover *PopOver
}
func NewSelect(options DataSource, changed func(DataItem)) *Select {
combo := &Select{baseWidget{}, nil, options, changed, false, nil}
options.AddListener(func(DataSource) {
combo.Refresh()
}())
Renderer(combo).Layout(combo.MinSize())
return combo
}