Add aria-describedby to input.

This commit is contained in:
Thomas Gideon 2020-06-24 12:57:00 -04:00
parent 271713ded1
commit 9d76d16d7e
3 changed files with 42 additions and 23 deletions

View File

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

View File

@ -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<String>,
#[prop_or_default]
pub aria_label: Option<String>,
#[prop_or_default]
pub aria_describedby: Option<String>,
#[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,

View File

@ -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: IntoBsClass>(c: &[&C]) -> Classes {
})
}
pub(crate) fn add_opt_attr<'a>(
attrs: &mut HashMap<&str, &'a String>,
name: &'static str,
opt: &'a Option<String>,
) {
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! { <div aria-label="test" /> })
);
}
#[test]
fn test_opt_attr() {
let props = Props {
aria_label: Some("some-label".into()),
..Props::default()
};
let expected = html! {
<div class="" aria-label="some-label" />
};
assert_eq!(
expected,
crate::render::render_with_prefix(&props, "", html! { <div /> })
);
}
}