Skip to content

Commit 33315c8

Browse files
ranilegeoffjay
authored andcommitted
Add IntoPropValue impl for converting to VList (yewstack#3444)
1 parent 7240747 commit 33315c8

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

packages/yew/src/html/component/children.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub type ChildrenWithProps<CHILD> = ChildrenRenderer<VChild<CHILD>>;
152152
/// A type used for rendering children html.
153153
#[derive(Clone)]
154154
pub struct ChildrenRenderer<T> {
155-
children: Vec<T>,
155+
pub(crate) children: Vec<T>,
156156
}
157157

158158
impl<T: PartialEq> PartialEq for ChildrenRenderer<T> {

packages/yew/src/html/conversion/into_prop_value.rs

+68-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use implicit_clone::ImplicitClone;
77
use super::ToHtml;
88
use crate::callback::Callback;
99
use crate::html::{BaseComponent, ChildrenRenderer, Component, NodeRef, Scope};
10-
use crate::virtual_dom::{AttrValue, VChild, VNode, VText};
10+
use crate::virtual_dom::{AttrValue, VChild, VList, VNode, VText};
1111

1212
impl ImplicitClone for NodeRef {}
1313
impl<Comp: Component> ImplicitClone for Scope<Comp> {}
@@ -154,6 +154,20 @@ impl IntoPropValue<ChildrenRenderer<VNode>> for VText {
154154
}
155155
}
156156

157+
impl IntoPropValue<VList> for ChildrenRenderer<VNode> {
158+
#[inline]
159+
fn into_prop_value(self) -> VList {
160+
VList::with_children(self.children, None)
161+
}
162+
}
163+
164+
impl<C: BaseComponent> IntoPropValue<VList> for VChild<C> {
165+
#[inline]
166+
fn into_prop_value(self) -> VList {
167+
VList::with_children(vec![self.into()], None)
168+
}
169+
}
170+
157171
macro_rules! impl_into_prop {
158172
(|$value:ident: $from_ty:ty| -> $to_ty:ty { $conversion:expr }) => {
159173
// implement V -> T
@@ -341,4 +355,57 @@ mod test {
341355
</App>
342356
};
343357
}
358+
359+
#[test]
360+
fn test_vlist_to_children_compiles() {
361+
use crate::prelude::*;
362+
use crate::virtual_dom::VList;
363+
364+
#[function_component]
365+
fn Foo() -> Html {
366+
todo!()
367+
}
368+
369+
#[derive(PartialEq, Properties)]
370+
pub struct ChildProps {
371+
#[prop_or_default]
372+
pub children: Html,
373+
}
374+
375+
#[function_component]
376+
fn Child(_props: &ChildProps) -> Html {
377+
html!()
378+
}
379+
380+
#[derive(PartialEq, Properties)]
381+
pub struct ParentProps {
382+
pub children: VList,
383+
}
384+
385+
#[function_component]
386+
fn Parent(_props: &ParentProps) -> Html {
387+
todo!()
388+
}
389+
390+
let _ = html! {
391+
<Parent>
392+
<Child></Child>
393+
</Parent>
394+
};
395+
396+
let _ = html! {
397+
<Parent>
398+
<Child />
399+
<Child />
400+
</Parent>
401+
};
402+
403+
let _ = html! {
404+
<Parent>
405+
<Child>
406+
<Foo />
407+
</Child>
408+
</Parent>
409+
};
410+
}
344411
}

0 commit comments

Comments
 (0)