-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
executable file
·86 lines (75 loc) · 1.7 KB
/
list.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package list
import (
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/mlctrez/goapp-mdc/pkg/base"
)
type List struct {
app.Compo
base.JsUtil
Id string
Items []app.UI
TwoLine bool
Type Type
jsApi app.Value
}
// Type drives the rendering of the list
type Type int
const (
None Type = iota
SingleSelection
RadioGroup
CheckBox
Navigation
)
func (l *List) Render() app.UI {
root := l.adapt()
root.Class("mdc-deprecated-list")
if l.Id != "" {
root.ID(l.Id)
}
if l.TwoLine {
root.Class("mdc-deprecated-list--two-line")
}
switch l.Type {
case SingleSelection:
root.Attr("role", "listbox")
case RadioGroup:
root.Attr("role", "radiogroup")
case CheckBox:
root.Attr("role", "group")
}
root.Body(l.Items...)
return root.UI()
}
type EventType string
const Action EventType = "MDCList:action"
const Select EventType = "MDCList:select"
func (l *List) OnMount(ctx app.Context) {
e := l.JSValue()
l.jsApi = l.JsNewAtPath("mdc.list.MDCList", e)
if l.Type == SingleSelection && l.jsApi.Truthy() {
l.jsApi.Set("singleSelection", true)
}
e.Call("addEventListener", string(Action), app.FuncOf(l.event(ctx, Action)))
}
func (l *List) Select(idx int) {
for _, item := range l.Items {
value := item.JSValue()
if value.Truthy() {
value.Call("blur")
}
}
l.jsApi.Set("selectedIndex", idx)
}
func (l *List) event(ctx app.Context, action EventType) func(this app.Value, args []app.Value) interface{} {
return func(this app.Value, args []app.Value) interface{} {
if len(args) < 1 {
return nil
}
idx := l.JsValueAt(args[0], "detail.index", false)
if !idx.IsUndefined() {
ctx.NewActionWithValue(string(Select), l, app.T("index", idx.Int()))
}
return nil
}
}