Browse Source

Refactor conditional assign and should render.

main
Thomas Gideon 2 years ago
parent
commit
d9ddde9436
  1. 2
      Cargo.toml
  2. 2
      src/alert/mod.rs
  3. 2
      src/breadcrumb/item.rs
  4. 2
      src/breadcrumb/mod.rs
  5. 4
      src/button/group.rs
  6. 4
      src/button/mod.rs
  7. 4
      src/button/toolbar.rs
  8. 2
      src/card/body.rs
  9. 2
      src/card/header.rs
  10. 2
      src/card/mod.rs
  11. 2
      src/card/text.rs
  12. 2
      src/container.rs
  13. 2
      src/form/mod.rs
  14. 2
      src/input/group.rs
  15. 2
      src/input/mod.rs
  16. 2
      src/input/textarea.rs
  17. 73
      src/prelude/mod.rs

2
Cargo.toml

@ -1,6 +1,6 @@
[package]
name = "bootstrap-rs"
version = "0.2.13"
version = "0.3.0"
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
edition = "2018"

2
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 {

2
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 {

2
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 {

4
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 {

4
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 {

4
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 {

2
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 {

2
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 {

2
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 {

2
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 {

2
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 {

2
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 {

2
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 {

2
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 {

2
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);
}

73
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<P: Properties + PartialEq>(
props_on_comp: &mut P,
props: P,
pub fn render_if_ne<T: PartialEq>(assign_to: &mut T, argument: T) -> ShouldRender {
render_if(assign_to, argument, identity, is_ne)
}
pub fn render_if_opt_str<S: AsRef<str>>(
assign_to: &mut Option<String>,
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<T, A>(
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<bool>) -> &'static str {
Some(false) => " is-invalid",
}
}
fn identity<T>(t: T) -> T {
t
}
fn opt_from_str<S: AsRef<str>>(a: S) -> Option<String> {
if a.as_ref().is_empty() {
None
} else {
Some(a.as_ref().to_owned())
}
}
fn is_ne<T: PartialEq>(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);
}
}

Loading…
Cancel
Save