-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (79 loc) · 1.64 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
81
82
83
84
85
86
87
88
89
90
91
/** @jsx h */
const { h, patch } = require('../../lib');
var DATAS = [
{
task: 'Read a good book',
done: false
},
{
task: 'Learn Javascript',
done: false
},
{
task: 'Learn Haskell',
done: false
}
];
// Events
const onAddTask = event => {
const { value } = event.target;
if (event.keyCode === 13 && value) {
DATAS = [{ task: value, done: false }, ...DATAS];
event.target.value = '';
render();
}
};
const onToggleTask = index => {
DATAS = DATAS.map((i, idx) => {
if (index === idx) {
i.done = !i.done;
}
return i;
});
render();
};
const onRemoveTask = index => {
DATAS = DATAS.filter((i, idx) => index !== idx);
render();
};
// Elements
const Input = props => {
return (
<div className="insert">
<input type="text" placeholder="What needs to be done?" onkeyup={onAddTask} />
</div>
);
};
const Item = props => {
const { item, index } = props;
return (
<li className={`todo${item.done ? ' done' : ''}`}>
<span onclick={() => onToggleTask(index)}>{item.task}</span>
<button className="destroy" onclick={() => onRemoveTask(index)}></button>
</li>
);
};
const List = props => {
const { datas } = props;
return (
<ul className="list">
{datas.map((item, index) => <Item item={item} index={index} />)}
</ul>
);
};
const App = props => {
return (
<div>
<h1>To Do List</h1>
<Input />
<List datas={props.datas} />
</div>
);
};
var currentTree;
function render() {
const nextTree = <App datas={DATAS} />;
patch(document.getElementById('app'), nextTree, currentTree);
currentTree = nextTree;
}
render();