~repos /gromer

#golang#htmx#ssr

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.


47531d97 pyros2097

5 years ago
fix row col
Files changed (4) hide show
  1. attributes.go +90 -0
  2. css_test.go → attributes_test.go +1 -11
  3. css.go +0 -77
  4. html.go +49 -10
attributes.go ADDED
@@ -0,0 +1,90 @@
1
+ package app
2
+
3
+ import (
4
+ "context"
5
+
6
+ "github.com/pyros2097/wapp/errors"
7
+ )
8
+
9
+ type baseAttribute struct {
10
+ UI
11
+ }
12
+
13
+ func (c baseAttribute) Kind() Kind {
14
+ return Attribute
15
+ }
16
+
17
+ func (c baseAttribute) JSValue() Value {
18
+ return nil
19
+ }
20
+
21
+ func (c baseAttribute) Mounted() bool {
22
+ return false
23
+ }
24
+
25
+ func (c baseAttribute) name() string {
26
+ return "css"
27
+ }
28
+
29
+ func (c baseAttribute) self() UI {
30
+ return c
31
+ }
32
+
33
+ func (c baseAttribute) setSelf(UI) {
34
+ }
35
+
36
+ func (c baseAttribute) context() context.Context {
37
+ return nil
38
+ }
39
+
40
+ func (c baseAttribute) attributes() map[string]string {
41
+ return nil
42
+ }
43
+
44
+ func (c baseAttribute) eventHandlers() map[string]eventHandler {
45
+ return nil
46
+ }
47
+
48
+ func (c baseAttribute) parent() UI {
49
+ return nil
50
+ }
51
+
52
+ func (c baseAttribute) setParent(UI) {
53
+ }
54
+
55
+ func (c baseAttribute) children() []UI {
56
+ return nil
57
+ }
58
+
59
+ func (c baseAttribute) mount() error {
60
+ return errors.New("condition is not mountable").
61
+ Tag("name", c.name()).
62
+ Tag("kind", c.Kind())
63
+ }
64
+
65
+ func (c baseAttribute) dismount() {
66
+ }
67
+
68
+ func (c baseAttribute) update(UI) error {
69
+ return errors.New("condition cannot be updated").
70
+ Tag("name", c.name()).
71
+ Tag("kind", c.Kind())
72
+ }
73
+
74
+ type CssAttribute struct {
75
+ baseAttribute
76
+ classes string
77
+ }
78
+
79
+ func Css(d string) UI {
80
+ return CssAttribute{classes: d}
81
+ }
82
+
83
+ type OnClickAttribute struct {
84
+ baseAttribute
85
+ cb func()
86
+ }
87
+
88
+ func OnClick(cb func()) UI {
89
+ return OnClickAttribute{cb: cb}
90
+ }
css_test.go → attributes_test.go RENAMED
@@ -7,20 +7,10 @@ import (
7
7
  "github.com/stretchr/testify/assert"
8
8
  )
9
9
 
10
- func Row(uis ...UI) UI {
11
- uis = append(uis, Css("flex flex-row justify-center align-items-center"))
12
- return Div(uis...)
13
- }
14
-
15
- func Col(uis ...UI) UI {
16
- uis = append(uis, Css("flex flex-row justify-center align-items-center"))
17
- return Div(uis...)
18
- }
19
-
20
10
  func Counter(c *RenderContext) UI {
21
11
  count, _ := c.UseInt(0)
22
12
  return Col(
23
- Div(Css("yellow"),
13
+ Row(Css("yellow"),
24
14
  Text("Counter"),
25
15
  ),
26
16
  Row(
css.go DELETED
@@ -1,77 +0,0 @@
1
- package app
2
-
3
- import (
4
- "context"
5
-
6
- "github.com/pyros2097/wapp/errors"
7
- )
8
-
9
- type CSSClass struct {
10
- UI
11
- classes string
12
- }
13
-
14
- func Css(d string) UI {
15
- return CSSClass{classes: d}
16
- }
17
-
18
- func (c CSSClass) Kind() Kind {
19
- return Attribute
20
- }
21
-
22
- func (c CSSClass) JSValue() Value {
23
- return nil
24
- }
25
-
26
- func (c CSSClass) Mounted() bool {
27
- return false
28
- }
29
-
30
- func (c CSSClass) name() string {
31
- return "css"
32
- }
33
-
34
- func (c CSSClass) self() UI {
35
- return c
36
- }
37
-
38
- func (c CSSClass) setSelf(UI) {
39
- }
40
-
41
- func (c CSSClass) context() context.Context {
42
- return nil
43
- }
44
-
45
- func (c CSSClass) attributes() map[string]string {
46
- return nil
47
- }
48
-
49
- func (c CSSClass) eventHandlers() map[string]eventHandler {
50
- return nil
51
- }
52
-
53
- func (c CSSClass) parent() UI {
54
- return nil
55
- }
56
-
57
- func (c CSSClass) setParent(UI) {
58
- }
59
-
60
- func (c CSSClass) children() []UI {
61
- return nil
62
- }
63
-
64
- func (c CSSClass) mount() error {
65
- return errors.New("condition is not mountable").
66
- Tag("name", c.name()).
67
- Tag("kind", c.Kind())
68
- }
69
-
70
- func (c CSSClass) dismount() {
71
- }
72
-
73
- func (c CSSClass) update(UI) error {
74
- return errors.New("condition cannot be updated").
75
- Tag("name", c.name()).
76
- Tag("kind", c.Kind())
77
- }
html.go CHANGED
@@ -48,26 +48,65 @@ func Script(str string) *elem {
48
48
  }
49
49
  }
50
50
 
51
+ // func (e *elem) OnBlur(h EventHandler) *elem {
52
+ // e.setEventHandler("blur", h)
53
+ // return e
54
+ // }
55
+
56
+ // func (e *elem) OnChange(h EventHandler) *elem {
57
+ // e.setEventHandler("change", h)
58
+ // return e
59
+ // }
60
+
61
+ // func (e *elem) OnClick(h EventHandler) *elem {
62
+ // e.setEventHandler("click", h)
63
+ // return e
64
+ // }
65
+
66
+ // func (e *elem) OnFocus(h EventHandler) *elem {
67
+ // e.setEventHandler("focus", h)
68
+ // return e
69
+ // }
70
+
71
+ // func (e *elem) OnInput(h EventHandler) *elem {
72
+ // e.setEventHandler("input", h)
73
+ // return e
74
+ // }
75
+
51
- func RemoveAttributes(uis ...UI) ([]UI, string) {
76
+ func mergeAttributes(parent *elem, uis ...UI) {
52
77
  elems := make([]UI, 0, len(uis))
53
- classes := ""
54
78
  for _, v := range uis {
55
79
  if v.Kind() == Attribute {
56
- cc, _ := v.(CSSClass)
80
+ switch c := v.(type) {
81
+ case CssAttribute:
82
+ if vv, ok := parent.attrs["classes"]; ok {
83
+ parent.setAttr("class", vv+" "+c.classes)
84
+ } else {
57
- classes = classes + cc.classes
85
+ parent.setAttr("class", c.classes)
86
+ }
87
+ case OnClickAttribute:
88
+ parent.setEventHandler("click", func(ctx Context, e Event) {
89
+ c.cb()
90
+ })
91
+ }
58
92
  } else {
59
93
  elems = append(elems, v)
60
94
  }
61
95
 
62
96
  }
63
- return elems, classes
97
+ parent.setBody(elems...)
64
98
  }
65
99
 
66
100
  func Div(uis ...UI) *elem {
67
- elems, classes := RemoveAttributes(uis...)
68
- e := &elem{tag: "div", body: elems}
101
+ e := &elem{tag: "div"}
69
- if classes != "" {
70
- e.setAttr("class", classes)
102
+ mergeAttributes(e, uis...)
71
- }
72
103
  return e
73
104
  }
105
+
106
+ func Row(uis ...UI) UI {
107
+ return Div(append([]UI{Css("flex flex-row justify-center align-items-center")}, uis...)...)
108
+ }
109
+
110
+ func Col(uis ...UI) UI {
111
+ return Div(append([]UI{Css("flex flex-col justify-center align-items-center")}, uis...)...)
112
+ }