~repos /gromer
git clone https://pyrossh.dev/repos/gromer.git
gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.
0a85abbb
—
Peter John 4 years ago
add useState in counter
- css.go +8 -0
- example/components/button.go +1 -1
- example/components/counter.go +16 -17
- example/pages/get.go +2 -1
- hooks.go +21 -8
- html.go +6 -3
css.go
CHANGED
|
@@ -476,6 +476,14 @@ func mapApply(obj KeyValues) {
|
|
|
476
476
|
}
|
|
477
477
|
}
|
|
478
478
|
|
|
479
|
+
func CreateTheme() {
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
func Styled(name string, classes string) {
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// var Button = Styled("button", "px-20 bg-gray-500")
|
|
486
|
+
|
|
479
487
|
var normalizeStyles = `
|
|
480
488
|
*, ::before, ::after { box-sizing: border-box; }
|
|
481
489
|
html { -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; line-height: 1.15; -webkit-text-size-adjust: 100%; }
|
example/components/button.go
CHANGED
|
@@ -6,7 +6,7 @@ import (
|
|
|
6
6
|
|
|
7
7
|
func Button2(title, clickHandler string) *Element {
|
|
8
8
|
return Button(Css("bg-gray-300 border-b-2 border-white hover:bg-gray-200 focus:outline-none rounded text-gray-700"), OnClick(clickHandler),
|
|
9
|
-
Div(Css("flex flex-row flex-1 justify-center items-center
|
|
9
|
+
Div(Css("flex flex-row flex-1 justify-center items-center py-4 px-6"),
|
|
10
10
|
Text(title),
|
|
11
11
|
),
|
|
12
12
|
)
|
example/components/counter.go
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
package components
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"context"
|
|
4
5
|
"strconv"
|
|
5
6
|
|
|
6
7
|
. "github.com/pyros2097/gromer"
|
|
7
8
|
)
|
|
8
9
|
|
|
9
|
-
// func CounterReducer(start int) Reducer {
|
|
10
|
-
// return Reducer{
|
|
11
|
-
// Name: "counter",
|
|
12
|
-
// State: State{
|
|
13
|
-
// "count": start,
|
|
14
|
-
// },
|
|
15
|
-
// Actions: Actions{
|
|
16
|
-
// "increment": func() string { return "this.state.count += 1;" },
|
|
17
|
-
// "decrement": func() string { return "this.state.count -= 1;" },
|
|
18
|
-
// },
|
|
19
|
-
// }
|
|
20
|
-
// }
|
|
21
|
-
|
|
22
|
-
func Counter(start int) *Element {
|
|
10
|
+
func Counter(c context.Context, start int) *Element {
|
|
23
|
-
|
|
11
|
+
count, setCount := UseState(c, start)
|
|
12
|
+
increment := func() {
|
|
13
|
+
setCount(count().(int) + 1)
|
|
14
|
+
}
|
|
15
|
+
decrement := func() {
|
|
16
|
+
setCount(count().(int) + 1)
|
|
17
|
+
}
|
|
24
18
|
return Col(Css("text-3xl text-gray-700"),
|
|
25
19
|
Row(
|
|
26
20
|
Row(Css("underline"),
|
|
@@ -29,10 +23,15 @@ func Counter(start int) *Element {
|
|
|
29
23
|
),
|
|
30
24
|
Row(
|
|
31
25
|
Button2("-", "decrement"),
|
|
32
|
-
Row(Css("m-20 text-5xl"), XText("
|
|
26
|
+
Row(Css("m-20 text-5xl"), XText("count"),
|
|
33
|
-
Text(
|
|
27
|
+
Text("count"),
|
|
34
28
|
),
|
|
35
29
|
Button2("+", "increment"),
|
|
36
30
|
),
|
|
31
|
+
M{
|
|
32
|
+
"count": strconv.Itoa(count().(int)),
|
|
33
|
+
"increment": increment,
|
|
34
|
+
"decrement": decrement,
|
|
35
|
+
},
|
|
37
36
|
)
|
|
38
37
|
}
|
example/pages/get.go
CHANGED
|
@@ -9,6 +9,7 @@ import (
|
|
|
9
9
|
)
|
|
10
10
|
|
|
11
11
|
func GET(c context.Context) (HtmlPage, int, error) {
|
|
12
|
+
ctx := WithState(c)
|
|
12
13
|
userID := GetUserID(c)
|
|
13
14
|
return Html(
|
|
14
15
|
Head(
|
|
@@ -28,7 +29,7 @@ func GET(c context.Context) (HtmlPage, int, error) {
|
|
|
28
29
|
H2(Text("Hello this is a h2")),
|
|
29
30
|
H3(XData("{ message: 'I ❤️ Alpine' }"), XText("message"), Text("")),
|
|
30
31
|
Div(Css("mt-10"),
|
|
31
|
-
Counter(4),
|
|
32
|
+
Counter(ctx, 4),
|
|
32
33
|
),
|
|
33
34
|
),
|
|
34
35
|
),
|
hooks.go
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
package gromer
|
|
2
2
|
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
)
|
|
6
|
+
|
|
3
|
-
type
|
|
7
|
+
type StateContext struct {
|
|
4
8
|
index int
|
|
5
9
|
datas []interface{}
|
|
6
10
|
}
|
|
@@ -17,15 +21,24 @@ type Context struct {
|
|
|
17
21
|
// }
|
|
18
22
|
// }
|
|
19
23
|
|
|
24
|
+
func getState(ctx context.Context) *StateContext {
|
|
25
|
+
return ctx.Value("state").(*StateContext)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func WithState(ctx context.Context) context.Context {
|
|
29
|
+
return context.WithValue(ctx, "state", &StateContext{})
|
|
30
|
+
}
|
|
31
|
+
|
|
20
|
-
func UseState(
|
|
32
|
+
func UseState(ctx context.Context, s interface{}) (func() interface{}, func(v interface{})) {
|
|
33
|
+
stateContext := getState(ctx)
|
|
21
|
-
localIndex :=
|
|
34
|
+
localIndex := stateContext.index
|
|
22
|
-
if len(
|
|
35
|
+
if len(stateContext.datas) <= stateContext.index+1 {
|
|
23
|
-
|
|
36
|
+
stateContext.datas = append(stateContext.datas, s)
|
|
24
|
-
|
|
37
|
+
stateContext.index += 1
|
|
25
38
|
}
|
|
26
39
|
return func() interface{} {
|
|
27
|
-
return
|
|
40
|
+
return stateContext.datas[localIndex].(interface{})
|
|
28
41
|
}, func(v interface{}) {
|
|
29
|
-
|
|
42
|
+
stateContext.datas[localIndex] = v
|
|
30
43
|
}
|
|
31
44
|
}
|
html.go
CHANGED
|
@@ -2,6 +2,7 @@ package gromer
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"bytes"
|
|
5
|
+
"context"
|
|
5
6
|
"fmt"
|
|
6
7
|
"io"
|
|
7
8
|
"strconv"
|
|
@@ -45,6 +46,7 @@ func mergeAttributes(parent *Element, uis ...interface{}) *Element {
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
type HtmlPage struct {
|
|
49
|
+
ctx context.Context
|
|
48
50
|
classLookup map[string]bool
|
|
49
51
|
css *bytes.Buffer
|
|
50
52
|
js *bytes.Buffer
|
|
@@ -87,6 +89,7 @@ func (p *HtmlPage) WriteHtml(w io.Writer) {
|
|
|
87
89
|
|
|
88
90
|
func Html(h *Element, b *Element) HtmlPage {
|
|
89
91
|
return HtmlPage{
|
|
92
|
+
// ctx: context.WithValue(ctx, "state", &StateContext{}),
|
|
90
93
|
classLookup: map[string]bool{},
|
|
91
94
|
js: bytes.NewBuffer(nil),
|
|
92
95
|
css: bytes.NewBuffer(nil),
|
|
@@ -97,9 +100,9 @@ func Html(h *Element, b *Element) HtmlPage {
|
|
|
97
100
|
|
|
98
101
|
func Head(elems ...*Element) *Element {
|
|
99
102
|
basic := []*Element{
|
|
100
|
-
|
|
103
|
+
{tag: "meta", selfClosing: true, attrs: map[string]string{"charset": "UTF-8"}},
|
|
101
|
-
|
|
104
|
+
{tag: "meta", selfClosing: true, attrs: map[string]string{"http-equiv": "Content-Type", "content": "text/html;charset=utf-8"}},
|
|
102
|
-
|
|
105
|
+
{tag: "meta", selfClosing: true, attrs: map[string]string{"http-equiv": "encoding", "content": "utf-8"}},
|
|
103
106
|
}
|
|
104
107
|
return &Element{tag: "head", children: append(basic, elems...)}
|
|
105
108
|
}
|