Skip to content

Commit 3bcb9a5

Browse files
committed
Add todos example
1 parent 71a7fde commit 3bcb9a5

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

examples/index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010

1111
<body>
1212
<h1>Devtools examples</h1>
13-
<a href="./counter.html">Counter</a>
13+
<ul>
14+
<li>
15+
<a href="./counter.html">Counter</a>
16+
</li>
17+
<li>
18+
<a href="./todos.html">Todo list</a>
19+
</li>
20+
</ul>
1421
</body>
1522

1623
</html>

examples/todos.html

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<title>Counter example with debugger</title>
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
10+
<script src="https://unpkg.com/hyperapp"></script>
11+
<script src="hyperapp-devtools.js"></script>
12+
13+
<style>
14+
main {
15+
width: 40%;
16+
margin-left: 3rem;
17+
font-size: 1rem;
18+
}
19+
20+
.input {
21+
width: 100%;
22+
}
23+
24+
button {
25+
border-radius: 0px;
26+
border: 1px solid black;
27+
font-size: 1rem;
28+
}
29+
30+
.item {
31+
display: flex;
32+
justify-content: space-between;
33+
margin: 0.4rem 0rem;
34+
}
35+
36+
.item span {
37+
flex-grow: 1;
38+
cursor: pointer;
39+
}
40+
41+
.done {
42+
text-decoration: line-through;
43+
}
44+
</style>
45+
</head>
46+
47+
<body>
48+
<div id="app" />
49+
<br />
50+
<br />
51+
<a href="index.html">&nbsp;Back to index</a>
52+
53+
<script>
54+
const h = hyperapp.h
55+
56+
const state = {
57+
todos: [],
58+
input: ""
59+
}
60+
61+
const actions = {
62+
add: name => state => ({ todos: state.todos.concat({ name }), input: "" }),
63+
toggle: id => state => ({
64+
todos: state.todos.map(
65+
(t, i) => (i === id ? { name: t.name, done: !t.done } : t)
66+
)
67+
}),
68+
remove: id => state => ({ todos: state.todos.filter((t, i) => i !== id) }),
69+
input: input => () => ({ input })
70+
}
71+
72+
const Todo = ({ item, id, actions }) => (
73+
h("div", { class: item.done ? "item done" : "item" },
74+
h("span", { onclick: () => actions.toggle(id) }, item.name),
75+
h("button", { onclick: () => actions.remove(id) }, "x")
76+
)
77+
)
78+
79+
const view = (state, actions) => (
80+
h("main", {},
81+
h("h1", {}, "Todo list"),
82+
h("input", {
83+
type: "text",
84+
class: "input",
85+
value: state.input,
86+
onkeyup: e => (e.keyCode === 13 ? actions.add(state.input) : ""),
87+
oninput: e => actions.input(e.target.value),
88+
placeholder: "Enter item..."
89+
}),
90+
state.todos.map((item, id) => Todo({ item, id, actions }))
91+
)
92+
)
93+
94+
devtools(hyperapp.app)(state, actions, view, document.getElementById("app"))
95+
96+
</script>
97+
98+
</body>
99+
100+
</html>

0 commit comments

Comments
 (0)