on master help sign in
examples/input-form: use the builder for rendering
f3b0625Nick Fitzgerald
examples/input-form/src/lib.rs+28−37
@@ -1,5 +1,5 @@
1use dodrio::bumpalo::{self, Bump};
1use dodrio::bumpalo::{self, Bump};
2−use dodrio::{on, Attribute, Node, Render};
2+use dodrio::{Node, Render};
3use wasm_bindgen::prelude::*;
3use wasm_bindgen::prelude::*;
4use wasm_bindgen::JsCast;
4use wasm_bindgen::JsCast;
5
5
@@ -25,48 +25,39 @@ impl SayHelloTo {
25// The `Render` implementation has a text `<input>` and a `<div>` that shows a
25// The `Render` implementation has a text `<input>` and a `<div>` that shows a
26// greeting to the `<input>`'s value.
26// greeting to the `<input>`'s value.
27impl Render for SayHelloTo {
27impl Render for SayHelloTo {
28− fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> dodrio::Node<'bump>
28+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
29 where
29 where
30 'a: 'bump,
30 'a: 'bump,
31 {
31 {
32− let input = Node::element(
32+ use dodrio::builder::*;
33− bump,
34− "input",
35− [on(bump, "input", |root, vdom, event| {
36− // If the event's target is our input...
37− let input = match event
38− .target()
39− .and_then(|t| t.dyn_into::<web_sys::HtmlInputElement>().ok())
40− {
41− None => return,
42− Some(input) => input,
43− };
44
33
45− // ...then get its value and update who we are greeting.
34+ div(bump)
46− let value = input.value();
35+ .children([
47− let hello = root.unwrap_mut::<SayHelloTo>();
36+ input(bump)
48− hello.set_who(value);
37+ .attr("type", "text")
38+ .attr("value", &self.who)
39+ .on("input", |root, vdom, event| {
40+ // If the event's target is our input...
41+ let input = match event
42+ .target()
43+ .and_then(|t| t.dyn_into::<web_sys::HtmlInputElement>().ok())
44+ {
45+ None => return,
46+ Some(input) => input,
47+ };
49
48
50− // Finally, re-render the component on the next animation frame.
49+ // ...then get its value and update who we are greeting.
51− vdom.schedule_render();
50+ let value = input.value();
52− })],
51+ let hello = root.unwrap_mut::<SayHelloTo>();
53− [
52+ hello.set_who(value);
54− Attribute {
55− name: "type",
56− value: "text",
57− },
58− Attribute {
59− name: "value",
60− value: &self.who,
61− },
62− ],
63− [],
64− );
65
53
54+ // Finally, re-render the component on the next animation frame.
55+ vdom.schedule_render();
56+ })
57+ .finish(),
66− let hello = bumpalo::format!(in bump, "Hello, {}!", self.who);
58+ text(bumpalo::format!(in bump, "Hello, {}!", self.who).into_bump_str()),
67− let hello = Node::text(hello.into_bump_str());
59+ ])
68−
60+ .finish()
69− Node::element(bump, "div", [], [], [input, hello])
70 }
61 }
71}
62}
72
63