diff options
author | jelemux <jeremias.weber@protonmail.com> | 2021-02-17 17:06:48 +0100 |
---|---|---|
committer | jelemux <jeremias.weber@protonmail.com> | 2021-02-17 17:06:48 +0100 |
commit | cb310a66e94db4e0c4f7d0373a670156b012412a (patch) | |
tree | 501fd4ed17d892d693d4123fffaca8d942144e3b /src/view/property_group.rs | |
parent | be3367dd2921eb30ae7970d233b83ac3af861952 (diff) | |
parent | 0660151a8b641fa0a23dde2598132029970f7ae4 (diff) | |
download | wasm-card-cb310a66e94db4e0c4f7d0373a670156b012412a.tar.gz wasm-card-cb310a66e94db4e0c4f7d0373a670156b012412a.tar.bz2 |
Merge branch 'main' of codeberg.org:jelemux/wasm-card
Diffstat (limited to 'src/view/property_group.rs')
-rw-r--r-- | src/view/property_group.rs | 81 |
1 files changed, 81 insertions, 0 deletions
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<M> + Clone, + M: 'static + PartialEq + Clone, +> { + pub generated: Callback<O>, + pub weak_link: WeakComponentLink<PropertyGroupInputComponent<O, M>>, +} + +#[derive(Clone, PartialEq)] +pub struct PropertyGroupInputComponent< + O: 'static + VCardPropertyInputObject<M>, + M: 'static + PartialEq + Clone, +> { + pub props: InputProps<O, M>, + pub value: O, + pub error: Option<Error>, +} + +impl<O: 'static + VCardPropertyInputObject<M>, M: 'static + PartialEq + Clone> Component + for PropertyGroupInputComponent<O, M> +{ + type Message = M; + type Properties = InputProps<O, M>; + fn create(props: <Self as yew::Component>::Properties, link: yew::html::Scope<Self>) -> Self { + props.weak_link.borrow_mut().replace(link); + Self { + props, + value: O::new(), + error: None, + } + } + fn update(&mut self, msg: <Self as yew::Component>::Message) -> bool { + self.value.update(self.props.clone(), msg) + } + fn change(&mut self, props: <Self as yew::Component>::Properties) -> bool { + self.props.neq_assign(props) + } + fn view(&self) -> yew::virtual_dom::VNode { + let link = self.props.weak_link.borrow().clone().unwrap(); + + html! { + <div class="box"> + { self.render_error() } + + <h3 class="subtitle">{ self.value.get_title() }</h3> + + { self.value.render(&link) } + + </div> + } + } +} + +impl<O: VCardPropertyInputObject<M>, M: 'static + PartialEq + Clone> + PropertyGroupInputComponent<O, M> +{ + /// Returns the error as `Html` + fn render_error(&self) -> Html { + html! { + <> + { + if self.error.is_some() { + html!{ + <div class="notification is-danger is-light"> + { self.error.clone().unwrap().msg } + </div> + } + } else { + html!{} + } + } + </> + } + } +} |