-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtodo.js
More file actions
71 lines (60 loc) · 1.4 KB
/
todo.js
File metadata and controls
71 lines (60 loc) · 1.4 KB
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
import { useState } from 'react';
import { createHook } from 'hookleton';
const initial = ['Do one', 'Do two', 'Do three'];
const useStore = createHook(init => {
const [state, setState] = useState(init);
const add = todo => setState(state.concat(todo));
const remove = todo => setState(state.filter(i => i !== todo));
return [state, { add, remove }];
});
const TodoForm = () => {
const [todo, setTodo] = useState('');
const [, { add }] = useStore.use(initial);
const handleTodoChange = e => setTodo(e.target.value);
const handleTodoAdd = () => {
add(todo);
setTodo(''); // clean form
};
return (
<>
<input value={todo} onChange={handleTodoChange} />
<button onClick={handleTodoAdd}>Add</button>
</>
);
};
const TodoHeader = () => {
const [todos] = useStore();
return (
<>
{todos.length === 0 ? (
<h4>All todos are done! </h4>
) : (
<div>
<span>
Todo List, remain: <span>{todos.length}</span> todos.
</span>
</div>
)}
</>
);
};
const TodoList = () => {
const [todos, { remove }] = useStore();
return (
<ul>
{todos.map((todo, idx) => (
<li key={idx}>
{todo}
<button onClick={() => remove(todo)}>Complete</button>
</li>
))}
</ul>
);
};
export default () => (
<>
<TodoForm />
<TodoHeader />
<TodoList />
</>
);