Created
December 22, 2017 02:07
-
-
Save asmasa/f8e0ed397bc6737b36c6f7a196397f5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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