| 1 |
module Main exposing (..) |
| 2 |
|
| 3 |
-- Press buttons to increment and decrement a counter. |
| 4 |
-- |
| 5 |
-- Read how it works: |
| 6 |
-- https://guide.elm-lang.org/architecture/buttons.html |
| 7 |
-- |
| 8 |
|
| 9 |
|
| 10 |
import Browser |
| 11 |
import Html exposing (Html, button, div, text) |
| 12 |
import Html.Events exposing (onClick) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
-- MAIN |
| 17 |
|
| 18 |
|
| 19 |
main = |
| 20 |
Browser.sandbox { init = init, update = update, view = view } |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
-- MODEL |
| 25 |
|
| 26 |
|
| 27 |
type alias Model = Int |
| 28 |
|
| 29 |
|
| 30 |
init : Model |
| 31 |
init = |
| 32 |
0 |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
-- UPDATE |
| 37 |
|
| 38 |
|
| 39 |
type Msg |
| 40 |
= Increment |
| 41 |
| Decrement |
| 42 |
|
| 43 |
|
| 44 |
update : Msg -> Model -> Model |
| 45 |
update msg model = |
| 46 |
case msg of |
| 47 |
Increment -> |
| 48 |
model + 1 |
| 49 |
|
| 50 |
Decrement -> |
| 51 |
model - 1 |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
-- VIEW |
| 56 |
|
| 57 |
|
| 58 |
view : Model -> Html Msg |
| 59 |
view model = |
| 60 |
div [] |
| 61 |
[ button [ onClick Decrement ] [ text "-" ] |
| 62 |
, div [] [ text (String.fromInt model) ] |
| 63 |
, button [ onClick Increment ] [ text "+" ] |
| 64 |
] |
| 65 |
|
| 66 |
-- From https://elm-lang.org/examples/buttons |