This repository was archived by the owner on Aug 27, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtodo_app.go
More file actions
98 lines (78 loc) · 2.01 KB
/
Copy pathtodo_app.go
File metadata and controls
98 lines (78 loc) · 2.01 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
package immtodoapp // import "myitcv.io/react/examples/immtodoapp"
import (
"fmt"
"honnef.co/go/js/dom"
r "myitcv.io/react"
)
//go:generate reactGen
//go:generate immutableGen
// TodoAppDef is the definition fo the TodoApp component
type TodoAppDef struct {
r.ComponentDef
}
type _Imm_item struct {
name string
}
type _Imm_itemS []*item
// TodoAppState is the state type for the TodoApp component
type TodoAppState struct {
items *itemS
currItem string
}
// TodoApp creates instances of the TodoApp component
func TodoApp() *TodoAppDef {
res := &TodoAppDef{}
r.BlessElement(res, nil)
return res
}
func (t *TodoAppDef) GetInitialState() TodoAppState {
return TodoAppState{
items: new(itemS),
}
}
// Render renders the TodoApp component
func (t *TodoAppDef) Render() r.Element {
var entries []*r.LiDef
for _, v := range t.State().items.Range() {
entry := r.Li(nil, r.S(v.name()))
entries = append(entries, entry)
}
return r.Div(nil,
r.H3(nil, r.S("TODO")),
r.Ul(nil, entries...),
r.Form(&r.FormProps{ClassName: "form-inline"},
r.Div(
&r.DivProps{ClassName: "form-group"},
r.Label(&r.LabelProps{ClassName: "sr-only", For: "todoText"}, r.S("Todo Item")),
r.Input(&r.InputProps{
Type: "text",
ClassName: "form-control",
ID: "todoText",
Placeholder: "Todo Item",
Value: t.State().currItem,
OnChange: inputChange{t},
}),
r.Button(&r.ButtonProps{
Type: "submit",
ClassName: "btn btn-default",
OnClick: add{t},
}, r.S(fmt.Sprintf("Add #%v", t.State().items.Len()+1))),
),
),
)
}
type inputChange struct{ t *TodoAppDef }
type add struct{ t *TodoAppDef }
func (i inputChange) OnChange(se *r.SyntheticEvent) {
target := se.Target().(*dom.HTMLInputElement)
ns := i.t.State()
ns.currItem = target.Value
i.t.SetState(ns)
}
func (a add) OnClick(se *r.SyntheticMouseEvent) {
ns := a.t.State()
ns.items = ns.items.Append(new(item).setName(ns.currItem))
ns.currItem = ""
a.t.SetState(ns)
se.PreventDefault()
}