use stylist::{css, StyleSource};
use wasm_bindgen_test::*;
use yew::prelude::*;
use yew::{utils, App};
pub struct DropdownItem {
link: ComponentLink<Self>,
props: Props,
}
#[derive(Properties, Clone, PartialEq)]
pub struct Props {
#[prop_or(Callback::noop())]
pub onclick_signal: Callback<MouseEvent>,
#[prop_or_default]
pub key: String,
#[prop_or_default]
pub class_name: String,
#[prop_or_default]
pub id: String,
#[prop_or(css!(""))]
pub styles: StyleSource<'static>,
pub children: Children,
}
pub enum Msg {
Clicked(MouseEvent),
}
impl Component for DropdownItem {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { link, props }
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Clicked(mouse_event) => {
self.props.onclick_signal.emit(mouse_event);
}
}
true
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
if self.props != props {
self.props = props;
return true;
}
false
}
fn view(&self) -> Html {
html! {
<li
class=classes!("dropdown-item", self.props.class_name.clone(), self.props.styles.clone())
id=self.props.id.clone()
key=self.props.key.clone()
onclick=self.link.callback(Msg::Clicked)
>{self.props.children.clone()}</li>
}
}
}
#[wasm_bindgen_test]
fn should_create_dropdown_item() {
let dropdown_item_props = Props {
onclick_signal: Callback::noop(),
key: String::from("dropdown-item-1"),
class_name: String::from("class-test"),
id: String::from("id-test"),
styles: css!("background-color: #918d94;"),
children: Children::new(vec![html! {
<div id="item">{"Item"}</div>
}]),
};
let dropdown_item: App<DropdownItem> = App::new();
dropdown_item.mount_with_props(
utils::document().get_element_by_id("output").unwrap(),
dropdown_item_props,
);
let content_element = utils::document().get_element_by_id("item").unwrap();
assert_eq!(content_element.text_content().unwrap(), "Item".to_string());
}