Skip to content

Commit

Permalink
Use separated react -core and -dom facades
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Aug 26, 2021
1 parent c11989e commit 2f0a105
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 154 deletions.
61 changes: 0 additions & 61 deletions core/src/main/resources/scommons/react/raw/CreateClass.js

This file was deleted.

49 changes: 8 additions & 41 deletions core/src/main/scala/scommons/react/ClassComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,15 @@ trait ClassComponent[T] extends UiComponent[T] {
componentWillUnmount: Self[T, S] => Unit = null,
componentDidCatch: (Self[T, S], js.Object, js.Dynamic) => Unit = null
): ReactClass = {

raw.CreateClass.create(
React.createClass(
render = render,
displayName = displayName,
render = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
render(Self(native))
}),
getInitialState = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
if (getInitialState != null) {
React.stateToNative(getInitialState(Self(native)))
} else {
React.stateToNative(())
}
}),
componentDidMount = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
if (componentDidMount != null) {
componentDidMount(Self(native))
}
}),
shouldComponentUpdate = js.ThisFunction.fromFunction3((native: js.Dynamic, nextProps: js.Dynamic, nextState: js.Dynamic) => {
if (shouldComponentUpdate != null) {
shouldComponentUpdate(Self(native), Props(nextProps), React.stateFromNative(nextState))
}
else true
}),
componentDidUpdate = js.ThisFunction.fromFunction3((native: js.Dynamic, prevProps: js.Dynamic, prevState: js.Dynamic) => {
if (componentDidUpdate != null) {
componentDidUpdate(Self(native), Props(prevProps), React.stateFromNative(prevState))
}
}),
componentWillUnmount = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
if (componentWillUnmount != null) {
componentWillUnmount(Self(native))
}
}),
componentDidCatch = {
if (componentDidCatch != null) {
js.ThisFunction.fromFunction3((native: js.Dynamic, error: js.Object, info: js.Dynamic) => {
componentDidCatch(Self(native), error, info)
})
}
else null
}
componentDidMount = componentDidMount,
shouldComponentUpdate = shouldComponentUpdate,
componentDidUpdate = componentDidUpdate,
componentWillUnmount = componentWillUnmount,
componentDidCatch = componentDidCatch,
getInitialState = getInitialState
)
}
}
23 changes: 0 additions & 23 deletions core/src/main/scala/scommons/react/raw/CreateClass.scala

This file was deleted.

10 changes: 10 additions & 0 deletions dom/src/main/scala/scommons/react/dom/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package scommons.react

import io.github.shogowada.scalajs.reactjs.ReactDomVirtualDOM
import io.github.shogowada.scalajs.reactjs.VirtualDOM._

package object dom {

implicit class ReactDomVirtualDOMAttributes(attributes: VirtualDOMAttributes)
extends ReactDomVirtualDOM.EventVirtualDOMAttributes(attributes)
}
10 changes: 5 additions & 5 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"
//resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

val scalaJSVersion =
Option(System.getenv("SCALAJS_VERSION")).getOrElse("0.6.31")

if (scalaJSVersion.startsWith("0.6")) {
//addSbtPlugin(("org.scommons.sbt" % "sbt-scommons-plugin-sjs06" % "0.5.0-SNAPSHOT").changing())
addSbtPlugin("org.scommons.sbt" % "sbt-scommons-plugin-sjs06" % "0.5.0")
addSbtPlugin(("org.scommons.sbt" % "sbt-scommons-plugin-sjs06" % "0.6.0-SNAPSHOT").changing())
//addSbtPlugin("org.scommons.sbt" % "sbt-scommons-plugin-sjs06" % "0.5.0")
}
else {
//addSbtPlugin(("org.scommons.sbt" % "sbt-scommons-plugin" % "0.5.0-SNAPSHOT").changing())
addSbtPlugin("org.scommons.sbt" % "sbt-scommons-plugin" % "0.5.0")
addSbtPlugin(("org.scommons.sbt" % "sbt-scommons-plugin" % "0.6.0-SNAPSHOT").changing())
//addSbtPlugin("org.scommons.sbt" % "sbt-scommons-plugin" % "0.5.0")
}

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.5")
Expand Down
9 changes: 4 additions & 5 deletions project/src/main/scala/common/Libs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import scommons.sbtplugin.project.CommonLibs
object Libs extends CommonLibs {

val scommonsNodejsVersion = "0.5.0"
private val sjsReactJsVer = "0.15.0"
private val sjsReactJsVer = "0.16.0-SNAPSHOT"

lazy val sjsReactJs = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs" % sjsReactJsVer)
lazy val sjsReactJsRouterDom = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs-router-dom" % sjsReactJsVer)
lazy val sjsReactJsRouterRedux = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs-router-redux" % sjsReactJsVer)
lazy val sjsReactJsCore = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs-core" % sjsReactJsVer)
lazy val sjsReactJsDom = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs-dom" % sjsReactJsVer)
lazy val sjsReactJsRedux = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs-redux" % sjsReactJsVer)
lazy val sjsReactJsReduxDevTools = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs-redux-devtools" % sjsReactJsVer)
//lazy val sjsReactJsReduxDevTools = Def.setting("org.scommons.shogowada" %%% "scalajs-reactjs-redux-devtools" % sjsReactJsVer)
}
4 changes: 1 addition & 3 deletions project/src/main/scala/definitions/ReactCore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import sbt.Keys._
import sbt._
import scoverage.ScoverageKeys.coverageExcludedPackages

import scalajsbundler.sbtplugin.ScalaJSBundlerPlugin.autoImport._

object ReactCore extends ScalaJsModule {

override val id: String = "scommons-react-core"
Expand All @@ -22,7 +20,7 @@ object ReactCore extends ScalaJsModule {
override val internalDependencies: Seq[ClasspathDep[ProjectReference]] = Nil

override val runtimeDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Seq(
Libs.sjsReactJs.value
Libs.sjsReactJsCore.value
))

override val testDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Seq(
Expand Down
1 change: 1 addition & 0 deletions project/src/main/scala/definitions/ReactDom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object ReactDom extends ScalaJsModule {
)

override val runtimeDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Seq(
Libs.sjsReactJsDom.value,
Libs.scalajsDom.value
))

Expand Down
5 changes: 0 additions & 5 deletions project/src/main/scala/definitions/ReactTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ object ReactTest extends ScalaJsModule {
npmDependencies in Compile ++= Seq(
"react-test-renderer" -> "^16.8.0"
)

// npmResolutions in Test := Map(
// "react" -> "^16.8.0",
// "react-dom" -> "^16.8.0" //TODO: remove dependency on the react-dom !!!
// ),
)

override val internalDependencies: Seq[ClasspathDep[ProjectReference]] = Seq(
Expand Down
41 changes: 39 additions & 2 deletions project/src/main/scala/definitions/ScalaJsModule.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package definitions

import org.scalajs.jsenv.nodejs.NodeJSEnv
import org.scalajs.sbtplugin.ScalaJSPlugin
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
import sbt.Keys._
import sbt._
import scalajsbundler.sbtplugin.ScalaJSBundlerPlugin
import scommons.sbtplugin.project.CommonClientModule
import scalajsbundler.sbtplugin.ScalaJSBundlerPlugin.autoImport._
import scommons.sbtplugin.project.CommonModule.ideExcludedDirectories
import scoverage.ScoverageKeys.{coverageEnabled, coverageScalacPluginVersion}
import scoverage.ScoverageSbtPlugin._

Expand All @@ -14,7 +16,7 @@ trait ScalaJsModule extends ReactModule {
override def definition: Project = {
super.definition
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.settings(CommonClientModule.settings: _*)
.settings(ScalaJsModule.settings: _*)
.settings(
//TODO: remove these temporal fixes for Scala.js 1.1+ and scoverage
coverageScalacPluginVersion := {
Expand All @@ -39,3 +41,38 @@ trait ScalaJsModule extends ReactModule {
)
}
}

object ScalaJsModule {

val settings: Seq[Setting[_]] = Seq(
scalaJSLinkerConfig ~= {
_.withModuleKind(ModuleKind.CommonJSModule)
.withSourceMap(false)
.withESFeatures(_.withUseECMAScript2015(false))
},
//Opt-in @ScalaJSDefined by default
scalacOptions += {
if (scalaJSVersion.startsWith("0.6")) "-P:scalajs:sjsDefinedByDefault"
else ""
},
requireJsDomEnv in Test := false,
version in webpack := "4.29.0",
webpackEmitSourceMaps := false,

// required for node.js >= v12.12.0
// see:
// https://github.com/nodejs/node/pull/29919
scalaJSLinkerConfig in Test ~= {
_.withSourceMap(true)
},
jsEnv in Test := new NodeJSEnv(NodeJSEnv.Config().withArgs(List("--enable-source-maps"))),

ideExcludedDirectories ++= {
val base = baseDirectory.value
List(
base / "build",
base / "node_modules"
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scommons.react.showcase
import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent
import org.scalajs.dom.raw.HTMLInputElement
import scommons.react._
import scommons.react.dom._

object ReactRefDemo extends FunctionComponent[Unit] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scommons.react.showcase.app.counter
import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent
import io.github.shogowada.scalajs.reactjs.redux.Redux.Dispatch
import scommons.react._
import scommons.react.dom._

case class CounterPanelProps(dispatch: Dispatch,
actions: CounterActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scommons.react.showcase.hooks

import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent
import scommons.react._
import scommons.react.dom._
import scommons.react.hooks._

import scala.scalajs.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scommons.react.showcase.hooks

import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent
import scommons.react._
import scommons.react.dom._
import scommons.react.hooks._

object UseReducerDemo extends FunctionComponent[Unit] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scommons.react.showcase.hooks
import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent
import org.scalajs.dom.raw.HTMLInputElement
import scommons.react._
import scommons.react.dom._
import scommons.react.hooks._

object UseRefDemo extends FunctionComponent[Unit] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scommons.react.showcase.hooks

import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent
import scommons.react._
import scommons.react.dom._
import scommons.react.hooks._

object UseStateDemo extends FunctionComponent[Unit] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scommons.react.test.dom.raw

import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent
import scommons.react.dom._
import scommons.react.test.TestSpec
import scommons.react.test.dom.util.TestDOMUtils

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package scommons.react.test.raw

import org.scalajs.dom.raw.HTMLInputElement
import scommons.react._
import scommons.react.test.TestSpec
import scommons.react.test.raw.TestRendererSpec._
Expand All @@ -12,9 +11,9 @@ class TestRendererSpec extends TestSpec {
it should "mock reference" in {
//given
val comp = new ClassComponent[Unit] {
protected def create(): ReactClass = createClass[ReactRef[HTMLInputElement]](
protected def create(): ReactClass = createClass[ReactRef[js.Dynamic]](
getInitialState = { _ =>
ReactRef.create[HTMLInputElement]
ReactRef.create[js.Dynamic]
},
componentDidMount = { self =>
self.state.current.focus()
Expand Down Expand Up @@ -131,9 +130,9 @@ class TestRendererSpec extends TestSpec {

object TestRendererSpec {

def createHTMLInputElement(focusMock: () => Unit): HTMLInputElement = {
def createHTMLInputElement(focusMock: () => Unit): js.Object = {
js.Dynamic.literal(
"focus" -> (focusMock: js.Function)
).asInstanceOf[HTMLInputElement]
)
}
}
Loading

0 comments on commit 2f0a105

Please sign in to comment.