-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtextfield.rs
196 lines (181 loc) · 6.8 KB
/
textfield.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
#![allow(unused_variables)]
#![allow(dead_code)]
use super::set_on_input_handler;
use crate::text_inputs::{
// validity_state::ValidityStateJS,
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;
use web_sys::ValidityState as NativeValidityState;
use yew::prelude::*;
/// The `textfield` component
///
/// [Documentation](https://github.com/material-components/material-components-web-components/tree/master/packages/textfield)
pub struct TextField {
props: TextFieldProps,
node_ref: NodeRef,
// validity_transform_closure:
// Option<Closure<dyn Fn(String, NativeValidityState) -> ValidityStateJS>>,
input_listener: Option<EventListener>,
}
/// Props for [`TextField`]
///
/// Documentation:
///
/// - [Properties](https://github.com/material-components/material-components-web-components/tree/master/packages/textfield#propertiesattributes)
#[derive(Properties, Clone)]
pub struct TextFieldProps {
#[prop_or_default]
pub open: bool,
#[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 prefix: String,
#[prop_or_default]
pub suffix: String,
#[prop_or_default]
pub icon: String,
#[prop_or_default]
pub icon_trailing: String,
#[prop_or_default]
pub disabled: bool,
#[prop_or_default]
pub char_counter: bool,
#[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,
#[prop_or_default]
pub pattern: 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,
// What you doing...
#[prop_or_default]
pub size: Option<i64>,
// ...step size
#[prop_or_default]
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 TextField {
type Message = ();
type Properties = TextFieldProps;
fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
// TextField::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 {
// open=self.props.open
// label?=to_option_string(&self.props.label)
// placeholder?=to_option_string(&self.props.placeholder)
// prefix?=to_option_string(&self.props.prefix)
// suffix?=to_option_string(&self.props.suffix)
// icon?=to_option_string(&self.props.icon)
// iconTrailing?=to_option_string(&self.props.icon_trailing)
// disabled=self.props.disabled
// charCounter?=to_option(self.props.char_counter)
// 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)
// pattern?=to_option_string(&self.props.pattern)
// min?=to_option_string(&self.props.min)
// max?=to_option_string(&self.props.max)
// size?=self.props.size //.map_or("null".to_string(), |v| v.to_string())
// step?=self.props.step //.map_or("null".to_string(), |v| v.to_string())
// 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">
<span class="mdc-text-field__ripple"></span>
<span class="mdc-floating-label" id="my-label-id">{ "Hint text" }</span>
<input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id"/>
<span class="mdc-line-ripple"></span>
</label>
}
}
fn rendered(&mut self, first_render: bool) {
// 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::<TextFieldInputEvent>()
// .target()
// .value(),
// event: input_event,
// }
// },
// ));
// let element = self.node_ref.cast::<TextField>().unwrap();
// element.set_type(&JsValue::from(&self.props.field_type.to_string()));
// element.set_value(&JsValue::from(&self.props.value));
// let this = self.node_ref.cast::<TextField>().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 TextField {
pub fn validity_transform<F: Fn(String, NativeValidityState) -> ValidityState + 'static>(
func: F,
) -> ValidityTransform {
ValidityTransform::new(func)
}
}