This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0
Use a Monocle Prism to handle <select> conflict between ADTs and Strings
version 1.0.1
license MIT
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.1
Committed At 2017-01-14 07:19:03 UTC
elm-lang/html 2.0.0 <= v < 3.0.0 2.0.0
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1
arturopala/elm-monocle 1.4.0 <= v < 2.0.0 1.7.0

README

select-prism

A <select> is basically the UI representation of a union type or ADT.Using a Prism and it's data structure for a data transformation that's not quite isomorphic, we can use go from an ADT to a String and back like we'd prefer to do.

import Html exposing (..)
import Html.SelectPrism exposing (selectp)


view : Model -> Html Msg
view { selectedColor } =
    -- Right Here ↓
    selectp colorp ChangeColor selectedColor [] colorOptions


colorOptions : List ( String, Color )
colorOptions =
    [ ( "❤️  Red", Red )
    , ( "💙 Blue", Blue )
    , ( "💚 Green", Green )
    ]


type alias Model =
    { selectedColor : Color }


type Msg
    = ChangeColor (Result String Color)


type Color
    = Red
    | Blue
    | Green


{-| You the developer are responsible for this `Prism`s correctness
-}
colorp : Prism String Color
colorp =
    let
        colorFromString : String -> Maybe Color
        colorFromString s =
            case s of
                "red" ->
                    Just Red

                "green" ->
                    Just Green

                "blue" ->
                    Just Blue

                _ ->
                    Nothing

        colorToString : Color -> String
        colorToString c =
            case c of
                Red ->
                    "red"

                Green ->
                    "green"

                Blue ->
                    "blue"
    in
        -- Using `Prism` as a constructor
        Prism colorFromString colorToString

Do check out the example and/or read my blog entry which goes into more depth.