open Hyperapp

type msg = Increment | Decrement | Reset | Edit of string | Set

let initModel = 0, "0"

let view (value, edit) msg =
  let text = "Current value is: " ^ string_of_int value in

  h "div" [
    h_ "p" ~a:[%obj { _class = "main" }] text;
    h_ "button" ~a:[%obj { onclick = fun _ -> msg Increment }] "Increment";
    h_ "button" ~a:[%obj { onclick = fun _ -> msg Decrement }] "Decrement";
    h_ "button" ~a:[%obj { onclick = fun _ -> msg Reset }] "Reset";
    h "input" ~a:[%obj { value = edit; onchange = fun e -> msg (Edit (valueOfEvent e)) }] [];
    h_ "button" ~a:[%obj { onclick = fun _ -> msg Set }] "Set" ]

let update ((value, edit) as model) = function
  | Increment -> value + 1, edit
  | Decrement -> value - 1, edit
  | Reset -> initModel
  | Edit edit -> value, edit
  | Set -> try int_of_string edit, edit with  _ -> model

let () = app ~model:initModel ~view ~update "root"


open Hyperapp

type msg =
  Increment | Decrement | Reset | Edit of int | Set | Disable of bool

(** Current value, edited value, 'set' button disabled or not *)
let initModel = 0, 0, false

let view (value, edit, disabled) msg =
  let text = "Current value is: " ^ string_of_int value in
  let onchange e =
    let m = try Edit (int_of_string (valueOfEvent e))
      with _ -> Disable true in

    msg m in

  let button ?(disabled=false) title m =
    h_ "button" ~a:[%obj { disabled; onclick = fun _ -> msg m }] title in

  h "div" [
    h_ "p" ~a:[%obj { _class = "main" }] text;
    button "Increment" Increment;
    button "Decrement" Decrement;
    button "Reset" Reset;
    h "input" ~a:[%obj { value = edit; onchange }] [];
    button ~disabled "Set" Set ]

let update (value, edit, disabled) = function
  | Increment -> value + 1, edit, disabled
  | Decrement -> value - 1, edit, disabled
  | Reset -> initModel
  | Edit edit -> value, edit, false
  | Set -> edit, edit, disabled
  | Disable bool -> value, edit, bool

let () = app ~model:initModel ~view ~update "root"

Add edit and set feature to counter example
Add edit and set feature to counter example
