Skip to content

Commit

Permalink
make remove method immutable and test if attributes are still render
Browse files Browse the repository at this point in the history
  • Loading branch information
matheo committed Aug 18, 2023
1 parent 66b7f58 commit 416c46a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ public function add($stimulusDto): self
return $clone->defaults($controllersAttributes);
}

public function remove($key): void
public function remove($key): self
{
unset($this->attributes[$key]);
$attributes = $this->attributes;

unset($attributes[$key]);

return new self($attributes);
}
}
2 changes: 1 addition & 1 deletion src/TwigComponent/src/Twig/PropsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function compile(Compiler $compiler): void
foreach ($this->getAttribute('names') as $name) {
$compiler
->addDebugInfo($this)
->write('$context[\'attributes\']->remove(\''.$name.'\');')
->write('$context[\'attributes\'] = $context[\'attributes\']->remove(\''.$name.'\');')
->write('if (!isset($context[\''.$name.'\'])) {')
;

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<twig:UserCard :user='user'/>
<twig:UserCard :user='user' class='foo'/>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% props user %}

<div {{ attributes }} class='user-card'>
<div {{ attributes }}>
<p>{{ user.name }}</p>
<p>{{ user.email }}</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function testRenderAnonymousComponentWithNonScalarProps(): void

$output = self::getContainer()->get(Environment::class)->render('anonymous_component_none_scalar_prop.html.twig', ['user' => $user]);

$this->assertStringContainsString('class="foo"', $output);
$this->assertStringContainsString('Fabien', $output);
$this->assertStringContainsString('[email protected]', $output);
}
Expand Down

0 comments on commit 416c46a

Please sign in to comment.