Skip to content

Commit d9bb513

Browse files
authored
[11.x] Always inherit parent attributes (#53011)
* Always inherit parent attributes * tests
1 parent d9ecee5 commit d9bb513

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Illuminate/View/ComponentAttributeBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSeria
3434
*/
3535
public function __construct(array $attributes = [])
3636
{
37-
$this->attributes = $attributes;
37+
$this->setAttributes($attributes);
3838
}
3939

4040
/**

tests/View/ViewComponentTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\View\Component;
66
use Illuminate\View\ComponentAttributeBag;
7+
use Illuminate\View\ComponentSlot;
78
use PHPUnit\Framework\TestCase;
89
use ReflectionMethod;
910

@@ -70,6 +71,22 @@ public function testAttributeParentInheritance(): void
7071
$this->assertSame('class="override" type="submit"', (string) $component->attributes);
7172
}
7273

74+
public function testSlotAttributeParentInheritance(): void
75+
{
76+
$attributes = new ComponentAttributeBag(['class' => 'bar', 'type' => 'button']);
77+
78+
$slot = new ComponentSlot('test', [
79+
'class' => 'foo',
80+
'attributes' => $attributes,
81+
]);
82+
83+
$this->assertSame('class="foo bar" type="button"', (string) $slot->attributes);
84+
85+
// Test overriding parent class attributes
86+
$slot->withAttributes(['class' => 'override', 'type' => 'submit']);
87+
$this->assertSame('class="override" type="submit"', (string) $slot->attributes);
88+
}
89+
7390
public function testPublicMethodsWithNoArgsAreConvertedToStringableCallablesInvokedAndNotCached()
7491
{
7592
$component = new TestSampleViewComponent;

0 commit comments

Comments
 (0)