Skip to content

Instantly share code, notes, and snippets.

@virtualirfan
Forked from bfleischhacker/LcHoc.scala
Created March 17, 2018 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtualirfan/d93ac5569c7f4e6571ba7373e46ab6fe to your computer and use it in GitHub Desktop.
Save virtualirfan/d93ac5569c7f4e6571ba7373e46ab6fe to your computer and use it in GitHub Desktop.
Using a JS Higher-Order-Component (https://github.com/viktorbergehall/lcHOC) with scala-react-interface (sri)
import sri.core.{ComponentConstructor, ReactClass}
import scala.scalajs.js
import scala.scalajs.js.ConstructorTag
object LcHoc {
def apply[T <: ReactClass {type PropsType; type StateType}](params: js.UndefOr[LcHocParams] = js.undefined)
(implicit tag: ConstructorTag[T]): ComponentConstructor {
type ComponentType = T
type PropsType = T#PropsType
type StateType = T#StateType
} = LcHocJs(tag.constructor, params).asInstanceOf[
ComponentConstructor {
type ComponentType = T
type PropsType = T#PropsType
type StateType = T#StateType
}]
}
import sri.core.ReactClass
import scala.scalajs.js
import scala.scalajs.js.UndefOr
import scala.scalajs.js.annotation.{JSImport, ScalaJSDefined}
@ScalaJSDefined
trait LcHocParams extends js.Object {
val log: js.UndefOr[Log] = js.undefined
val flash: js.UndefOr[Boolean] = js.undefined
}
object LcHocParams {
@ScalaJSDefined
trait Log extends js.Object {
val use: js.UndefOr[Boolean] = js.undefined
val expanded: js.UndefOr[Boolean] = js.undefined
val renderCount: js.UndefOr[Boolean] = js.undefined
val state: js.UndefOr[Boolean] = js.undefined
val props: js.UndefOr[Boolean] = js.undefined
val timings: js.UndefOr[Boolean] = js.undefined
}
}
@js.native
@JSImport("lc-hoc", JSImport.Default)
object LcHocJs extends js.Function2[js.Dynamic, js.UndefOr[LcHocParams], ReactClass] {
override def apply(constructor: js.Dynamic, params: UndefOr[LcHocParams]): ReactClass = js.native
}
import sri.core.{ComponentP, CreateElement}
object Usage {
class MyComponent extends ComponentP[MyComponent.Props]
object MyComponent {
case class Props()
}
CreateElementJS[MySriComponent](LcHoc[MyComponent](), props= MyComponent.Props())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment