Skip to content

Commit

Permalink
add: fix component like structures
Browse files Browse the repository at this point in the history
  • Loading branch information
alindesign committed May 24, 2020
1 parent 04b4a95 commit 3b6b553
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
34 changes: 33 additions & 1 deletion component.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
package react

import (
"reflect"
)

type ComponentStruct interface {
Render(ctx *Context) interface{}
}

func IsComponent(value interface{}) bool {
_, ok := value.(ComponentStruct)
ok := false

if value != nil {
rVal := reflect.TypeOf(value)
_, ok = rVal.MethodByName("Render")
}

return ok
}

func renderComponent(value interface{}, ctx interface{}) interface{} {
if !IsComponent(value) {
return nil
}

rVal := reflect.ValueOf(value)
method := rVal.MethodByName("Render")

if method.IsZero() || method.IsNil() {
return nil
}

var result []reflect.Value

if ctx != nil {
result = method.Call([]reflect.Value{reflect.ValueOf(ctx)})
} else {
result = method.Call([]reflect.Value{reflect.ValueOf(NewContext())})
}

return result[0].Interface()
}
59 changes: 59 additions & 0 deletions component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ func TestIsComponent(t *testing.T) {
})
}

type LayoutMountPoints struct {
Head *Element
Header *Element
Content *Element
}

type LayoutBaseComponent struct {
mounts *LayoutMountPoints
}

func (l *LayoutBaseComponent) Render(ctx *Context) interface{} {
if l.mounts == nil {
l.mounts = &LayoutMountPoints{}
}

return Document(&DocumentProps{
Head: Fragment(
MetaCharset(),
l.mounts.Head,
),
Body: Fragment(
CreateElement("header", nil, l.mounts.Header),
l.mounts.Content,
),
}, nil)
}

func LayoutBase(mounts *LayoutMountPoints) *LayoutBaseComponent {
return &LayoutBaseComponent{mounts: mounts}
}

func TestRenderClassLike(t *testing.T) {
t.Run("it should correctly render component class like", func(t *testing.T) {
content, err := Render(Div(nil,
Expand All @@ -34,4 +65,32 @@ func TestRenderClassLike(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "<div><span>span el</span><h1>Hello David! How are you?</h1></div>", content)
})

t.Run("it should correctly render advanced layout structure", func(t *testing.T) {
comp := LayoutBase(&LayoutMountPoints{
Content: Fragment(
H1(nil, "Hello World!"),
),
})

content, err := Render(Fragment(
comp,
))

assert.True(t, IsComponent(comp))
assert.NoError(t, err)
assert.Equal(t, `<!doctype html><html lang="en"><head><meta charset="UTF-8" /></head><body><header></header><h1>Hello World!</h1></body></html>`, content)
})
}

type TestStringComp struct{}

func (t TestStringComp) Render(ctx *Context) interface{} {
return "test string"
}

func TestRenderComponent(t *testing.T) {
r := renderComponent(&TestStringComp{}, nil)

assert.Equal(t, "test string", r)
}
8 changes: 8 additions & 0 deletions fragment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package react

func IsFragment(value interface{}) bool {
if value == nil {
return false
}
v, ok := value.(*Element)

if v == nil {
return false
}

return ok && v.Fragment
}

Expand Down
16 changes: 10 additions & 6 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func genHtml(tag string, attrs *Props, childs []string) string {
}

func render(component *Element, ctx *Context) (string, error) {
if component == nil {
return "", nil
}

if component.Tag == "" && !component.Fragment {
return "", errors.New("component has an empty tag")
}
Expand All @@ -73,12 +77,12 @@ func render(component *Element, ctx *Context) (string, error) {
var chunk string
var errChunk error

switch c.(type) {
case ComponentStruct:
result := c.(ComponentStruct).Render(ctx)
chunk, errChunk = renderElement(result, ctx)
default:
chunk, errChunk = renderElement(c, ctx)
if c != nil {
if IsComponent(c) {
chunk, errChunk = renderElement(renderComponent(c, ctx), ctx)
} else {
chunk, errChunk = renderElement(c, ctx)
}
}

if errChunk != nil {
Expand Down

0 comments on commit 3b6b553

Please sign in to comment.