-
Hi :) So I am in the middle of trying to rewrite from 0.6 to 0.7, and I have ran into some trouble, and I am therefore coming here to see if anyone has any tips in how to do this :) So, I have this code: pub struct StepStruct {
pub label: String,
pub child: AnyView,
}
#[component(transparent)]
pub fn Step<F, E>(label: &'static str, child: F) -> StepStruct
where
F: Fn() -> E + 'static,
E: IntoView + 'static,
{
StepStruct {
label: label.to_string(),
child: child().into_any(),
}
} Then I am also implementing the traits Then I have a component like this: #[component]
pub fn Stepper(children: TypedChildren<Vec<StepStruct>>) -> impl IntoView {
todo!();
} and I am trying to use it like this: view! {
<Stepper>
<Step label="foo" child=move || view! { <p>"Something"</p> }/>
<Step label="bar" child=move || view! { <p>"Test"</p> }/>
</Stepper>
} This doesn't work because it will convert it expectes So, I want a way to be able to retrieve the actual type Any help would be very much appreciated🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can do it. By one way of looking at it, it's kind of gross; with another way of looking at it, it is just a matter of adding the right trait implementations. Specifically, Here's an example: pub struct StepStruct {
pub label: String,
pub child: ViewFn,
}
struct Steps(Box<dyn FnOnce() -> Vec<StepStruct>>);
impl<F, C> ToChildren<F> for Steps
where
F: FnOnce() -> C + Send + 'static,
C: IntoSteps,
{
#[inline]
fn to_children(f: F) -> Self {
Steps(Box::new(move || f().into_steps()))
}
}
trait IntoSteps {
fn into_steps(self) -> Vec<StepStruct>;
}
impl IntoSteps for () {
fn into_steps(self) -> Vec<StepStruct> {
vec![]
}
}
impl IntoSteps for (StepStruct,) {
fn into_steps(self) -> Vec<StepStruct> {
vec![self.0]
}
}
impl IntoSteps for (StepStruct, StepStruct) {
fn into_steps(self) -> Vec<StepStruct> {
vec![self.0, self.1]
}
}
#[component(transparent)]
pub fn Stepper(children: Steps) -> impl IntoView {
(children.0)()
.into_iter()
.map(|step| {
view! {
<h2>{step.label}</h2>
{step.child.run()}
}
})
.collect_view()
}
#[component(transparent)]
pub fn Step(label: &'static str, #[prop(into)] child: ViewFn) -> StepStruct {
StepStruct {
label: label.to_string(),
child,
}
}
#[component]
pub fn App() -> impl IntoView {
view! {
<Stepper>
<Step label="foo" child=move || view! { <p>"Something"</p> }/>
<Step label="bar" child=move || view! { <p>"Test"</p> }/>
</Stepper>
}
} The gross part here is mostly to do with the need to implement |
Beta Was this translation helpful? Give feedback.
You can do it. By one way of looking at it, it's kind of gross; with another way of looking at it, it is just a matter of adding the right trait implementations. Specifically,
ToChildren
.Here's an example: