From 0660151a8b641fa0a23dde2598132029970f7ae4 Mon Sep 17 00:00:00 2001 From: jelemux Date: Thu, 11 Feb 2021 12:07:22 +0100 Subject: refactoring - reduced code size by about a third --- src/view/property_group.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/view/property_group.rs (limited to 'src/view/property_group.rs') diff --git a/src/view/property_group.rs b/src/view/property_group.rs new file mode 100644 index 0000000..9c86e80 --- /dev/null +++ b/src/view/property_group.rs @@ -0,0 +1,81 @@ +use crate::model::*; +use crate::view::weak_links::WeakComponentLink; +use yew::prelude::*; +use yewtil::NeqAssign; + +#[derive(Clone, PartialEq, Properties)] +pub struct InputProps< + O: 'static + VCardPropertyInputObject + Clone, + M: 'static + PartialEq + Clone, +> { + pub generated: Callback, + pub weak_link: WeakComponentLink>, +} + +#[derive(Clone, PartialEq)] +pub struct PropertyGroupInputComponent< + O: 'static + VCardPropertyInputObject, + M: 'static + PartialEq + Clone, +> { + pub props: InputProps, + pub value: O, + pub error: Option, +} + +impl, M: 'static + PartialEq + Clone> Component + for PropertyGroupInputComponent +{ + type Message = M; + type Properties = InputProps; + fn create(props: ::Properties, link: yew::html::Scope) -> Self { + props.weak_link.borrow_mut().replace(link); + Self { + props, + value: O::new(), + error: None, + } + } + fn update(&mut self, msg: ::Message) -> bool { + self.value.update(self.props.clone(), msg) + } + fn change(&mut self, props: ::Properties) -> bool { + self.props.neq_assign(props) + } + fn view(&self) -> yew::virtual_dom::VNode { + let link = self.props.weak_link.borrow().clone().unwrap(); + + html! { +
+ { self.render_error() } + +

{ self.value.get_title() }

+ + { self.value.render(&link) } + +
+ } + } +} + +impl, M: 'static + PartialEq + Clone> + PropertyGroupInputComponent +{ + /// Returns the error as `Html` + fn render_error(&self) -> Html { + html! { + <> + { + if self.error.is_some() { + html!{ +
+ { self.error.clone().unwrap().msg } +
+ } + } else { + html!{} + } + } + + } + } +} -- cgit v1.2.3