Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tea.Html.map implementation is broken #59

Open
abradley2 opened this issue Jul 21, 2023 · 1 comment
Open

Tea.Html.map implementation is broken #59

abradley2 opened this issue Jul 21, 2023 · 1 comment

Comments

@abradley2
Copy link

If a vdom node switches in place with another node, map does not work as expected

I've managed to replicate this in a small example.

open Tea.App
open Tea.Html

type countUpMsg = Increment
type countDownMsg = Decrement

type page =
  | CountUp
  | CountDown

type msg =
  | SetPage(page)
  | CountUpMsg(countUpMsg)
  | CountDownMsg(countDownMsg)

type model = (int, page)

let init = () => (CountDown, 0)

let update = ((page, count), msg) => {
  switch msg {
  | SetPage(newPage) => (newPage, count)
  | CountUpMsg(subMsg) =>
    switch subMsg {
    | Increment => (page, count + 1)
    }
  | CountDownMsg(subMsg) =>
    switch subMsg {
    | Decrement => (page, count - 1)
    }
  }
}

let viewButton = (~title, ~disabled=false, ~msg, ()) =>
  button(list{Events.onClick(msg), Attributes.disabled(disabled)}, list{text(title)})

let view = ((page, count)) => div(list{},
  list{
    h3(list{}, list{text("Count: " ++ string_of_int(count))}),
    viewButton(~title="Count Up", ~msg=SetPage(CountUp), ~disabled=page == CountUp, ()),
    viewButton(~title="Count Down", ~msg=SetPage(CountDown), ~disabled=page == CountDown, ()),
    switch page {
    | CountUp => Tea.Html.map(msg => CountUpMsg(msg), viewButton(~title="Increment", ~msg=Increment, ()))
    | CountDown => Tea.Html.map(msg => CountDownMsg(msg), viewButton(~title="Decrement", ~msg=Decrement, ()))
    },
  }
)

let main = beginnerProgram({ model: init(), update, view })
@pbiggar
Copy link
Member

pbiggar commented Jul 22, 2023

I looked at the code and this seems complicated. If you've any idea how to solve it, we'd certainly take the patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants