/
TodoApp.cs
81 lines (76 loc) · 2.74 KB
/
TodoApp.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace YewLib
{
public class TodoItem
{
public int Id { get; set; }
public string Text { get; set; }
public bool Completed { get; set; }
}
public class TodoState
{
public List<TodoItem> TodoItems = new List<TodoItem>();
public string CurrentItemText { get; set; }
public int IdGen { get; set; }
}
public record TodoApp : View
{
public class Atoms
{
public static Key<TodoState> App = new();
}
public class Component : YewLib.Component
{
public View TodoItemView(Action update, TodoItem item) =>
new Flex("item-" + item.Id, "todo-item")
{
new Checkbox(item.Completed, v =>
{
item.Completed = v;
update();
}),
new Label(item.Text)
};
public override View Render()
{
var state = UseAtom(Atoms.App, () => new TodoState());
return new StackLayout()
{
new ScrollView
{
state.Value.TodoItems
.Where(x => !x.Completed)
.Select(item => TodoItemView(state.Update, item)),
},
new Flex(className: "new-todo") {
new TextField(
label: "What do you need to do?",
value: state.Value.CurrentItemText,
className: "textfield",
onChanged:(v) => state.Value.CurrentItemText = v
),
new Button("Add!", () =>
{
state.Value.TodoItems.Add(new TodoItem
{
Id = state.Value.IdGen++,
Text = state.Value.CurrentItemText
});
state.Value.CurrentItemText = string.Empty;
state.Update();
})
},
state.Value.TodoItems.Any(x => x.Completed) ? new ScrollView(classNames: "completed")
{
Label("Completed Items:"),
state.Value.TodoItems
.Where(x => x.Completed)
.Select(item => TodoItemView(state.Update, item)),
} : null
};
}
}
}
}