/
UpDownWithSelectList.elm
138 lines (106 loc) · 2.68 KB
/
UpDownWithSelectList.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module UpDownWithSelectList exposing (main)
import Browser
import Html exposing (Html, div, text)
import Html.Attributes as Attributes
import Html.Events as Events
import SelectList exposing (SelectList)
-- APP
main : Program () Model Msg
main =
Browser.element
{ init = \_ -> init
, view = view
, update = update
, subscriptions = subscriptions
}
init : ( Model, Cmd Msg )
init =
( { todos =
[ "foo"
, "bar"
, "baz"
, "foobar"
, "foobaz"
, "barbaz"
, "foobarbaz"
]
}
, Cmd.none
)
-- MODEL
type alias Model =
{ todos : List String
}
-- UPDATE
type Msg
= UpdateTodo (List String)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UpdateTodo todo ->
( { model | todos = todo }
, Cmd.none
)
-- VIEW
view : Model -> Html Msg
view model =
div []
[ div [] <|
SelectList.selectedMapForList renderRow model.todos
, div []
[ text "Results:"
, div [ Attributes.style "padding-left" "1em" ] <|
List.map (\str -> div [] [ text str ]) model.todos
]
]
renderRow : SelectList String -> Html Msg
renderRow selected =
div
[]
[ Html.input
[ Attributes.type_ "text"
, Events.onInput
(\str ->
SelectList.replaceSelected str selected
|> SelectList.toList
|> UpdateTodo
)
, Attributes.value <| SelectList.selected selected
]
[]
, Html.button
[ Attributes.type_ "button"
, Events.onClick
(SelectList.delete selected
|> Maybe.map SelectList.toList
|> Maybe.withDefault []
|> UpdateTodo
)
]
[ text "×"
]
, Html.button
[ Attributes.type_ "button"
, Events.onClick
(SelectList.moveBy -1 selected
|> SelectList.toList
|> UpdateTodo
)
]
[ text "△"
]
, Html.button
[ Attributes.type_ "button"
, Events.onClick
(SelectList.moveBy 1 selected
|> SelectList.toList
|> UpdateTodo
)
]
[ text "▽"
]
]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none