-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathApp.cs
More file actions
79 lines (71 loc) · 2.62 KB
/
App.cs
File metadata and controls
79 lines (71 loc) · 2.62 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
72
73
74
75
76
77
78
79
// <snippet:todo-app>
using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using static Microsoft.UI.Reactor.Factories;
using Microsoft.UI.Xaml;
ReactorApp.Run<TodoApp>("Todo App", width: 550, height: 600, devtools: true);
class TodoApp : Component
{
public override Element Render()
{
var (items, updateItems) = UseReducer(new List<TodoItem>
{
new("Learn Reactor basics", true),
new("Build a todo app", false),
new("Explore hooks", false),
});
var (newText, setNewText) = UseState("");
var doneCount = items.Count(i => i.Done);
return VStack(16,
Heading("Todo List"),
TextBlock($"{doneCount}/{items.Count} completed").Opacity(0.6),
// Input row
HStack(8,
TextField(newText, setNewText, placeholder: "What needs to be done?")
.Width(300),
Button("Add", () =>
{
if (!string.IsNullOrWhiteSpace(newText))
{
updateItems(list => [.. list, new TodoItem(newText.Trim(), false)]);
setNewText("");
}
}).IsEnabled(!(string.IsNullOrWhiteSpace(newText)))
),
// Item list
VStack(4,
items.Select((item, index) =>
HStack(8,
CheckBox(item.Done, done =>
updateItems(list =>
{
var copy = new List<TodoItem>(list);
copy[index] = item with { Done = done };
return copy;
}),
label: item.Text
),
Button("Remove", () =>
updateItems(list =>
{
var copy = new List<TodoItem>(list);
copy.RemoveAt(index);
return copy;
})
)
).WithKey($"todo-{index}")
).ToArray()
),
// Clear completed button
When(doneCount > 0, () =>
Button($"Clear completed ({doneCount})", () =>
updateItems(list => list.Where(i => !i.Done).ToList())
)
)
).Padding(24);
}
}
// </snippet:todo-app>
// <snippet:todo-record>
record TodoItem(string Text, bool Done);
// </snippet:todo-record>