33use std:: fmt;
44
55use crate :: html:: Html ;
6- use crate :: virtual_dom:: { VChild , VNode } ;
6+ use crate :: virtual_dom:: VChild ;
77use crate :: Properties ;
88
99/// A type used for accepting children elements in Component::Properties.
@@ -163,7 +163,7 @@ impl<T: PartialEq> PartialEq for ChildrenRenderer<T> {
163163
164164impl < T > ChildrenRenderer < T >
165165where
166- T : Clone + Into < VNode > ,
166+ T : Clone ,
167167{
168168 /// Create children
169169 pub fn new ( children : Vec < T > ) -> Self {
@@ -186,6 +186,28 @@ where
186186 // This way `self.iter().next()` only has to clone a single node.
187187 self . children . iter ( ) . cloned ( )
188188 }
189+
190+ /// Convert the children elements to another object (if there are any).
191+ ///
192+ /// ```
193+ /// # let children = Children::new(Vec::new());
194+ /// # use yew::{classes, html, Children};
195+ /// children.map(|children| {
196+ /// html! {
197+ /// <div class={classes!("container")}>
198+ /// {children}
199+ /// </div>
200+ /// }
201+ /// })
202+ /// # ;
203+ /// ```
204+ pub fn map < OUT : Default > ( & self , closure : impl FnOnce ( & Self ) -> OUT ) -> OUT {
205+ if self . is_empty ( ) {
206+ Default :: default ( )
207+ } else {
208+ closure ( self )
209+ }
210+ }
189211}
190212
191213impl < T > Default for ChildrenRenderer < T > {
@@ -218,3 +240,18 @@ pub struct ChildrenProps {
218240 #[ prop_or_default]
219241 pub children : Children ,
220242}
243+
244+ #[ cfg( test) ]
245+ mod tests {
246+ use super :: * ;
247+
248+ #[ test]
249+ fn children_map ( ) {
250+ let children = Children :: new ( vec ! [ ] ) ;
251+ let res = children. map ( |children| Some ( children. clone ( ) ) ) ;
252+ assert ! ( res. is_none( ) ) ;
253+ let children = Children :: new ( vec ! [ Default :: default ( ) ] ) ;
254+ let res = children. map ( |children| Some ( children. clone ( ) ) ) ;
255+ assert ! ( res. is_some( ) ) ;
256+ }
257+ }
0 commit comments