-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API: Use by-name arguments for
when
and whenNot
.
- Note: Scala 2 does not support by-name varargs, so in Scala 2 these methods are now limited to at most one argument - This required an alternative implementation vs #169.
- Loading branch information
Showing
6 changed files
with
160 additions
and
56 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/scala-2.13/com/raquo/laminar/api/LaminarPlatformSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.raquo.laminar.api | ||
|
||
trait LaminarPlatformSpecific { this: LaminarAliases => | ||
|
||
/** Returns a Modifier that applies another Modifier if `condition` is true | ||
* | ||
* Note: | ||
* - The inner Modifier is evaluated only if `condition` is `true`. | ||
* - Scala 3 version of `when` supports passing multiple modifiers. | ||
*/ | ||
def when[El <: Element](condition: Boolean)(mod: => Modifier[El]): Modifier[El] = { | ||
if (condition) { | ||
mod // implicitly converted to a single modifier | ||
} else { | ||
Modifier.empty | ||
} | ||
} | ||
|
||
/** Returns a Modifier that applies another Modifier if `condition` is true. | ||
* | ||
* Note: | ||
* - The inner Modifier is evaluated only if `condition` is `false`. | ||
* - Scala 3 version of `when` supports passing multiple modifiers. | ||
*/ | ||
@inline def whenNot[El <: Element](condition: Boolean)(mod: => Modifier[El]): Modifier[El] = when(!condition)(mod) | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/scala-3/com/raquo/laminar/api/LaminarPlatformSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.raquo.laminar.api | ||
|
||
trait LaminarPlatformSpecific { this: LaminarAliases => | ||
|
||
/** Returns a Modifier that applies one or more Modifiers if `condition` is `true`. | ||
* Note: The inner Modifier-s are evaluated only if the condition is `true`. | ||
*/ | ||
def when[El <: Element](condition: Boolean)(mods: => Modifier[El]*): Modifier[El] = { | ||
if (condition) { | ||
mods // implicitly converted to a single modifier | ||
} else { | ||
Modifier.empty | ||
} | ||
} | ||
|
||
/** Returns a Modifier that applies one or more modifiers if `condition` is `true`. | ||
* Note: The inner Modifier-s are evaluated only if the condition is `false`. | ||
*/ | ||
@inline def whenNot[El <: Element](condition: Boolean)(mods: => Modifier[El]*): Modifier[El] = when(!condition)(mods) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/scala-2.13/com/raquo/laminar/tests/basic/ModSpecScala2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.raquo.laminar.tests.basic | ||
|
||
import com.raquo.laminar.api.L._ | ||
|
||
import com.raquo.laminar.utils.UnitSpec | ||
|
||
class ModSpecScala2 extends UnitSpec { | ||
|
||
it("when keyword") { | ||
|
||
var evaluatedUsed = false | ||
var evaluatedUnused = false | ||
|
||
val el = div( | ||
when(true) { | ||
evaluatedUsed = true | ||
title("foo") | ||
}, | ||
when(false) { | ||
evaluatedUnused = true | ||
title("bar") | ||
}, | ||
when(true)( | ||
height.px(100) | ||
), | ||
when(true) { | ||
List(minAttr("10"), maxAttr("20")) | ||
}, | ||
when(true)(div("hello")), | ||
when(true) { | ||
onMountInsert(_ => | ||
"world" | ||
) | ||
}, | ||
when(true) { | ||
text <-- Val("text") | ||
} | ||
) | ||
|
||
mount(el) | ||
|
||
expectNode(div.of( | ||
title is "foo", | ||
height is "100px", | ||
minAttr is "10", | ||
maxAttr is "20", | ||
div.of("hello"), | ||
"world", | ||
"text" | ||
)) | ||
|
||
assertEquals(evaluatedUsed, true) | ||
assertEquals(evaluatedUnused, false) | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/scala-3/com/raquo/laminar/tests/basic/ModSpecScala3.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.raquo.laminar.tests.basic | ||
|
||
import com.raquo.laminar.api.L.{*, given} | ||
|
||
import com.raquo.laminar.utils.UnitSpec | ||
|
||
class ModSpecScala3 extends UnitSpec { | ||
|
||
it("when keyword") { | ||
|
||
var evaluatedUsed = false | ||
var evaluatedUnused = false | ||
|
||
val el = div( | ||
when(true) { | ||
evaluatedUsed = true | ||
title("foo") | ||
}, | ||
when(false) { | ||
evaluatedUnused = true | ||
title("bar") | ||
}, | ||
when(true)( | ||
height.px(100), | ||
width.px(200) | ||
), | ||
when(true) { | ||
List(minAttr("10"), maxAttr("20")) | ||
}, | ||
when(true)(div("hello")), | ||
when(true) { | ||
onMountInsert(_ => | ||
"world" | ||
) | ||
}, | ||
when(true) { | ||
text <-- Val("text") | ||
} | ||
) | ||
|
||
mount(el) | ||
|
||
expectNode(div.of( | ||
title is "foo", | ||
height is "100px", | ||
width is "200px", | ||
minAttr is "10", | ||
maxAttr is "20", | ||
div.of("hello"), | ||
"world", | ||
"text" | ||
)) | ||
|
||
assertEquals(evaluatedUsed, true) | ||
assertEquals(evaluatedUnused, false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters