-
-
Notifications
You must be signed in to change notification settings - Fork 403
[LiveComponent] Support DataModel bindings with LiveComponent modifiers #3302
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
Open
Wertisdk
wants to merge
3
commits into
symfony:2.x
Choose a base branch
from
Wertisdk:fix-data-model-modifier-detection
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/LiveComponent/tests/Fixtures/Component/ParentComponentDataModelWithModifiers.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component; | ||
|
|
||
| use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
| use Symfony\UX\LiveComponent\Attribute\LiveProp; | ||
| use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
|
|
||
| #[AsLiveComponent('parent_component_data_model_with_modifiers')] | ||
| final class ParentComponentDataModelWithModifiers | ||
| { | ||
| use DefaultActionTrait; | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/LiveComponent/tests/Fixtures/Component/ParentComponentDataModelWithModifiers2.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component; | ||
|
|
||
| use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
| use Symfony\UX\LiveComponent\Attribute\LiveProp; | ||
| use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
|
|
||
| #[AsLiveComponent('parent_component_data_model_with_modifiers_2')] | ||
| final class ParentComponentDataModelWithModifiers2 | ||
| { | ||
| use DefaultActionTrait; | ||
|
|
||
| #[LiveProp(writable: true)] | ||
| public string $content; | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/LiveComponent/tests/Fixtures/Component/ParentFormComponentWithModifiers.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component; | ||
|
|
||
| use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
| use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
|
|
||
| #[AsLiveComponent('parent_form_component_with_modifiers')] | ||
| final class ParentFormComponentWithModifiers | ||
| { | ||
| use DefaultActionTrait; | ||
|
|
||
| public ?string $content = null; | ||
|
|
||
| public ?string $content2 = null; | ||
| } |
2 changes: 2 additions & 0 deletions
2
.../tests/Fixtures/templates/components/parent_component_data_model_with_modifiers.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| {% component parent_component_data_model_with_modifiers_2 with { content: 'default content on mount' } %} | ||
| {% endcomponent %} |
2 changes: 2 additions & 0 deletions
2
...ests/Fixtures/templates/components/parent_component_data_model_with_modifiers_2.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| {{ component('textarea_component', { dataModel: 'norender|content' }) }} | ||
| {% component input_component with { dataModel: 'norender|content' } %}{% endcomponent %} |
11 changes: 11 additions & 0 deletions
11
...ponent/tests/Fixtures/templates/components/parent_form_component_with_modifiers.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <div> | ||
| {{ component('textarea_component', { | ||
| 'data-model': 'norender|content:value' | ||
| }) }} | ||
| </div> | ||
|
|
||
| <div> | ||
| {{ component('textarea_component', { | ||
| 'dataModel': 'norender|content2:value' | ||
| }) }} | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. was this mix of the two syntaxes ever supported by LiveComponent ? I mean, documented etc ?
If not (what i'd say but with not certainety) this is not a bug fix but a feature :)
And one i fear I would be very critical of... as it creates (another) layer of confusion for people.
Do you think there any way we can solve your original core need, that does not require to add another degree of complexity here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smnandre Do you mean
norender|content:valuesyntax where both '|' and ':' is used together orI could fix 1. by checking that ':' is not present before trying to to extract the property name and amending the test to expect an exception thrown because the property is not present (because it is being returned literally and did not match a property).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More "2" here : I had the impression data-model was either used for child component using the xx:yy syntax, or into single components with modifiers, but not both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for taking the time to look at this!
I completely understand the concern about adding complexity, and I also agree that if mixing the two syntaxes was never intended, then treating this as a feature rather than a bugfix is reasonable.
The main reason we experienced this as a bug is not so much the specific behavior, but the inconsistency that emerges from reusing the same data-model attribute in both contexts.
If I write
this works exactly as documented. But if I wrap that same
<select>as the root element of a Twig component and use:the exact same syntax suddenly fails, even though from my perspective it feels like the same operation, just composed.
This is where my surprise comes from: not that modifiers exist, but that the same attribute value behaves differently depending on whether it’s applied directly to a DOM element or passed through a Twig component. If these two concepts were never meant to interact, then reusing data-model for both makes that distinction very hard to discover or reason about as a developer.
Since the different behaviour is not explicitly mentioned in the documentation, I believed this was a bug.
That said, I absolutely defer to the project’s direction here. If it's preferred to not support modifiers in Twig component bindings, I think documenting that limitation (or even failing fast with a clearer error) would already help a lot.
The approach in the PR felt like the least intrusive way to align the behavior with user expectations.
We can work around this issue in a couple of ways, one of them by manually adding modifiers in our Twig component like I describe in #3152 (comment)
Happy to adjust the PR in whatever direction makes the most sense for Symfony UX or contributing to explicitly documenting the behavior, and thanks again for the guidance! :)