mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
Use into_prop_value to convert str prop to Option<String> (#2080)
This commit is contained in:
parent
a442ce5df5
commit
97498cdfcd
@ -6,7 +6,7 @@ use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
spanned::Spanned,
|
||||
token::Dot2,
|
||||
Expr, ExprLit, Lit,
|
||||
Expr,
|
||||
};
|
||||
|
||||
struct BaseExpr {
|
||||
@ -110,15 +110,8 @@ impl ComponentProps {
|
||||
Some(expr) => {
|
||||
let ident = Ident::new("__yew_props", props_ty.span());
|
||||
let set_props = self.props.iter().map(|Prop { label, value, .. }| {
|
||||
if is_string_literal(value) {
|
||||
// String literals should be implicitly converted into `String`
|
||||
quote_spanned! {value.span()=>
|
||||
#ident.#label = ::std::convert::Into::into(#value);
|
||||
}
|
||||
} else {
|
||||
quote_spanned! {value.span()=>
|
||||
#ident.#label = #value;
|
||||
}
|
||||
quote_spanned! {value.span()=>
|
||||
#ident.#label = ::yew::html::IntoPropValue::into_prop_value(#value);
|
||||
}
|
||||
});
|
||||
let set_children = children_renderer.map(|children| {
|
||||
@ -145,16 +138,6 @@ impl ComponentProps {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_string_literal(expr: &Expr) -> bool {
|
||||
matches!(
|
||||
expr,
|
||||
Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(_),
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
impl Parse for ComponentProps {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
let props = validate(input.parse()?)?;
|
||||
|
||||
@ -246,6 +246,15 @@ fn compile_pass() {
|
||||
<div>{ "hello world" }</div>
|
||||
</Container>
|
||||
|
||||
<Container int=1 ..::std::clone::Clone::clone(&props)>
|
||||
<Child int=2 opt_str="hello" ..::std::clone::Clone::clone(&child_props) />
|
||||
</Container>
|
||||
|
||||
<Container int=1 ..::std::clone::Clone::clone(&props)>
|
||||
<Child int=2 vec={::std::vec![0]} ..::std::clone::Clone::clone(&child_props) />
|
||||
</Container>
|
||||
|
||||
|
||||
<Container int=1 ..props>
|
||||
<Child int=2 string="hello" ..child_props />
|
||||
</Container>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user