-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
271: Support passing a struct instance as properties r=DenisKolodin a=limira If we have ```rust #[derive(Debug, Default, Eq, PartialEq)] pub struct MyProps { val: i32, } impl Component<Context> for MyComp { type Message = Msg; type Properties = MyProps; } ``` then if we have an instance of `MyProps`, we can pass it directly to its Component: ```rust let my_props = MyProps {val: 64 }; html! { <MyComp: props=my_props, /> } ``` Is there a better syntax for this? If this is merged as is, any `struct` that is used for a Component's properties will not allowed to contain a field named `props`! Edit: I you merge this, please squash all commits. While working on this, I got stuck by an error I don't know how to solve, just commit to get back to the original working code (from master). Co-authored-by: Limira <20672976+limira@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#[macro_use] | ||
extern crate yew; | ||
|
||
use yew::html::{Component, Env, Html, Renderable, ShouldRender}; | ||
use yew::virtual_dom::VNode; | ||
|
||
type Ctx = (); | ||
|
||
struct Comp; | ||
|
||
#[derive(PartialEq, Clone)] | ||
struct Props { | ||
field_1: u32, | ||
field_2: u32, | ||
} | ||
|
||
impl Default for Props { | ||
fn default() -> Self { | ||
Props { | ||
field_1: 0, | ||
field_2: 0, | ||
} | ||
} | ||
} | ||
|
||
impl Component<Ctx> for Comp { | ||
type Message = (); | ||
type Properties = Props; | ||
|
||
fn create(_: Self::Properties, _: &mut Env<Ctx, Self>) -> Self { | ||
Comp | ||
} | ||
|
||
fn update(&mut self, _: Self::Message, _: &mut Env<Ctx, Self>) -> ShouldRender { | ||
unimplemented!(); | ||
} | ||
} | ||
|
||
impl Renderable<Ctx, Comp> for Comp { | ||
fn view(&self) -> Html<Ctx, Self> { | ||
unimplemented!(); | ||
} | ||
} | ||
|
||
#[test] | ||
fn set_properties_to_component() { | ||
let _: VNode<Ctx, Comp> = html! { | ||
<Comp: /> | ||
}; | ||
|
||
let _: VNode<Ctx, Comp> = html! { | ||
<Comp: field_1=1, /> | ||
}; | ||
|
||
let _: VNode<Ctx, Comp> = html! { | ||
<Comp: field_2=2, /> | ||
}; | ||
|
||
let _: VNode<Ctx, Comp> = html! { | ||
<Comp: field_1=1, field_2=2, /> | ||
}; | ||
|
||
let props = Props { | ||
field_1: 1, | ||
field_2: 1, | ||
}; | ||
|
||
let _: VNode<Ctx, Comp> = html! { | ||
<Comp: with props, field_2=2, /> | ||
}; | ||
} |