Skip to content

Instantly share code, notes, and snippets.

@szabba
Created June 5, 2016 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szabba/17d49bf0cd24acd1f7d273a84f56d03f to your computer and use it in GitHub Desktop.
Save szabba/17d49bf0cd24acd1f7d273a84f56d03f to your computer and use it in GitHub Desktop.
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