diff --git a/src/Views/Columns/ViewComponentColumn.php b/src/Views/Columns/ViewComponentColumn.php index 814949fab..b4f6d3a8c 100644 --- a/src/Views/Columns/ViewComponentColumn.php +++ b/src/Views/Columns/ViewComponentColumn.php @@ -9,13 +9,16 @@ use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ViewComponentColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ViewComponentColumnHelpers; +use ReflectionClass; class ViewComponentColumn extends Column { use ViewComponentColumnConfiguration, ViewComponentColumnHelpers; - protected string $componentView; + protected ?string $componentView; + + protected ?string $customComponentView; public function __construct(string $title, ?string $from = null) { @@ -30,7 +33,7 @@ public function getContents(Model $row): null|string|HtmlString|DataTableConfigu throw new DataTableConfigurationException('You can not use a label column with a component column'); } - if ($this->hasComponentView() === false) { + if ($this->hasComponentView() === false && $this->hasCustomComponent() === false) { throw new DataTableConfigurationException('You must specify a component view for a component column'); } @@ -45,6 +48,15 @@ public function getContents(Model $row): null|string|HtmlString|DataTableConfigu } } - return view($this->getComponentView())->with($attributes); + if ($this->hasCustomComponent()) { + $reflectionClass = new ReflectionClass($this->getCustomComponent()); + + $reflectionInstance = $reflectionClass->newInstanceArgs($attributes); + + return $reflectionInstance->render(); + } else { + return view($this->getComponentView())->with($attributes); + } + } } diff --git a/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php b/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php index 7e0300854..17a630742 100644 --- a/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php @@ -13,4 +13,11 @@ public function component(string $component): self return $this; } + + public function customComponent(string $customComponentView): self + { + $this->customComponentView = $customComponentView; + + return $this; + } } diff --git a/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php b/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php index 8de648e84..0dcddc278 100644 --- a/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php +++ b/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php @@ -19,4 +19,14 @@ public function hasComponentView(): bool { return isset($this->componentView); } + + public function hasCustomComponent(): bool + { + return isset($this->customComponentView); + } + + public function getCustomComponent(): string + { + return $this->customComponentView; + } } diff --git a/tests/Views/Columns/ViewComponentColumnTest.php b/tests/Views/Columns/ViewComponentColumnTest.php index 0b80b3c01..3fff77cd4 100644 --- a/tests/Views/Columns/ViewComponentColumnTest.php +++ b/tests/Views/Columns/ViewComponentColumnTest.php @@ -28,10 +28,12 @@ public function test_can_set_the_column_title(): void public function test_can_have_component_view(): void { $column = ViewComponentColumn::make('Age 2', 'age') - ->component('test-component') ->attributes(fn ($value, $row, Column $column) => [ 'age' => $row->age, ]); + + $this->assertFalse($column->hasComponentView()); + $column->component('test-component'); $this->assertTrue($column->hasComponentView()); } @@ -48,6 +50,21 @@ public function test_can_not_omit_component(): void } + public function test_can_use_custom_component(): void + { + $column = ViewComponentColumn::make('Age 2', 'age') + ->attributes(fn ($value, $row, Column $column) => [ + 'age' => $row->age, + ]); + + $this->assertFalse($column->hasCustomComponent()); + $column->customComponent(\Rappasoft\LaravelLivewireTables\Tests\Http\TestComponent::class); + $contents = $column->getContents(Pet::find(1)); + $this->assertSame('