Skip to content

Instantly share code, notes, and snippets.

@KevinGreene
Created June 23, 2016 13:16
Show Gist options
  • Save KevinGreene/85d2e41534eed4f02816b1b2c31908b4 to your computer and use it in GitHub Desktop.
Save KevinGreene/85d2e41534eed4f02816b1b2c31908b4 to your computer and use it in GitHub Desktop.
Select Troubles in Elm
import Debug
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App as Html
import Http
import Json.Decode as Json
import Task
import String
import Char
main = Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions}
type alias Model =
{ selectedOption: String
, options: List String
}
init : (Model, Cmd Msg)
init =
(Model "A" ["A", "B"], Cmd.none)
type Msg
= SelectOption String
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
SelectOption newOption ->
Debug.log newOption
({model | selectedOption = newOption}, Cmd.none)
view : Model -> Html Msg
view model =
div []
[ text ("Selected Option: " ++ model.selectedOption)
, optionSelect model.options
]
optionSelect : List String -> Html Msg
optionSelect options =
select [] (List.map optionView options)
optionView : String -> Html Msg
optionView optionText =
option
[ value optionText
, on "change" (Json.map SelectOption targetValue)
]
[ text optionText]
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment