Feat: view -> render

This commit is contained in:
Jonathan Kelley committed 2021-02-28 21:21:17 -05:00
1 parent 10a236fc65
commit c8bb392cad
24 files changed
+204 -48

No files matched your search

+4 -6
View File
@@ -1,10 +1,8 @@
//! An alternative function syntax
//!
use bumpalo::Bump;
use dioxus_core::prelude::{VNode};
use dioxus_core::prelude::VNode;
fn main() {}
@@ -47,7 +45,7 @@ fn Example(ctx: Context2<Props>) -> DTree {
let val = use_state(&ctx, || String::from("asd"));
let props = ctx.props();
ctx.view(move |b| {
ctx.render(move |b| {
dioxus_core::nodebuilder::div(b)
.child(dioxus_core::nodebuilder::text(props.c.as_str()))
.child(virtual_child(b, Props2 { a: val }, AltChild))
@@ -59,7 +57,7 @@ fn Example(ctx: Context2<Props>) -> DTree {
fn Example2(ctx: Context2<()>, name: &str, _blah: &str) -> DTree {
let val = use_state(&ctx, || String::from("asd"));
ctx.view(move |b| {
ctx.render(move |b| {
dioxus_core::nodebuilder::div(b)
.child(dioxus_core::nodebuilder::text(name))
.child(virtual_child(b, Props2 { a: val }, AltChild))
@@ -81,7 +79,7 @@ struct Props2<'a> {
impl Properties for Props2<'_> {}
fn AltChild(ctx: Context2<Props2>) -> DTree {
ctx.view(|_b| {
ctx.render(|_b| {
//
todo!()
})
+151 -3
View File
@@ -19,14 +19,20 @@ struct ListItem {
}
fn app(ctx: Context, props: &Props) -> DomTree {
ctx.view(move |b| {
let (f, setter) = use_state(&ctx, || 0);
ctx.render(move |b| {
let mut root = builder::div(b);
for child in &props.items {
// notice that the child directly borrows from our vec
// this makes lists very fast (simply views reusing lifetimes)
root = root.child(builder::virtual_child(
b,
ChildProps { item: child },
ChildProps {
item: child,
item_handler: setter,
},
// <ChildItem item=child hanldler=setter />
child_item,
));
}
@@ -34,10 +40,152 @@ fn app(ctx: Context, props: &Props) -> DomTree {
})
}
type StateSetter<T> = dyn Fn(T);
struct ChildProps<'a> {
// Pass down complex structs
item: &'a ListItem,
// Even pass down handlers!
item_handler: &'a StateSetter<i32>,
}
fn child_item(_ctx: Context, _props: &ChildProps) -> DomTree {
fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
todo!()
// ctx.render(rsx! {
// div {
// item: child,
// handler: setter,
// abc: 123,
// onclick: props.item_handler,
// h1 { "abcd123" }
// h2 { "abcd123" }
// div {
// "abcd123"
// h2 { }
// p { }
// },
// }
// })
}
/*
rsx! {
ChildItem {
// props
item: child, handler: setter,
// children
div { class:"abcd", abc: 123 },
div { class:"abcd", abc: 123 },
// Auto-text coercion
"eyo matie {abc}",
// Anything that accepts Into<VChild>
{},
}
}
// dreaming of this syntax
#[derive(Properties)]
struct ChildProps<'a> {
username: &'a str,
item_handler: &'a dyn Fn(i32),
}
fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
ctx.render(rsx! {
div {
class: "abc123",
abc: 123,
onclick: props.item_handler,
h1 { "Hello, {props.username}!" },
h2 { "Welcome the RSX syntax" },
div {
h3 { "This is a subheader" }
button {
onclick: props.handler,
"This is a button"
}
"This is child text"
},
}
})
}
// This is also nice
#[dioxus::component]
static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
ctx.render(rsx! {
div {
class: "abc123",
abc: 123,
onclick: handler,
h1 { "Hello, {username}!" },
h2 { "Welcome the RSX syntax" },
div {
h3 { "This is a subheader" }
button {
onclick: props.handler,
"This is a button"
}
"This is child text"
},
}
})
}
Menlo, Monaco, 'Courier New', monospace
struct Item {
name: String,
content: String,
}
#[dioxus::live_component]
static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
// return lazy nodes or
let ssr = ctx.suspend(async {
let data = fetch("https://google.com")
.await?
.json::<Item>()
.await?;
rsx! {
div {
h1 { "Welcome: {data.name}" }
p { "Content: \n {data.content}" }
}
}
});
ctx.render(rsx! {
div {
class: "abc123",
abc: 123,
onclick: handler,
h1 { "Hello, {username}!" },
h2 { "Welcome the RSX syntax" },
div {
h3 { "This is a subheader" }
button {
onclick: props.handler,
"This is a button"
}
{ssr}
},
}
})
}
*/
+2 -2
View File
@@ -14,7 +14,7 @@ struct Props {
static Example: FC<Props> = |ctx, props| {
let value = ctx.use_context(|c: &SomeContext| c.items.last().unwrap());
ctx.view(move |bump| {
ctx.render(move |bump| {
button(bump)
.on("click", move |_| {
println!("Value is {}", props.name);
@@ -26,7 +26,7 @@ static Example: FC<Props> = |ctx, props| {
})
.finish()
})
// ctx.view(html! {
// ctx.render(html! {
// <div>
// <button onclick={move |_| println!("Value is {}", value)} />
// <button onclick={move |_| println!("Value is {}", value)} />
+1 -2
View File
@@ -5,7 +5,6 @@
// use dioxus_core::prelude::*;
// use once_cell::sync::{Lazy, OnceCell};
use std::ops::Deref;
/*
@@ -97,7 +96,7 @@ fn t<'a>(ctx: Context<'a>) {
// let val2 = refed.as_bytes();
});
ctx.view(move |_b| {});
ctx.render(move |_b| {});
}
fn main() {}
+1 -1
View File
@@ -13,7 +13,7 @@ fn main() {
static Example: FC<()> = |ctx, props| {
let (name, set_name) = use_state(&ctx, || "...?");
ctx.view(html! {
ctx.render(html! {
<div>
<h1> "Hello, {name}" </h1>
<button onclick={move |_| set_name("jack")}> "jack!" </button>
+3 -3
View File
@@ -70,7 +70,7 @@ fn main() {}
// });
// // todo!()
// ctx.view(html! {
// ctx.render(html! {
// <div>
// <h1> "Products" </h1>
// <button> "hello!" </button>
@@ -86,7 +86,7 @@ fn main() {}
// // VNodes can be constructed via a builder or the html! macro
// // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
// // We can "view" them with Context for ultimate speed while inside components
// ctx.view(|bump| {
// ctx.render(|bump| {
// div(bump)
// .attr("class", "edit")
// .child(text("Hello"))
@@ -102,7 +102,7 @@ fn main() {}
// // We can "view" them with Context for ultimate speed while inside components
// // use "phase" style allocation;
// ctx.view(html! {
// ctx.render(html! {
// <div>
// // your template goes here
// // feel free to directly use "name"
+2 -2
View File
@@ -12,7 +12,7 @@ static Header: FC<()> = |ctx, props| {
let handler1 = move || println!("Value is {}", inner.current());
ctx.view(|bump| {
ctx.render(|bump| {
builder::div(bump)
.child(VNode::Component(VComponent::new(
Bottom,
@@ -24,7 +24,7 @@ static Header: FC<()> = |ctx, props| {
};
static Bottom: FC<()> = |ctx, props| {
ctx.view(html! {
ctx.render(html! {
<div>
<h1> "bruh 1" </h1>
<h1> "bruh 2" </h1>
+1 -1
View File
@@ -30,7 +30,7 @@ struct Props {
// }
static Example: FC<Props> = |ctx, _props| {
ctx.view(html! {
ctx.render(html! {
<div>
<h1> "hello world!" </h1>
<h1> "hello world!" </h1>