forked from bryanjenningz/25-elm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-counter.elm
61 lines (48 loc) · 1.42 KB
/
08-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
59
60
61
module Main exposing (..)
import Html exposing (Html, text, div, beginnerProgram, button)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
-- We've added another new Msg value that we're going to call Reset.
type Msg
= Increment
| Decrement
| Reset
type alias Model =
Int
view : Model -> Html Msg
view model =
div [ class "text-center" ]
[ div [] [ text (toString model) ]
, div [ class "btn-group" ]
[ button
[ class "btn btn-primary", onClick Increment ]
[ text "+" ]
, button
[ class "btn btn-danger", onClick Decrement ]
[ text "-" ]
-- We added a new button that will trigger an event
-- that will pass the Reset value as a message to the
-- update function.
, button
[ class "btn btn-default", onClick Reset ]
[ text "Reset" ]
]
]
-- We added a new entry in the case expression that checks for if the message
-- is Reset. If it is, then the new model value will be 0.
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
Reset ->
0
main : Program Never Model Msg
main =
beginnerProgram
{ model = 0
, view = view
, update = update
}