A couple small DX improvements re: we, and making sure it builds /tests properly

This commit is contained in:
Greg Johnston 2022-11-23 15:12:21 -05:00
parent f3947abdc2
commit 7a10ffd150
3 changed files with 26 additions and 6 deletions

View file

@ -53,6 +53,20 @@ features = [
"Text",
"TreeWalker",
"Window",
# Events we cast to in leptos_macro -- added here so we don't force users to import them
"MouseEvent",
"DragEvent",
"FocusEvent",
"KeyboardEvent",
"ProgressEvent",
"WheelEvent",
"InputEvent",
"SubmitEvent",
"AnimationEvent",
"PointerEvent",
"TouchEvent",
"TransitionEvent"
]
[dev-dependencies]

View file

@ -1,7 +1,7 @@
use std::time::Duration;
use wasm_bindgen::{prelude::Closure, JsCast, JsValue, UnwrapThrowExt};
use wasm_bindgen::convert::FromWasmAbi;
use wasm_bindgen::{prelude::Closure, JsCast, JsValue, UnwrapThrowExt};
use crate::{debug_warn, event_delegation, is_server};
@ -182,8 +182,12 @@ where
/// Helper function to extract `event.target.value` from an event.
///
/// This is useful in the `on:input` or `on:change` listeners for an `<input>` element.
pub fn event_target_value(event: &web_sys::Event) -> String {
pub fn event_target_value<T>(event: &T) -> String
where
T: JsCast,
{
event
.unchecked_ref::<web_sys::Event>()
.target()
.unwrap_throw()
.unchecked_into::<web_sys::HtmlInputElement>()
@ -255,7 +259,9 @@ pub fn add_event_listener<E>(
target: &web_sys::Element,
event_name: &'static str,
cb: impl FnMut(E) + 'static,
) where E: FromWasmAbi + 'static {
) where
E: FromWasmAbi + 'static,
{
let cb = Closure::wrap(Box::new(cb) as Box<dyn FnMut(E)>).into_js_value();
let key = event_delegation::event_delegation_key(event_name);
_ = js_sys::Reflect::set(target, &JsValue::from_str(&key), &cb);

View file

@ -590,11 +590,11 @@ fn attr_to_tokens(
});
} else if let Some(event_type) = EVENTS.get(&name.as_str()).map(|&e| e.parse::<TokenStream>().unwrap_or_default()) {
expressions.push(quote_spanned! {
span => ::leptos::add_event_listener::<#event_type>(#el_id.unchecked_ref(), #name, #handler);
span => ::leptos::add_event_listener::<web_sys::#event_type>(#el_id.unchecked_ref(), #name, #handler);
});
} else {
expressions.push(quote_spanned! {
span => ::leptos::add_event_listener::<Event>(#el_id.unchecked_ref(), #name, #handler);
span => ::leptos::add_event_listener::<web_sys::Event>(#el_id.unchecked_ref(), #name, #handler);
});
}
} else {
@ -1117,7 +1117,7 @@ fn create_component(cx: &Ident, node: &NodeElement, mode: Mode) -> TokenStream {
})
} else {
Some(quote_spanned! {
span => ::leptos::add_event_listener::<Event>(#component_name.unchecked_ref(), #event_name, #handler)
span => ::leptos::add_event_listener::<web_sys::Event>(#component_name.unchecked_ref(), #event_name, #handler)
})
}
}