Skip to content

Commit 0cafa36

Browse files
committed
[TwigComponent] Merge data-action when using ComponentAttributes
1 parent ea497d7 commit 0cafa36

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)