/
string.go
101 lines (90 loc) · 1.86 KB
/
string.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package views
import (
"fmt"
"strconv"
"github.com/dave/dst"
"github.com/dave/dst/dstutil"
"github.com/dave/frizz/actions"
"github.com/dave/frizz/stores"
"github.com/dave/jsgo/server/frizz/gotypes"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
"github.com/gopherjs/vecty/prop"
)
type String struct {
vecty.Core
root gotypes.Object
app *stores.App
Data dst.Expr `vecty:"prop"`
input *vecty.HTML
}
func NewString(app *stores.App, root gotypes.Object, data dst.Expr) *String {
v := &String{
app: app,
root: root,
Data: data,
}
return v
}
/*
func (v *String) Mount() {
v.app.Watch(v, func(done chan struct{}) {
defer close(done)
// Things that happen on every refresh
})
// Things that happen once at initialisation
}
func (v *String) Unmount() {
v.app.Delete(v)
}
*/
func (v *String) Render() vecty.ComponentOrHTML {
var value string
switch data := v.Data.(type) {
case *dst.BasicLit:
value, _ = strconv.Unquote(data.Value)
}
v.input = elem.Input(
vecty.Markup(
prop.Type(prop.TypeText),
vecty.Class("form-control"),
prop.Value(value),
event.KeyPress(func(ev *vecty.Event) {
if ev.Get("keyCode").Int() == 13 {
ev.Call("preventDefault")
v.save(ev)
}
}),
),
)
return elem.Form(
elem.Div(
vecty.Markup(
vecty.Class("form-group"),
),
v.input,
),
elem.Button(
vecty.Markup(
prop.Type(prop.TypeSubmit),
vecty.Class("btn", "btn-primary"),
event.Click(v.save).PreventDefault(),
),
vecty.Text("Submit"),
),
)
}
func (v *String) save(*vecty.Event) {
value := v.input.Node().Get("value").String()
v.app.Dispatch(&actions.UserMutatedValue{
Root: v.root,
Change: func(c *dstutil.Cursor) bool {
if c.Node() != v.Data {
return true
}
c.Node().(*dst.BasicLit).Value = fmt.Sprintf("%q", value)
return true
},
})
}