-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtextarea.rs
210 lines (194 loc) · 7.19 KB
/
textarea.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
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
#![allow(unused_variables)]
#![allow(dead_code)]
use super::set_on_input_handler;
// use crate::text_inputs::validity_state::ValidityStateJS;
use crate::text_inputs::{TextFieldType, ValidityState, ValidityTransform};
use crate::{to_option, to_option_string};
use gloo::events::EventListener;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::Node;
pub use web_sys::ValidityState as NativeValidityState;
use yew::prelude::*;
/// The `textarea` component
///
/// [Documentation](https://github.com/material-components/material-components-web-components/tree/master/packages/textarea)
pub struct TextArea {
props: TextAreaProps,
node_ref: NodeRef,
// validity_transform_closure:
// Option<Closure<dyn Fn(String, NativeValidityState) -> ValidityStateJS>>,
input_listener: Option<EventListener>,
}
/// Type for [`TextAreaProps::char_counter`].
///
/// Equivalent to `type TextAreaCharCounter = 'external'|'internal';` Typescript
/// type.
#[derive(Clone)]
pub enum TextAreaCharCounter {
Internal,
External,
}
impl ToString for TextAreaCharCounter {
fn to_string(&self) -> String {
use TextAreaCharCounter::*;
match self {
Internal => "internal",
External => "external",
}
.to_string()
}
}
/// Props for [`TextArea`]
///
/// Documentation:
///
/// - [Properties](https://github.com/material-components/material-components-web-components/tree/master/packages/checkbox#propertiesattributes)
#[derive(Properties, Clone)]
pub struct TextAreaProps {
#[prop_or_default]
pub rows: Option<i64>,
#[prop_or_default]
pub cols: Option<i64>,
#[prop_or_default]
pub value: String,
#[prop_or(TextFieldType::Text)]
pub field_type: TextFieldType,
#[prop_or_default]
pub label: String,
#[prop_or_default]
pub placeholder: String,
#[prop_or_default]
pub icon: String,
#[prop_or_default]
pub icon_trailing: String,
#[prop_or_default]
pub disabled: bool,
/// For boolean value `true`, `TextAreaCharCounter::External` is to be used.
/// Boolean value `false` results in character counter not being shown so
/// `None` should be used
#[prop_or_default]
pub char_counter: Option<TextAreaCharCounter>,
#[prop_or_default]
pub outlined: bool,
#[prop_or_default]
pub helper: String,
#[prop_or_default]
pub helper_persistent: bool,
#[prop_or_default]
pub required: bool,
#[prop_or_default]
pub max_length: String,
#[prop_or_default]
pub validation_message: String,
/// Type: `number | string` so I'll leave it as a string
#[prop_or_default]
pub min: String,
/// Type: `number | string` so I'll leave it as a string
#[prop_or_default]
pub max: String,
#[prop_or_default]
pub size: Option<i64>, // --|
#[prop_or_default] // | -- What you doing step size
pub step: Option<i64>, // --|
#[prop_or_default]
pub auto_validate: bool,
#[prop_or_default]
pub validity_transform: Option<ValidityTransform>,
#[prop_or_default]
pub validate_on_initial_render: bool,
#[prop_or_default]
pub oninput: Callback<InputData>,
#[prop_or_default]
pub name: String,
}
impl Component for TextArea {
type Message = ();
type Properties = TextAreaProps;
fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
// TextArea::ensure_loaded();
Self {
props,
node_ref: NodeRef::default(),
// validity_transform_closure: None,
input_listener: None,
}
}
fn update(&mut self, _msg: Self::Message) -> ShouldRender {
false
}
fn change(&mut self, props: Self::Properties) -> bool {
self.props = props;
true
}
fn view(&self) -> Html {
// rows?=self.props.rows
// cols?=self.props.cols
// label?=to_option_string(&self.props.label)
// placeholder?=to_option_string(&self.props.placeholder)
// icon?=to_option_string(&self.props.icon)
// iconTrailing?=to_option_string(&self.props.icon_trailing)
// disabled=self.props.disabled
// charCounter?=self.props.char_counter.as_ref().map(|it| it.to_string())
// outlined?=to_option(self.props.outlined)
// helper?=to_option_string(&self.props.helper)
// helperPersistent?=to_option(self.props.helper_persistent)
// required=self.props.required
// maxLength?=to_option_string(&self.props.max_length)
// validationMessage?=to_option_string(&self.props.validation_message)
// min?=to_option_string(&self.props.min)
// max?=to_option_string(&self.props.max)
// size?=self.props.size
// step?=self.props.step
// autoValidate?=to_option(self.props.auto_validate)
// validateOnInitialRender?=to_option(self.props.validate_on_initial_render)
// name?=to_option_string(&self.props.name)
// ref=self.node_ref.clone()
html! {
<label class="mdc-text-field mdc-text-field--filled mdc-text-field--textarea mdc-text-field--no-label">
<span class="mdc-text-field__ripple"></span>
<span class="mdc-text-field__resizer">
<textarea class="mdc-text-field__input" rows="8" cols="40" aria-label="Label"></textarea>
</span>
<span class="mdc-line-ripple"></span>
</label>
}
}
fn rendered(&mut self, first_render: bool) {
// let element = self.node_ref.cast::<TextArea>().unwrap();
// element.set_type(&JsValue::from(&self.props.field_type.to_string()));
// element.set_value(&JsValue::from(&self.props.value));
// if first_render {
// self.input_listener = Some(set_on_input_handler(
// &self.node_ref,
// self.props.oninput.clone(),
// |(input_event, detail)| {
// InputData {
// value: detail
// .unchecked_into::<TextAreaInputEvent>()
// .target()
// .value(),
// event: input_event,
// }
// },
// ));
// let this = self.node_ref.cast::<TextArea>().unwrap();
// if let Some(transform) = self.props.validity_transform.clone() {
// self.validity_transform_closure = Some(Closure::wrap(Box::new(
// move |s: String, v: NativeValidityState| -> ValidityStateJS {
// transform.0(s, v).into()
// },
// )
// as Box<dyn Fn(String, NativeValidityState) -> ValidityStateJS>));
// this.set_validity_transform(&self.validity_transform_closure.as_ref().unwrap());
// }
// }
}
}
impl TextArea {
pub fn validity_transform<F: Fn(String, NativeValidityState) -> ValidityState + 'static>(
func: F,
) -> ValidityTransform {
ValidityTransform::new(func)
}
}