Skip to content

Commit

Permalink
Add renderIfTrue and renderIfFalse convenience functions
Browse files Browse the repository at this point in the history
Both provide a shortcut to render content if a given
boolean flow's value is `true` or `false` respectively.
  • Loading branch information
haukesomm committed Mar 15, 2024
1 parent 7669d2a commit d391979
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core/src/jsMain/kotlin/dev/fritz2/core/rendercontext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@ interface RenderContext : WithJob, WithScope {
}
}

/**
* Renders the data of a boolean [Flow] only if it's value is `true`.
*
* @receiver [Flow] containing the data
* @param into target to mount content to. If not set a child div is added to the [Tag] this method is called on
* @param content [RenderContext] for rendering the data to the DOM
*
* @see renderIf
* @see renderIfFalse
*/
fun Flow<Boolean>.renderIfTrue(
into: Tag<HTMLElement>? = null,
content: Tag<*>.() -> Unit
) {
renderIf(predicate = { it }, into) { _ ->
content()
}
}

/**
* Renders the data of a boolean [Flow] only if it's value is `false`.
*
* @receiver [Flow] containing the data
* @param into target to mount content to. If not set a child div is added to the [Tag] this method is called on
* @param content [RenderContext] for rendering the data to the DOM
*
* @see renderIf
* @see renderIfTrue
*/
fun Flow<Boolean>.renderIfFalse(
into: Tag<HTMLElement>? = null,
content: Tag<*>.() -> Unit
) {
renderIf(predicate = { !it }, into) { _ ->
content()
}
}

/**
* Renders the non-null data of a [Flow].
*
Expand Down
78 changes: 78 additions & 0 deletions core/src/jsTest/kotlin/dev/fritz2/core/rendercontext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,84 @@ class RenderContextTests {
assertNull(div(div2))
}

@Test
fun testRenderIfTrueFunction() = runTest {
val store = storeOf(true)

val id = Id.next()
val expectedContentIfTrue = "rendered"
val expectedContentIfFalse = ""


render {
div(id = id) {
store.data.renderIfTrue(into = this) {
+expectedContentIfTrue
}
}
}


delay(100)

val divContent = document.getElementById(id)?.textContent
assertEquals(
expected = expectedContentIfTrue,
actual = divContent,
message = "Content must be present"
)


store.update(false)
delay(100)

val divContentAfterUpdate = document.getElementById(id)?.textContent
assertEquals(
expected = expectedContentIfFalse,
actual = divContentAfterUpdate,
message = "Content must be absent"
)
}

@Test
fun testRenderIfFalseFunction() = runTest {
val store = storeOf(false)

val id = Id.next()
val expectedContentIfFalse = "rendered"
val expectedContentIfTrue = ""


render {
div(id = id) {
store.data.renderIfFalse(into = this) {
+expectedContentIfFalse
}
}
}


delay(100)

val divContent = document.getElementById(id)?.textContent
assertEquals(
expected = expectedContentIfFalse,
actual = divContent,
message = "Content must be present"
)


store.update(true)
delay(100)

val divContentAfterUpdate = document.getElementById(id)?.textContent
assertEquals(
expected = expectedContentIfTrue,
actual = divContentAfterUpdate,
message = "Content must be absent"
)
}

@Test
fun testRenderNotNullFunction() = runTest {
val store = storeOf<String?>(null)
Expand Down

0 comments on commit d391979

Please sign in to comment.