Feat: revert FC changes (like the old style).
This commit reverts to the old style of props + FC. The old style is desirable because people comfortable with react can automatically be comfortable with dioxus. It's also nice in that the same props can be used to drive two different components - something the trait version couldn't do. Now, our trait bound forces implementations to have the #[derive(Props)] flag. This will need to implement the Properties trait as well as PartialEq (using ptr::equal for closure fields).
This commit is contained in:
1 parent
55a5541dba
commit
7158bc3575
15 files changed
+1339
-137
No files matched your search
@@ -7,12 +7,15 @@
|
||||
|
||||
fn main() {}
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use dioxus_core::{component::Properties, prelude::*};
|
||||
|
||||
struct Props {
|
||||
items: Vec<ListItem>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct ListItem {
|
||||
name: String,
|
||||
age: u32,
|
||||
@@ -21,18 +24,18 @@ struct ListItem {
|
||||
fn app(ctx: Context, props: &Props) -> DomTree {
|
||||
let (f, setter) = use_state(&ctx, || 0);
|
||||
|
||||
ctx.render(move |b| {
|
||||
let mut root = builder::ElementBuilder::new(b, "div");
|
||||
ctx.render(move |c| {
|
||||
let mut root = builder::ElementBuilder::new(c, "div");
|
||||
for child in &props.items {
|
||||
// notice that the child directly borrows from our vec
|
||||
// this makes lists very fast (simply views reusing lifetimes)
|
||||
// <ChildItem item=child hanldler=setter />
|
||||
root = root.child(builder::virtual_child(
|
||||
b.bump,
|
||||
c,
|
||||
ChildProps {
|
||||
item: child,
|
||||
item_handler: setter,
|
||||
},
|
||||
// <ChildItem item=child hanldler=setter />
|
||||
child_item,
|
||||
));
|
||||
}
|
||||
@@ -41,7 +44,16 @@ fn app(ctx: Context, props: &Props) -> DomTree {
|
||||
}
|
||||
|
||||
type StateSetter<T> = dyn Fn(T);
|
||||
// struct StateSetter<T>(dyn Fn(T));
|
||||
|
||||
// impl<T> PartialEq for StateSetter<T> {
|
||||
// fn eq(&self, other: &Self) -> bool {
|
||||
// self as *const _ == other as *const _
|
||||
// }
|
||||
// }
|
||||
|
||||
// props should derive a partialeq implementation automatically, but implement ptr compare for & fields
|
||||
#[derive(Props)]
|
||||
struct ChildProps<'a> {
|
||||
// Pass down complex structs
|
||||
item: &'a ListItem,
|
||||
@@ -50,6 +62,19 @@ struct ChildProps<'a> {
|
||||
item_handler: &'a StateSetter<i32>,
|
||||
}
|
||||
|
||||
impl PartialEq for ChildProps<'_> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Properties for ChildProps<'a> {
|
||||
type Builder = ChildPropsBuilder<'a, ((), ())>;
|
||||
fn builder() -> <Self as Properties>::Builder {
|
||||
ChildProps::builder()
|
||||
}
|
||||
}
|
||||
|
||||
fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
|
||||
todo!()
|
||||
// ctx.render(rsx! {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::scope::FC;
|
||||
|
||||
use dioxus_core_macro::fc;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
@@ -22,7 +22,7 @@ use std::marker::PhantomData;
|
||||
|
||||
// always try to fill in with Default
|
||||
|
||||
#[fc]
|
||||
// #[fc]
|
||||
fn Example(ctx: Context, a: &str, b: &str, c: &str) -> DomTree {
|
||||
ctx.render(rsx! {
|
||||
div {
|
||||
@@ -33,7 +33,7 @@ fn Example(ctx: Context, a: &str, b: &str, c: &str) -> DomTree {
|
||||
})
|
||||
}
|
||||
|
||||
#[fc]
|
||||
// #[fc]
|
||||
fn SomeComponent(ctx: Context, a: &str, b: &str) -> DomTree {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@@ -1,32 +1,20 @@
|
||||
use std::marker::PhantomData;
|
||||
use dioxus_core_macro::Props;
|
||||
|
||||
fn main() {}
|
||||
#[derive(Debug, Props)]
|
||||
struct SomeProps {
|
||||
a: i32,
|
||||
|
||||
trait Props<'parent> {}
|
||||
|
||||
struct SomeProps<'p> {
|
||||
text: &'p str,
|
||||
// automatically do the default (none) and automatically Into<T>
|
||||
#[builder(default, setter(strip_option))]
|
||||
b: Option<i32>,
|
||||
}
|
||||
|
||||
impl<'p> Props<'p> for SomeProps<'p> {}
|
||||
// have we committed to the trait style yet?
|
||||
|
||||
struct OutputNode<'a> {
|
||||
_p: PhantomData<&'a ()>,
|
||||
fn main() {
|
||||
let g: SomeProps = SomeProps::builder().a(10).b(10).build();
|
||||
|
||||
let r = g.b.unwrap_or_else(|| 10);
|
||||
}
|
||||
|
||||
// combine reference to self (borrowed from self) and referenfce to parent (borrowed from parent)
|
||||
// borrow chain looks like 'p + 's -> 'p + 's -> 'p + 's
|
||||
// always adding new lifetimes from self into the mix
|
||||
// what does a "self" lifetime mean?
|
||||
// a "god" gives us our data
|
||||
// the god's lifetime is tied to Context, and the borrowed props object
|
||||
// for the sake of simplicity, we just clobber lifetimes.
|
||||
// user functions are just lies and we abuse lifetimes.
|
||||
// everything is managed at runtime because that's how we make something ergonomc
|
||||
// lifetime management in dioxus is just cheating around the rules
|
||||
// our kind god manages lifetimes for us so we don't have to, thanks god
|
||||
fn something<'s>(_props: &'s SomeProps<'s>) -> OutputNode<'s> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// type BC<'p, P: Props<'p>> = for<'a, 'b, 'c> fn(&'a P<'b>) -> OutputNode<'c>;
|
||||
fn auto_into_some() {}
|
||||
@@ -1,20 +1,19 @@
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::{component::Properties, prelude::*};
|
||||
|
||||
fn main() -> Result<(), ()> {
|
||||
let p1 = Props { name: "bob".into() };
|
||||
let p1 = SomeProps { name: "bob".into() };
|
||||
|
||||
let mut vdom = VirtualDom::new_with_props(Example, p1);
|
||||
// vdom.update_props(|p: &mut Props| {});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Props {
|
||||
#[derive(Debug, PartialEq, Props)]
|
||||
struct SomeProps {
|
||||
name: String,
|
||||
}
|
||||
|
||||
static Example: FC<Props> = |ctx, _props| {
|
||||
static Example: FC<SomeProps> = |ctx, _props| {
|
||||
ctx.render(html! {
|
||||
<div>
|
||||
<h1> "hello world!" </h1>
|
||||
@@ -24,3 +23,11 @@ static Example: FC<Props> = |ctx, _props| {
|
||||
</div>
|
||||
})
|
||||
};
|
||||
|
||||
// toodo: derive this
|
||||
impl Properties for SomeProps {
|
||||
type Builder = SomePropsBuilder<((),)>;
|
||||
fn builder() -> Self::Builder {
|
||||
SomeProps::builder()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user