diff --git a/src/macros.rs b/src/macros.rs index a4104860e39..f8ba7cd3d60 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -21,6 +21,13 @@ macro_rules! html_impl { let mut pair = $crate::virtual_dom::VComp::lazy::<$comp>(); html_impl! { @vcomp $stack pair ($($tail)*) } }; + // Set a whole struct as a properties + (@vcomp $stack:ident $pair:ident (with $props:ident, $($tail:tt)*)) => { + $pair.0 = $props; + html_impl! { @vcomp $stack $pair ($($tail)*) } + }; + // Set a specific field as a property. + // It uses `Transformer` trait to convert a type used in template to a type of the field. (@vcomp $stack:ident $pair:ident ($attr:ident = $val:expr, $($tail:tt)*)) => { // It cloned for ergonomics in templates. Attribute with // `self.param` value could be reused and sholdn't be cloned diff --git a/tests/vcomp_test.rs b/tests/vcomp_test.rs new file mode 100644 index 00000000000..84d08a9fabc --- /dev/null +++ b/tests/vcomp_test.rs @@ -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 for Comp { + type Message = (); + type Properties = Props; + + fn create(_: Self::Properties, _: &mut Env) -> Self { + Comp + } + + fn update(&mut self, _: Self::Message, _: &mut Env) -> ShouldRender { + unimplemented!(); + } +} + +impl Renderable for Comp { + fn view(&self) -> Html { + unimplemented!(); + } +} + +#[test] +fn set_properties_to_component() { + let _: VNode = html! { + + }; + + let _: VNode = html! { + + }; + + let _: VNode = html! { + + }; + + let _: VNode = html! { + + }; + + let props = Props { + field_1: 1, + field_2: 1, + }; + + let _: VNode = html! { + + }; +}