Skip to content

Commit 37aabf6

Browse files
Allow other props along with plain js props
1 parent 8a3cbf4 commit 37aabf6

File tree

2 files changed

+13
-13
lines changed
  • core/src/main/scala/io/github/shogowada/scalajs/reactjs
  • example/helloworld/src/main/scala/io/github/shogowada/scalajs/reactjs/example/helloworld

2 files changed

+13
-13
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,19 @@ trait VirtualDOM extends StaticTags {
131131
}
132132

133133
def toReactAttributes(attributes: Iterable[Attribute[_]]): js.Dictionary[js.Any] = {
134-
val maybeProps = attributes.find(_.name == "plainProps")
135-
maybeProps match {
136-
case Some(propsAttr) => propsAttr.value.asInstanceOf[js.Dictionary[js.Any]]
137-
case None =>
138-
attributes
139-
.map(attributeToReactAttribute)
140-
.toMap
141-
.toJSDictionary
134+
val result = js.Dictionary.empty[js.Any]
135+
attributes.foreach { attribute =>
136+
if (attribute.name == "plainProps") {
137+
js.Object.assign(result.asInstanceOf[js.Object], attribute.value.asInstanceOf[js.Object])
138+
}
139+
else {
140+
val name = toReactAttributeName(attribute.name)
141+
result(name) = attributeValueToReactAttributeValue(attribute)
142+
}
142143
}
143-
}
144144

145-
private def attributeToReactAttribute(attribute: Attribute[_]): (String, js.Any) =
146-
toReactAttributeName(attribute.name) -> attributeValueToReactAttributeValue(attribute)
145+
result
146+
}
147147

148148
private def attributeValueToReactAttributeValue(attribute: Attribute[_]): js.Any =
149149
attribute match {

example/helloworld/src/main/scala/io/github/shogowada/scalajs/reactjs/example/helloworld/Main.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ object Main {
4040
}
4141

4242
val reactClass = React.createClass[PlainJSProps, Unit] { self =>
43-
<.div(^.id := "hello-world")(s"Hello, ${self.props.plain.name}!")
43+
<.div(^.id := "hello-world")(s"${self.props.native.value}, ${self.props.plain.name}!")
4444
}
4545

4646
val mountNode = dom.document.getElementById("mount-node")
47-
ReactDOM.render(<(reactClass)(^.plain := PlainJSProps("World"))(), mountNode)
47+
ReactDOM.render(<(reactClass)(^.value := "Hello", ^.plain := PlainJSProps("World"))(), mountNode)
4848
}
4949

5050
renderUsingWrappedProps()

0 commit comments

Comments
 (0)