~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.


6dda02f1 pyros2097

5 years ago
add more event handlers
Files changed (2) hide show
  1. attributes.go +46 -0
  2. html.go +0 -44
attributes.go CHANGED
@@ -82,3 +82,49 @@ type OnClickAttribute struct {
82
82
  func OnClick(cb func()) UI {
83
83
  return OnClickAttribute{cb: cb}
84
84
  }
85
+
86
+ type OnChangeAttribute struct {
87
+ baseAttribute
88
+ cb EventHandler
89
+ }
90
+
91
+ func OnChange(cb EventHandler) UI {
92
+ return OnChangeAttribute{cb: cb}
93
+ }
94
+
95
+ type OnInputAttribute struct {
96
+ baseAttribute
97
+ cb EventHandler
98
+ }
99
+
100
+ func OnInput(cb EventHandler) UI {
101
+ return OnInputAttribute{cb: cb}
102
+ }
103
+
104
+ func mergeAttributes(parent *elem, uis ...UI) {
105
+ elems := make([]UI, 0, len(uis))
106
+ for _, v := range uis {
107
+ if v.Kind() == Attribute {
108
+ switch c := v.(type) {
109
+ case CssAttribute:
110
+ if vv, ok := parent.attrs["classes"]; ok {
111
+ parent.setAttr("class", vv+" "+c.classes)
112
+ } else {
113
+ parent.setAttr("class", c.classes)
114
+ }
115
+ case OnClickAttribute:
116
+ parent.setEventHandler("click", func(e Event) {
117
+ c.cb()
118
+ })
119
+ case OnChangeAttribute:
120
+ parent.setEventHandler("change", c.cb)
121
+ case OnInputAttribute:
122
+ parent.setEventHandler("input", c.cb)
123
+ }
124
+ } else {
125
+ elems = append(elems, v)
126
+ }
127
+
128
+ }
129
+ parent.setBody(elems...)
130
+ }
html.go CHANGED
@@ -48,50 +48,6 @@ 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) OnFocus(h EventHandler) *elem {
62
- // e.setEventHandler("focus", h)
63
- // return e
64
- // }
65
-
66
- // func (e *elem) OnInput(h EventHandler) *elem {
67
- // e.setEventHandler("input", h)
68
- // return e
69
- // }
70
-
71
- func mergeAttributes(parent *elem, uis ...UI) {
72
- elems := make([]UI, 0, len(uis))
73
- for _, v := range uis {
74
- if v.Kind() == Attribute {
75
- switch c := v.(type) {
76
- case CssAttribute:
77
- if vv, ok := parent.attrs["classes"]; ok {
78
- parent.setAttr("class", vv+" "+c.classes)
79
- } else {
80
- parent.setAttr("class", c.classes)
81
- }
82
- case OnClickAttribute:
83
- parent.setEventHandler("click", func(e Event) {
84
- c.cb()
85
- })
86
- }
87
- } else {
88
- elems = append(elems, v)
89
- }
90
-
91
- }
92
- parent.setBody(elems...)
93
- }
94
-
95
51
  func Div(uis ...UI) *elem {
96
52
  e := &elem{tag: "div"}
97
53
  mergeAttributes(e, uis...)