todo_list/
todo_list.rs

1use rui::*;
2
3fn add_button(todos: impl Binding<Vec<String>>) -> impl View {
4    state(String::new, move |name, _| {
5        hstack((
6            text_editor(name),
7            button(text("Add Item"), move |cx| {
8                let name_str = cx[name].clone();
9                todos.with_mut(cx, |todos| todos.push(name_str));
10                // Gotta fix a bug in text_editor!
11                // cx[name] = String::new();
12            }),
13        ))
14    })
15}
16
17fn todo_list(todos: impl Binding<Vec<String>>) -> impl View {
18    with_cx(move |cx| {
19        let len = todos.with(cx, |todos| todos.len());
20        let ids = (0usize..len).collect();
21
22        list(ids, move |id| {
23            let id = *id;
24            with_cx(move |cx| todos.with(cx, |todos| todos[id].clone()))
25        })
26    })
27}
28
29fn main() {
30    rui(state(std::vec::Vec::new, move |todos, _| {
31        vstack((add_button(todos), todo_list(todos)))
32    }));
33}