Skip to content

Add an example for handling events manually #8

@davesmith00000

Description

@davesmith00000

For instance, if you want to just handle the raw JS event for whatever reason.

E.g.:

diff --git a/sandbox-zio/src/main/scala/example/SandboxZIO.scala b/sandbox-zio/src/main/scala/example/SandboxZIO.scala
index 772bdab..94f36b5 100644
--- a/sandbox-zio/src/main/scala/example/SandboxZIO.scala
+++ b/sandbox-zio/src/main/scala/example/SandboxZIO.scala
@@ -1,5 +1,6 @@
 package example

+import org.scalajs.dom.KeyboardEvent
 import tyrian.*
 import tyrian.Html.*
 import tyrian.cmds.Logger
@@ -69,6 +70,26 @@ object SandboxZIO extends TyrianZIOApp[Msg, Model]:
         a(href := "http://tyrian.indigoengine.io/")("Tyrian website")
       ),
       div(
+        input(
+          placeholder := "Hello",
+          onKeyPress((e: KeyboardEvent) =>Msg.CustomEventKeyIsDown(e.key))
+            .noPreventDefault.noStopImmediatePropagation.noStopPropagation,
+          onKeyUp((e: KeyboardEvent) => Msg.CustomEventKeyReleased(e.key)),
+          Event(
+            "keypress",
+            e =>
+              e match {
+                case e: org.scalajs.dom.KeyboardEvent =>
+                  println("e: " + e.key)
+                  Msg.NoOp
+
+                case _ =>
+                  // Shouldn't get here.
+                  println("e: " + e)
+                  Msg.NoOp
+              }
+          )
+        ),
         input(placeholder := "Text to reverse", onInput(s => Msg.NewContent(s)), myStyle),
         div(myStyle)(text(model.field.reverse))
       ),
@@ -95,6 +116,8 @@ enum Msg:
   case NewRandomInt(i: Int)
   case FollowLink(href: String)
   case NoOp
+  case CustomEventKeyIsDown(key: String)
+  case CustomEventKeyReleased(key: String)

 object Counter:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions