From df7109b2064744d91ad28e93894ecbb88336133e Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 4 Aug 2024 15:48:31 +0100 Subject: [PATCH 1/4] Add customComponent approach --- src/Views/Columns/ViewComponentColumn.php | 19 +++++++++++++++++-- .../ViewComponentColumnConfiguration.php | 8 ++++++++ .../Helpers/ViewComponentColumnHelpers.php | 11 +++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Views/Columns/ViewComponentColumn.php b/src/Views/Columns/ViewComponentColumn.php index 814949fab..1f94b16a0 100644 --- a/src/Views/Columns/ViewComponentColumn.php +++ b/src/Views/Columns/ViewComponentColumn.php @@ -3,6 +3,7 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; use Illuminate\Database\Eloquent\Model; +use ReflectionClass; use Illuminate\Support\HtmlString; use Illuminate\View\ComponentAttributeBag; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; @@ -15,7 +16,9 @@ class ViewComponentColumn extends Column use ViewComponentColumnConfiguration, ViewComponentColumnHelpers; - protected string $componentView; + protected ?string $componentView; + + protected ?string $customComponentView; public function __construct(string $title, ?string $from = null) { @@ -45,6 +48,18 @@ 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..52851683f 100644 --- a/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php @@ -13,4 +13,12 @@ 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..60b7eab58 100644 --- a/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php +++ b/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php @@ -19,4 +19,15 @@ public function hasComponentView(): bool { return isset($this->componentView); } + + public function hasCustomComponent(): bool + { + return isset($this->customComponentView); + } + + public function getCustomComponent(): string + { + return $this->customComponentView; + } + } From 5a2335c9454db98b8c461cb7812a6442fb7bab2b Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 4 Aug 2024 14:48:53 +0000 Subject: [PATCH 2/4] Fix styling --- src/Views/Columns/ViewComponentColumn.php | 11 ++++------- .../ViewComponentColumnConfiguration.php | 1 - .../Traits/Helpers/ViewComponentColumnHelpers.php | 1 - 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Views/Columns/ViewComponentColumn.php b/src/Views/Columns/ViewComponentColumn.php index 1f94b16a0..dc99d313b 100644 --- a/src/Views/Columns/ViewComponentColumn.php +++ b/src/Views/Columns/ViewComponentColumn.php @@ -3,13 +3,13 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; use Illuminate\Database\Eloquent\Model; -use ReflectionClass; use Illuminate\Support\HtmlString; use Illuminate\View\ComponentAttributeBag; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; 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 { @@ -48,18 +48,15 @@ public function getContents(Model $row): null|string|HtmlString|DataTableConfigu } } - if ($this->hasCustomComponent()) - { + if ($this->hasCustomComponent()) { $reflectionClass = new ReflectionClass($this->getCustomComponent()); $reflectionInstance = $reflectionClass->newInstanceArgs($attributes); return $reflectionInstance->render(); + } else { + return view($this->getComponentView())->with($attributes); } - else { - return view($this->getComponentView())->with($attributes); - } - } } diff --git a/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php b/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php index 52851683f..17a630742 100644 --- a/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php @@ -20,5 +20,4 @@ public function customComponent(string $customComponentView): self return $this; } - } diff --git a/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php b/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php index 60b7eab58..0dcddc278 100644 --- a/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php +++ b/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php @@ -29,5 +29,4 @@ public function getCustomComponent(): string { return $this->customComponentView; } - } From a57f89cc5700015697d61d14b9414c24fbf7fc0b Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 4 Aug 2024 15:50:37 +0100 Subject: [PATCH 3/4] Allow for hasCustomComponent --- src/Views/Columns/ViewComponentColumn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Views/Columns/ViewComponentColumn.php b/src/Views/Columns/ViewComponentColumn.php index dc99d313b..b4f6d3a8c 100644 --- a/src/Views/Columns/ViewComponentColumn.php +++ b/src/Views/Columns/ViewComponentColumn.php @@ -33,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'); } From 3e183761aa13938b4de122314024aa1162965941 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 4 Aug 2024 16:01:34 +0100 Subject: [PATCH 4/4] Add Basic Tests --- .../Views/Columns/ViewComponentColumnTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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('
2420
', $contents); + $this->assertTrue($column->hasCustomComponent()); + + } + /*public function test_can_render_component(): void {