-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcounters.ml
36 lines (23 loc) · 1.32 KB
/
counters.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(* This file is part of the ocaml-vdom package, released under the terms of an MIT-like license. *)
(* See the attached LICENSE file. *)
(* Copyright (C) 2000-2024 LexiFi *)
(* Inspired from https://github.com/janestreet/incr_dom/blob/master/example/incr_decr/counters.ml *)
open Vdom
module IntMap = Map.Make(struct type t = int let compare : int -> int -> int = compare end)
type model = {
counters : int IntMap.t;
}
let update { counters } = function
| `New_counter -> { counters = IntMap.add (IntMap.cardinal counters) 0 counters }
| `Update (pos, diff) -> { counters = IntMap.add pos (IntMap.find pos counters + diff) counters }
let init = { counters = IntMap.empty }
let button txt msg = input [] ~a:[onclick (fun _ -> msg); type_button; value txt]
let view { counters } =
let row (pos, value) =
div [button "-" (`Update (pos, -1)); text (string_of_int value); button "+" (`Update (pos, 1))]
in
div (div [button "New counter" `New_counter] :: (IntMap.bindings counters |> List.map row))
let app = simple_app ~init ~view ~update ()
open Js_browser
let run () = Vdom_blit.run app |> Vdom_blit.dom |> Element.append_child (Document.body document)
let () = Window.set_onload window run