-
Notifications
You must be signed in to change notification settings - Fork 16
/
14-todos.elm
77 lines (51 loc) · 2.03 KB
/
14-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
76
77
module Main exposing (main)
import Browser exposing (sandbox)
import Html exposing (Html, button, div, input, text)
import Html.Attributes exposing (autofocus, class, value)
import Html.Events exposing (onClick, onInput)
-- We added a new AddTodo message type.
type Msg
= UpdateText String
| AddTodo
-- We added a new property called todos, which is a list of strings.
type alias Model =
{ text : String
, todos : List String
}
-- We added (autofocus True), which is like the native HTML autofocus attribute.
-- We also added a button that triggers an onClick event when clicked which
-- passes an AddTodo message to the update function.
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.map (\todo -> div [] [ text todo ]) model.todos)
]
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateText newText ->
{ model | text = newText }
-- We append the model.text value to the end of our list of todo strings.
AddTodo ->
{ model | text = "", todos = model.todos ++ [ model.text ] }
-- We set the todos property so that it's initially an empty list.
main : Program () Model Msg
main =
sandbox
{ init = { text = "", todos = [] }
, view = view
, update = update
}
-- Exercises to 15:
{-
Right now you cannot remove an item from the list.
Let us add that ability. If we want to get rid of an item
We should be able to click a button and remove it from the list.
Hint: add a message called RemoveToDo (or something equivalent)
and use the update function to remove that particular to do item
from the model. In the view function, you can add a little 'X'
button such that when the user clicks it, the RemoveToDo message
is sent to the update fucntion. Good luck and see you in lesson 15!
-}