From 013440d36abfab90414359e8266c5700498f4b54 Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Tue, 3 Oct 2023 19:38:48 +0900 Subject: [PATCH] Remove use of ChildrenRenderer from nested_list (#3436) * Remove ChildrenRenderer from nested_list. * Remove undeed conversion. --- examples/nested_list/src/app.rs | 53 +++++++++++++++------ examples/nested_list/src/list.rs | 79 +++++++------------------------- 2 files changed, 56 insertions(+), 76 deletions(-) diff --git a/examples/nested_list/src/app.rs b/examples/nested_list/src/app.rs index 6c62b7d1c44..eacbccdd2f2 100644 --- a/examples/nested_list/src/app.rs +++ b/examples/nested_list/src/app.rs @@ -1,3 +1,5 @@ +use std::iter; + use yew::prelude::*; use super::header::ListHeader; @@ -48,23 +50,48 @@ impl Component for App { // note the use of `html_nested!` instead of `html!`. let letters = ('A'..='C') - .map(|letter| html_nested! { }); + .map(|letter| html_nested! { }); html! {

{ "Nested List Demo" }

- - - - - -
{ "Sublist!" }
- - - - { for letters } - -
+ + } + ] + } + > + {vec![ + html_nested! { }, + html_nested! { }, + html_nested! { + +
{ "Sublist!" }
+ + }] + } + > + { + iter::once(html_nested! { }) + .chain(letters) + .collect::>() + } + +
+ }, + ]} + +
{ self.view_last_hovered() }
diff --git a/examples/nested_list/src/list.rs b/examples/nested_list/src/list.rs index 53c2101945e..46570610a96 100644 --- a/examples/nested_list/src/list.rs +++ b/examples/nested_list/src/list.rs @@ -1,64 +1,23 @@ use std::rc::Rc; -use yew::html::ChildrenRenderer; use yew::prelude::*; -use yew::virtual_dom::{VChild, VComp}; +use yew::virtual_dom::VChild; -use crate::header::{ListHeader, Props as HeaderProps}; -use crate::item::{ListItem, Props as ItemProps}; +use crate::header::ListHeader; +use crate::item::ListItem; use crate::{Hovered, WeakComponentLink}; -#[derive(Clone, PartialEq)] -pub enum Variants { - Item(Rc<::Properties>), - Header(Rc<::Properties>), -} - -impl From for Variants { - fn from(props: ItemProps) -> Self { - Variants::Item(Rc::new(props)) - } -} - -impl From for Variants { - fn from(props: HeaderProps) -> Self { - Variants::Header(Rc::new(props)) - } -} - -#[derive(PartialEq, Clone)] -pub struct ListVariant { - props: Variants, -} - -impl From> for ListVariant -where - CHILD: Component, - CHILD::Properties: Into + Clone, -{ - fn from(vchild: VChild) -> Self { - Self { - props: (*vchild.props).clone().into(), - } - } -} - -impl From for Html { - fn from(variant: ListVariant) -> Html { - match variant.props { - Variants::Header(props) => VComp::new::(props, None).into(), - Variants::Item(props) => VComp::new::(props, None).into(), - } - } -} - pub enum Msg { HeaderClick, } #[derive(Clone, PartialEq, Properties)] pub struct Props { - pub children: ChildrenRenderer, + #[prop_or_default] + pub header: Vec>, + #[prop_or_default] + pub children: Vec>, + pub on_hover: Callback, pub weak_link: WeakComponentLink, } @@ -97,9 +56,9 @@ impl Component for List { html! {
- { Self::view_header(&ctx.props().children) } + { ctx.props().header.clone() }
- { Self::view_items(&ctx.props().children) } + { Self::view_items(ctx.props().children.clone()) }
@@ -108,21 +67,15 @@ impl Component for List { } impl List { - fn view_header(children: &ChildrenRenderer) -> Html { - html! { for children.iter().filter(|c| matches!(c.props, Variants::Header(_))) } - } - - fn view_items(children: &ChildrenRenderer) -> Html { + fn view_items(children: Vec>) -> Html { children - .iter() - .filter(|c| matches!(&c.props, Variants::Item(props) if !props.hide)) + .into_iter() + .filter(|c| !c.props.hide) .enumerate() .map(|(i, mut c)| { - if let Variants::Item(props) = c.props { - let mut props = (*props).clone(); - props.name = format!("#{} - {}", i + 1, props.name); - c.props = Variants::Item(Rc::new(props)); - } + let mut props = (*c.props).clone(); + props.name = format!("#{} - {}", i + 1, props.name); + c.props = Rc::new(props); c }) .collect::()