yew_styles/components/dropdown/
dropdown_container.rs1use crate::styles::{get_palette, get_size, get_style, Palette, Size, Style};
2use stylist::{css, StyleSource};
3use wasm_bindgen_test::*;
4use yew::prelude::*;
5use yew::{utils, App};
6
7pub struct Dropdown {
73 props: Props,
74 active: bool,
75 link: ComponentLink<Self>,
76}
77
78#[derive(Clone, Properties, PartialEq)]
79pub struct Props {
80 pub main_content: Html,
82 #[prop_or(Palette::Standard)]
84 pub dropdown_palette: Palette,
85 #[prop_or(Style::Regular)]
87 pub dropdown_style: Style,
88 #[prop_or(Size::Medium)]
90 pub dropdown_size: Size,
91 #[prop_or_default]
93 pub key: String,
94 #[prop_or_default]
96 pub class_name: String,
97 #[prop_or_default]
99 pub id: String,
100 #[prop_or(css!(""))]
102 pub styles: StyleSource<'static>,
103 pub children: Children,
104}
105
106pub enum Msg {
107 ShowDropdown,
108}
109
110impl Component for Dropdown {
111 type Message = Msg;
112 type Properties = Props;
113
114 fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
115 Self {
116 props,
117 link,
118 active: false,
119 }
120 }
121
122 fn update(&mut self, msg: Self::Message) -> ShouldRender {
123 match msg {
124 Msg::ShowDropdown => {
125 self.active = !self.active;
126 }
127 }
128 true
129 }
130
131 fn change(&mut self, props: Self::Properties) -> ShouldRender {
132 if self.props != props {
133 return true;
134 }
135 false
136 }
137
138 fn view(&self) -> Html {
139 html! {
140 <div
141 class=classes!("dropdown", self.props.class_name.clone(), get_style(self.props.dropdown_style.clone()), get_palette(self.props.dropdown_palette.clone()), get_size(self.props.dropdown_size.clone()), self.props.styles.clone())
142 id=self.props.id.clone()
143 key=self.props.key.clone()
144 onclick=self.link.callback(|_| Msg::ShowDropdown)
145 >
146 <div class="main-content">{self.props.main_content.clone()}</div>
147 {get_items(self.active, self.props.children.clone())}
148 </div>
149 }
150 }
151}
152
153fn get_items(active: bool, children: Children) -> Html {
154 if active {
155 html! {
156 <ul>
157 {children.clone()}
158 </ul>
159 }
160 } else {
161 html! {}
162 }
163}
164
165wasm_bindgen_test_configure!(run_in_browser);
166
167#[wasm_bindgen_test]
168fn should_create_dropdown_container() {
169 let dropdown_container_props = Props {
170 main_content: html! {<div id="test">{"test"}</div>},
171 dropdown_palette: Palette::Clean,
172 dropdown_size: Size::Medium,
173 dropdown_style: Style::Outline,
174 key: String::from("dropdown-1"),
175 class_name: String::from("class-test"),
176 id: String::from("id-test"),
177 styles: css!("background-color: #918d94;"),
178 children: Children::new(vec![html! {
179 <div id="item">{"Item"}</div>
180 }]),
181 };
182
183 let dropdown_container: App<Dropdown> = App::new();
184
185 dropdown_container.mount_with_props(
186 utils::document().get_element_by_id("output").unwrap(),
187 dropdown_container_props,
188 );
189
190 let content_element = utils::document().get_element_by_id("test").unwrap();
191 assert_eq!(content_element.text_content().unwrap(), "test".to_string());
192}