Add aria-describedby to input.
This commit is contained in:
parent
271713ded1
commit
9d76d16d7e
3 changed files with 42 additions and 23 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "bootstrap-rs"
|
name = "bootstrap-rs"
|
||||||
version = "0.2.8"
|
version = "0.2.9"
|
||||||
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
|
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use super::InputType;
|
use super::InputType;
|
||||||
use crate::{
|
use crate::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
props::{collect_props, BootstrapProps},
|
props::{add_opt_attr, collect_props, BootstrapProps},
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use yew::{html::Children, prelude::*};
|
use yew::{html::Children, prelude::*};
|
||||||
|
@ -43,6 +43,10 @@ pub struct Props {
|
||||||
#[prop_or_default]
|
#[prop_or_default]
|
||||||
pub style: Option<String>,
|
pub style: Option<String>,
|
||||||
#[prop_or_default]
|
#[prop_or_default]
|
||||||
|
pub aria_label: Option<String>,
|
||||||
|
#[prop_or_default]
|
||||||
|
pub aria_describedby: Option<String>,
|
||||||
|
#[prop_or_default]
|
||||||
pub children: Children,
|
pub children: Children,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,15 +57,11 @@ impl<'a> From<&'a Props> for BootstrapProps<'a> {
|
||||||
let margins = collect_props(&props.margin, &props.margins);
|
let margins = collect_props(&props.margin, &props.margins);
|
||||||
let paddings = collect_props(&props.padding, &props.paddings);
|
let paddings = collect_props(&props.padding, &props.paddings);
|
||||||
let mut attributes = HashMap::new();
|
let mut attributes = HashMap::new();
|
||||||
if let Some(ref style) = props.style {
|
add_opt_attr(&mut attributes, "id", &props.id);
|
||||||
attributes.insert("style", style);
|
add_opt_attr(&mut attributes, "name", &props.name);
|
||||||
}
|
add_opt_attr(&mut attributes, "style", &props.style);
|
||||||
if let Some(ref id) = props.id {
|
add_opt_attr(&mut attributes, "aria-label", &props.aria_label);
|
||||||
attributes.insert("id", id);
|
add_opt_attr(&mut attributes, "aria-describedby", &props.aria_describedby);
|
||||||
}
|
|
||||||
if let Some(ref name) = props.name {
|
|
||||||
attributes.insert("name", name);
|
|
||||||
}
|
|
||||||
BootstrapProps {
|
BootstrapProps {
|
||||||
class,
|
class,
|
||||||
borders,
|
borders,
|
||||||
|
|
43
src/props.rs
43
src/props.rs
|
@ -54,18 +54,10 @@ impl<'a> From<&'a Props> for BootstrapProps<'a> {
|
||||||
let margins = collect_props(&props.margin, &props.margins);
|
let margins = collect_props(&props.margin, &props.margins);
|
||||||
let paddings = collect_props(&props.padding, &props.paddings);
|
let paddings = collect_props(&props.padding, &props.paddings);
|
||||||
let mut attributes = HashMap::new();
|
let mut attributes = HashMap::new();
|
||||||
if let Some(ref id) = props.id {
|
add_opt_attr(&mut attributes, "id", &props.id);
|
||||||
attributes.insert("id", id);
|
add_opt_attr(&mut attributes, "aria-label", &props.aria_label);
|
||||||
}
|
add_opt_attr(&mut attributes, "role", &props.role);
|
||||||
if let Some(ref aria_label) = props.aria_label {
|
add_opt_attr(&mut attributes, "style", &props.style);
|
||||||
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);
|
|
||||||
}
|
|
||||||
BootstrapProps {
|
BootstrapProps {
|
||||||
class,
|
class,
|
||||||
borders,
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -139,4 +141,21 @@ mod test {
|
||||||
crate::render::render_with_prefix(&props, "", html! { <div aria-label="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 /> })
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue