Skip to content

Instantly share code, notes, and snippets.

@nicolasparada
Created September 7, 2016 00:27
Show Gist options
  • Save nicolasparada/77382dc7843be7a070c0bae18d2ce62f to your computer and use it in GitHub Desktop.
Save nicolasparada/77382dc7843be7a070c0bae18d2ce62f to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.App as App
import Html.Attributes as Attr exposing (..)
import Html.Events as Event exposing (..)
main = App.beginnerProgram
{ model = model
, view = view
, update = update
}
-- Model
type alias Model =
{ todo: String
, todos: List String
}
model : Model
model =
{ todo = ""
, todos = []
}
-- Update
type Msg
= SetTodoText String
| AddTodo
| RemoveTodo String
update : Msg -> Model -> Model
update message model =
case message of
SetTodoText text ->
{ model | todo = text }
AddTodo ->
{ model | todos = model.todo :: model.todos }
RemoveTodo todo ->
{ model | todos = List.filter (\t -> t /= todo) model.todos }
-- View
todoForm : String -> Html Msg
todoForm todo =
Html.form [autocomplete False, onSubmit AddTodo]
[ input
[ type' "text"
, placeholder "Todo"
, onInput SetTodoText
, value todo
] []
, button [type' "submit"] [text "Submit"]
]
todoItem : String -> Html Msg
todoItem todo =
li []
[ text todo
, button [onClick (RemoveTodo todo)] [text "×"]]
todoList : List String -> Html Msg
todoList todos =
let
children = List.map todoItem todos
in
ul [] children
view : Model -> Html Msg
view model =
div []
[ todoForm model.todo
, todoList model.todos
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment