-
-
Notifications
You must be signed in to change notification settings - Fork 974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support generic components in rsx!()
macro
#385
Conversation
This is awesome - I'm excited to see it! However I can't seem to get it working properly. :( Also, I would imagine that the correct syntax would be For reference, the two ways of specifying explicit type parameters in Rust are: // for functions
let _ = eat::<MyThing>();
// for structs
let _ = MyStruct::<MyThing> { _p: PhantomData }; Can you add an example to the list of examples that showcases how this is supposed to work? use dioxus::prelude::*;
fn main() {}
fn app(cx: Scope) -> Element {
cx.render(rsx! {
div {
// I tried this
Child<MyThing> {}
// I also tried this
Child::<MyThing> {}
}
})
}
trait Message {
const MSG: &'static str;
}
struct MyThing {}
impl Message for MyThing {
const MSG: &'static str = "hello";
}
#[inline_props]
fn Child<T: Message>(cx: Scope) -> Element {
let me = <T as Message>::MSG;
cx.render(rsx! {
div {
p {
"Hello {me}"
}
}
})
} I get these errors:
|
Ops looks like I forget to do it for Now when I think about it, it will won't be funny task to do it for Anyway I was testing with // src/testing.rs
use core::fmt;
use std::{marker::PhantomData, str::FromStr};
use css_style::style;
use dioxus::prelude::*;
use dioxus_layout::Column;
#[derive(Props)]
pub struct Props<T: 'static> {
#[props(default)]
phantom: PhantomData<T>,
}
#[allow(non_snake_case)]
pub fn MyInput<'a, T>(cx: Scope<'a, Props<T>>) -> Element
where
T: FromStr + fmt::Display + Clone + 'static,
<T as FromStr>::Err: std::fmt::Display,
{
// states
let is_hover = use_state(&cx, || false);
let is_focus = use_state(&cx, || false);
let error: &UseState<Option<T::Err>> = use_state(&cx, || None);
// styles
let inner = style();
let container = style();
let error_style = style();
// elements
let input = rsx!(input {
style: "{inner}",
oninput: move |event| {
match T::from_str(&event.value) {
Ok(_) => error.set(None),
Err(err) => error.set(Some(err)),
}
},
});
cx.render(rsx!(
Column {
div {
style: "{container}",
onfocusin: move |_| is_focus.set(true),
onfocusout: move |_| is_focus.set(false),
onmouseover: move |_| is_hover.set(true),
onmouseout: move |_| is_hover.set(false),
input
}
error.get().as_ref().map(|err| {
rsx!(p {
style: "{error_style}",
"{err}"
})
})
}
))
} and here is how how to use it: rsx! {
MyInput<testing::Props<Url>> {}
};
True, but for some reason I used the style when we crate types: struct Input<T> { .. }
^^^^^^^^ I'll change it to be
Sure, will do once we got stable API/Design. |
now generic components is supported in the only left parts are docs, unit tests and examples, they will follow once you approve last changes, I also left some messages on Discord regarding these things, would you take look on them. |
I can add the docs and examples added - looks good to merge now. I'm going to run through a few tests and then merge it if all looks good. |
Sorry I'm kinda slow doing this PR, I try to work on it when i have free time :D. I have added a test in I just want to mention that it would be great if we make the docs part of our tests, that would make the docs up-to-date, correct and we cover more test. |
I have reused |
Hi @jkelleyrtp, I consider this PR done from my side, if you have comment on this or we can merge ? |
before that, we accepted this style `Input<..>` but to be consistent with Rust syntax this is dropped now. > For reference, the two ways of specifying explicit type parameters in > Rust are: > // for functions > let _ = foo::<MyType>(); > > // for structs > let _ = Foo::<MyType> { ... }; > > by @jkelleyrtp
previously I from some reason I thought this not allowed syntax. Some test failed because of my misunderstood, so now I fix this :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exciting! Thank you! Sorry for the wait - I left for vacation. When checks pass again I will merge :)
No description provided.