From 72aef1d040a21590c3ddca50d75400654577f78d Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Thu, 16 Sep 2021 22:43:00 -0400 Subject: [PATCH] Allow index.blade.php views for anonymous components --- .../View/Compilers/ComponentTagCompiler.php | 4 ++++ .../View/Blade/BladeComponentTagCompilerTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Illuminate/View/Compilers/ComponentTagCompiler.php b/src/Illuminate/View/Compilers/ComponentTagCompiler.php index 23bf15565734..469bd783664b 100644 --- a/src/Illuminate/View/Compilers/ComponentTagCompiler.php +++ b/src/Illuminate/View/Compilers/ComponentTagCompiler.php @@ -272,6 +272,10 @@ public function componentClass(string $component) return $view; } + if ($viewFactory->exists($view = $this->guessViewName($component).'.index')) { + return $view; + } + throw new InvalidArgumentException( "Unable to locate a class or view for component [{$component}]." ); diff --git a/tests/View/Blade/BladeComponentTagCompilerTest.php b/tests/View/Blade/BladeComponentTagCompilerTest.php index 3a5bc700ebb2..eafec7d047d7 100644 --- a/tests/View/Blade/BladeComponentTagCompilerTest.php +++ b/tests/View/Blade/BladeComponentTagCompilerTest.php @@ -249,6 +249,22 @@ public function testClasslessComponents() '@endComponentClass##END-COMPONENT-CLASS##', trim($result)); } + public function testClasslessComponentsWithIndexView() + { + $container = new Container; + $container->instance(Application::class, $app = Mockery::mock(Application::class)); + $container->instance(Factory::class, $factory = Mockery::mock(Factory::class)); + $app->shouldReceive('getNamespace')->andReturn('App\\'); + $factory->shouldReceive('exists')->andReturn(false, true); + Container::setInstance($container); + + $result = $this->compiler()->compileTags(''); + + $this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'anonymous-component', ['view' => 'components.anonymous-component.index','data' => ['name' => 'Taylor','age' => 31,'wire:model' => 'foo']]) +withAttributes(['name' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute('Taylor'),'age' => 31,'wire:model' => 'foo']); ?>\n". +'@endComponentClass##END-COMPONENT-CLASS##', trim($result)); + } + public function testPackagesClasslessComponents() { $container = new Container;