Created
December 16, 2021 08:47
-
-
Save PeteDevoy/c34a9dc34a0578efed3d1cbe2761522e to your computer and use it in GitHub Desktop.
Select Multiple example for Elm language
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
{- authored by https://github.com/lucamug -} | |
module Main exposing (main) | |
import Browser | |
import Html exposing (..) | |
import Html.Attributes | |
import Html.Events | |
import Json.Decode | |
type alias Model = | |
{ value : List ( String, Maybe String ) } | |
init : Model | |
init = | |
{ value = [] } | |
type Msg | |
= SetMultipleInts (List ( String, Maybe String )) | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
SetMultipleInts value -> | |
{ model | value = value } | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ select | |
[ Html.Events.on "change" | |
(Json.Decode.map SetMultipleInts targetSelectedOptions) | |
, Html.Attributes.multiple True | |
] | |
(List.map myOption (List.range 1 4)) | |
, div [] | |
[ text <| | |
Debug.toString | |
(model | |
|> .value | |
|> List.map Tuple.second | |
|> List.filterMap identity | |
) | |
] | |
] | |
targetSelectedOptions : Json.Decode.Decoder (List ( String, Maybe String )) | |
targetSelectedOptions = | |
Json.Decode.at [ "target", "selectedOptions" ] <| | |
Json.Decode.keyValuePairs <| | |
Json.Decode.maybe (Json.Decode.at [ "value" ] Json.Decode.string) | |
myOption : Int -> Html Msg | |
myOption id = | |
option [ Html.Attributes.value (String.fromInt id) ] | |
[ text <| "Option " ++ String.fromInt id ] | |
main : Program () Model Msg | |
main = | |
Browser.sandbox | |
{ init = init | |
, view = view | |
, update = update | |
} |
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
{- authored by https://github.com/lucamug -} | |
<html> | |
<head> | |
<style> | |
/* you can style your program here */ | |
</style> | |
</head> | |
<body> | |
<main></main> | |
<script> | |
var app = Elm.Main.init({ node: document.querySelector('main') }) | |
// you can use ports and stuff here | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment