Skip to content

Instantly share code, notes, and snippets.

@dtsao
Last active October 5, 2016 10:33
Show Gist options
  • Save dtsao/9fe779b24a24513723a515b63d64e939 to your computer and use it in GitHub Desktop.
Save dtsao/9fe779b24a24513723a515b63d64e939 to your computer and use it in GitHub Desktop.
Added Html.Keyed to fix display bug reported here: https://github.com/elm-lang/virtual-dom/issues/24
module Main exposing (..)
import Html exposing (..)
import Html.Keyed as Keyed
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 =
if target == todo.id then
Todo True todo.id
else
todo
in
model |> List.map updateStatus
NoOp ->
model
view : Model -> Html Msg
view model =
div []
[ text "Added Html.Keyed which fixes the bug reported in https://github.com/elm-lang/virtual-dom/issues/24"
, br [] []
, text "Check items 1 or 2, the next item now correctly displays unchecked:"
, div []
(model
|> List.filter (not << .completed)
|> List.map viewItem
)
, text <| toString model
]
viewItem : Todo -> Html Msg
viewItem todo =
Keyed.node "div"
[]
[ ( (toString todo.id)
, input
[ type' "checkbox"
, checked todo.completed
, onCheck
(\on ->
if on then
SetCompleted todo.id
else
NoOp
)
]
[]
)
, ( (toString todo.id), text <| toString todo.id )
]
@dtsao
Copy link
Author

dtsao commented Oct 5, 2016

Elm v0.17.1. Run this code using elm reactor instead of in Try Elm since Try Elm it doesn't import Html.Keyed (as of 10/5/16).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment