Skip to content

Instantly share code, notes, and snippets.

@A5308Y
Forked from chalmagean/onSelect.elm
Last active October 16, 2016 05:04
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 A5308Y/069e05e82d19de84a589334e5b16cfc7 to your computer and use it in GitHub Desktop.
Save A5308Y/069e05e82d19de84a589334e5b16cfc7 to your computer and use it in GitHub Desktop.
Elm onSelect decoder
-- Assuming we have a list of items in the model (type alias Model = { items : List Item }
-- where Item is a record like { id : Int, name : String }
-- The following goes in the view and generates an html dropdown.
-- In my example I get:
-- The argument to function `onSelect` is causing a mismatch.
-- Function `onSelect` is expecting the argument to be:
-- Int -> a
-- But it is:
-- String -> Msg
-- which I don't really understand
import Json.Decode as Json
select
[ onSelect ValueSelectedMsg ]
(List.map (\item -> option [ value (toString item.id) ] [ text item.name ]) model.items)
targetSelectedIndex : Json.Decoder Int
targetSelectedIndex =
Json.at [ "target", "selectedIndex" ] Json.int
onSelect : (Int -> msg) -> Html.Attribute msg
onSelect msg =
on "change" (Json.map msg targetSelectedIndex)
-- This will send a message containing the selected index (eg. ValueSelectedMsg 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment