Skip to content

Instantly share code, notes, and snippets.

@asmasa
Created December 22, 2017 02:07
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 asmasa/f8e0ed397bc6737b36c6f7a196397f5b to your computer and use it in GitHub Desktop.
Save asmasa/f8e0ed397bc6737b36c6f7a196397f5b to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events as Events exposing (on)
import Json.Decode as Json
type alias Model =
{ selectedText : Maybe String
}
init : ( Model, Cmd Msg )
init =
( Model Nothing, Cmd.none )
type Msg
= Change String
onChange : (String -> msg) -> Attribute msg
onChange handler =
on "change" (Json.map handler Events.targetValue)
view : Model -> Html Msg
view model =
let
selectedText =
Maybe.withDefault "" model.selectedText
handler selectedValue =
Change selectedValue
in
div []
[ div [] [ text selectedText ]
, select [ onChange handler ]
[ option [ value "a" ] [ text "a" ]
, option [ value "b" ] [ text "b" ]
, option [ value "c" ] [ text "c" ]
]
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Change text ->
( { model | selectedText = Just text }, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment