Skip to content

Commit

Permalink
text-inputs::event (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-duch authored May 19, 2022
1 parent 21341e3 commit 4ed8d76
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions material-yew/src/text_inputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub use web_sys::ValidityState as NativeValidityState;
use std::rc::Rc;

use gloo::events::EventListener;
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{Element, Event, InputEvent};
use wasm_bindgen::JsValue;
use web_sys::{Element, Event};
use yew::{Callback, NodeRef};

#[cfg(any(feature = "textfield", feature = "textarea"))]
Expand Down Expand Up @@ -55,16 +55,12 @@ impl PartialEq for ValidityTransform {
fn set_on_input_handler(
node_ref: &NodeRef,
callback: Callback<String>,
convert: impl Fn((InputEvent, JsValue)) -> String + 'static,
convert: impl Fn((Event, JsValue)) -> String + 'static,
) -> EventListener {
let element = node_ref.cast::<Element>().unwrap();
EventListener::new(&element, "input", move |event: &Event| {
let js_value = JsValue::from(event);

if let Some(input_event) = JsCast::dyn_ref::<web_sys::InputEvent>(&js_value) {
callback.emit(convert((input_event.clone(), js_value)))
} else {
panic!("could not convert to `InputEvent`");
}
callback.emit(convert((event.clone(), js_value)))
})
}

0 comments on commit 4ed8d76

Please sign in to comment.