diff --git a/component.go b/component.go index 0dd3355..59eda50 100644 --- a/component.go +++ b/component.go @@ -57,7 +57,7 @@ type Component interface { // A Factory is a Component that can construct Elements (analogous to a ReactClass or a ReactFactory). type Factory interface { Component - CreateElement(props Props) *Element + CreateElement(props Props, children ...*Element) *Element } // ReactComponent wraps a Facebook React component. @@ -107,8 +107,8 @@ func FromGlobal(path ...string) *ReactComponent { // Require loads a module the Node.js way. // Note that this requires that the require function is present; if in the browser, // and not in Node.js, try Browserify. -func Require(path string) *ReactComponent { - m, err := support.Require(path) +func Require(path ...string) *ReactComponent { + m, err := support.Require(path...) if err != nil { panic(err) } diff --git a/support/support.go b/support/support.go index 35702d8..8834184 100644 --- a/support/support.go +++ b/support/support.go @@ -26,18 +26,26 @@ import ( // Require loads a JS object the Node.js way. // Note that this requires that the require function is present; if in the browser, // and not in Node.js, try Browserify. -func Require(path string) (*js.Object, error) { +func Require(path ...string) (*js.Object, error) { require := js.Global.Get("require") if require == js.Undefined { - return nil, errors.New("require() not defined; if this is not Node.js, give Browserify a try.") + return nil, errors.New("require() not defined; if this is not Node.js, give Browserify a try") } - m := require.Invoke(path) + var component *js.Object - if m == js.Undefined { + for _, p := range path { + if component != nil { + component = component.Get(p) + } else { + component = require.Invoke(p) + } + } + + if component == js.Undefined { return nil, fmt.Errorf("Module %q not found", path) } - return m, nil + return component, nil } diff --git a/tests/component_test.go b/tests/component_test.go index 5a2ae33..0109eae 100644 --- a/tests/component_test.go +++ b/tests/component_test.go @@ -115,7 +115,7 @@ func TestCompositeComponents(t *testing.T) { elem := rc.CreateElement(gr.Props{"text": "Composite Test"}) r := grt.ShallowRender(elem) - grt.Equal(t, `

Composite Test

`, + grt.Equal(t, `

Composite Test

`, r.String()) // dive into the first subcomponent