Skip to content

Instantly share code, notes, and snippets.

@mrmurphy
Last active September 19, 2016 17:55
Show Gist options
  • Save mrmurphy/bb797fa0ae0025f9c812fe074df608da to your computer and use it in GitHub Desktop.
Save mrmurphy/bb797fa0ae0025f9c812fe074df608da to your computer and use it in GitHub Desktop.
A bug in the select
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App exposing (beginnerProgram)
type alias Model =
{ showSelect : Bool
, selectVal : Maybe String
}
type Msg
= SetShow Bool
| SetSelectVal String
main : Program Never
main =
beginnerProgram
{ model = { showSelect = True, selectVal = Nothing }
, update = update
, view = view
}
update : Msg -> Model -> Model
update msg model =
case msg of
SetShow v ->
{ model | showSelect = v }
SetSelectVal v ->
{ model | selectVal = Just v }
view : Model -> Html Msg
view model =
div []
[ case model.showSelect of
True ->
select [ onInput SetSelectVal, value <| Maybe.withDefault "--" model.selectVal ]
[ option [ value "--" ] [ text "--" ]
, option [ value "1" ] [ text "1" ]
]
False ->
span [] []
, label [] [ text "Show select" ]
, input [ type' "checkbox", checked model.showSelect, onCheck SetShow ] []
, div []
[ label [] [ text "Model: " ]
, text <| toString model
]
, ol []
[ li [] [ text "Pick a value in the select box" ]
, li [] [ text "Uncheck \"Show select\"" ]
, li [] [ text "Check it again" ]
, li [] [ text "Notice that the model still has a value, but the select incorrectly shows its first item" ]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment