-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.elm
58 lines (49 loc) · 1.43 KB
/
counter.elm
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Counter (Model, Action, update, view, init, viewWithRemoveButton, Context) where
import Html exposing (div, text, button, Html, Attribute)
import Html.Events exposing(onClick)
import Html.Attributes exposing(style)
-- Model
type alias Model = Int
init : Int -> Model
init count = count
type alias Context = {
actions : Signal.Address Action
, remove : Signal.Address ()
}
-- View
view : Signal.Address Action -> Model -> Html
view address model =
div [] [
button[onClick address Increment][text "+"]
,div [countStyle] [text (toString model)]
,button [onClick address Decrement ][text "-"]
]
type Action = Increment | Decrement
-- Update
update : Action -> Model -> Model
update action model =
case action of
Increment ->
model + 1
Decrement ->
model - 1
-- View with remove button
viewWithRemoveButton : Context -> Model -> Html
viewWithRemoveButton context model =
div [][
button [onClick context.actions Increment][text "+"]
, div [countStyle][text (toString model)]
, button [onClick context.actions Decrement] [text "-"]
, div [countStyle] []
, button [onClick context.remove ()] [text "X"]
]
-- Style
countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
,("font-family", "monospace")
,("display", "inline-block")
,("width", "50px")
,("text-align", "center")
]