Skip to content

Commit

Permalink
adding key (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Frachet authored Feb 21, 2019
1 parent 21632a1 commit 5ce3219
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
11 changes: 8 additions & 3 deletions h.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package vn

func H(tagName string, attrs *Attrs, children interface{}) *Vnode {
func H(tagName string, attrs *Attrs, children interface{}, params ...string) *Vnode {
sanitizedAttrs := Sanitize(attrs)
var key *string

if len(params) == 1 {
key = &params[0]
}

switch (children).(type) {
case string:
return NewVNode(tagName, sanitizedAttrs, nil, &TextNode{(children).(string), nil}, nil)
return NewVNode(tagName, sanitizedAttrs, nil, &TextNode{(children).(string), nil}, nil, key)
default:
return NewVNode(tagName, sanitizedAttrs, children.(Children), nil, nil)
return NewVNode(tagName, sanitizedAttrs, children.(Children), nil, nil, key)
}
}
14 changes: 14 additions & 0 deletions mock/node.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Node interface {
GetElement() *vnd.DomElement
GetTagName() string
GetAttrs() *Attrs
GetKey() *string
HasElement() bool
SetElement(vnd.DomElement)
HashCode() string
Expand Down
17 changes: 15 additions & 2 deletions vnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ type Vnode struct {
Children Children
Text *TextNode
Element *vnd.DomElement
key *string
}

func NewVNode(tagName string, attrs *Attrs, children Children, text *TextNode, element *vnd.DomElement) *Vnode {
return &Vnode{tagName, attrs, children, text, element}
func NewVNode(tagName string, attrs *Attrs, children Children, text *TextNode, element *vnd.DomElement, key *string) *Vnode {
return &Vnode{tagName, attrs, children, text, element, key}
}

func (vnode *Vnode) IsSame(other Node) bool {
currKey := vnode.GetKey()
otherKey := other.GetKey()

if currKey != nil && otherKey != nil {
fmt.Println("They are the same => ", *currKey)
return *currKey == *otherKey
}

if vnh.IsNil(vnode.Text) {
if vnh.IsNil(other.GetText()) {
return vnode.HashCode() == other.HashCode()
Expand Down Expand Up @@ -52,6 +61,10 @@ func (vnode *Vnode) ChildAt(index int) Node {
return nil
}

func (vnode *Vnode) GetKey() *string {
return vnode.key
}

func (vnode *Vnode) GetText() *TextNode {
return vnode.Text
}
Expand Down
18 changes: 13 additions & 5 deletions vnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ func TestVnode_IsSame_WithChildren(t *testing.T) {
H("li", nil, "Second item"),
})

c := H("ul", &Attrs{Props: &Props{"class": "navbar"}}, Children{
assert.Equal(t, true, a.IsSame(b))
}

func TestVnode_IsSame_WithKey(t *testing.T) {
a := H("ul", &Attrs{Props: &Props{"class": "navbar"}}, Children{
H("li", nil, "First item"),
H("li", nil, "Something else"),
})
H("li", nil, "Second item"),
}, "1")

assert.Equal(t, true, a.IsSame(b))
assert.Equal(t, false, a.IsSame(c))
b := H("ul", &Attrs{Props: &Props{"class": "navbar"}}, Children{
H("li", nil, "First item"),
H("li", nil, "Second item"),
}, "2")

assert.Equal(t, false, a.IsSame(b))
}

func TestVnode_ChildrenCount(t *testing.T) {
Expand Down

0 comments on commit 5ce3219

Please sign in to comment.