Skip to content

Commit

Permalink
add: support for append, prepend elements
Browse files Browse the repository at this point in the history
  • Loading branch information
alindesign committed Jun 5, 2020
1 parent e0f986e commit 1772840
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ html := react.RenderWithStats(react.H1(nil, react.Text("Hello!")))
// Print rendered nodes and time
// <h1>Hello!</h1>

html := react.RenderBytes(react.H1(nil, react.Text("Hello!"))) // render []byte("<h1>Hello!</h1>")
html := react.RenderToBytes(react.H1(nil, react.Text("Hello!"))) // render []byte("<h1>Hello!</h1>")
```

Passing data to context
Expand Down
15 changes: 15 additions & 0 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ func (e *Element) isVoidElement() bool {

return false
}

func (e *Element) Prepend(el *Element) *Element {
e.Childs = append([]*Element{el}, e.Childs...)
return e
}

func (e *Element) Append(el *Element) *Element {
e.Childs = append(e.Childs, el)
return e
}

func (e *Element) SetProp(prop string, value interface{}) *Element {
e.Props[prop] = value
return e
}
9 changes: 8 additions & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ func RenderWithStats(component *Element, data ...map[string]interface{}) string
return str
}

func RenderBytes(component *Element, data ...map[string]interface{}) []byte {
func RenderToBytes(component *Element, data ...map[string]interface{}) []byte {
return render(component, data...).Bytes()
}

func RenderToBytesWithStats(component *Element, data ...map[string]interface{}) []byte {
r := render(component, data...)
bytes := r.Bytes()
r.Stats()
return bytes
}
28 changes: 27 additions & 1 deletion render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRender(t *testing.T) {
})

t.Run("it should render to bytes an empty div", func(t *testing.T) {
div := RenderBytes(CreateElement("div", nil))
div := RenderToBytes(CreateElement("div", nil))

assert.Equal(t, []byte("<div></div>"), div)
})
Expand Down Expand Up @@ -126,4 +126,30 @@ func TestRenderElement(t *testing.T) {

assert.Equal(t, `Sample text &lt;script type=&#34;text/javascript&#34;&gt;alert(&#34;Hi!&#34;);&lt;/script&gt;`, str)
})

t.Run("it should prepend an element correctly", func(t *testing.T) {
el := CreateElement("div", Props{
"className": "container",
})

el.Prepend(H1(nil, Text("item 1")))
el.Prepend(H1(nil, Text("item 2")))

div := Render(el)

assert.Equal(t, `<div class="container"><h1>item 2</h1><h1>item 1</h1></div>`, div)
})

t.Run("it should append an element correctly", func(t *testing.T) {
el := CreateElement("div", Props{
"className": "container",
})

el.Append(H1(nil, Text("item 1")))
el.Append(H1(nil, Text("item 2")))

div := Render(el)

assert.Equal(t, `<div class="container"><h1>item 1</h1><h1>item 2</h1></div>`, div)
})
}

0 comments on commit 1772840

Please sign in to comment.