mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 13:36:59 +01:00
Support click events
This commit is contained in:
parent
1bd65fd49c
commit
124239925d
16 changed files with 277 additions and 162 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue