Skip to content

Commit

Permalink
Add connect for ComponentBase
Browse files Browse the repository at this point in the history
Don't have the time to properly rewrite to NativeComponentType at the moment
  • Loading branch information
nigredo-tori committed Mar 29, 2017
1 parent dd1e227 commit db12c7e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
26 changes: 26 additions & 0 deletions eldis/src/main/scala/react/eldis/package.scala
Expand Up @@ -22,6 +22,8 @@ package object eldis {
}
}

// TODO: Rewrite all of this to use new scalajs-react idioms

@inline
def connect[C, P, OP](connector: C, comp: FunctionalComponent[P])(
implicit
Expand Down Expand Up @@ -67,6 +69,30 @@ package object eldis {
C(connector)
)(comp)

@inline
def connect[C, P, OP](connector: C, comp: js.ConstructorTag[_ <: ComponentBase[Wrapped, P]])(
implicit
C: base.ConnectorLike[C, P, OP]
): NativeComponentType.WithChildren[Wrapped[OP]] = {
type F[A] = NativeComponentType.WithChildren[Wrapped[A]]
base.connect[C.State, C.Action, Any, P, OP, F, Wrapped, Wrapped[P], Wrapped[OP]](
C(connector)
)(comp)
}

@inline
def connect[C, P <: js.Any, OP <: js.Any](connector: C, comp: js.ConstructorTag[_ <: ComponentBase[Identity, P]])(
implicit
C: base.ConnectorLike[C, P, OP],
// To break the tie with connect for wrapped ComponentBase
unused: Int =:= Int
): NativeComponentType.WithChildren[OP] = {
val c0: NativeComponentType.WithChildren[Identity[P]] = comp
base.connect[C.State, C.Action, js.Any, P, OP, NativeComponentType.WithChildren, Identity, Identity[P], Identity[OP]](
C(connector)
)(c0)
}

@js.native
private trait ProviderProps extends js.Object {
val store: js.Any = js.native
Expand Down
56 changes: 56 additions & 0 deletions examples/eldis-components/src/main/scala/Components.scala
Expand Up @@ -4,6 +4,7 @@ import scala.scalajs.js
import js.annotation._
import js.JSConverters._
import eldis.react._
import eldis.react.util.ElementBuilder
import vdom.prefix_<^._

import eldis.redux
Expand Down Expand Up @@ -74,6 +75,61 @@ object NativeFunctionalWithChildren {
def apply(children: ReactNode*) = connected((), js.Array(children: _*))
}

object BaseIdentity {

@ScalaJSDefined
class ComponentImpl extends ComponentBase[eldis.react.Identity, JSProps] {

type State = Unit

def initialState = ()

def render(): ReactNode =
<.div()((
<.div()(props.jsValue) +:
propsChildren
): _*)
}

val component = js.constructorTag[ComponentImpl]

val connected = connect(
(_: Dispatcher[Action]) => (state: State, ownProps: js.Any) =>
state.baseIdentity,
component
)

def apply(children: ReactNode*): ReactNode =
ElementBuilder(connected, js.undefined: js.Any, children)
}

object BaseWrapped {

@ScalaJSDefined
class ComponentImpl extends Component[ScalaProps]("BaseWrapped.ComponentImpl") {

type State = Unit

def initialState = ()

def render(): ReactNode =
<.div()((
<.div()(props.value) +:
propsChildren
): _*)
}

val component = js.constructorTag[ComponentImpl]

val connected = connect(
(_: Dispatcher[Action]) => (state: State, ownProps: Unit) =>
state.baseWrapped,
component
)

def apply(children: ReactNode*) = ElementBuilder(connected, (), children)
}

object JS {

@JSImport("JsComponent", JSImport.Namespace)
Expand Down
16 changes: 16 additions & 0 deletions examples/eldis-components/src/main/scala/Main.scala
Expand Up @@ -31,6 +31,20 @@ object Main extends JSApp {
)
)
),
<.li()(
BaseIdentity(
<.p()(" Child 1 OK"),
<.p()(" Child 2 OK")
)
),
<.li()(
BaseWrapped(
js.Array[ReactNode](
<.p()(" Child 1 OK"),
<.p()(" Child 2 OK")
)
)
),
<.li()(JS(
<.p()(" Child 1 OK"),
<.p()(" Child 2 OK")
Expand All @@ -47,6 +61,8 @@ object Main extends JSApp {
ScalaProps("Functional with children OK"),
JSProps("Native functional OK"),
JSProps("Native functional with children OK"),
JSProps("Base identity OK"),
ScalaProps("Base wrapped OK"),
JSProps("JS OK")
))),
dom.document.getElementById("root")
Expand Down
2 changes: 2 additions & 0 deletions examples/eldis-components/src/main/scala/State.scala
Expand Up @@ -9,6 +9,8 @@ case class State(
functionalWithChildren: ScalaProps,
nativeFunctional: JSProps,
nativeFunctionalWithChildren: JSProps,
baseIdentity: JSProps,
baseWrapped: ScalaProps,
js: JSProps
)

Expand Down

0 comments on commit db12c7e

Please sign in to comment.