Skip to content

Commit 7b4e52d

Browse files
Switch to Scala.js-defined React Class component
1 parent f29670b commit 7b4e52d

File tree

3 files changed

+86
-92
lines changed

3 files changed

+86
-92
lines changed

core/src/main/resources/io/github/shogowada/scalajs/reactjs/CreateClass.js

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.github.shogowada.scalajs.reactjs
2+
3+
import io.github.shogowada.scalajs.reactjs.classes.ReactClass
4+
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
5+
6+
import scala.scalajs.js
7+
8+
object NativeCreateClass {
9+
10+
def create(
11+
displayName: String,
12+
renderDef: js.Function1[js.Dynamic, ReactElement],
13+
getInitialState: js.Function1[js.Dynamic, js.Dynamic],
14+
componentDidMountDef: js.Function1[js.Dynamic, Unit],
15+
shouldComponentUpdateDef: js.Function3[js.Dynamic, js.Dynamic, js.Dynamic, Boolean],
16+
componentDidUpdateDef: js.Function3[js.Dynamic, js.Dynamic, js.Dynamic, Unit],
17+
componentWillUnmountDef: js.Function1[js.Dynamic, Unit],
18+
componentDidCatchDef: js.Function3[js.Dynamic, js.Object, js.Dynamic, Unit]
19+
): ReactClass = {
20+
21+
class ReactComponentImpl(props: js.Dynamic) extends ReactComponent(props) {
22+
23+
this.asInstanceOf[js.Dynamic].state = getInitialState(this.asInstanceOf[js.Dynamic])
24+
25+
def componentDidMount(): Unit = {
26+
componentDidMountDef(this.asInstanceOf[js.Dynamic])
27+
}
28+
29+
def shouldComponentUpdate(nextProps: js.Dynamic, nextState: js.Dynamic): Boolean = {
30+
shouldComponentUpdateDef(this.asInstanceOf[js.Dynamic], nextProps, nextState)
31+
}
32+
33+
def componentDidUpdate(prevProps: js.Dynamic, prevState: js.Dynamic): Unit = {
34+
componentDidUpdateDef(this.asInstanceOf[js.Dynamic], prevProps, prevState)
35+
}
36+
37+
def componentWillUnmount(): Unit = {
38+
componentWillUnmountDef(this.asInstanceOf[js.Dynamic])
39+
}
40+
41+
def render(): ReactElement = {
42+
renderDef(this.asInstanceOf[js.Dynamic])
43+
}
44+
}
45+
46+
val res =
47+
if (componentDidCatchDef != null) {
48+
49+
class ErrorBoundaryImpl(props: js.Dynamic) extends ReactComponentImpl(props) {
50+
51+
def componentDidCatch(error: js.Object, info: js.Dynamic): Unit = {
52+
componentDidCatchDef(this.asInstanceOf[js.Dynamic], error, info)
53+
}
54+
}
55+
56+
js.constructorOf[ErrorBoundaryImpl]
57+
}
58+
else js.constructorOf[ReactComponentImpl]
59+
60+
if (displayName != null) {
61+
res.displayName = displayName
62+
res.updateDynamic("toString")({ () =>
63+
displayName
64+
}: js.Function0[String])
65+
}
66+
67+
res.asInstanceOf[ReactClass]
68+
}
69+
}

core/src/main/scala/io/github/shogowada/scalajs/reactjs/React.scala

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,42 +76,42 @@ object React {
7676
): ReactClass = {
7777
NativeCreateClass.create(
7878
displayName = displayName,
79-
render = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
79+
renderDef = { (native: js.Dynamic) =>
8080
render(Self(native))
81-
}),
82-
getInitialState = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
81+
},
82+
getInitialState = { (native: js.Dynamic) =>
8383
if (getInitialState != null) {
8484
React.stateToNative(getInitialState(Self(native)))
8585
} else {
8686
React.stateToNative(())
8787
}
88-
}),
89-
componentDidMount = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
88+
},
89+
componentDidMountDef = { (native: js.Dynamic) =>
9090
if (componentDidMount != null) {
9191
componentDidMount(Self(native))
9292
}
93-
}),
94-
shouldComponentUpdate = js.ThisFunction.fromFunction3((native: js.Dynamic, nextProps: js.Dynamic, nextState: js.Dynamic) => {
93+
},
94+
shouldComponentUpdateDef = { (native: js.Dynamic, nextProps: js.Dynamic, nextState: js.Dynamic) =>
9595
if (shouldComponentUpdate != null) {
9696
shouldComponentUpdate(Self(native), Props(nextProps), React.stateFromNative(nextState))
9797
}
9898
else true
99-
}),
100-
componentDidUpdate = js.ThisFunction.fromFunction3((native: js.Dynamic, prevProps: js.Dynamic, prevState: js.Dynamic) => {
99+
},
100+
componentDidUpdateDef = { (native: js.Dynamic, prevProps: js.Dynamic, prevState: js.Dynamic) =>
101101
if (componentDidUpdate != null) {
102102
componentDidUpdate(Self(native), Props(prevProps), React.stateFromNative(prevState))
103103
}
104-
}),
105-
componentWillUnmount = js.ThisFunction.fromFunction1((native: js.Dynamic) => {
104+
},
105+
componentWillUnmountDef = { (native: js.Dynamic) =>
106106
if (componentWillUnmount != null) {
107107
componentWillUnmount(Self(native))
108108
}
109-
}),
110-
componentDidCatch = {
109+
},
110+
componentDidCatchDef = {
111111
if (componentDidCatch != null) {
112-
js.ThisFunction.fromFunction3((native: js.Dynamic, error: js.Object, info: js.Dynamic) => {
112+
(native: js.Dynamic, error: js.Object, info: js.Dynamic) => {
113113
componentDidCatch(Self(native), error, info)
114-
})
114+
}
115115
}
116116
else null
117117
}
@@ -139,17 +139,5 @@ object NativeReact extends js.Object {
139139
}
140140

141141
@js.native
142-
@JSImport("./io/github/shogowada/scalajs/reactjs/CreateClass.js", JSImport.Namespace)
143-
object NativeCreateClass extends js.Object {
144-
145-
def create(
146-
displayName: String,
147-
render: js.ThisFunction0[js.Dynamic, ReactElement],
148-
getInitialState: js.ThisFunction0[js.Dynamic, js.Dynamic],
149-
componentDidMount: js.ThisFunction0[js.Dynamic, Unit],
150-
shouldComponentUpdate: js.ThisFunction2[js.Dynamic, js.Dynamic, js.Dynamic, Boolean],
151-
componentDidUpdate: js.ThisFunction2[js.Dynamic, js.Dynamic, js.Dynamic, Unit],
152-
componentWillUnmount: js.ThisFunction0[js.Dynamic, Unit],
153-
componentDidCatch: js.ThisFunction2[js.Dynamic, js.Object, js.Dynamic, Unit]
154-
): ReactClass = js.native
155-
}
142+
@JSImport("react", "Component")
143+
class ReactComponent(props: js.Dynamic) extends js.Object

0 commit comments

Comments
 (0)