Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View component column fixes #1825

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
}
}

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);

Check warning on line 58 in src/Views/Columns/ViewComponentColumn.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Columns/ViewComponentColumn.php#L58

Added line #L58 was not covered by tests
}

}
}
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
Loading