Skip to content

Commit

Permalink
- refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketlaunchr-cto committed Oct 16, 2018
1 parent bfda483 commit 5f4d436
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
11 changes: 0 additions & 11 deletions react.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ var (
CreateReactClass = js.Global
)

// ForceUpdate will force a rerender of the component.
// See: https://reactjs.org/docs/react-component.html#forceupdate
func ForceUpdate(this *js.Object, callback ...func()) {

if len(callback) > 0 && callback[0] != nil {
this.Call("forceUpdate", callback[0])
} else {
this.Call("forceUpdate")
}
}

// Render will render component to the specified target dom element.
func Render(element *js.Object, domTarget dom.Element, callback ...func()) *js.Object {
if len(callback) > 0 && callback[0] != nil {
Expand Down
11 changes: 11 additions & 0 deletions react_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ type SetState func(updater interface{}, callback ...func())

type ClassDef map[string]interface{}

// ForceUpdate will force a rerender of the component.
// See: https://reactjs.org/docs/react-component.html#forceupdate
func ForceUpdate(this *js.Object, callback ...func()) {

if len(callback) > 0 && callback[0] != nil {
this.Call("forceUpdate", callback[0])
} else {
this.Call("forceUpdate")
}
}

// NewClassDef will create an empty class definition which can immediately be used
// to create a React component.
func NewClassDef(displayName string) ClassDef {
Expand Down
23 changes: 13 additions & 10 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"github.com/mitchellh/mapstructure"
)

// SToMap will convert a struct or pass-through a map
// ToMap will return a map. If the argument is a struct,
// it will convert it to a map. If the argument is a map,
// it will pass it through. If the argument is nil, it will
// return nil.
// SToMap will convert a struct or pass-through a map.
// If the argument is a struct, it will convert it to a map.
// If the argument is a map, it will pass it through.
// If the argument is nil, it will return nil.
func SToMap(s interface{}) map[string]interface{} {

if s == nil {
Expand Down Expand Up @@ -41,7 +40,7 @@ func jsObjectIsNil(x interface{}) bool {
}

// jsObjectIsFunction returns true if x is a
// js object and is a js function
// js object and is a js function.
func jsObjectIsFunction(x interface{}) (ret bool) {

v, ok := x.(*js.Object)
Expand Down Expand Up @@ -106,10 +105,10 @@ func convertStruct(sIn interface{}) map[string]interface{} {
// Deal with dangerouslySetInnerHTML as a special case
if fieldName == "DangerouslySetInnerHTML" && strings.TrimSuffix(fieldTag, ",omitempty") == "dangerouslySetInnerHTML" {
if fn, ok := fieldVal.(func() interface{}); ok {
mp := EscapeHTMLFunc(fn)
mp := SetInnerHTMLFunc(fn)
out["dangerouslySetInnerHTML"] = mp["dangerouslySetInnerHTML"]
} else {
mp := EscapeHTML(fieldVal)
mp := SetInnerHTML(fieldVal)
out["dangerouslySetInnerHTML"] = mp["dangerouslySetInnerHTML"]
}
continue
Expand Down Expand Up @@ -137,7 +136,7 @@ func convertStruct(sIn interface{}) map[string]interface{} {
return out
}

// isStruct returns if s is a struct or not.
// isStruct returns true if s is a struct.
func isStruct(s interface{}) bool {
v := reflect.ValueOf(s)
if v.Kind() == reflect.Ptr {
Expand All @@ -154,7 +153,7 @@ func isStruct(s interface{}) bool {

// ConvertMap will hydrate a struct with values from a map.
// strct must be a pointer to a map. Use struct tag "react" for linking
// map keys to struct.
// map keys to the struct's fields.
func ConvertMap(mp interface{}, strct interface{}) error {

decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Expand All @@ -169,11 +168,15 @@ func ConvertMap(mp interface{}, strct interface{}) error {
return decoder.Decode(mp)
}

// HydrateProps will hydrate a given struct with values from
// the component's prop.
func HydrateProps(this *js.Object, strct interface{}) error {
props := this.Get("props").Interface()
return ConvertMap(props, strct)
}

// HydrateState will hydrate a given struct with values from
// the component's state.
func HydrateState(this *js.Object, strct interface{}) error {
state := this.Get("state").Interface()
return ConvertMap(state, strct)
Expand Down
21 changes: 12 additions & 9 deletions utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"strings"
)

// Set is used for conveniently dealign with
// Set is used for conveniently dealing with
// data-* and aria-* attributes.
// See: https://reactjs.org/docs/dom-elements.html
type Set map[string]string

// Convert converts a Set into a map containing the actual
// attribute names by prefixing the base.
// Convert is used to transform a set of suffix attributes
// to the actual attributes by prefixing them with a base.
func (s Set) Convert(base string) map[string]string {

out := map[string]string{}
Expand All @@ -22,19 +22,22 @@ func (s Set) Convert(base string) map[string]string {
return out
}

// EscapeHTMLFunc is used to create a prop that can escape and set
// the inner html of an element.
func EscapeHTMLFunc(inside func() interface{}) map[string]interface{} {
// SetInnerHTMLFunc is a convience function used for setting the DOM object's
// inner html. The functon takes a function for the argument.
// See: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
func SetInnerHTMLFunc(inside func() interface{}) map[string]interface{} {
return map[string]interface{}{
"dangerouslySetInnerHTML": map[string]interface{}{
"__html": inside(),
},
}
}

// EscapeHTML is a convience function.
func EscapeHTML(inside interface{}) map[string]interface{} {
return EscapeHTMLFunc(func() interface{} { return inside })
// SetInnerHTML is a convience function used for setting the DOM object's
// inner html. The function takes the inner html content directly.
// See: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
func SetInnerHTML(inside interface{}) map[string]interface{} {
return SetInnerHTMLFunc(func() interface{} { return inside })
}

// AddClass adds a new class to an existing list of classes.
Expand Down

0 comments on commit 5f4d436

Please sign in to comment.