/
demo_select.go
68 lines (57 loc) · 1.84 KB
/
demo_select.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package demo
import (
"fmt"
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/mlctrez/goapp-mdc/pkg/base"
"github.com/mlctrez/goapp-mdc/pkg/layout"
"github.com/mlctrez/goapp-mdc/pkg/list"
"github.com/mlctrez/goapp-mdc/pkg/selectm"
"github.com/mlctrez/goapp-mdc/pkg/snackbar"
)
type SelectDemo struct {
app.Compo
base.JsUtil
snack *snackbar.SnackBar
}
func buildList(hasEmpty bool, selected int) list.Items {
items := list.Items{}
if hasEmpty {
items = append(items, &list.Item{Type: list.ItemTypeOption, Text: "", Value: ""})
}
for i := 0; i < 10; i++ {
iStr := fmt.Sprintf("%d", i)
items = append(items, &list.Item{Type: list.ItemTypeOption, Text: "Option " + iStr, Value: "value" + iStr})
}
items.Select(selected)
return items
}
func (s *SelectDemo) Render() app.UI {
if s.snack == nil {
s.snack = &snackbar.SnackBar{ActionButtonText: "OK", LabelText: "messages will appear here"}
}
var rows []app.UI
for _, outlined := range []bool{false, true} {
for _, required := range []bool{false, true} {
for _, hasEmpty := range []bool{true, false} {
for _, selectedIndex := range []int{-1, 2} {
label := fmt.Sprintf("Outlined:%t Required:%t hasEmpty=%t selected=%d", outlined, required, hasEmpty, selectedIndex)
sel := &selectm.MDCSelect{Label: "Label", ItemsLabel: "Item Label", Id: s.UUID(),
Outlined: outlined,
Items: buildList(hasEmpty, selectedIndex),
Required: required,
}
rows = append(rows, GridRow(label, sel))
}
}
}
}
return PageBody(s.snack, layout.Grid().Body(rows...))
}
func (s *SelectDemo) OnMount(ctx app.Context) {
s.snack.TimeoutMs(-1)
ctx.Handle(string(selectm.MDCSelectChange), func(context app.Context, action app.Action) {
s.snack.LabelText = fmt.Sprintf("%v", action)
s.snack.Update()
ctx.NewActionWithValue(string(snackbar.Open), s.snack)
})
}