From 4ce53774d7c9d23d8c3725579b1ebf8935496455 Mon Sep 17 00:00:00 2001 From: Roy Duineveld Date: Wed, 2 Oct 2024 09:08:00 +0200 Subject: [PATCH 1/2] Always inherit parent attributes --- src/Illuminate/View/ComponentAttributeBag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/View/ComponentAttributeBag.php b/src/Illuminate/View/ComponentAttributeBag.php index 868a97f5aac3..0e3fdc3ed566 100644 --- a/src/Illuminate/View/ComponentAttributeBag.php +++ b/src/Illuminate/View/ComponentAttributeBag.php @@ -34,7 +34,7 @@ class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSeria */ public function __construct(array $attributes = []) { - $this->attributes = $attributes; + $this->setAttributes($attributes); } /** From f99125dbe40b41413a0289b9ca8a0fa2a5835311 Mon Sep 17 00:00:00 2001 From: Roy Duineveld Date: Thu, 3 Oct 2024 11:48:11 +0200 Subject: [PATCH 2/2] tests --- tests/View/ViewComponentTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/View/ViewComponentTest.php b/tests/View/ViewComponentTest.php index e4e3701bbaa5..c21573d77dd2 100644 --- a/tests/View/ViewComponentTest.php +++ b/tests/View/ViewComponentTest.php @@ -4,6 +4,7 @@ use Illuminate\View\Component; use Illuminate\View\ComponentAttributeBag; +use Illuminate\View\ComponentSlot; use PHPUnit\Framework\TestCase; use ReflectionMethod; @@ -70,6 +71,22 @@ public function testAttributeParentInheritance(): void $this->assertSame('class="override" type="submit"', (string) $component->attributes); } + public function testSlotAttributeParentInheritance(): void + { + $attributes = new ComponentAttributeBag(['class' => 'bar', 'type' => 'button']); + + $slot = new ComponentSlot('test', [ + 'class' => 'foo', + 'attributes' => $attributes, + ]); + + $this->assertSame('class="foo bar" type="button"', (string) $slot->attributes); + + // Test overriding parent class attributes + $slot->withAttributes(['class' => 'override', 'type' => 'submit']); + $this->assertSame('class="override" type="submit"', (string) $slot->attributes); + } + public function testPublicMethodsWithNoArgsAreConvertedToStringableCallablesInvokedAndNotCached() { $component = new TestSampleViewComponent;