Skip to content

Commit 7794893

Browse files
committed
bug #1288 [TwigComponent] Merge data-action in ComponentAttributes (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [TwigComponent] Merge data-action in ComponentAttributes | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | None that I know of | License | MIT If you pass `data-action` into a component & the root `attributes` has a `data-action` already thanks to `attributes.defaults`, they should be merged together. Same behavior as `data-controller`. I found this while adding a `data-action` to a component and suddenly losing my existing `data-action` from inside. Cheers! Commits ------- 0cafa36 [TwigComponent] Merge data-action when using ComponentAttributes
2 parents e521321 + 0cafa36 commit 7794893

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function defaults(iterable $attributes): self
8080
}
8181

8282
foreach ($this->attributes as $key => $value) {
83-
if (\in_array($key, ['class', 'data-controller'], true) && isset($attributes[$key])) {
83+
if (\in_array($key, ['class', 'data-controller', 'data-action'], true) && isset($attributes[$key])) {
8484
$attributes[$key] = "{$attributes[$key]} {$value}";
8585

8686
continue;

src/TwigComponent/tests/Unit/ComponentAttributesTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ public function testCanAddStimulusControllerViaStimulusAttributes(): void
146146
], $attributes->all());
147147
}
148148

149+
public function testCanAddStimulusActionViaStimulusAttributes(): void
150+
{
151+
// if PHP less than 8.1, skip
152+
if (version_compare(\PHP_VERSION, '8.1.0', '<')) {
153+
$this->markTestSkipped('PHP 8.1+ required');
154+
}
155+
156+
$attributes = new ComponentAttributes([
157+
'class' => 'foo',
158+
'data-action' => 'live#foo',
159+
]);
160+
161+
$stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
162+
$stimulusAttributes->addAction('foo', 'barMethod');
163+
$attributes = $attributes->defaults([...$stimulusAttributes]);
164+
165+
$this->assertEquals([
166+
'class' => 'foo',
167+
'data-action' => 'foo#barMethod live#foo',
168+
], $attributes->all());
169+
}
170+
149171
public function testBooleanBehaviour(): void
150172
{
151173
$attributes = new ComponentAttributes(['disabled' => true]);

0 commit comments

Comments
 (0)