mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
* Fix #2553 - automatically convert closure to callback for component properties. * Support F -> Option<Callback<_>> too. * test stderr.
This commit is contained in:
parent
e1f89a8352
commit
ea8a530454
@ -346,7 +346,7 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
|
||||
<&'static str as IntoPropValue<Classes>>
|
||||
<&'static str as IntoPropValue<Option<AttrValue>>>
|
||||
<&'static str as IntoPropValue<Option<String>>>
|
||||
and 15 others
|
||||
and 18 others
|
||||
|
||||
error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfied
|
||||
--> tests/html_macro/component-fail.rs:79:34
|
||||
@ -359,7 +359,7 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
|
||||
<&'static str as IntoPropValue<Classes>>
|
||||
<&'static str as IntoPropValue<Option<AttrValue>>>
|
||||
<&'static str as IntoPropValue<Option<String>>>
|
||||
and 15 others
|
||||
and 18 others
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> tests/html_macro/component-fail.rs:80:31
|
||||
|
||||
@ -303,6 +303,7 @@ error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<AttrVal
|
||||
= help: the following implementations were found:
|
||||
<Option<&'static str> as IntoPropValue<Option<AttrValue>>>
|
||||
<Option<&'static str> as IntoPropValue<Option<String>>>
|
||||
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
|
||||
<Option<String> as IntoPropValue<Option<AttrValue>>>
|
||||
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
|
||||
note: required by `into_prop_value`
|
||||
@ -320,6 +321,7 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<AttrValue
|
||||
= help: the following implementations were found:
|
||||
<Option<&'static str> as IntoPropValue<Option<AttrValue>>>
|
||||
<Option<&'static str> as IntoPropValue<Option<String>>>
|
||||
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
|
||||
<Option<String> as IntoPropValue<Option<AttrValue>>>
|
||||
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
|
||||
note: required by `into_prop_value`
|
||||
@ -416,6 +418,7 @@ error[E0277]: the trait bound `Option<yew::NodeRef>: IntoPropValue<yew::NodeRef>
|
||||
= help: the following implementations were found:
|
||||
<Option<&'static str> as IntoPropValue<Option<AttrValue>>>
|
||||
<Option<&'static str> as IntoPropValue<Option<String>>>
|
||||
<Option<F> as IntoPropValue<Option<yew::Callback<I, O>>>>
|
||||
<Option<String> as IntoPropValue<Option<AttrValue>>>
|
||||
<Option<std::rc::Rc<str>> as IntoPropValue<Option<AttrValue>>>
|
||||
note: required by `into_prop_value`
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use super::super::callback::Callback;
|
||||
use super::{Component, NodeRef, Scope};
|
||||
use crate::virtual_dom::AttrValue;
|
||||
use std::rc::Rc;
|
||||
@ -64,6 +65,33 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, O, F> IntoPropValue<Callback<I, O>> for F
|
||||
where
|
||||
F: 'static + Fn(I) -> O,
|
||||
{
|
||||
fn into_prop_value(self) -> Callback<I, O> {
|
||||
Callback::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, O, F> IntoPropValue<Option<Callback<I, O>>> for F
|
||||
where
|
||||
F: 'static + Fn(I) -> O,
|
||||
{
|
||||
fn into_prop_value(self) -> Option<Callback<I, O>> {
|
||||
Some(Callback::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, O, F> IntoPropValue<Option<Callback<I, O>>> for Option<F>
|
||||
where
|
||||
F: 'static + Fn(I) -> O,
|
||||
{
|
||||
fn into_prop_value(self) -> Option<Callback<I, O>> {
|
||||
self.map(|f| Callback::from(f))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_into_prop {
|
||||
(|$value:ident: $from_ty:ty| -> $to_ty:ty { $conversion:expr }) => {
|
||||
// implement V -> T
|
||||
@ -111,4 +139,14 @@ mod test {
|
||||
let _: Option<AttrValue> = "foo".into_prop_value();
|
||||
let _: Option<AttrValue> = Rc::<str>::from("foo").into_prop_value();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_callback() {
|
||||
let _: Callback<String> = (|_: String| ()).into_prop_value();
|
||||
let _: Option<Callback<String>> = (|_: String| ()).into_prop_value();
|
||||
let _: Option<Callback<String>> = Some(|_: String| ()).into_prop_value();
|
||||
let _: Callback<String, String> = (|s: String| s).into_prop_value();
|
||||
let _: Option<Callback<String, String>> = (|s: String| s).into_prop_value();
|
||||
let _: Option<Callback<String, String>> = Some(|s: String| s).into_prop_value();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user