DioxusLabs / dioxus

Fullstack app framework for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
21.56k stars 829 forks source link

`impl ComponentFunction for fn(props: Properties<T>) -> Element` #3188

Closed markcda closed 1 week ago

markcda commented 1 week ago

Feature Request

We have dioxus-free-icons library, and for Dioxus v0.6.0-alpha.4 it doesn't compile.

P.S. see this issue.

For code:

#[component]
pub fn AccordionTrigger(props: AccordionTriggerProps) -> Element {
  let div_class = cn(&[
    "flex flex-1 items-center justify-between py-4 font-medium transition-all hover:underline".into(),
    props.class.unwrap_or("".into()),
  ]);
  let icon_class = cn(&[
    "h-4 w-4 shrink-0 transition-transform duration-200",
    if *props.is_open.read() { "rotate-180" } else { "" },
  ]);

  rsx! {
    div {
      class: "flex",
      button {
        class: "{div_class}",
        onclick: move |_| { *props.is_open.write() = !*props.is_open.read(); },
        {props.children},
        // here we go
        Icon {
          icon: HiChevronDown,
          class: "{icon_class}"
        }
      }
    }
  }
}

it says:

error[E0277]: `Component<_>` is not implemented for `fn(IconProps<HiChevronDown>) -> Option<dioxus_core::nodes::VNode> {Icon::<HiChevronDown>}`
    = help: the trait `ComponentFunction<_, _>` is not implemented for fn item `fn(IconProps<HiChevronDown>) -> Option<dioxus_core::nodes::VNode> {Icon::<HiChevronDown>}`
    = note: Components are functions in the form `fn() -> Element`, `fn(props: Properties) -> Element`, or `#[component] fn(partial_eq1: u32, partial_eq2: u32) -> Element`.
    = note: You may have forgotten to add `#[component]` to your function to automatically implement the `ComponentFunction` trait.
note: required by a bound in `dioxus::prelude::fc_to_builder`
   --> /home/titoffklim/.cargo/git/checkouts/dioxus-1e619ce595d3799d/e0575b3/packages/core/src/properties.rs:110:36
    |
110 | pub fn fc_to_builder<P, M>(_: impl ComponentFunction<P, M>) -> <P as Properties>::Builder

Implement Suggestion

So I'm guessing we need to manually implement ComponentFunction for Properties<T>

ealmloff commented 1 week ago

As long as AccordionTriggerProps implements Properties with #[derive(Props)], it should implement that trait. I agree with marc, this is probably a version issue. The version of diouxs-free-icons you are pulling in is probably implementing a different version of the Properties trait than dioxus needs. Could you share the complete code that causes this error (with the cargo.toml)

markcda commented 1 week ago

Oh... I just created a project from scratch and everything compiled.

I'm so sorry for wasting your time. Thank for your patience!