diff --git a/Cargo.toml b/Cargo.toml index 8a5e3ce..6643865 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bootstrap-rs" -version = "0.2.8" +version = "0.2.9" authors = ["Thomas Gideon "] edition = "2018" diff --git a/src/input/props.rs b/src/input/props.rs index 3d02947..2f72e5a 100644 --- a/src/input/props.rs +++ b/src/input/props.rs @@ -1,7 +1,7 @@ use super::InputType; use crate::{ prelude::*, - props::{collect_props, BootstrapProps}, + props::{add_opt_attr, collect_props, BootstrapProps}, }; use std::collections::HashMap; use yew::{html::Children, prelude::*}; @@ -43,6 +43,10 @@ pub struct Props { #[prop_or_default] pub style: Option, #[prop_or_default] + pub aria_label: Option, + #[prop_or_default] + pub aria_describedby: Option, + #[prop_or_default] pub children: Children, } @@ -53,15 +57,11 @@ impl<'a> From<&'a Props> for BootstrapProps<'a> { let margins = collect_props(&props.margin, &props.margins); let paddings = collect_props(&props.padding, &props.paddings); let mut attributes = HashMap::new(); - if let Some(ref style) = props.style { - attributes.insert("style", style); - } - if let Some(ref id) = props.id { - attributes.insert("id", id); - } - if let Some(ref name) = props.name { - attributes.insert("name", name); - } + add_opt_attr(&mut attributes, "id", &props.id); + add_opt_attr(&mut attributes, "name", &props.name); + add_opt_attr(&mut attributes, "style", &props.style); + add_opt_attr(&mut attributes, "aria-label", &props.aria_label); + add_opt_attr(&mut attributes, "aria-describedby", &props.aria_describedby); BootstrapProps { class, borders, diff --git a/src/props.rs b/src/props.rs index 81fb440..90f7cc7 100644 --- a/src/props.rs +++ b/src/props.rs @@ -54,18 +54,10 @@ impl<'a> From<&'a Props> for BootstrapProps<'a> { let margins = collect_props(&props.margin, &props.margins); let paddings = collect_props(&props.padding, &props.paddings); let mut attributes = HashMap::new(); - if let Some(ref id) = props.id { - attributes.insert("id", id); - } - if let Some(ref aria_label) = props.aria_label { - attributes.insert("aria-label", aria_label); - } - if let Some(ref role) = props.role { - attributes.insert("role", role); - } - if let Some(ref style) = props.style { - attributes.insert("style", style); - } + add_opt_attr(&mut attributes, "id", &props.id); + add_opt_attr(&mut attributes, "aria-label", &props.aria_label); + add_opt_attr(&mut attributes, "role", &props.role); + add_opt_attr(&mut attributes, "style", &props.style); BootstrapProps { class, borders, @@ -119,6 +111,16 @@ fn into_classnames(c: &[&C]) -> Classes { }) } +pub(crate) fn add_opt_attr<'a>( + attrs: &mut HashMap<&str, &'a String>, + name: &'static str, + opt: &'a Option, +) { + if let Some(val) = opt.as_ref() { + attrs.insert(name, val); + } +} + #[cfg(test)] mod test { use super::*; @@ -139,4 +141,21 @@ mod test { crate::render::render_with_prefix(&props, "", html! {
}) ); } + + #[test] + fn test_opt_attr() { + let props = Props { + aria_label: Some("some-label".into()), + ..Props::default() + }; + + let expected = html! { +
+ }; + + assert_eq!( + expected, + crate::render::render_with_prefix(&props, "", html! {
}) + ); + } }