-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path07-counter.elm
69 lines (50 loc) · 1.82 KB
/
07-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
62
63
64
65
66
67
68
69
module Main exposing (main)
import Browser exposing (sandbox)
import Html exposing (Html, button, div, text)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
-- We've added a new value called Decrement that is type Msg.
-- Think of the Msg type as a type that can either be Increment
-- or it can be Decrement. We use "|" between all the possible values a Msg type can be.
type Msg
= Increment
| Decrement
type alias Model =
Int
view : Model -> Html Msg
view model =
div [ class "text-center" ]
[ div [] [ text (String.fromInt model) ]
, div [ class "btn-group" ]
[ button
[ class "btn btn-primary", onClick Increment ]
[ text "+" ]
-- We added a new button that will trigger a Decrement
-- value as the message when the button is clicked. The
-- Decrement value will get passed into the update function
-- whenever this button gets clicked.
, button
[ class "btn btn-danger", onClick Decrement ]
[ text "-" ]
]
]
-- Now that there are 2 possible Msg values, we added a new entry to the case
-- expression that deals with messages that are equal to the Decrement value.
-- When the message is a Decrement value, the new model value that's returned
-- is one less than what it was. After the new model state is returned, the view
-- function will get passed the new model value and return the new HTML, which
-- will get displayed for the user to see.
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
main : Program () Model Msg
main =
sandbox
{ init = 0
, view = view
, update = update
}