Skip to content

Commit

Permalink
View component column fixes (#1825)
Browse files Browse the repository at this point in the history
* Add customComponent approach

* Add Basic Tests

---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe authored Aug 4, 2024
1 parent 2c4b775 commit 0b5486e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/Views/Columns/ViewComponentColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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');
}

Expand All @@ -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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ public function component(string $component): self

return $this;
}

public function customComponent(string $customComponentView): self
{
$this->customComponentView = $customComponentView;

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Views/Traits/Helpers/ViewComponentColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 18 additions & 1 deletion tests/Views/Columns/ViewComponentColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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('<div>2420</div>', $contents);
$this->assertTrue($column->hasCustomComponent());

}

/*public function test_can_render_component(): void
{
Expand Down

0 comments on commit 0b5486e

Please sign in to comment.