Implement TextInput::on_submit support in iced_web

This commit is contained in:
Héctor Ramón Jiménez 2020-02-06 03:06:06 +01:00
parent 282ae1dc9e
commit 57aed1d5c6
2 changed files with 23 additions and 2 deletions

View File

@ -39,4 +39,5 @@ features = [
"Event", "Event",
"EventTarget", "EventTarget",
"InputEvent", "InputEvent",
"KeyboardEvent",
] ]

View File

@ -155,7 +155,9 @@ where
style_sheet.insert(bump, css::Rule::Padding(self.padding)); style_sheet.insert(bump, css::Rule::Padding(self.padding));
let on_change = self.on_change.clone(); let on_change = self.on_change.clone();
let event_bus = bus.clone(); let on_submit = self.on_submit.clone();
let input_event_bus = bus.clone();
let submit_event_bus = bus.clone();
let style = self.style_sheet.active(); let style = self.style_sheet.active();
input(bump) input(bump)
@ -200,7 +202,17 @@ where
Some(text_input) => text_input, Some(text_input) => text_input,
}; };
event_bus.publish(on_change(text_input.value())); input_event_bus.publish(on_change(text_input.value()));
})
.on("keypress", move |_root, _vdom, event| {
if let Some(on_submit) = on_submit.clone() {
let event = event.unchecked_into::<web_sys::KeyboardEvent>();
match event.key_code() {
13 => { submit_event_bus.publish(on_submit); }
_ => {}
}
}
}) })
.finish() .finish()
} }
@ -228,4 +240,12 @@ impl State {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
/// Creates a new [`State`], representing a focused [`TextInput`].
///
/// [`State`]: struct.State.html
pub fn focused() -> Self {
// TODO
Self::default()
}
} }