-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.py
More file actions
69 lines (51 loc) · 1.58 KB
/
todo.py
File metadata and controls
69 lines (51 loc) · 1.58 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
from retink import run, Text, View, Button, TextInput, component, create_state, create_effect
from typing import Callable
@component
def TodoForm(on_submit: Callable[[str], None]):
text, set_text = create_state("")
def submit():
on_submit(text)
set_text("")
return TextInput(text, on_change=set_text), Button("submit", on_click=submit)
@component
def Todo(
text: str,
on_delete: Callable[[int], None],
edit_callback: Callable[[int, str], None],
index: int,
):
is_editing, set_editing = create_state(False)
internal_text, set_internal_text = create_state(text)
normal_ui = [
Text(text),
Button("delete", on_click=lambda: on_delete(index)),
Button(
"edit",
on_click=lambda: set_editing(True),
),
]
def submit_edit():
set_editing(False)
edit_callback(index, internal_text)
editing_ui = [
TextInput(internal_text, on_change=set_internal_text),
Button("submit", on_click=submit_edit),
]
return View(
editing_ui if is_editing else normal_ui,
row=True,
)
@component
def App():
todos, set_todos = create_state([])
create_effect(lambda: print(todos), [todos])
def delete_todo(i):
set_todos(todos[:i] + todos[i + 1 :])
def edit_todo(i, text):
todos[i] = text
set_todos(todos)
return TodoForm(on_submit=lambda text: set_todos(todos + [text])), *(
Todo(text, on_delete=delete_todo, edit_callback=edit_todo, index=i)
for i, text in enumerate(todos)
)
run(App)