Add dynamic attributes

This commit is contained in:
Paul-Henri Froidmont 2025-08-15 01:04:01 +02:00
parent 82c0922cfa
commit 62c1a8a9f4
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
4 changed files with 53 additions and 3 deletions

View file

@ -10,6 +10,7 @@ object LiveViewSpec extends TestSuite:
title: String = "title value",
bool: Boolean = false,
nestedTitle: String = "nested title value",
cls: String = "text-sm",
items: List[NestedModel] = List.empty
)
final case class NestedModel(name: String, age: Int)
@ -79,6 +80,43 @@ object LiveViewSpec extends TestSuite:
}
}
test("Dynamic attribute") {
val lv =
LiveView(
new View[TestModel]:
val root: HtmlElement[TestModel] =
div(cls := model(_.cls))
,
TestModel()
)
test("init") {
assertEqualsJson(
lv.fullDiff,
Json
.Obj(
"s" -> Json
.Arr(Json.Str("<div class=\""), Json.Str("\"></div>")),
"0" -> Json.Str("text-sm")
)
)
}
test("diff no update") {
assertEqualsJson(lv.diff, emptyDiff)
}
test("diff with update") {
lv.update(TestModel(cls = "text-md"))
assertEqualsJson(
lv.diff,
Json.Obj("0" -> Json.Str("text-md"))
)
}
test("diff with update and no change") {
lv.update(TestModel(cls = "text-md"))
lv.update(TestModel(cls = "text-md"))
assertEqualsJson(lv.diff, emptyDiff)
}
}
test("when mod") {
val lv =
LiveView(