Last active
May 29, 2016 10:41
-
-
Save jonaslu/bac24bce4f2a122b32fbfde975b32e9e to your computer and use it in GitHub Desktop.
First try of a todo-app in elm. It works but it ain't pretty. Created with my parter in crime @popstr
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 (..) | |
import Html.App exposing (beginnerProgram) | |
import Html.Events exposing (..) | |
import Html.Attributes exposing (..) | |
import List exposing (..) | |
import String | |
main = | |
beginnerProgram { model = model, view = view, update = update } | |
type alias Model = { items: List String, currentItem: String } | |
model: Model | |
model = { | |
items = [], | |
currentItem = "" | |
} | |
type Msg = Add | Update String | Remove Int | |
update: Msg -> Model -> Model | |
update msg model = | |
case msg of | |
Add -> | |
{ items = append model.items [model.currentItem], | |
currentItem = "" } | |
Update text -> | |
{ model | currentItem = text } | |
Remove idx -> | |
{ model | items = | |
(take idx model.items ++ drop (idx+1) model.items) | |
} | |
view model = | |
div [] | |
[ div [] | |
[ ul [] | |
(indexedMap (\ idx item -> (li [] [ | |
text (item ++ " "), | |
button [onClick (Remove idx)] [ text "X"] | |
])) model.items) | |
] | |
, input [value model.currentItem, onInput Update] [] | |
, button [onClick Add] [ text "+" ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment