Skip to content

Commit

Permalink
Fixed #180: onChange passes value to msg function
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Mar 21, 2023
1 parent 1f32776 commit 515feef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion project/AttributeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ object AttributeGen {
EventEmitting("onBlur"),
EventEmitting("onCanPlay"),
EventEmitting("onCanPlayThrough"),
EventEmitting("onChange", "change"),
// EventEmitting("onChange", "change"), // Provided manually as it doesn't fit the pattern
EventEmitting("onClick", "click"),
EventEmitting("onContextMenu"),
EventEmitting("onCopy"),
Expand Down
20 changes: 18 additions & 2 deletions sandbox/src/main/scala/example/Sandbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ object Sandbox extends TyrianApp[Msg, Model]:
(Model.init, cmds)

def update(model: Model): Msg => (Model, Cmd[IO, Msg]) =
case Msg.NewFlavour(f) =>
(model.copy(flavour = Option(f)), Cmd.None)

case Msg.MouseMove(to) =>
(model.copy(mousePosition = to), Cmd.None)

Expand Down Expand Up @@ -236,6 +239,16 @@ object Sandbox extends TyrianApp[Msg, Model]:
case Page.Page1 =>
div(onMouseMove(evt => Msg.MouseMove((evt.screenX.toInt, evt.screenY.toInt))))(
div(id := "mousepos")().innerHtml(s"<p><i>Mouse Coords ${model.mousePosition}</i></p>"),
label(
p("Choose an ice cream flavour:"),
select(cls := "ice-cream", name := "ice-cream", onChange(Msg.NewFlavour(_)))(
option(value := "")("Select One ..."),
option(value := "chocolate")("Chocolate"),
option(value := "sardine")("Sardine"),
option(value := "vanilla")("Vanilla")
)
),
p(model.flavour.map(f => s"You like $f").getOrElse("Pick a flavour...")),
input(
placeholder := "What should we save?",
value := model.tmpSaveData,
Expand Down Expand Up @@ -436,6 +449,7 @@ enum Msg:
case UpdateHttpDetails(newUrl: String)
case FrameTick(runningTime: Double)
case MouseMove(to: (Int, Int))
case NewFlavour(name: String)

enum Status:
case Connecting
Expand Down Expand Up @@ -480,7 +494,8 @@ final case class Model(
currentTime: js.Date,
http: HttpDetails,
time: Time,
mousePosition: (Int, Int)
mousePosition: (Int, Int),
flavour: Option[String]
)

final case class Time(running: Double, delta: Double):
Expand Down Expand Up @@ -541,7 +556,8 @@ object Model:
new js.Date(),
HttpDetails.initial,
Time(0.0d, 0.0d),
(0, 0)
(0, 0),
None
)

// We're only saving/loading the input field contents as an example
Expand Down
3 changes: 3 additions & 0 deletions tyrian/shared/src/main/scala/tyrian/Html.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ object Html extends HtmlTags with HtmlAttributes:
def onInput[M](msg: String => M): Attr[M] =
onEvent("input", (e: Tyrian.Event) => msg(e.target.asInstanceOf[Tyrian.HTMLInputElement].value))

def onChange[M](msg: String => M): Attr[M] =
onEvent("change", (e: Tyrian.Event) => msg(e.target.asInstanceOf[Tyrian.HTMLInputElement].value))

def style(name: String, value: String): Attr[Nothing] = Attribute("style", Style(name, value).toString)
@targetName("style_Style")
def style(style: Style): Attr[Nothing] = Attribute("style", style.toString)
Expand Down

0 comments on commit 515feef

Please sign in to comment.