diff --git a/Cargo.toml b/Cargo.toml index 04695dd..c2883d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bootstrap-rs" -version = "0.2.13" +version = "0.3.0" authors = ["Thomas Gideon "] edition = "2018" diff --git a/src/alert/mod.rs b/src/alert/mod.rs index 5d4f3ef..64e8412 100644 --- a/src/alert/mod.rs +++ b/src/alert/mod.rs @@ -27,7 +27,7 @@ impl Component for Alert { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/breadcrumb/item.rs b/src/breadcrumb/item.rs index 45432f4..3d0b1d0 100644 --- a/src/breadcrumb/item.rs +++ b/src/breadcrumb/item.rs @@ -49,7 +49,7 @@ impl Component for BreadcrumbItem { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/breadcrumb/mod.rs b/src/breadcrumb/mod.rs index fd297a9..55ffa58 100644 --- a/src/breadcrumb/mod.rs +++ b/src/breadcrumb/mod.rs @@ -21,7 +21,7 @@ impl Component for Breadcrumb { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/button/group.rs b/src/button/group.rs index cfbee20..ae46e97 100644 --- a/src/button/group.rs +++ b/src/button/group.rs @@ -1,4 +1,4 @@ -use crate::{prelude::render_on_change, props::Props, render}; +use crate::{prelude::*, props::Props, render}; use yew::prelude::*; pub struct ButtonGroup { @@ -18,7 +18,7 @@ impl Component for ButtonGroup { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/button/mod.rs b/src/button/mod.rs index c8ff058..2444c9f 100644 --- a/src/button/mod.rs +++ b/src/button/mod.rs @@ -4,7 +4,7 @@ mod toolbar; use self::props::Props; pub use self::{group::ButtonGroup, toolbar::ButtonToolbar}; -use crate::{prelude::render_on_change, render}; +use crate::{prelude::*, render}; use std::fmt::{Display, Formatter, Result}; use yew::prelude::*; @@ -51,7 +51,7 @@ impl Component for Button { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/button/toolbar.rs b/src/button/toolbar.rs index 009e663..5cee5ab 100644 --- a/src/button/toolbar.rs +++ b/src/button/toolbar.rs @@ -1,4 +1,4 @@ -use crate::{prelude::render_on_change, props::Props, render}; +use crate::{prelude::*, props::Props, render}; use yew::prelude::*; pub struct ButtonToolbar { @@ -18,7 +18,7 @@ impl Component for ButtonToolbar { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/card/body.rs b/src/card/body.rs index 5d32aac..7e3e16d 100644 --- a/src/card/body.rs +++ b/src/card/body.rs @@ -18,7 +18,7 @@ impl Component for CardBody { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/card/header.rs b/src/card/header.rs index 3b22afd..02f2281 100644 --- a/src/card/header.rs +++ b/src/card/header.rs @@ -18,7 +18,7 @@ impl Component for CardHeader { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/card/mod.rs b/src/card/mod.rs index b3c9808..afd1afa 100644 --- a/src/card/mod.rs +++ b/src/card/mod.rs @@ -23,7 +23,7 @@ impl Component for Card { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/card/text.rs b/src/card/text.rs index 5be721b..536a2c7 100644 --- a/src/card/text.rs +++ b/src/card/text.rs @@ -18,7 +18,7 @@ impl Component for CardText { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/container.rs b/src/container.rs index 338d801..58954b5 100644 --- a/src/container.rs +++ b/src/container.rs @@ -18,7 +18,7 @@ impl Component for Container { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/form/mod.rs b/src/form/mod.rs index f030572..6e822e0 100644 --- a/src/form/mod.rs +++ b/src/form/mod.rs @@ -18,7 +18,7 @@ impl Component for FormGroup { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/input/group.rs b/src/input/group.rs index ca50dc4..e5a6e31 100644 --- a/src/input/group.rs +++ b/src/input/group.rs @@ -18,7 +18,7 @@ impl Component for InputGroup { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/input/mod.rs b/src/input/mod.rs index b392da5..31a57ea 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -61,7 +61,7 @@ impl Component for Input { if self.props.value != props.value { self.state = props.value.clone(); } - render_on_change(&mut self.props, props) + render_if_ne(&mut self.props, props) } fn view(&self) -> Html { diff --git a/src/input/textarea.rs b/src/input/textarea.rs index 7329912..06de6a8 100644 --- a/src/input/textarea.rs +++ b/src/input/textarea.rs @@ -31,7 +31,7 @@ impl Component for TextArea { } fn change(&mut self, props: Self::Properties) -> ShouldRender { - let should_render = render_on_change(&mut self.props, props); + let should_render = render_if_ne(&mut self.props, props); if should_render { self.state = extract_state(&self.props); } diff --git a/src/prelude/mod.rs b/src/prelude/mod.rs index 29c8005..2e8731b 100644 --- a/src/prelude/mod.rs +++ b/src/prelude/mod.rs @@ -7,15 +7,29 @@ mod padding; pub use self::{border::Border, color::Color, edge::Edge, margin::Margin, padding::Padding}; use yew::prelude::*; -pub fn render_on_change( - props_on_comp: &mut P, - props: P, +pub fn render_if_ne(assign_to: &mut T, argument: T) -> ShouldRender { + render_if(assign_to, argument, identity, is_ne) +} + +pub fn render_if_opt_str>( + assign_to: &mut Option, + argument: S, ) -> ShouldRender { - if props_on_comp == &props { - false - } else { - *props_on_comp = props; + render_if(assign_to, argument, opt_from_str, is_ne) +} + +pub fn render_if( + assign_to: &mut T, + argument: A, + map: impl Fn(A) -> T, + assign_render: impl Fn(&mut T, &T) -> bool, +) -> ShouldRender { + let argument = map(argument); + if assign_render(assign_to, &argument) { + *assign_to = argument; true + } else { + false } } @@ -26,3 +40,48 @@ pub fn valid_as_class(v: &Option) -> &'static str { Some(false) => " is-invalid", } } + +fn identity(t: T) -> T { + t +} + +fn opt_from_str>(a: S) -> Option { + if a.as_ref().is_empty() { + None + } else { + Some(a.as_ref().to_owned()) + } +} + +fn is_ne(t: &mut T, a: &T) -> bool { + t != a +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn render_true() { + let mut t = String::from("foo"); + let should = render_if_ne(&mut t, String::from("bar")); + assert!(should, "Should have determined to render"); + assert_eq!(t, String::from("bar")); + } + + #[test] + fn render_false() { + let mut t = String::from("foo"); + let should = render_if_ne(&mut t, String::from("foo")); + assert!(!should, "Should have determined to render"); + assert_eq!(t, String::from("foo")); + } + + #[test] + fn render_if_opt() { + let mut t = Some(String::from("test")); + let should = render_if_opt_str(&mut t, String::default()); + assert!(should, "Should have determined to render"); + assert_eq!(t, None); + } +}