Skip to content

Commit

Permalink
Fix __macro_new to use IntoEventCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mc1098 committed Aug 26, 2021
1 parent 2560356 commit 7dc56d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
39 changes: 22 additions & 17 deletions packages/yew-macro/tests/html_macro/element-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -302,38 +302,38 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<Cow<'stat
<Option<String> as IntoPropValue<Option<Cow<'static, str>>>>
= note: required by `into_prop_value`

error[E0277]: the trait bound `{integer}: IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not satisfied
error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `{integer}`
--> $DIR/element-fail.rs:51:29
|
51 | html! { <input on:click=1 /> };
| ^ the trait `IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not implemented for `{integer}`
| ^ expected an `Fn<(MouseEvent,)>` closure, found `{integer}`
|
= help: the following implementations were found:
<&'static str as IntoPropValue<Cow<'static, str>>>
<&'static str as IntoPropValue<Option<Cow<'static, str>>>>
<&'static str as IntoPropValue<Option<String>>>
<&'static str as IntoPropValue<String>>
and 11 others
= help: the trait `Fn<(MouseEvent,)>` is not implemented for `{integer}`
= note: required because of the requirements on the impl of `IntoEventCallback<MouseEvent>` for `{integer}`
= note: required by `Wrapper::<T>::__macro_new`

error[E0277]: the trait bound `yew::Callback<String>: IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not satisfied
error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback<String>`
--> $DIR/element-fail.rs:52:30
|
52 | html! { <input on:click={Callback::from(|a: String| ())} /> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not implemented for `yew::Callback<String>`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected an implementor of trait `IntoEventCallback<MouseEvent>`
| help: consider borrowing here: `&Callback::from(|a: String| ())`
|
= note: the trait bound `yew::Callback<String>: IntoEventCallback<MouseEvent>` is not satisfied
= note: required because of the requirements on the impl of `IntoEventCallback<MouseEvent>` for `yew::Callback<String>`
= note: required by `Wrapper::<T>::__macro_new`

error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<yew::Callback<FocusEvent>>>` is not satisfied
error[E0277]: the trait bound `Option<{integer}>: IntoEventCallback<FocusEvent>` is not satisfied
--> $DIR/element-fail.rs:53:30
|
53 | html! { <input on:focus={Some(5)} /> };
| ^^^^^^^ the trait `IntoPropValue<Option<yew::Callback<FocusEvent>>>` is not implemented for `Option<{integer}>`
| ^^^^^^^ the trait `IntoEventCallback<FocusEvent>` is not implemented for `Option<{integer}>`
|
= help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<Cow<'static, str>>>>
<Option<&'static str> as IntoPropValue<Option<String>>>
<Option<String> as IntoPropValue<Option<Cow<'static, str>>>>
<Option<T> as IntoEventCallback<EVENT>>
<Option<yew::Callback<EVENT>> as IntoEventCallback<EVENT>>
= note: required by `Wrapper::<T>::__macro_new`

error[E0277]: the trait bound `(): IntoPropValue<yew::NodeRef>` is not satisfied
Expand All @@ -356,12 +356,17 @@ error[E0277]: the trait bound `Option<yew::NodeRef>: IntoPropValue<yew::NodeRef>
<Option<String> as IntoPropValue<Option<Cow<'static, str>>>>
= note: required by `into_prop_value`

error[E0277]: the trait bound `yew::Callback<String>: IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not satisfied
error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback<String>`
--> $DIR/element-fail.rs:58:30
|
58 | html! { <input on:click={Callback::from(|a: String| ())} /> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not implemented for `yew::Callback<String>`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected an implementor of trait `IntoEventCallback<MouseEvent>`
| help: consider borrowing here: `&Callback::from(|a: String| ())`
|
= note: the trait bound `yew::Callback<String>: IntoEventCallback<MouseEvent>` is not satisfied
= note: required because of the requirements on the impl of `IntoEventCallback<MouseEvent>` for `yew::Callback<String>`
= note: required by `Wrapper::<T>::__macro_new`

error[E0277]: the trait bound `NotToString: IntoPropValue<Option<Cow<'static, str>>>` is not satisfied
Expand Down
9 changes: 4 additions & 5 deletions packages/yew/src/html/listener/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ pub trait StaticEvent {
}

use crate::callback::Callback;
use crate::html::IntoPropValue;
use crate::virtual_dom::Listener;
use gloo::events::{EventListener, EventListenerOptions};
use std::rc::Rc;
use web_sys::{Element, EventTarget};

use super::IntoEventCallback;

/// A wrapper for a callback which attaches event listeners to elements.
#[derive(Clone, Debug)]
pub struct Wrapper<T: StaticEvent> {
Expand All @@ -140,10 +141,8 @@ impl<T: StaticEvent + 'static> Wrapper<T> {

#[doc(hidden)]
#[inline]
pub fn __macro_new(
callback: impl IntoPropValue<Option<Callback<T::Event>>>,
) -> Option<Rc<dyn Listener>> {
let callback = callback.into_prop_value()?;
pub fn __macro_new(callback: impl IntoEventCallback<T::Event>) -> Option<Rc<dyn Listener>> {
let callback = callback.into_event_callback()?;
Some(Rc::new(Self::new(callback)))
}
}
Expand Down

0 comments on commit 7dc56d9

Please sign in to comment.