From 6a6391319ee27c5b70f1438a1633b98aeee5743e Mon Sep 17 00:00:00 2001 From: Thomas Gideon Date: Sat, 20 Jun 2020 12:03:49 -0400 Subject: [PATCH] Add alert component. --- Cargo.toml | 2 +- src/alert/convert.rs | 17 ++++++++++ src/alert/mod.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/alert/convert.rs create mode 100644 src/alert/mod.rs diff --git a/Cargo.toml b/Cargo.toml index ac4dccc..8b268d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bootstrap-rs" -version = "0.2.4" +version = "0.2.5" authors = ["Thomas Gideon "] edition = "2018" diff --git a/src/alert/convert.rs b/src/alert/convert.rs new file mode 100644 index 0000000..0c7bdf7 --- /dev/null +++ b/src/alert/convert.rs @@ -0,0 +1,17 @@ +use super::Props; +use crate::prelude::*; + +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/alert/mod.rs b/src/alert/mod.rs new file mode 100644 index 0000000..46c1349 --- /dev/null +++ b/src/alert/mod.rs @@ -0,0 +1,74 @@ +mod convert; + +use crate::prelude::*; +use yew::{html::Children, prelude::*}; + +pub struct Alert { + link: ComponentLink, + props: Props, +} + +#[derive(Properties, Default, Clone, PartialEq)] +pub struct Props { + pub on_close: Callback<()>, + pub color: Color, + #[prop_or_default] + pub aria_label: String, + #[prop_or_default] + pub role: String, + #[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, +} + +impl Component for Alert { + type Properties = Props; + type Message = (); + + fn create(props: Self::Properties, link: ComponentLink) -> Self { + Self { link, props } + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + self.props.on_close.emit(()); + true + } + + fn change(&mut self, props: Self::Properties) -> ShouldRender { + render_on_change(&mut self.props, props) + } + + fn view(&self) -> Html { + let color_class = self.props.color.with_prefix("alert-"); + let class = calculate_classes(format!("alert {}", color_class), (&self.props).into()); + html! { +
+ { self.props.children.render() } + +
+ } + } +} diff --git a/src/lib.rs b/src/lib.rs index 59f4c5a..cfe2e61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod alert; mod breadcrumb; mod button; mod card; @@ -8,6 +9,7 @@ mod jumbotron; pub mod prelude; pub use self::{ + alert::Alert, breadcrumb::{Breadcrumb, BreadcrumbItem}, button::ButtonGroup, card::{Card, CardBody, CardHeader, CardText},