yewstack / yew-autoprops

Apache License 2.0
8 stars 1 forks source link

Pattern binding for auto-props #2

Open kirillsemyonkin opened 1 year ago

kirillsemyonkin commented 1 year ago

As of writing, yew-autoprops does not support pattern-binding the props using the built-in @ operator (syntactically correct without the #[autoprops_component]):

#[derive(PartialEq)]
pub struct ExampleProp {
    i: u64,
}

#[autoprops_component]
pub fn Example(example_prop @ ExampleProp { i }: &ExampleProp) -> Html {
    html! {
    }
}

#[function_component]
pub fn App() -> Html {
    html! {
        <Example example_prop={ ExampleProp { i: 0 } } />
    }
}

Errors:

expected `:`, found `@`
the trait bound `ExampleProps: Properties` is not satisfied
no field `example_prop` on type `ExampleProps`

Possible Solution

Handle pattern after the @ operator.

#[derive(PartialEq)]
pub struct ExampleProp {
    i: u64,
}

#[derive(PartialEq, Properties)]
pub struct ExampleProps {
    example_prop: ExampleProp,
}

#[function_component]
pub fn Example(
    ExampleProps {
        // without the `example_prop @` the `example_prop` name will not be accessible in the function body
        example_prop: example_prop @ ExampleProp { i }, 
    }: &ExampleProps,
) -> Html {
    html! {
    }
}

#[function_component]
pub fn App() -> Html {
    html! {
        <Example example_prop={ ExampleProp { i: 0 } } />
    }
}