-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
101 lines (94 loc) · 2.13 KB
/
todo.js
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
import { h, app } from "hyperapp"
import "./index.css"
const FilterInfo = { All: 0, Todo: 1, Done: 2 }
const TodoItem = ({ id, value, done, toggle }) => (
<li
class={done && "done"}
onclick={e =>
toggle({
value: done,
id: id
})
}
>
{value}
</li>
)
const state = {
todos: [],
filter: FilterInfo.All,
input: "",
placeholder: "Do that thing..."
}
const view = (state, actions) => (
<main>
<h1>Todo</h1>
<p>
{Object.keys(FilterInfo)
.filter(key => FilterInfo[key] !== state.filter)
.map(key => (
<span>
<a
href=""
onclick={() =>
actions.filter({
value: FilterInfo[key]
})
}
>
{key}
</a>{" "}
</span>
))}
</p>
<div class="flex">
<input
type="text"
onkeyup={e => (e.keyCode === 13 ? actions.add() : "")}
oninput={e => actions.input({ value: e.target.value })}
value={state.input}
placeholder={state.placeholder}
/>
<button onclick={actions.add}>+</button>
</div>
<p>
<ul>
{state.todos
.filter(
t =>
state.filter === FilterInfo.Done
? t.done
: state.filter === FilterInfo.Todo
? !t.done
: state.filter === FilterInfo.All
)
.map(t => (
<TodoItem
id={t.id}
value={t.value}
done={t.done}
toggle={actions.toggle}
/>
))}
</ul>
</p>
</main>
)
const actions = {
add: () => state => ({
input: "",
todos: state.todos.concat({
done: false,
value: state.input,
id: state.todos.length + 1
})
}),
toggle: ({ id, value }) => state => ({
todos: state.todos.map(
t => (id === t.id ? Object.assign({}, t, { done: !value }) : t)
)
}),
input: ({ value }) => ({ input: value }),
filter: ({ value }) => ({ filter: value })
}
app(state, actions, view, document.body)