Refactor conditional assign and should render.
This commit is contained in:
parent
4a2087a94e
commit
d9ddde9436
17 changed files with 85 additions and 26 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "bootstrap-rs"
|
||||
version = "0.2.13"
|
||||
version = "0.3.0"
|
||||
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
|
||||
edition = "2018"
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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…
Reference in a new issue