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},
|
parse::{Parse, ParseStream},
|
||||||
spanned::Spanned,
|
spanned::Spanned,
|
||||||
token::Dot2,
|
token::Dot2,
|
||||||
Expr, ExprLit, Lit,
|
Expr,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BaseExpr {
|
struct BaseExpr {
|
||||||
@ -110,15 +110,8 @@ impl ComponentProps {
|
|||||||
Some(expr) => {
|
Some(expr) => {
|
||||||
let ident = Ident::new("__yew_props", props_ty.span());
|
let ident = Ident::new("__yew_props", props_ty.span());
|
||||||
let set_props = self.props.iter().map(|Prop { label, value, .. }| {
|
let set_props = self.props.iter().map(|Prop { label, value, .. }| {
|
||||||
if is_string_literal(value) {
|
quote_spanned! {value.span()=>
|
||||||
// String literals should be implicitly converted into `String`
|
#ident.#label = ::yew::html::IntoPropValue::into_prop_value(#value);
|
||||||
quote_spanned! {value.span()=>
|
|
||||||
#ident.#label = ::std::convert::Into::into(#value);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
quote_spanned! {value.span()=>
|
|
||||||
#ident.#label = #value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let set_children = children_renderer.map(|children| {
|
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 {
|
impl Parse for ComponentProps {
|
||||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||||
let props = validate(input.parse()?)?;
|
let props = validate(input.parse()?)?;
|
||||||
|
|||||||
@ -246,6 +246,15 @@ fn compile_pass() {
|
|||||||
<div>{ "hello world" }</div>
|
<div>{ "hello world" }</div>
|
||||||
</Container>
|
</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>
|
<Container int=1 ..props>
|
||||||
<Child int=2 string="hello" ..child_props />
|
<Child int=2 string="hello" ..child_props />
|
||||||
</Container>
|
</Container>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user