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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use crate::{
InputState, ValidatingComponent, ValidatingComponentProperties, ValidationContext, Validator,
};
use web_sys::HtmlInputElement;
use yew::prelude::*;
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum TextInputIcon {
None,
Calendar,
Clock,
Search,
Custom,
}
impl Default for TextInputIcon {
fn default() -> Self {
Self::None
}
}
#[derive(Clone, PartialEq, Properties)]
pub struct TextInputProps {
#[prop_or_default]
pub name: String,
#[prop_or_default]
pub id: String,
#[prop_or_default]
pub value: String,
#[prop_or_default]
pub required: bool,
#[prop_or_default]
pub disabled: bool,
#[prop_or_default]
pub readonly: bool,
#[prop_or_default]
pub state: InputState,
#[prop_or_default]
pub icon: TextInputIcon,
#[prop_or("text".into())]
pub r#type: String,
#[prop_or_default]
pub placeholder: String,
#[prop_or_default]
pub autofocus: bool,
#[prop_or_default]
pub form: Option<String>,
#[prop_or_default]
pub onchange: Callback<String>,
#[prop_or_default]
pub oninput: Callback<String>,
#[prop_or_default]
pub onvalidate: Callback<ValidationContext<String>>,
#[prop_or_default]
pub validator: Validator<String, InputState>,
}
impl ValidatingComponent for TextInput {
type Value = String;
}
impl ValidatingComponentProperties<String> for TextInputProps {
fn set_onvalidate(&mut self, onvalidate: Callback<ValidationContext<String>>) {
self.onvalidate = onvalidate;
}
fn set_input_state(&mut self, state: InputState) {
self.state = state;
}
}
pub struct TextInput {
value: Option<String>,
refs: Refs,
}
#[derive(Default)]
struct Refs {
input: NodeRef,
}
pub enum TextInputMsg {
Init,
Changed(String),
Input(String),
}
impl Component for TextInput {
type Message = TextInputMsg;
type Properties = TextInputProps;
fn create(ctx: &Context<Self>) -> Self {
ctx.link().send_message(Self::Message::Init);
Self {
value: None,
refs: Default::default(),
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
TextInputMsg::Init => {
ctx.props().onvalidate.emit(ValidationContext {
value: self.value(ctx),
initial: true,
});
}
TextInputMsg::Changed(data) => {
self.value = Some(data.clone());
ctx.props().onchange.emit(data.clone());
ctx.props().onvalidate.emit(data.into());
}
TextInputMsg::Input(data) => {
ctx.props().oninput.emit(data);
if let Some(value) = self.extract_value() {
self.value = Some(value.clone());
ctx.props().onchange.emit(value.clone());
ctx.props().onvalidate.emit(value.into());
}
return ctx.props().validator.is_custom();
}
}
true
}
fn changed(&mut self, ctx: &Context<Self>) -> bool {
if ctx.props().readonly {
self.value = None;
}
true
}
fn view(&self, ctx: &Context<Self>) -> Html {
let mut classes = Classes::from("pf-c-form-control");
match ctx.props().icon {
TextInputIcon::None => {}
TextInputIcon::Search => classes.push("pf-m-search"),
TextInputIcon::Calendar => classes.extend(vec!["pf-m-icon", "pf-m-calendar"]),
TextInputIcon::Clock => classes.extend(vec!["pf-m-icon", "pf-m-clock"]),
TextInputIcon::Custom => classes.extend(vec!["pf-m-icon"]),
};
let (classes, aria_invalid) = self.input_state(ctx).convert(classes);
let input_ref = self.refs.input.clone();
let onchange = ctx.link().batch_callback(move |_| {
input_ref
.cast::<HtmlInputElement>()
.map(|input| TextInputMsg::Changed(input.value()))
});
let oninput = ctx
.link()
.callback(|evt: InputEvent| TextInputMsg::Input(evt.data().unwrap_or_default()));
let value = self.value(ctx);
html! {
<input
ref={self.refs.input.clone()}
class={classes}
type={ctx.props().r#type.clone()}
name={ctx.props().name.clone()}
id={ctx.props().id.clone()}
required={ctx.props().required}
disabled={ctx.props().disabled}
readonly={ctx.props().readonly}
aria-invalid={aria_invalid.to_string()}
value={value}
placeholder={ctx.props().placeholder.clone()}
form={ctx.props().form.clone()}
onchange={onchange}
oninput={oninput}
/>
}
}
fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
if first_render && ctx.props().autofocus {
self.focus();
}
}
}
impl TextInput {
fn extract_value(&self) -> Option<String> {
self.refs
.input
.cast::<HtmlInputElement>()
.map(|input| input.value())
}
fn value(&self, ctx: &Context<Self>) -> String {
self.value
.clone()
.unwrap_or_else(|| ctx.props().value.clone())
}
fn focus(&self) {
if let Some(input) = self.refs.input.cast::<HtmlInputElement>() {
input.focus().ok();
}
}
fn input_state(&self, ctx: &Context<Self>) -> InputState {
ctx.props()
.validator
.run_if(|| ValidationContext::from(self.value(ctx)))
.unwrap_or_else(|| ctx.props().state)
}
}