Skip to content

Commit

Permalink
airframe-rx: #958 Support embedding Iterable[X <: RxElement] inside D…
Browse files Browse the repository at this point in the history
…OM (#974)

* airframe-rx: #958 Support embedding Iterable[X <: RxElement] inside DOM

* Use Iterable for DOMRenderer
  • Loading branch information
xerial committed Mar 12, 2020
1 parent 346b24f commit f224234
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
Expand Up @@ -137,7 +137,7 @@ object DOMRenderer extends LogSupport {
Cancelable.empty
case Some(x) =>
traverse(x, anchor)
case s: Seq[_] =>
case s: Iterable[_] =>
val cancelables = for (el <- s) yield {
traverse(el, anchor)
}
Expand Down
Expand Up @@ -94,5 +94,4 @@ class HtmlTest extends AirSpec {
val d = button("hello", onclick { () => println("clicked") })
render(d)
}

}
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.rx.html
import wvlet.airframe.rx.Rx
import wvlet.airframe.rx.html.all._
import wvlet.airspec.AirSpec

/**
*
*/
object NestedElementTest extends AirSpec {

class MyElement(name: String) extends RxElement {
override def render: RxElement = div(
cls -> "button",
name
)
}

test("compile nested elements") {
val lst = (1 to 3)
div(
id -> "nested",
// Seq[RxElement]
lst.toSeq.map { i => new MyElement(s"button ${i}") },
// Iterable[RxElement]
lst.toIterable.map { i => new MyElement(s"button ${i}") }
)
}

test("embed nested Rx[Seq[X]]") {
val rx = Rx.variable(Seq(1, 2, 3))
div(
rx.map { i => p(i) }
)
}
}
Expand Up @@ -44,7 +44,7 @@ object Cancelable {
}
}

def merge(lst: Seq[Cancelable]): Cancelable = {
def merge(lst: Iterable[Cancelable]): Cancelable = {
lst.size match {
case 1 => lst.head
case _ =>
Expand Down
Expand Up @@ -108,7 +108,7 @@ package object html {
@inline implicit def embedString: EN[String] = null
@inline implicit def embedHtmlNode[A <: HtmlNode]: EN[A] = null
@inline implicit def embedRx[C[x] <: Rx[x], A: EN]: EN[C[A]] = null
@inline implicit def embedSeq[C[x] <: Seq[x], A: EN]: EN[C[A]] = null
@inline implicit def embedSeq[C[x] <: Iterable[x], A: EN]: EN[C[A]] = null
@inline implicit def embedOption[C[x] <: Option[x], A: EN]: EN[C[A]] = null
}

Expand Down

0 comments on commit f224234

Please sign in to comment.