Skip to content

Add transformer String to VNode#1626

Closed
yozhgoor wants to merge 4 commits into
yewstack:masterfrom
yozhgoor:transformer-string-to-vnode
Closed

Add transformer String to VNode#1626
yozhgoor wants to merge 4 commits into
yewstack:masterfrom
yozhgoor:transformer-string-to-vnode

Conversation

@yozhgoor
Copy link
Copy Markdown

@yozhgoor yozhgoor commented Oct 19, 2020

Description

I contribute to yewprint. I use VNode as a type of some props and I want to allow the user to pass a component instead of just a String.

Example: cecton/yewprint@d2f5ee8

error[E0277]: the trait bound `yew::virtual_dom::VComp: yew::virtual_dom::Transformer<&str, yew::virtual_dom::VNode>` is not satisfied
  --> yewprint-doc/src/progressbar/mod.rs:84:35
   |
84 | ...                   label="Animate"
   |                             ^^^^^^^^^ the trait `yew::virtual_dom::Transformer<&str, yew::virtual_dom::VNode>` is not implemented for `yew::virtual_dom::VComp`
   |

In this example, I have try to use a VNode as a label and now I need to replace all the labels in my code with label=html!("Animate") and that in 31 places.

Checklist

  • I have run cargo make pr-flow
  • I have reviewed my own code
  • I have added tests

@siku2
Copy link
Copy Markdown
Member

siku2 commented Oct 19, 2020

This, to me, feels too implicit given that a more explicit version already exists:

html! {
  <Model label=html!("Cooperative") />
}

If you think that's too verbose for your use case you can create a dedicated type and implement the transformation for that.

struct MyWrapper(Html);
impl Transformer<Html, MyWrapper> for VComp {}
impl Transformer<&'_ str, MyWrapper> for VComp {}

struct Props {
  label: MyWrapper,
}

html! {
  <Model label=html! { <strong>{ "works!" }</strong> } />
  <Model label="also works" />
}

@cecton
Copy link
Copy Markdown
Contributor

cecton commented Oct 19, 2020

I'm mentoring @yozhgoor , the PR was my idea 🙂

I forgot you can do html!("string"). In a node you can't do that so it feels counter intuitive. (html!(<div>"string"</div>) is invalid)

I guess this is fine by me. Thanks! We can close this PR if @yozhgoor think it's ok.

@cecton
Copy link
Copy Markdown
Contributor

cecton commented Oct 19, 2020

(The wrapper thingy is a no go solution imo. It would introduce some specific magic to a library of yew, it will feel counter intuitive to the user)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants