Support click events

This commit is contained in:
Paul-Henri Froidmont 2025-08-27 02:28:22 +02:00
parent 1bd65fd49c
commit 124239925d
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
16 changed files with 277 additions and 162 deletions

View file

@ -1,9 +1,12 @@
import ExampleLiveView.Evt
import monocle.syntax.all.*
import scalive.*
import zio.json.*
final case class ExampleModel(elems: List[NestedModel], cls: String = "text-xs")
final case class NestedModel(name: String, age: Int)
class ExampleLiveView(someParam: String) extends LiveView[Nothing]:
class ExampleLiveView(someParam: String) extends LiveView[Evt, String]:
val model = Var(
ExampleModel(
@ -15,11 +18,15 @@ class ExampleLiveView(someParam: String) extends LiveView[Nothing]:
)
)
def handleCommand(cmd: Nothing): Unit = ()
override def handleClientEvent(evt: Evt): Unit =
evt match
case Evt.IncAge(value) =>
model.update(_.focus(_.elems.index(2).age).modify(_ + value))
val el =
div(
h1(someParam),
h2(model(_.cls)),
idAttr := "42",
cls := model(_.cls),
ul(
@ -31,6 +38,14 @@ class ExampleLiveView(someParam: String) extends LiveView[Nothing]:
elem(_.age.toString)
)
)
),
button(
phx.click := Evt.IncAge(1),
"Inc age"
)
)
end ExampleLiveView
object ExampleLiveView:
enum Evt derives JsonCodec:
case IncAge(value: Int)