2025-08-17 22:52:22 +02:00
|
|
|
import scalive.*
|
|
|
|
|
|
|
|
|
|
final case class MyModel(cls: String = "text-xs", bool: Boolean = true)
|
|
|
|
|
final case class Elem(name: String, age: Int)
|
|
|
|
|
|
|
|
|
|
class TestView extends LiveView[TestView.Cmd]:
|
|
|
|
|
import TestView.Cmd.*
|
|
|
|
|
|
2025-08-18 04:52:07 +02:00
|
|
|
private val textCls = Dyn[String]
|
|
|
|
|
private val someBool = Dyn[Boolean]
|
|
|
|
|
private val elems = Dyn[List[Elem]]
|
2025-08-17 22:52:22 +02:00
|
|
|
|
|
|
|
|
def mount(state: LiveState): LiveState =
|
|
|
|
|
state
|
|
|
|
|
.set(textCls, "text-xs")
|
|
|
|
|
.set(someBool, true)
|
|
|
|
|
.set(
|
|
|
|
|
elems,
|
|
|
|
|
List(
|
|
|
|
|
Elem("a", 10),
|
|
|
|
|
Elem("b", 15),
|
|
|
|
|
Elem("c", 20)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def handleCommand(cmd: TestView.Cmd, state: LiveState): LiveState = cmd match
|
|
|
|
|
case UpdateElems(es) => state.set(elems, es)
|
|
|
|
|
case UpdateBool(b) => state.set(someBool, b)
|
|
|
|
|
case UpdateTextCls(cls) => state.set(textCls, cls)
|
|
|
|
|
|
|
|
|
|
val render =
|
|
|
|
|
div(
|
|
|
|
|
idAttr := "42",
|
|
|
|
|
cls := textCls,
|
|
|
|
|
draggable := someBool,
|
|
|
|
|
disabled := someBool,
|
|
|
|
|
ul(
|
|
|
|
|
elems.splitByIndex(elem =>
|
|
|
|
|
li(
|
|
|
|
|
"Nom: ",
|
|
|
|
|
elem(_.name),
|
|
|
|
|
" Age: ",
|
|
|
|
|
elem(_.age.toString)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
end TestView
|
|
|
|
|
|
|
|
|
|
object TestView:
|
|
|
|
|
enum Cmd:
|
|
|
|
|
case UpdateElems(es: List[Elem])
|
|
|
|
|
case UpdateBool(b: Boolean)
|
|
|
|
|
case UpdateTextCls(cls: String)
|