1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::styles::{get_palette, get_size, get_style, Palette, Position, Size, Style};
use wasm_bindgen_test::*;
use yew::prelude::*;
use yew::{utils, App};
pub struct Tooltip {
props: Props,
link: ComponentLink<Self>,
show_tooltip: bool,
}
#[derive(Clone, Properties, PartialEq)]
pub struct Props {
#[prop_or(Palette::Standard)]
pub tooltip_palette: Palette,
#[prop_or(Style::Regular)]
pub tooltip_style: Style,
#[prop_or(Size::Medium)]
pub tooltip_size: Size,
#[prop_or_default]
pub key: String,
#[prop_or_default]
pub code_ref: NodeRef,
#[prop_or_default]
pub class_name: String,
#[prop_or_default]
pub id: String,
pub tooltip_position: Position,
pub content: Html,
pub children: Children,
}
pub enum Msg {
TargetOver,
TargetLeave,
}
impl Component for Tooltip {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self {
props,
link,
show_tooltip: false,
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::TargetOver => self.show_tooltip = true,
Msg::TargetLeave => self.show_tooltip = false,
};
true
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
if self.props != props {
self.props = props;
return true;
}
false
}
fn view(&self) -> Html {
let tooltip = html! {
<div
id=self.props.id.clone()
key=self.props.key.clone()
ref=self.props.code_ref.clone()
class=classes!(
"tooltip",
get_position(self.props.tooltip_position.clone()),
get_palette(self.props.tooltip_palette.clone()),
get_style(self.props.tooltip_style.clone()),
get_size(self.props.tooltip_size.clone()),
self.props.class_name.clone()
)
>
{self.props.content.clone()}
</div>
};
html! {
<div class="tooltip-container"
onmouseover = self.link.callback(|_| Msg::TargetOver)
onmouseleave = self.link.callback(|_| Msg::TargetLeave)
>
{if self.show_tooltip {
tooltip
}else {
html!{}
}}
{self.props.children.clone()}
</div>
}
}
}
fn get_position(position: Position) -> String {
match position {
Position::Left => String::from("left"),
Position::Right => String::from("right"),
Position::Above => String::from("above"),
Position::Below => String::from("below"),
}
}
#[wasm_bindgen_test]
fn should_create_tooltip() {
let tooltip_props = Props {
tooltip_palette: Palette::Clean,
tooltip_style: Style::Regular,
tooltip_size: Size::Medium,
tooltip_position: Position::Above,
content: html! {<p>{"tooltip"}</p>},
code_ref: NodeRef::default(),
key: String::from("dropdown-1"),
class_name: String::from("class-test"),
id: String::from("id-test"),
children: Children::new(vec![html! {<div id="result">{"result"}</div>}]),
};
let tooltip: App<Tooltip> = App::new();
tooltip.mount_with_props(
utils::document().get_element_by_id("output").unwrap(),
tooltip_props,
);
let tooltip_element = utils::document()
.get_elements_by_class_name("tooltip-container")
.get_with_index(0)
.unwrap();
let child = tooltip_element.first_element_child().unwrap();
assert_eq!(child.id(), "result".to_string());
}