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

[TwigComponent] Passing props from a parent component to a child with different HTML syntax #2514

Closed
seb-jean opened this issue Jan 20, 2025 · 5 comments
Labels

Comments

@seb-jean
Copy link
Contributor

seb-jean commented Jan 20, 2025

Hi,
I have a problem but I don't know if it's a bug or if it's an intended behavior :

I want to pass a props (for my example, the props is called disabled) from a parent component to the child component.

Props are passed when it is a component with a self-closing HTML syntax. For my example: <twig:Form:Label>Description</twig:Form:Label>
However, when it is a component with opening and closing tag HTML syntax, the props are not passed from the parent to the child. For my example <twig:Form:Textarea />

{# templates/index.html.twig #}

<twig:Form:Row disabled>
    <twig:Form:Label>Description</twig:Form:Label>
    <twig:Form:Textarea />
</twig:Form:Row>
{# templates/components/Form/Label.html.twig #}
{{ disabled }}  {# work #}
{# templates/components/Form/Textarea.html.twig #}
{{ disabled }}  {# not work #}

Is this normal?

@seb-jean seb-jean added the Bug Bug Fix label Jan 20, 2025
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Jan 20, 2025
@seb-jean seb-jean changed the title Passing props from a parent component to a child with different HTML syntax [TwigComponent] Passing props from a parent component to a child with different HTML syntax Jan 20, 2025
@smnandre
Copy link
Member

Not a bug, but something we could document better.

Self-closing component have better performance, isolation, and modern architecture.. embedding ones have more features and extension capacities :)

@smnandre smnandre removed the Bug Bug Fix label Jan 20, 2025
@Kocal
Copy link
Member

Kocal commented Jan 20, 2025

Hi, this is caused by our TwigPreLexer, which behaves differently if the <twig:...> is self closing or not:

                if ($isSelfClosing) {
                    // use the simpler component() format, so that the system doesn't think
                    // this is an "embedded" component with blocks
                    // see https://github.com/symfony/ux/issues/810
                    $output .= "{{ component('{$componentName}'".($attributes ? ", { {$attributes} }" : '').') }}';
                } else {
                    $output .= "{% component '{$componentName}'".($attributes ? " with { {$attributes} }" : '').' %}';
                }

When the component is not self-closing, then it is replaced by {% component '...' with { disabled: ... } %}, without the only keyword.

To me, that's definitely an issue, when using components like Vue or React, you do not expect variables to be secretly passed down to your component.

@smnandre
Copy link
Member

This is a feature, it has been asked for and documented :)

@Kocal
Copy link
Member

Kocal commented Jan 20, 2025

So... it is broken by design? What about <twig:Form:Textarea /> vs <twig:Form:Textarea></twig:Form:Textarea>?

I don't see where it has been asked / documented, can you give me some hints please? 🙏🏻

@smnandre
Copy link
Member

Image

@smnandre smnandre closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants