Skip to content

Commit 1849591

Browse files
committed
onclick update
1 parent b23c503 commit 1849591

5 files changed

Lines changed: 50 additions & 13 deletions

File tree

div/div.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package div
22

33
import (
4-
"github.com/google/uuid"
54
"github.com/man.go/mango/style"
65
"github.com/man.go/mango/widget"
76
)
@@ -24,7 +23,6 @@ func New(ctx *widget.WidgetContext, options ...Options) widget.Widget {
2423
option(div)
2524
}
2625

27-
div.SetID(uuid.New().String())
2826
div.SetStyle(div.style.String())
2927
div.SetOnClick(div.onClick)
3028

div/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ func Children(children ...widget.Widget) Options {
1212
}
1313
}
1414

15+
func ID(id string) Options {
16+
return func(div *Div) {
17+
div.SetID(id)
18+
}
19+
}
20+
1521
func Style(styleOptions ...StyleOptions) Options {
1622
return func(div *Div) {
1723
for _, styleOption := range styleOptions {

main.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package main
22

33
import (
4-
"fmt"
5-
64
"github.com/man.go/mango/div"
75
"github.com/man.go/mango/style"
86
"github.com/man.go/mango/widget"
97
)
108

119
func main() {
1210
ctx := widget.Context()
11+
div2 := buildDiv2(ctx)
12+
1313
ctx.AppendChild(
1414
div.New(
1515
ctx,
@@ -26,20 +26,24 @@ func main() {
2626
div.Background(style.Background{Color: "blue"}),
2727
),
2828
div.OnClick(func(this widget.Widget) {
29-
fmt.Println("clicked")
3029
}),
3130
),
32-
div.New(
33-
ctx,
34-
div.Style(
35-
div.Size(32, 32),
36-
div.Border(style.Border{Width: 1, Style: "solid", Color: "red"}),
37-
div.Background(style.Background{Color: "green"}),
38-
),
39-
),
31+
div2,
4032
),
4133
),
4234
)
4335

4436
select {}
4537
}
38+
39+
func buildDiv2(ctx *widget.WidgetContext) widget.Widget {
40+
return div.New(
41+
ctx,
42+
div.ID("test"),
43+
div.Style(
44+
div.Size(32, 32),
45+
div.Border(style.Border{Width: 1, Style: "solid", Color: "red"}),
46+
div.Background(style.Background{Color: "green"}),
47+
),
48+
)
49+
}

widget/widget.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ func (w Widget) Call(m string, args ...any) Widget {
1414
return Widget(js.Value(w).Call(m, args...))
1515
}
1616

17+
func (w Widget) ChildAt(index int) Widget {
18+
return Widget(js.Value(w).Get("children").Index(index))
19+
}
20+
21+
func (w Widget) Get(p string) js.Value {
22+
return js.Value(w).Get(p)
23+
}
24+
25+
func (w Widget) ID() string {
26+
return w.Get("id").String()
27+
}
28+
29+
func (w Widget) IsNull() bool {
30+
return js.Value(w).IsNull()
31+
}
32+
33+
func (w Widget) Parent() Widget {
34+
return Widget(js.Value(w).Get("parentElement"))
35+
}
36+
1737
func (w Widget) SetID(id string) {
1838
w.Set("id", id)
1939
}

widget/widget_context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ func (c *WidgetContext) AppendChild(child Widget) {
2828
func (c *WidgetContext) CreateElement(tag string) Widget {
2929
return Widget(c.Doc.Call("createElement", tag))
3030
}
31+
32+
func (c *WidgetContext) GetElementByID(id string) *Widget {
33+
element := c.Doc.Call("getElementById", id)
34+
if element.IsNull() {
35+
return nil
36+
}
37+
w := Widget(element)
38+
return &w
39+
}

0 commit comments

Comments
 (0)