Skip to content

Instantly share code, notes, and snippets.

@joanllenas
Last active January 4, 2018 11:39
Show Gist options
  • Save joanllenas/a15071090feec2c2ee7d7e4d7d6f636e to your computer and use it in GitHub Desktop.
Save joanllenas/a15071090feec2c2ee7d7e4d7d6f636e to your computer and use it in GitHub Desktop.
Ejemplo del post "Otro sorbo de TEA"
module Counter exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
programParams =
{ model = modelo
, view = vista
, update = actualizar
}
main =
Html.beginnerProgram programParams
-- MODEL
modelo =
{ cont = 0
, incremento = 1
}
-- UPDATE
type Msg
= Incrementar
| Decrementar
| CambiarIncremento String
actualizar mensaje miModelo =
case mensaje of
Incrementar ->
{ miModelo | cont = miModelo.cont + miModelo.incremento }
Decrementar ->
{ miModelo | cont = miModelo.cont - miModelo.incremento }
CambiarIncremento incr ->
{ miModelo | incremento = Result.withDefault 0 (String.toInt incr) }
-- VIEW
vista miModelo =
div []
[ span [ style [ ( "padding", "5px" ) ] ] [ text (toString miModelo.cont) ]
, button [ onClick Incrementar ] [ text "+" ]
, button [ onClick Decrementar ] [ text "-" ]
, input [ type_ "number", value (toString miModelo.incremento), onInput CambiarIncremento ] []
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment