Created
June 5, 2016 21:11
-
-
Save szabba/17d49bf0cd24acd1f7d273a84f56d03f 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 (..) | |
import Html.Events exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.App as App | |
main : Program Never | |
main = | |
App.beginnerProgram | |
{ model = init | |
, update = update | |
, view = view | |
} | |
type alias Model = | |
List Todo | |
type alias Todo = | |
{ completed : Bool | |
, id : Int | |
} | |
init : Model | |
init = | |
[ Todo False 1 | |
, Todo False 2 | |
, Todo False 3 | |
] | |
type Msg | |
= SetCompleted Int | |
| NoOp | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
SetCompleted target -> | |
let | |
updateStatus todo = | |
{ todo | completed = target == todo.id } | |
in | |
model |> List.map updateStatus | |
NoOp -> | |
model | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ text "Check items 1 or 2, the next item will display checked when it should be unchecked:" | |
, div [] | |
(model | |
|> List.filter (Debug.log "todo" >> .completed >> not) | |
|> List.map viewItem | |
) | |
, text <| toString model | |
] | |
viewItem : Todo -> Html Msg | |
viewItem todo = | |
div [] | |
[ input | |
[ type' "checkbox" | |
, checked todo.completed | |
, onCheck | |
(\on -> | |
if on then | |
SetCompleted todo.id | |
else | |
NoOp | |
) | |
] | |
[] | |
, text <| toString todo.id | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment