Skip to content

Commit e00d0cd

Browse files
authored
fix: view components with multiple attributes (#599)
1 parent fef15c0 commit e00d0cd

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

src/Tempest/View/src/Attributes/DataAttribute.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public function __construct(
1919

2020
public function apply(Element $element): Element
2121
{
22-
if (! $element instanceof ViewComponentElement) {
22+
if (
23+
! $element instanceof ViewComponentElement
24+
&& ! $element instanceof PhpDataElement
25+
) {
2326
return $element;
2427
}
2528

src/Tempest/View/src/Elements/PhpDataElement.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function __construct(
1919
) {
2020
}
2121

22+
public function getAttribute(string $name): string|null
23+
{
24+
return $this->wrappingElement->getAttribute($name);
25+
}
26+
2227
public function compile(): string
2328
{
2429
$name = ltrim($this->name, ':');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<x-component name="x-view-component-with-multiple-attributes">
2+
<div class="a">
3+
{{ $a }}
4+
</div>
5+
<div class="b">
6+
{{ $b }}
7+
</div>
8+
</x-component>

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,24 @@ public function test_render_element_with_attribute_with_dash(): void
372372
$html,
373373
);
374374
}
375+
376+
public function test_view_component_with_multiple_attributes(): void
377+
{
378+
$expected = '<div class="a">
379+
a </div>
380+
<div class="b">
381+
b </div>';
382+
383+
$html = $this->render(view('<x-view-component-with-multiple-attributes a="a" b="b"></x-view-component-with-multiple-attributes>'));
384+
$this->assertStringEqualsStringIgnoringLineEndings($expected, $html);
385+
386+
$html = $this->render(view('<x-view-component-with-multiple-attributes a="a" :b="\'b\'"></x-view-component-with-multiple-attributes>'));
387+
$this->assertStringEqualsStringIgnoringLineEndings($expected, $html);
388+
389+
$html = $this->render(view('<x-view-component-with-multiple-attributes :a="\'a\'" :b="\'b\'"></x-view-component-with-multiple-attributes>'));
390+
$this->assertStringEqualsStringIgnoringLineEndings($expected, $html);
391+
392+
$html = $this->render(view('<x-view-component-with-multiple-attributes :a="\'a\'" b="b"></x-view-component-with-multiple-attributes>'));
393+
$this->assertStringEqualsStringIgnoringLineEndings($expected, $html);
394+
}
375395
}

tests/Integration/View/ViewComponentTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ public function test_nested_components(): void
6161
$this->assertStringEqualsStringIgnoringLineEndings(
6262
expected: <<<'HTML'
6363
<form action="#" method="post"><div><div><label for="a">a</label><input type="number" name="a" id="a" value></input></div>
64+
65+
6466
</div>
6567
<div><label for="b">b</label><input type="text" name="b" id="b" value></input></div>
6668
69+
70+
6771
</form>
6872
HTML,
6973
actual: $this->render(view(

0 commit comments

Comments
 (0)