Skip to content

Commit

Permalink
Add from' for customization of labels and values.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgastako committed Nov 9, 2016
1 parent 0afbe87 commit fa32f91
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/Select.elm
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
module Select exposing (from)
module Select exposing (from, from')

{-| This module provides the `Select.from` helper make working with `select`
elements from Elm easier.
# Helpers
@docs from, from'
-}

import Html exposing (Html, option, select, text)
import Html.Attributes exposing (value)
import Html.Events exposing (onInput)


{-| Convert a list of values and stringifying function for values of that type
into a `select` element which contains as it's values the list of values.
from [North, South, East, West] Direction
would produce a select element with box option values and labels being the
string versions of four cardinal directions which would send messages
like (Direction North) or (Direction East) when the user selects new directions
from the drop down.
-}
from : List a -> (a -> msg) -> Html msg
from xs msg =
from' xs msg toString toString


{-| Convert a list of values and stringifying function for values of that type
into a `select` element which contains as it's values the list of values.
from' [North, South, East, West] Direction toId toLabel
would produce a select element with box option values and labels being decided
by the `toId` and `toLabel` functions, which would send messages
like (Direction North) or (Direction East) when the user selects new directions
from the drop down.
-}
from' : List a -> (a -> msg) -> (a -> String) -> (a -> String) -> Html msg
from' xs msg toId toLabel =
let
optionize x =
option [ (value << toString) x ]
[ (text << toString) x ]
option [ (value << toId) x ]
[ (text << toLabel) x ]
in
select [ onInput (msg << makeFromString' xs) ]
(List.map optionize xs)
Expand Down

0 comments on commit fa32f91

Please sign in to comment.