Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use into_prop_value to convert str prop to Option<String> #2080

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions packages/yew-macro/src/props/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use syn::{
parse::{Parse, ParseStream},
spanned::Spanned,
token::Dot2,
Expr, ExprLit, Lit,
Expr,
};

struct BaseExpr {
Expand Down Expand Up @@ -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| {
Expand All @@ -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()?)?;
Expand Down
9 changes: 9 additions & 0 deletions packages/yew-macro/tests/html_macro/component-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,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>
Expand Down