elm-architecture-tutorial/examples/3/Counter.elm

49 lines
876 B
Elm
Raw Permalink Normal View History

2015-01-11 19:11:22 +00:00
module Counter (Model, init, Action, update, view) where
2015-04-19 00:23:32 +00:00
import Html exposing (..)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
2015-01-11 19:11:22 +00:00
-- MODEL
type alias Model = Int
init : Int -> Model
init count = count
-- UPDATE
type Action = Increment | Decrement
update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1
-- VIEW
2015-04-19 00:23:32 +00:00
view : Signal.Address Action -> Model -> Html
view address model =
2015-01-11 19:11:22 +00:00
div []
2015-04-19 00:23:32 +00:00
[ button [ onClick address Decrement ] [ text "-" ]
2015-01-11 19:11:22 +00:00
, div [ countStyle ] [ text (toString model) ]
2015-04-19 00:23:32 +00:00
, button [ onClick address Increment ] [ text "+" ]
2015-01-11 19:11:22 +00:00
]
countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("text-align", "center")
]