Skip to content

Commit

Permalink
- updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketlaunchr-cto committed Nov 17, 2018
1 parent e41fe5a commit 000288f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// 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{}{}
Expand Down Expand Up @@ -38,12 +39,14 @@ func JSFn(name string, args ...interface{}) *js.Object {
}

// 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 {
Expand All @@ -67,6 +70,7 @@ func ForwardRef(component interface{}) *js.Object {

// CreateContext is used when you want to pass data to a deeply
// embedded child component without using props.
//
// See: https://reactjs.org/docs/context.html#reactcreatecontext
func CreateContext(defaultValue ...interface{}) (Provider *js.Object, Consumer *js.Object) {

Expand All @@ -82,6 +86,7 @@ func CreateContext(defaultValue ...interface{}) (Provider *js.Object, Consumer *
}

// CloneElement is used to clone and return a new React Element.
//
// See: https://reactjs.org/docs/react-api.html#cloneelement
func CloneElement(element interface{}, props interface{}, children ...interface{}) *js.Object {

Expand Down
3 changes: 3 additions & 0 deletions react.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import (
var (
// React points to the React library. Change it
// if it is not in your global namespace.
//
// See: https://www.npmjs.com/package/react
React = js.Global.Get("React")
// ReactDOM points to the ReactDOM library. Change it
// if it is not in your global namespace.
//
// See: https://www.npmjs.com/package/react-dom
ReactDOM = js.Global.Get("ReactDOM")
// CreateReactClass points to create-react-class module.
//
// See: https://www.npmjs.com/package/create-react-class
CreateReactClass = js.Global
)
Expand Down
6 changes: 5 additions & 1 deletion react_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import (
type Map func(key string) *js.Object

// UpdaterFunc is the first argument for SetState function.
//
// See: https://reactjs.org/docs/react-component.html#setstate
type UpdaterFunc func(props, state Map) interface{}

// SetState is used to asynchronously update the state.
// If the new state is dependent on the current props or state,
// updater must be of type UpdaterFunc.
//
// See: https://reactjs.org/docs/react-component.html#setstate
type SetState func(updater interface{}, callback ...func())

// 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 {
Expand Down
15 changes: 15 additions & 0 deletions react_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,64 @@ import (
)

// SyntheticEvent represents a SyntheticEvent.
//
// See: https://reactjs.org/docs/events.html#overview
type SyntheticEvent struct {
// O represents the original React SyntheticEvent.
O *js.Object
}

// Bubbles
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) Bubbles() bool {
return s.O.Get("bubbles").Bool()
}

// Cancelable
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) Cancelable() bool {
return s.O.Get("cancelable").Bool()
}

// CurrentTarget
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) CurrentTarget() dom.HTMLElement {
return dom.WrapHTMLElement(s.O.Get("currentTarget"))
}

// DefaultPrevented
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) DefaultPrevented() bool {
return s.O.Get("defaultPrevented").Bool()
}

// EventPhase
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) EventPhase() int {
return s.O.Get("eventPhase").Int()
}

// IsTrusted
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) IsTrusted() bool {
return s.O.Get("isTrusted").Bool()
}

// NativeEvents
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) NativeEvent() dom.Event {
return dom.WrapEvent(s.O.Get("nativeEvent"))
}

// PreventDefault
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) PreventDefault() {
s.O.Call("preventDefault")
Expand All @@ -69,36 +78,42 @@ func (s *SyntheticEvent) IsDefaultPrevented() bool {
}

// StopPropagation
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) StopPropagation() {
s.O.Call("stopPropagation")
}

// IsPropagationStopped
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) IsPropagationStopped() bool {
return s.O.Call("isPropagationStopped").Bool()
}

// Target
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) Target() dom.HTMLElement {
return dom.WrapHTMLElement(s.O.Get("target"))
}

// TimeStamp
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) TimeStamp() float64 {
return s.O.Get("timeStamp").Float()
}

// Type
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) Type() string {
return s.O.Get("type").String()
}

// Persist is used if you want to access properties in an asynchronous way.
//
// See: https://reactjs.org/docs/events.html#event-pooling
func (s *SyntheticEvent) Persist() *SyntheticEvent {
p := s.O.Call("persist")
Expand Down
10 changes: 10 additions & 0 deletions react_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (def ClassDef) GetInitialState(f func(this *js.Object, props Map) interface
}

// GetDerivedStateFromProps sets the getDerivedStateFromProps class method.
//
// See: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
func (def ClassDef) GetDerivedStateFromProps(f func(nextProps, prevState Map) interface{}) {

Expand All @@ -61,6 +62,7 @@ func (def ClassDef) GetDerivedStateFromProps(f func(nextProps, prevState Map) in
}

// ComponentDidMount sets the componentDidMount method.
//
// See: https://reactjs.org/docs/react-component.html#componentdidmount
func (def ClassDef) ComponentDidMount(f func(this *js.Object, props, state Map, setState SetState)) {
def.SetMethod(componentDidMount, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
Expand All @@ -70,6 +72,7 @@ func (def ClassDef) ComponentDidMount(f func(this *js.Object, props, state Map,
}

// ComponentWillUnmount sets the componentWillUnmount method.
//
// See: https://reactjs.org/docs/react-component.html#componentwillunmount
func (def ClassDef) ComponentWillUnmount(f func(this *js.Object, props, state Map)) {
def.SetMethod(componentWillUnmount, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
Expand All @@ -79,6 +82,7 @@ func (def ClassDef) ComponentWillUnmount(f func(this *js.Object, props, state Ma
}

// ShouldComponentUpdate sets the shouldComponentUpdate method.
//
// See: https://reactjs.org/docs/react-component.html#shouldcomponentupdate
func (def ClassDef) ShouldComponentUpdate(f func(this *js.Object, props, nextProps, state, nextState Map) bool) {
def.SetMethod(shouldComponentUpdate, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
Expand All @@ -93,6 +97,7 @@ func (def ClassDef) ShouldComponentUpdate(f func(this *js.Object, props, nextPro
}

// GetSnapshotBeforeUpdate sets the getSnapshotBeforeUpdate method.
//
// See: https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate
func (def ClassDef) GetSnapshotBeforeUpdate(f func(this *js.Object, prevProps, props, prevState, state Map) interface{}) {
def.SetMethod(getSnapshotBeforeUpdate, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
Expand All @@ -115,6 +120,7 @@ func (def ClassDef) GetSnapshotBeforeUpdate(f func(this *js.Object, prevProps, p
}

// ComponentDidUpdate sets the componentDidUpdate method.
//
// See: https://reactjs.org/docs/react-component.html#componentdidupdate
func (def ClassDef) ComponentDidUpdate(f func(this *js.Object, prevProps, props, prevState, state Map, setState SetState, snapshot *js.Object)) {
def.SetMethod(componentDidUpdate, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
Expand All @@ -131,13 +137,17 @@ func (def ClassDef) ComponentDidUpdate(f func(this *js.Object, prevProps, props,
}

// Render sets the render method.
//
// See: https://reactjs.org/docs/react-component.html#render
func (def ClassDef) Render(f func(this *js.Object, props, state Map) interface{}) {
def.SetMethod(render, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
return f(this, props, state)
})
}

// ComponentDidCatch sets the componentDidCatch method.
//
// See: https://reactjs.org/docs/react-component.html#componentdidcatch
func (def ClassDef) ComponentDidCatch(f func(this *js.Object, err, info *js.Object, props, state Map, setState SetState)) {
def.SetMethod(componentDidCatch, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
err := arguments[0]
Expand Down
3 changes: 3 additions & 0 deletions utility_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

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

Expand All @@ -26,6 +27,7 @@ func (s Set) Convert(base string) map[string]string {

// DangerouslySetInnerHTMLFunc 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 DangerouslySetInnerHTMLFunc(inside func() interface{}) map[string]interface{} {
return map[string]interface{}{
Expand All @@ -37,6 +39,7 @@ func DangerouslySetInnerHTMLFunc(inside func() interface{}) map[string]interface

// DangerouslySetInnerHTML 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 DangerouslySetInnerHTML(inside interface{}) map[string]interface{} {
return DangerouslySetInnerHTMLFunc(func() interface{} { return inside })
Expand Down

0 comments on commit 000288f

Please sign in to comment.