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", "Text",
"TreeWalker", "TreeWalker",
"Window", "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] [dev-dependencies]

View file

@ -1,7 +1,7 @@
use std::time::Duration; use std::time::Duration;
use wasm_bindgen::{prelude::Closure, JsCast, JsValue, UnwrapThrowExt};
use wasm_bindgen::convert::FromWasmAbi; use wasm_bindgen::convert::FromWasmAbi;
use wasm_bindgen::{prelude::Closure, JsCast, JsValue, UnwrapThrowExt};
use crate::{debug_warn, event_delegation, is_server}; use crate::{debug_warn, event_delegation, is_server};
@ -182,8 +182,12 @@ where
/// Helper function to extract `event.target.value` from an event. /// 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. /// 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 event
.unchecked_ref::<web_sys::Event>()
.target() .target()
.unwrap_throw() .unwrap_throw()
.unchecked_into::<web_sys::HtmlInputElement>() .unchecked_into::<web_sys::HtmlInputElement>()
@ -255,7 +259,9 @@ pub fn add_event_listener<E>(
target: &web_sys::Element, target: &web_sys::Element,
event_name: &'static str, event_name: &'static str,
cb: impl FnMut(E) + 'static, 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 cb = Closure::wrap(Box::new(cb) as Box<dyn FnMut(E)>).into_js_value();
let key = event_delegation::event_delegation_key(event_name); let key = event_delegation::event_delegation_key(event_name);
_ = js_sys::Reflect::set(target, &JsValue::from_str(&key), &cb); _ = 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()) { } else if let Some(event_type) = EVENTS.get(&name.as_str()).map(|&e| e.parse::<TokenStream>().unwrap_or_default()) {
expressions.push(quote_spanned! { 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 { } else {
expressions.push(quote_spanned! { 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 { } else {
@ -1117,7 +1117,7 @@ fn create_component(cx: &Ident, node: &NodeElement, mode: Mode) -> TokenStream {
}) })
} else { } else {
Some(quote_spanned! { 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)
}) })
} }
} }