2021-03-29 16:31:47 +00:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
2021-03-26 19:50:28 +00:00
|
|
|
|
2021-03-29 16:31:47 +00:00
|
|
|
use dioxus::{events::on::MouseEvent, prelude::*};
|
2021-03-26 19:50:28 +00:00
|
|
|
use dioxus_core as dioxus;
|
|
|
|
use dioxus_web::WebsysRenderer;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
|
|
|
|
console_error_panic_hook::set_once();
|
|
|
|
wasm_bindgen_futures::spawn_local(async move {
|
|
|
|
WebsysRenderer::new_with_props(App, ())
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
.expect("major crash");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
lazy_static! {
|
2021-03-29 16:31:47 +00:00
|
|
|
static ref DummyData: BTreeMap<usize, String> = {
|
2021-03-26 19:50:28 +00:00
|
|
|
let vals = vec![
|
2021-03-29 16:31:47 +00:00
|
|
|
"abc123", //
|
|
|
|
"abc124", //
|
|
|
|
"abc125", //
|
|
|
|
"abc126", //
|
|
|
|
"abc127", //
|
|
|
|
"abc128", //
|
|
|
|
"abc129", //
|
|
|
|
"abc1210", //
|
|
|
|
"abc1211", //
|
|
|
|
"abc1212", //
|
|
|
|
"abc1213", //
|
|
|
|
"abc1214", //
|
|
|
|
"abc1215", //
|
|
|
|
"abc1216", //
|
|
|
|
"abc1217", //
|
|
|
|
"abc1218", //
|
|
|
|
"abc1219", //
|
|
|
|
"abc1220", //
|
|
|
|
"abc1221", //
|
|
|
|
"abc1222", //
|
2021-03-26 19:50:28 +00:00
|
|
|
];
|
|
|
|
vals.into_iter()
|
2021-03-29 16:31:47 +00:00
|
|
|
.map(ToString::to_string)
|
|
|
|
.enumerate()
|
2021-03-26 19:50:28 +00:00
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static App: FC<()> = |ctx, _| {
|
2021-03-29 16:31:47 +00:00
|
|
|
let items = use_state_new(&ctx, || DummyData.clone());
|
2021-03-26 19:50:28 +00:00
|
|
|
|
|
|
|
// handle new elements
|
|
|
|
let add_new = move |_| {
|
|
|
|
items.modify(|m| {
|
|
|
|
let k = m.len();
|
|
|
|
let v = match (k % 3, k % 5) {
|
|
|
|
(0, 0) => "FizzBuzz".to_string(),
|
|
|
|
(0, _) => "Fizz".to_string(),
|
|
|
|
(_, 0) => "Buzz".to_string(),
|
|
|
|
_ => k.to_string(),
|
|
|
|
};
|
2021-03-29 16:31:47 +00:00
|
|
|
m.insert(k, v);
|
2021-03-26 19:50:28 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let elements = items.iter().map(|(k, v)| {
|
|
|
|
rsx! {
|
2021-03-29 16:31:47 +00:00
|
|
|
ListHelper {
|
|
|
|
name: k,
|
|
|
|
value: v
|
|
|
|
onclick: move |_| {
|
|
|
|
let key = k.clone();
|
|
|
|
items.modify(move |m| { m.remove(&key); } )
|
2021-03-26 19:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.render(rsx!(
|
|
|
|
div {
|
|
|
|
h1 {"Some list"}
|
2021-03-29 16:31:47 +00:00
|
|
|
button {
|
|
|
|
"Remove all"
|
|
|
|
onclick: move |_| items.set(BTreeMap::new())
|
|
|
|
}
|
2021-03-26 19:50:28 +00:00
|
|
|
button {
|
|
|
|
"add new"
|
|
|
|
onclick: {add_new}
|
|
|
|
}
|
|
|
|
ul {
|
|
|
|
{elements}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
};
|
2021-03-29 16:31:47 +00:00
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
struct ListProps<'a, F: Fn(MouseEvent) + 'a> {
|
|
|
|
name: &'a usize,
|
|
|
|
value: &'a str,
|
|
|
|
onclick: F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Fn(MouseEvent)> PartialEq for ListProps<'_, F> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
// no references are ever the same
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ListHelper<F: Fn(MouseEvent)>(ctx: Context, props: &ListProps<F>) -> DomTree {
|
|
|
|
let k = props.name;
|
|
|
|
let v = props.value;
|
|
|
|
ctx.render(rsx! {
|
|
|
|
li {
|
|
|
|
class: "flex items-center text-xl"
|
|
|
|
key: "{k}"
|
|
|
|
span { "{k}: {v}" }
|
|
|
|
button {
|
|
|
|
"__ Remove"
|
|
|
|
onclick: {&props.onclick}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|