-
Notifications
You must be signed in to change notification settings - Fork 0
/
for.go
60 lines (51 loc) · 1.24 KB
/
for.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
package solidgo
import (
"reflect"
"syscall/js"
)
func For(collection func() []interface{}, renderer func(index int) Element) Element {
parent := Window.Document.CreateElement("div")
currentEffect = func() {
//TODO for now just rebuild whole thing
}
for i := 0; i < reflect.ValueOf(collection()).Len(); i++ {
parent.AppendChild(renderer(i))
}
currentEffect = nil
return parent
}
type Appendable interface {
AppendTo(Element)
}
type ForStruct[T comparable] struct {
Collection func() []T
Render func(T) Element
}
func (forR ForStruct[T]) AppendTo(parent Element) {
var previous []Element
currentEffect = func() {
for _, item := range previous {
// replaced Remove with binding
js.Value(item).Call("remove")
}
previous = nil
for _, item := range forR.Collection() {
element := forR.Render(item)
previous = append(previous, element)
parent.AppendChild(element)
}
}
currentEffect()
currentEffect = nil
}
// Generic implementation of For
func ForG[T any](collection func() []T, renderer func(item T) Element) Element {
parent := Window.Document.CreateElement("div")
currentEffect = func() {
}
for _, item := range collection() {
parent.AppendChild(renderer(item))
}
currentEffect = nil
return parent
}