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

Component Callback documentation suggests non-existent conversion of closure to Callback #2553

Closed
2 of 9 tasks
finnbear opened this issue Mar 27, 2022 · 2 comments · Fixed by #2554
Closed
2 of 9 tasks
Labels
A-yew Area: The main yew crate documentation

Comments

@finnbear
Copy link
Contributor

finnbear commented Mar 27, 2022

This is about:

  • A typo
  • Innaccurate/misleading documentation (e.g. technically incorrect advice)
  • Undocumented code
  • Outdated documentation
  • Other

Problem

Documentation:

Closures or Functions provided as props inside the html! macro are automatically converted to Callbacks.
(https://yew.rs/docs/concepts/components/callbacks)

Reality: <MyComponent callback={|_| ...}>, where Props { callback: Callback<()> } leads to IntoPropValue<yew::Callback<()>> is not implemented for [closure@src/ui.rs:15:35: 15:75]. Manually wrapping closure in Callback::from is a workaround.

Details about the solution you'd like (Optional)

Ideally, passing closures to props of type Callback<_> would be supported, for components, not just elements.

Additional context (Optional)

https://discord.com/channels/701068342760570933/703449306497024049/957453333403152385

In particular, WorldSEnder wrote

the page was probably moved at some point by mistake, since all the examples refer to elements, but it's under "components"

Questionaire (Optional)

  • I'd like to write this documentation
  • I'd like to write this documentation but I'm not sure what's needed
  • I don't have time to add this right now, but maybe later
  • I could delete the incorrect phrase from the documentation, but if the solution I like is adopted, I'm not prepared to implement it without guidance
@futursolo
Copy link
Member

I would like to see this to be supported.

To add this, you need to add an IntoPropValue implementation.

Something like the following:

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)
    }
}

Also, to improve API ergonomics, you may also want to implement IntoPropValue for the following conversions:

F -> Option<Callback<I, O>>
Option<F> -> Option<Callback<I, O>>

@finnbear
Copy link
Contributor Author

I've shamelessly copied your hypothetical implementation, and adaptations of it for the other two cases, into a pull request to hopefully expedite this 😂

WorldSEnder pushed a commit that referenced this issue Mar 27, 2022
…roperties (#2554)

* Fix #2553 - automatically convert closure to callback for component properties.

* Support F -> Option<Callback<_>> too.

* test stderr.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew Area: The main yew crate documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants