fix: allow generic parameters for directive functions (closes #2808) (#2812)

This commit is contained in:
Greg Johnston 2024-08-11 20:37:57 -04:00 committed by GitHub
parent 84590b98ed
commit 27dbadb7d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 7 deletions

View file

@ -53,7 +53,6 @@ impl From<()> for Amount {
}
}
// .into() will automatically be called on the parameter
pub fn add_dot(el: Element, amount: Amount) {
use leptos::wasm_bindgen::JsCast;
let el = el.unchecked_into::<web_sys::HtmlElement>();
@ -82,12 +81,17 @@ pub fn App() -> impl IntoView {
let data = "Hello World!";
view! {
<a href="#" use:copy_to_clipboard=data>"Copy \"" {data} "\" to clipboard"</a>
<a href="#" use:copy_to_clipboard=data>
"Copy \""
{data}
"\" to clipboard"
</a>
// automatically applies the directive to every root element in `SomeComponent`
<SomeComponent use:highlight />
<SomeComponent use:highlight/>
// no value will default to `().into()`
<button use:add_dot>"Add a dot"</button>
// `5.into()` automatically called
<button use:add_dot=5>"Add 5 dots"</button>
// can manually call `.into()` to convert to the correct type
// (automatically calling `.into()` prevents using generics in directive functions)
<button use:add_dot=5.into()>"Add 5 dots"</button>
}
}

View file

@ -520,7 +520,7 @@ pub(crate) fn attribute_absolute(
} else if id == "use" {
let key = &parts[1];
let param = if let Some(value) = node.value() {
quote!(::std::convert::Into::into(#value))
quote!(#value)
} else {
quote_spanned!(node.key.span()=> ().into())
};
@ -1071,7 +1071,7 @@ pub(crate) fn directive_call_from_attribute_node(
let handler = syn::Ident::new(directive_name, attr.key.span());
let param = if let Some(value) = attr.value() {
quote!(::std::convert::Into::into(#value))
quote!(#value)
} else {
quote_spanned!(attr.key.span()=> ().into())
};