Skip to content

Commit

Permalink
Add From<&[T]> for Classes (#1448)
Browse files Browse the repository at this point in the history
* Add From<&[T]> for Classes

* Round out the test cases for From<&[T]> for Classes
  • Loading branch information
ilyvion authored Jul 25, 2020
1 parent 07dcbb9 commit bfc7162
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
6 changes: 6 additions & 0 deletions yew/src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ impl<T: AsRef<str>> From<&Option<T>> for Classes {

impl<T: AsRef<str>> From<Vec<T>> for Classes {
fn from(t: Vec<T>) -> Self {
Classes::from(t.as_slice())
}
}

impl<T: AsRef<str>> From<&[T]> for Classes {
fn from(t: &[T]) -> Self {
let set = t
.iter()
.map(|x| x.as_ref())
Expand Down
44 changes: 41 additions & 3 deletions yew/src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,38 @@ mod tests {
}
}

#[test]
fn supports_multiple_classes_slice() {
let classes = ["class-1", "class-2"];
let a = html! {
<div class=&classes[..]></div>
};

if let VNode::VTag(vtag) = a {
assert!(get_class_str(&vtag).contains("class-1"));
assert!(get_class_str(&vtag).contains("class-2"));
assert!(!get_class_str(&vtag).contains("class-3"));
} else {
panic!("vtag expected");
}
}

#[test]
fn supports_multiple_classes_one_value_slice() {
let classes = ["class-1 class-2", "class-1"];
let a = html! {
<div class=&classes[..]></div>
};

if let VNode::VTag(vtag) = a {
assert!(get_class_str(&vtag).contains("class-1"));
assert!(get_class_str(&vtag).contains("class-2"));
assert!(!get_class_str(&vtag).contains("class-3"));
} else {
panic!("vtag expected");
}
}

#[test]
fn supports_multiple_classes_vec() {
let mut classes = vec!["class-1"];
Expand Down Expand Up @@ -784,12 +816,12 @@ mod tests {
}

#[test]
fn filter_empty_string_classes_vec() {
let mut classes = vec![""];
classes.push("class-2");
fn filter_empty_string_classes() {
let a = html! { <div class=vec![""]></div> };
let b = html! { <div class=("")></div> };
let c = html! { <div class=""></div> };
let d_arr = [""];
let d = html! { <div class=&d_arr[..]></div> };

if let VNode::VTag(vtag) = a {
assert!(!vtag.attributes.contains_key("class"));
Expand All @@ -808,6 +840,12 @@ mod tests {
} else {
panic!("vtag expected");
}

if let VNode::VTag(vtag) = d {
assert!(!vtag.attributes.contains_key("class"));
} else {
panic!("vtag expected");
}
}

fn assert_vtag(node: &mut VNode) -> &mut VTag {
Expand Down

0 comments on commit bfc7162

Please sign in to comment.