-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (65 loc) · 1.69 KB
/
index.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
const e = React.createElement;
function Header() {
return e("header", { className: "header" }, [
e("h1", null, "TODO"),
e("span", null, "✔"),
]);
}
function AddInput({ addItem }) {
const [text, updateText] = React.useState("");
const input = e("input", {
type: "text",
className: "add-input",
placeholder: "Create a new todo...",
value: text,
onChange(e) {
updateText(e.target.value);
},
onKeyUp(e) {
if (e.code !== "Enter" && e.code !== "NumpadEnter") return;
const value = e.target.value.trim();
if (value) {
addItem(value);
updateText("");
}
},
});
return e("div", { className: "input-container" }, input);
}
function TodoList({ todoList, doneItem }) {
const list = todoList.map((item, index) =>
e(
"div",
{
className: item.done ? 'done' : '',
onClick() {
doneItem(index);
},
},
e("span", null, item.text)
)
);
return e("section", { className: "todo-list" }, list);
}
function useList() {
const [todoList, updateTodoList] = React.useState([]);
function addItem(text) {
updateTodoList((prevTodoList) => [{ text, done: false }, ...prevTodoList]);
}
function doneItem(index) {
updateTodoList((prevTodoList) => {
prevTodoList[index].done = !prevTodoList[index].done;
return [...prevTodoList];
});
}
return [todoList, addItem, doneItem];
}
function App() {
const [todoList, addItem, doneItem] = useList();
return e("div", { className: "todo-app" }, [
e(Header),
e(AddInput, { addItem }),
e(TodoList, { todoList, doneItem }),
]);
}
ReactDOM.render(e(App), document.getElementById("app"));