Skip to content

Commit

Permalink
feat: support arrays for Classes/classes!() (#3393)
Browse files Browse the repository at this point in the history
* feat: support arrays for Classes/classes!()

* style: update
  • Loading branch information
pouriya authored Sep 23, 2023
1 parent 7706bcf commit 2cbe6ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/yew-macro/tests/classes_macro/classes-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ fn compile_pass() {
// single expression
::yew::classes!(::std::vec!["one", "two"]);

// single array
::yew::classes!(["one", "two"]);
// multiple arrays
::yew::classes!(["one"], ["two"]);

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

Expand Down
6 changes: 6 additions & 0 deletions packages/yew/src/html/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
fn eq(&self, other: &Self) -> bool {
self.set.len() == other.set.len() && self.set.iter().eq(other.set.iter())
Expand Down
4 changes: 1 addition & 3 deletions website/docs/concepts/html/classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ html! {
```rust
use yew::{classes, html};

let my_classes = ["class-1", "class-2"];

html! {
<div class={classes!(my_classes.as_ref())}></div>
<div class={classes!(["class-1", "class-2"])}></div>
};
```

Expand Down

0 comments on commit 2cbe6ce

Please sign in to comment.