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
use crate::{
InputState, ValidatingComponent, ValidatingComponentProperties, ValidationContext, Validator,
};
use std::fmt::{Display, Formatter};
use web_sys::HtmlInputElement;
use yew::prelude::*;
#[derive(Clone, PartialEq, Eq)]
pub enum ResizeOrientation {
Horizontal,
Vertical,
Both,
}
impl Default for ResizeOrientation {
fn default() -> Self {
Self::Both
}
}
#[derive(Clone, PartialEq, Eq)]
pub enum Wrap {
Hard,
Soft,
Off,
}
impl Default for Wrap {
fn default() -> Self {
Self::Soft
}
}
impl Display for Wrap {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Off => f.write_str("off"),
Self::Soft => f.write_str("soft"),
Self::Hard => f.write_str("hard"),
}
}
}
#[derive(Clone, PartialEq, Properties)]
pub struct TextAreaProps {
#[prop_or_default]
pub name: 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 placeholder: String,
#[prop_or_default]
pub spellcheck: Option<bool>,
#[prop_or_default]
pub wrap: Wrap,
#[prop_or_default]
pub rows: Option<usize>,
#[prop_or_default]
pub cols: Option<usize>,
#[prop_or_default]
pub resize: ResizeOrientation,
#[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>,
}
pub struct TextArea {
value: String,
input_ref: NodeRef,
}
pub enum TextAreaMsg {
Init,
Changed(String),
Input(String),
}
impl Component for TextArea {
type Message = TextAreaMsg;
type Properties = TextAreaProps;
fn create(ctx: &Context<Self>) -> Self {
let value = ctx.props().value.clone();
ctx.link().send_message(Self::Message::Init);
Self {
value,
input_ref: NodeRef::default(),
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
TextAreaMsg::Init => {
ctx.props().onvalidate.emit(ValidationContext {
value: self.value.clone(),
initial: true,
});
false
}
TextAreaMsg::Changed(data) => {
self.value = data.clone();
ctx.props().onchange.emit(data.clone());
ctx.props().onvalidate.emit(data.into());
false
}
TextAreaMsg::Input(data) => {
ctx.props().oninput.emit(data);
if let Some(value) = self.extract_value() {
self.value = value.clone();
ctx.props().onchange.emit(value.clone());
ctx.props().onvalidate.emit(value.clone().into());
}
false
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let classes = Classes::from("pf-c-form-control");
let (mut classes, aria_invalid) = self.input_state(ctx).convert(classes);
match ctx.props().resize {
ResizeOrientation::Horizontal => classes.push("pf-m-resize-horizontal"),
ResizeOrientation::Vertical => classes.push("pf-m-resize-vertical"),
_ => {}
}
let input_ref = self.input_ref.clone();
let onchange = ctx.link().batch_callback(move |_| {
input_ref
.cast::<HtmlInputElement>()
.map(|input| TextAreaMsg::Changed(input.value()))
});
let oninput = ctx
.link()
.callback(|data: InputEvent| TextAreaMsg::Input(data.data().unwrap_or_default()));
html! {
<textarea
ref={self.input_ref.clone()}
class={classes}
name={ctx.props().name.clone()}
required={ctx.props().required}
disabled={ctx.props().disabled}
readonly={ctx.props().readonly}
aria-invalid={aria_invalid.to_string()}
value={ctx.props().value.clone()}
cols={ctx.props().cols.as_ref().map(|v|v.to_string())}
rows={ctx.props().rows.as_ref().map(|v|v.to_string())}
wrap={ctx.props().wrap.to_string()}
spellcheck={ctx.props().spellcheck.map(|v|v.to_string())}
placeholder={ctx.props().placeholder.clone()}
onchange={onchange}
oninput={oninput}
/>
}
}
}
impl TextArea {
fn extract_value(&self) -> Option<String> {
self.input_ref
.cast::<HtmlInputElement>()
.map(|input| input.value())
}
fn input_state(&self, ctx: &Context<Self>) -> InputState {
ctx.props()
.validator
.run_if(|| ValidationContext::from(self.value.clone()))
.unwrap_or_else(|| ctx.props().state)
}
}
impl ValidatingComponent for TextArea {
type Value = String;
}
impl ValidatingComponentProperties<String> for TextAreaProps {
fn set_onvalidate(&mut self, onvalidate: Callback<ValidationContext<String>>) {
self.onvalidate = onvalidate;
}
fn set_input_state(&mut self, state: InputState) {
self.state = state;
}
}