This commit is contained in:
Justin Starry 2019-08-25 18:28:51 -04:00
parent 7ae088647b
commit 23c23d74ec
7 changed files with 71 additions and 62 deletions

View File

@ -218,6 +218,9 @@ html! {
<nav class="menu">
<MyButton title="First Button" />
<MyButton title="Second Button "/>
<MyList name="Grocery List">
<MyListItem text="Apples" />
</MyList>
</nav>
}
```

View File

@ -7,7 +7,6 @@ pub struct ListItem {
}
#[derive(Properties)]
// #[props(ListItem)]
pub struct Props {
pub hide: bool,
#[props(required)]

View File

@ -1,5 +1,4 @@
#![recursion_limit = "128"]
use yew::prelude::*;
mod header;
mod item;
@ -8,6 +7,7 @@ mod list;
use header::ListHeader;
use item::ListItem;
use list::{List, Msg as ListMsg};
use yew::prelude::*;
pub struct Model;

View File

@ -14,21 +14,6 @@ pub enum Hovered {
None,
}
impl fmt::Display for Hovered {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Hovered::Header => "Header",
Hovered::Item(name) => name,
Hovered::List => "List container",
Hovered::None => "Nothing",
}
)
}
}
pub enum Msg {
Hover(Hovered),
}
@ -61,28 +46,6 @@ pub struct Props {
pub children: ChildrenRenderer<ListVariant>,
}
impl<CHILD> From<VChild<CHILD, List>> for ListVariant
where
CHILD: Component + Renderable<CHILD>,
CHILD::Properties: Into<Variants>,
{
fn from(vchild: VChild<CHILD, List>) -> Self {
ListVariant {
props: vchild.props.into(),
scope: vchild.scope,
}
}
}
impl Into<VNode<List>> for ListVariant {
fn into(self) -> VNode<List> {
match self.props {
Variants::Header(props) => VComp::new::<ListHeader>(props, self.scope).into(),
Variants::Item(props) => VComp::new::<ListItem>(props, self.scope).into(),
}
}
}
pub struct List {
props: Props,
hovered: Hovered,
@ -128,6 +91,29 @@ impl Renderable<List> for List {
}
impl List {
fn view_header(&self) -> Html<Self> {
html! {{
for self.props.children.iter().filter(|c| match c.props {
Variants::Header(_) => true,
_ => false
})
}}
}
fn view_items(&self) -> Html<Self> {
html! {{
for self.props.children.iter().filter(|c| match &c.props {
Variants::Item(props) => !props.hide,
_ => false,
}).enumerate().map(|(i, mut c)| {
if let Variants::Item(ref mut props) = c.props {
props.name = format!("#{} - {}", i + 1, props.name);
}
c
})
}}
}
fn view_last_hovered(&self) -> Html<Self> {
html! {
<div class="last-hovered">
@ -140,31 +126,39 @@ impl List {
}
}
impl List {
fn view_header(&self) -> Html<Self> {
html! {
{
for self.props.children.iter().filter(|c| match c.props {
Variants::Header(_) => true,
_ => false
})
impl fmt::Display for Hovered {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Hovered::Header => "Header",
Hovered::Item(name) => name,
Hovered::List => "List container",
Hovered::None => "Nothing",
}
}
)
}
}
fn view_items(&self) -> Html<Self> {
html! {
{
for self.props.children.iter().filter(|c| match &c.props {
Variants::Item(props) => !props.hide,
_ => false,
}).enumerate().map(|(i, mut c)| {
if let Variants::Item(ref mut props) = c.props {
props.name = format!("#{} - {}", i + 1, props.name);
}
c
})
}
impl<CHILD> From<VChild<CHILD, List>> for ListVariant
where
CHILD: Component + Renderable<CHILD>,
CHILD::Properties: Into<Variants>,
{
fn from(vchild: VChild<CHILD, List>) -> Self {
ListVariant {
props: vchild.props.into(),
scope: vchild.scope,
}
}
}
impl Into<VNode<List>> for ListVariant {
fn into(self) -> VNode<List> {
match self.props {
Variants::Header(props) => VComp::new::<ListHeader>(props, self.scope).into(),
Variants::Item(props) => VComp::new::<ListItem>(props, self.scope).into(),
}
}
}

View File

@ -149,7 +149,8 @@ pub mod prelude {
pub use crate::callback::Callback;
pub use crate::events::*;
pub use crate::html::{
Children, Component, ComponentLink, Href, Html, Properties, Renderable, ShouldRender,
Children, ChildrenWithProps, Component, ComponentLink, Href, Html, Properties, Renderable,
ShouldRender,
};
pub use crate::macros::*;

View File

@ -49,6 +49,7 @@ fn compile_fail() {
html! { </ChildComponent> };
html! { <ChildComponent><ChildComponent></ChildComponent> };
html! { <ChildComponent></ChildComponent><ChildComponent></ChildComponent> };
html! { <ChildComponent>{ "Not allowed" }</ChildComponent> };
}
fn main() {}

View File

@ -171,5 +171,16 @@ error[E0599]: no method named `string` found for type `ChildPropertiesBuilder<Ch
48 | html! { <ChildComponent string="abc" /> };
| ^^^^^^
error[E0599]: no method named `children` found for type `ChildPropertiesBuilder<ChildPropertiesBuilderStep_missing_required_prop_int>` in the current scope
--> $DIR/html-component-fail.rs:52:5
|
5 | #[derive(Properties, PartialEq)]
| - method `children` not found for this
...
52 | html! { <ChildComponent>{ "Not allowed" }</ChildComponent> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
Some errors occurred: E0308, E0425, E0599, E0609.
For more information about an error, try `rustc --explain E0308`.