Created
January 6, 2017 11:31
-
-
Save LukaJCB/3a648dd5b9163e23409a5a2733809be8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (beginnerProgram, div, button, text, ul, li, input, Html) | |
import Html.Events exposing (onClick, onInput) | |
import Array exposing (Array, toList, fromList, push, filter) | |
main = | |
beginnerProgram { model = initialState, view = view, update = update } | |
type alias Model = { list: Array String, currentText: String } | |
initialState : Model | |
initialState = { list = Array.fromList [], currentText = "" } | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ input [ onInput UpdateText] [] | |
, button [ onClick (AddTodo model.currentText) ] [ text "Add Todo" ] | |
, button [ onClick (RemoveTodo model.currentText) ] [ text "Remove Todo" ] | |
, ul [] (Array.toList (Array.map (\t -> li [] [text t]) model.list )) | |
] | |
type Msg = AddTodo String | RemoveTodo String | UpdateText String | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
AddTodo todo -> | |
{ model | list = Array.push todo model.list } | |
RemoveTodo todo -> | |
{ model | list = Array.filter (\t -> t /= todo) model.list } | |
UpdateText todo -> | |
{ model | currentText = todo } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment