feat: support arrays for Classes/classes!() (#3393)

* feat: support arrays for Classes/classes!()

* style: update
This commit is contained in:
Pouriya 2023-09-23 16:08:59 +03:30 committed by GitHub
parent 7706bcf3a3
commit 2cbe6cee8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 4 deletions

View File

@ -49,6 +49,11 @@ fn compile_pass() {
// single expression // single expression
::yew::classes!(::std::vec!["one", "two"]); ::yew::classes!(::std::vec!["one", "two"]);
// single array
::yew::classes!(["one", "two"]);
// multiple arrays
::yew::classes!(["one"], ["two"]);
// optional classes // optional classes
::yew::classes!( ::yew::classes!(
::std::option::Option::Some("one"), ::std::option::Option::Some("one"),
@ -58,7 +63,7 @@ fn compile_pass() {
// mixed types // mixed types
{ {
use ::std::borrow::ToOwned; use ::std::borrow::ToOwned;
::yew::classes!("one".to_owned(), "two", ::std::vec!["three"]); ::yew::classes!("one".to_owned(), "two", ::std::vec!["three"], ["four", "five"]);
} }
} }

View File

@ -274,6 +274,12 @@ impl<T: Into<Classes> + Clone> From<&[T]> for Classes {
} }
} }
impl<T: Into<Classes>, const SIZE: usize> From<[T; SIZE]> for Classes {
fn from(t: [T; SIZE]) -> Self {
t.into_iter().collect()
}
}
impl PartialEq for Classes { impl PartialEq for Classes {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.set.len() == other.set.len() && self.set.iter().eq(other.set.iter()) self.set.len() == other.set.len() && self.set.iter().eq(other.set.iter())

View File

@ -84,10 +84,8 @@ html! {
```rust ```rust
use yew::{classes, html}; use yew::{classes, html};
let my_classes = ["class-1", "class-2"];
html! { html! {
<div class={classes!(my_classes.as_ref())}></div> <div class={classes!(["class-1", "class-2"])}></div>
}; };
``` ```