Skip to content

Commit

Permalink
[TwigComponent] Merge data-action when using ComponentAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Nov 22, 2023
1 parent ea497d7 commit 0cafa36
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function defaults(iterable $attributes): self
}

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

continue;
Expand Down
22 changes: 22 additions & 0 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ public function testCanAddStimulusControllerViaStimulusAttributes(): void
], $attributes->all());
}

public function testCanAddStimulusActionViaStimulusAttributes(): void
{
// if PHP less than 8.1, skip
if (version_compare(\PHP_VERSION, '8.1.0', '<')) {
$this->markTestSkipped('PHP 8.1+ required');
}

$attributes = new ComponentAttributes([
'class' => 'foo',
'data-action' => 'live#foo',
]);

$stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader()));
$stimulusAttributes->addAction('foo', 'barMethod');
$attributes = $attributes->defaults([...$stimulusAttributes]);

$this->assertEquals([
'class' => 'foo',
'data-action' => 'foo#barMethod live#foo',
], $attributes->all());
}

public function testBooleanBehaviour(): void
{
$attributes = new ComponentAttributes(['disabled' => true]);
Expand Down

0 comments on commit 0cafa36

Please sign in to comment.