-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelect.elm
56 lines (48 loc) · 1.53 KB
/
Select.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Select exposing (selectList, Config)
import Html exposing (Html)
import Html.Attributes
import Html.Events
import Json.Decode as Json
type alias Config a b =
{ items : List a
, getId : a -> String
, getLabel : a -> String
, onSelect : a -> b
, selected : a
}
selectList : Config a b -> Html b
selectList config =
(Html.select (attributes config) (options config))
attributes : Config a b -> List (Html.Attribute b)
attributes config =
let
idToMessage =
(\id -> config.onSelect (findItemById config id))
handleChange =
Json.map (\id -> idToMessage id) Html.Events.targetValue
in
[ Html.Events.on "change" handleChange ]
options : Config a b -> List (Html b)
options config =
let
value =
(\item -> Html.Attributes.value (config.getId item))
selected =
(\item -> Html.Attributes.selected ((config.getId item) == (config.getId config.selected)))
label =
(\item -> Html.text (config.getLabel item))
itemToOption =
(\item -> Html.option [ (selected item), (value item) ] [ label item ])
in
List.map (\item -> itemToOption item) config.items
findItemById : Config a b -> String -> a
findItemById config id =
let
candidates =
List.filter (\item -> (config.getId item) == id) config.items
in
case candidates of
[] ->
Debug.crash "Selected an ID which was not in the list"
x :: xs ->
x