forked from bryanjenningz/25-elm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-todos.elm
91 lines (74 loc) · 2.32 KB
/
16-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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module Main exposing (..)
import Html exposing (Html, text, div, beginnerProgram, input, button, span)
import Html.Attributes exposing (class, value, autofocus, placeholder)
import Html.Events exposing (onInput, onClick)
type Msg
= UpdateText String
| AddTodo
| RemoveTodo Int
type alias Model =
{ text : String
, todos : List String
}
-- We added some nice little touches to the web app. We added a placeholder
-- attribute that's similar to the placeholder attribute you're used to with
-- native HTML.
view : Model -> Html Msg
view model =
div [ class "col-12 col-sm-6 offset-sm-3" ]
[ div [ class "row" ]
[ div [ class "col-9" ]
[ input
[ onInput UpdateText
, value model.text
, autofocus True
, class "form-control"
, placeholder "Enter a todo"
]
[]
]
, div [ class "col-3" ]
[ button
[ onClick AddTodo, class "btn btn-primary form-control" ]
[ text "+" ]
]
]
, div [] (List.indexedMap viewTodo model.todos)
]
-- We made the styling nicer by taking advantage of Bootstrap classes.
viewTodo : Int -> String -> Html Msg
viewTodo index todo =
div [ class "card" ]
[ div [ class "card-block" ]
[ text todo
, span
[ onClick (RemoveTodo index)
, class "float-right"
]
[ text "✖" ]
]
]
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateText newText ->
{ model | text = newText }
AddTodo ->
{ model | text = "", todos = model.todos ++ [ model.text ] }
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
}