-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
main.rs
119 lines (109 loc) · 3.93 KB
/
main.rs
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
mod input;
use input::InputComponent;
use web_sys::HtmlInputElement;
use yew::prelude::*;
pub enum Msg {
HoverIndex(usize),
Submit,
}
pub struct App {
refs: Vec<NodeRef>,
focus_index: usize,
email_error: String,
password_error: String,
}
impl App {
fn apply_focus(&self) {
if let Some(input) = self.refs[self.focus_index].cast::<HtmlInputElement>() {
input.focus().unwrap();
}
}
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
Self {
focus_index: 0,
refs: vec![NodeRef::default(), NodeRef::default()],
email_error: "".to_string(),
password_error: "".to_string(),
}
}
fn rendered(&mut self, _ctx: &Context<Self>, first_render: bool) {
if first_render {
self.apply_focus();
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::HoverIndex(index) => {
self.focus_index = index;
self.apply_focus();
false
}
Msg::Submit => {
let email = &self.refs[0];
let password = &self.refs[1];
let email_value = email.cast::<HtmlInputElement>().unwrap().value();
let password_value = password.cast::<HtmlInputElement>().unwrap().value();
self.email_error.clear();
self.password_error.clear();
if !(email_value.contains('@') && email_value.contains('.')) {
self.email_error.push_str("Invalid email.")
}
if password_value.len() < 8 {
self.password_error
.push_str("Password must be at least 8 characters long.")
}
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<div class="main">
<div id="left-pane">
<div>
<h1>{"Create your account"}</h1>
<div class="input-container">
<label>{ "Email" }</label>
<input
type="text"
ref={&self.refs[0]}
class="input-element"
onmouseover={ctx.link().callback(|_| Msg::HoverIndex(0))}
placeholder="abcd@xyz.com"
/>
<div class="error">{self.email_error.clone()}</div>
</div>
<div class="input-container">
<label>{ "Password" }</label>
<InputComponent
ref={&self.refs[1]}
on_hover={ctx.link().callback(|_| Msg::HoverIndex(1))}
placeholder="password"
/>
<div class="error">{self.password_error.clone()}</div>
</div>
<button onclick={ctx.link().callback(|_| Msg::Submit)}>{"Create"}</button>
</div>
</div>
<div id="right-pane">
<div>
<div id="graphic"></div>
<h1>{ "Node Refs Example" }</h1>
<p>{ "Refs can be used to access and manipulate DOM elements directly" }</p>
<ul>
<li>{ "First input will focus on mount" }</li>
<li>{ "Each input will focus on hover" }</li>
</ul>
</div>
</div>
</div>
}
}
}
fn main() {
yew::Renderer::<App>::new().render();
}