Created
February 6, 2018 11:26
-
-
Save arvindershinh/9a7596466b8891e6d7650c7c434e0eca to your computer and use it in GitHub Desktop.
Html Select example with Elm
This file contains 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 (Html, div, form, input, option, select, text) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onInput) | |
main : Program Never Model Msg | |
main = | |
Html.beginnerProgram { model = model, update = update, view = view } | |
timeRangeList : List ( String, String ) | |
timeRangeList = | |
[ ( "AllTime", "All time" ), ( "OneWeek", "One week" ), ( "OneDay", "24h" ) ] | |
--model----------------------------------- | |
type alias Model = | |
{ timeRange : String } | |
model : Model | |
model = | |
{ timeRange = "AllTime" } | |
--update----------------------------------- | |
type Msg | |
= ChangeTimeRange String | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
ChangeTimeRange timeDuration -> | |
let | |
filterTimeRangeList = | |
List.filter | |
(\n -> | |
let | |
( a, b ) = n | |
in | |
a == timeDuration | |
) | |
timeRangeList | |
boolTimeRangeList = | |
List.length filterTimeRangeList == 0 | |
in | |
if boolTimeRangeList then | |
model | |
else | |
{ model | timeRange = timeDuration } | |
--view------------------------------------- | |
timeRangeOptionHtml : String -> ( String, String ) -> Html msg | |
timeRangeOptionHtml selectTimeRange rangeTuple = | |
let | |
( a, b ) = rangeTuple | |
optionValue = a | |
optionTitle = b | |
isSelected = selectTimeRange == a | |
in | |
option | |
[ value optionValue, selected isSelected ] | |
[ text optionTitle ] | |
view : Model -> Html Msg | |
view model = | |
div [] [ select [ onInput ChangeTimeRange ] (List.map (timeRangeOptionHtml model.timeRange) timeRangeList) ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment