forked from bryanjenningz/25-elm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-todos.elm
75 lines (60 loc) · 1.99 KB
/
15-todos.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
module Main exposing (..)
import Html exposing (Html, text, div, beginnerProgram, input, button, span)
import Html.Attributes exposing (class, value, autofocus)
import Html.Events exposing (onInput, onClick)
-- We added a (RemoveTodo Int) value to the Msg type, which will allow us
-- to remove a todo by index.
type Msg
= UpdateText String
| AddTodo
| RemoveTodo Int
type alias Model =
{ text : String
, todos : List String
}
view : Model -> Html Msg
view model =
div [ class "text-center" ]
[ input [ onInput UpdateText, value model.text, autofocus True ] []
, button
[ onClick AddTodo, class "btn btn-primary" ]
[ text "Add Todo" ]
, div []
(List.indexedMap
(\index todo ->
div []
[ text todo
-- We add a little "X" that we can click to remove
-- the todo at the specified index.
, span [ onClick (RemoveTodo index) ] [ text " X" ]
]
)
model.todos
)
]
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateText newText ->
{ model | text = newText }
AddTodo ->
{ model | text = "", todos = model.todos ++ [ model.text ] }
-- We use a let expression to get the todos before and after, then
-- we set the newTodos value to them concatenated together.
RemoveTodo index ->
let
beforeTodos =
List.take index model.todos
afterTodos =
List.drop (index + 1) model.todos
newTodos =
beforeTodos ++ afterTodos
in
{ model | todos = newTodos }
main : Program Never Model Msg
main =
beginnerProgram
{ model = { text = "", todos = [] }
, view = view
, update = update
}