-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathExample.elm
124 lines (104 loc) · 2.82 KB
/
Example.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
module Example exposing
( Model
, Msg(..)
, initialModel
, update
, view
)
import Html exposing (..)
import Html.Attributes exposing (class, style)
import Select
import Shared
type alias Model item =
{ id : String
, available : List item
, itemToLabel : item -> String
, selected : List item
, selectState : Select.State
, selectConfig : Select.Config (Msg item) item
}
type alias InitArgs item =
{ id : String
, available : List item
, selected : List item
, selectConfig : Select.Config (Msg item) item
, itemToLabel : item -> String
}
initialModel : InitArgs item -> Model item
initialModel args =
{ id = args.id
, available = args.available
, itemToLabel = args.itemToLabel
, selected = args.selected
, selectState = Select.init args.id
, selectConfig = args.selectConfig
}
type Msg item
= NoOp
| OnSelect (Maybe item)
| OnRemoveItem item
| SelectMsg (Select.Msg item)
| OnFocus
| OnBlur
| OnEsc
update : Msg item -> Model item -> ( Model item, Cmd (Msg item) )
update msg model =
case msg of
OnSelect maybeColor ->
let
selected =
maybeColor
|> Maybe.map (List.singleton >> List.append model.selected)
|> Maybe.withDefault []
in
( { model | selected = selected }, Cmd.none )
OnRemoveItem colorToRemove ->
let
selected =
List.filter (\curColor -> curColor /= colorToRemove)
model.selected
in
( { model | selected = selected }, Cmd.none )
SelectMsg subMsg ->
let
( updated, cmd ) =
Select.update
model.selectConfig
subMsg
model.selectState
in
( { model | selectState = updated }, cmd )
NoOp ->
( model, Cmd.none )
OnFocus ->
( model, Cmd.none )
OnBlur ->
( model, Cmd.none )
OnEsc ->
( model, Cmd.none )
view : Model item -> String -> String -> Html (Msg item)
view model title prompt =
let
currentSelection =
p
[]
[ text (String.join ", " <| List.map model.itemToLabel model.selected) ]
select =
Select.view
model.selectConfig
model.selectState
model.available
model.selected
in
div [ class "demo-box" ]
[ h3 [] [ text title ]
, p
[]
[ label [] [ text prompt ]
]
, p
[]
[ select
]
, currentSelection
]