From c833bb1957f4fa23e6dd6cf9694c824d2b8ab601 Mon Sep 17 00:00:00 2001 From: Thomas Gideon Date: Wed, 3 Jun 2020 15:33:25 -0400 Subject: [PATCH] Standardize props --- examples/Cargo.toml | 19 ++++++++ examples/src/lib.rs | 52 ++++++++++++++++++++ src/breadcrumb/item.rs | 45 ++++++++++++++++- src/breadcrumb/mod.rs | 40 ++-------------- src/button/mod.rs | 26 +--------- src/card/body.rs | 33 ++----------- src/card/mod.rs | 62 ++---------------------- src/container.rs | 32 +++++++++---- src/input/group.rs | 31 ++---------- src/input/mod.rs | 39 +++++++++++++-- src/input/textarea.rs | 37 ++++++++++++-- src/jumbotron.rs | 17 ++----- src/prelude/border.rs | 21 +++----- src/prelude/border_color.rs | 22 --------- src/prelude/color.rs | 36 ++++++++++++++ src/prelude/mod.rs | 96 ++++++++++++++++++++++++++++++++++--- src/prelude/padding.rs | 2 +- 17 files changed, 358 insertions(+), 252 deletions(-) create mode 100644 examples/Cargo.toml create mode 100644 examples/src/lib.rs delete mode 100644 src/prelude/border_color.rs create mode 100644 src/prelude/color.rs diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 0000000..06b2361 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "examples" +version = "0.1.0" +authors = ["Thomas Gideon "] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +bootstrap-rs = { path = "../" } +yew = "~0.16.2" +log = "~0.4.8" +anyhow = "^1.0.28" +wasm-bindgen = "~0.2.61" +web-sys = "~0.3.38" +wasm-logger = "~0.2.0" +yew-router = "~0.13.0" +yew-components = "~0.1.2" diff --git a/examples/src/lib.rs b/examples/src/lib.rs new file mode 100644 index 0000000..7d6fec9 --- /dev/null +++ b/examples/src/lib.rs @@ -0,0 +1,52 @@ +use bootstrap_rs::{prelude::*, Breadcrumb, BreadcrumbItem, Container, Jumbotron}; +use yew::{html::Children, prelude::*}; + +pub struct Example { + props: Props, +} + +#[derive(Properties, Clone, PartialEq)] +pub struct Props { + #[prop_or_default] + pub children: Children, +} + +impl Component for Example { + type Properties = Props; + type Message = (); + + fn create(props: Self::Properties, _: ComponentLink) -> Self { + Self { props } + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + false + } + + fn change(&mut self, props: Self::Properties) -> ShouldRender { + render_on_change(&mut self.props, props) + } + + fn view(&self) -> Html { + html! { + + +

{ "Example!" }

+
+ + + { "Grandparent" } + + + { "Parent" } + + + { "Active" } + + +
+ } + } +} diff --git a/src/breadcrumb/item.rs b/src/breadcrumb/item.rs index cb11304..8eedfed 100644 --- a/src/breadcrumb/item.rs +++ b/src/breadcrumb/item.rs @@ -9,6 +9,22 @@ pub struct BreadcrumbItem { pub struct Props { pub active: bool, #[prop_or_default] + pub border: Option, + #[prop_or_default] + pub borders: Vec, + #[prop_or_default] + pub margin: Option, + #[prop_or_default] + pub margins: Vec, + #[prop_or_default] + pub padding: Option, + #[prop_or_default] + pub paddings: Vec, + #[prop_or_default] + pub class: String, + #[prop_or_default] + pub style: String, + #[prop_or_default] pub children: Children, } @@ -31,16 +47,41 @@ impl Component for BreadcrumbItem { fn view(&self) -> Html { if self.props.active { html! { -
  • { self.props.children.render() }
  • } } else { html! { -
  • { self.props.children.render() }
  • } } } } + +impl BreadcrumbItem { + fn classes(&self) -> String { + if self.props.active { + calculate_classes("breadcrumb-item active", (&self.props).into()) + } else { + calculate_classes("breadcrumb-item", (&self.props).into()) + } + } +} + +impl<'a> From<&'a Props> for BootstrapProps<'a> { + fn from(props: &Props) -> BootstrapProps { + let class = &props.class; + let borders = collect_bs(&props.border, &props.borders); + let margins = collect_bs(&props.margin, &props.margins); + let paddings = collect_bs(&props.padding, &props.paddings); + BootstrapProps { + class, + borders, + margins, + paddings, + } + } +} diff --git a/src/breadcrumb/mod.rs b/src/breadcrumb/mod.rs index 74c43e7..bfe8aee 100644 --- a/src/breadcrumb/mod.rs +++ b/src/breadcrumb/mod.rs @@ -2,34 +2,12 @@ mod item; use crate::prelude::*; pub use item::BreadcrumbItem; -use yew::{html::Children, prelude::*}; +use yew::prelude::*; pub struct Breadcrumb { props: Props, } -#[derive(Properties, Clone, PartialEq)] -pub struct Props { - #[prop_or_default] - pub border: Option, - #[prop_or_default] - pub borders: Vec, - #[prop_or_default] - pub border_color: Option, - #[prop_or_default] - pub border_colors: Vec, - #[prop_or_default] - pub margin: Option, - #[prop_or_default] - pub margins: Vec, - #[prop_or_default] - pub class: String, - #[prop_or_default] - pub style: String, - #[prop_or_default] - pub children: Children, -} - impl Component for Breadcrumb { type Properties = Props; type Message = (); @@ -47,9 +25,10 @@ impl Component for Breadcrumb { } fn view(&self) -> Html { + let class = calculate_classes("breadcrumb", (&self.props).into()); html! {