-
Notifications
You must be signed in to change notification settings - Fork 11
/
helpers.go
64 lines (55 loc) · 1.67 KB
/
helpers.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
61
62
63
64
package react
import (
"github.com/gopherjs/gopherjs/js"
)
// Fragment is used to group a list of children
// without adding extra nodes to the DOM.
// See: https://reactjs.org/docs/fragments.html
func Fragment(key *string, children ...interface{}) *js.Object {
props := map[string]interface{}{}
if key != nil {
props["key"] = *key
}
return JSX(React.Get("Fragment"), props, children...)
}
// JSX is used to create an Element.
func JSX(component interface{}, props interface{}, children ...interface{}) *js.Object {
args := []interface{}{
component,
SToMap(props),
}
if len(children) > 0 {
args = append(args, children...)
}
return React.Call("createElement", args...)
}
// JSFn is a convenience function used to call javascript functions that are
// part of the standard library.
func JSFn(name string, args ...interface{}) *js.Object {
return js.Global.Call(name, args...)
}
// CreateRef will create a Ref.
// See: https://reactjs.org/docs/refs-and-the-dom.html
func CreateRef() *js.Object {
return React.Call("createRef")
}
// ForwardRef will forward a Ref to child components.
// See: https://reactjs.org/docs/forwarding-refs.html
func ForwardRef(component interface{}) *js.Object {
return React.Call("forwardRef", func(props *js.Object, ref *js.Object) *js.Object {
props.Set("ref", ref)
n := React.Get("Children").Call("count", props.Get("children")).Int()
switch n {
case 0:
return JSX(component, props)
case 1:
return JSX(component, props, props.Get("children"))
default:
children := []interface{}{}
for i := 0; i < n; i++ {
children = append(children, props.Get("children").Index(i))
}
return JSX(component, props, children...)
}
})
}