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

Add ViewComponentColumn #1779

Merged
merged 6 commits into from
Jul 19, 2024
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
32 changes: 32 additions & 0 deletions docs/column-types/view_component_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: View Component Columns
weight: 14
---

View Component columns let you specify a component name and attributes and provide attributes to the View Component. This will render the View Component in it's entirety.

```php

ViewComponentColumn::make('E-mail', 'email')
->component('email')
->attributes(fn ($value, $row, Column $column) => [
'id' => $row->id,
'email' => $value,
]),
```

Please also see the following for other available methods:
<ul>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/columns/available-methods">Available Methods</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/columns/column-selection">Column Selection</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/columns/secondary-header">Secondary Header</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/columns/footer">Footer</a>
</li>
</ul>
2 changes: 1 addition & 1 deletion docs/column-types/wire_link_column.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Wire Link Column (beta)
weight: 14
weight: 15
---

WireLink columns provide a way to display Wired Links in your table without having to use `format()` or partial views, with or without a Confirmation Message
Expand Down
51 changes: 51 additions & 0 deletions src/Views/Columns/ViewComponentColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Columns;

use Illuminate\Database\Eloquent\Model;
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;

class ViewComponentColumn extends Column
{
use ViewComponentColumnConfiguration,
ViewComponentColumnHelpers;

protected string $componentView;

public function __construct(string $title, ?string $from = null)
{
parent::__construct($title, $from);
$this->html();

}

public function getContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
if ($this->isLabel()) {
throw new DataTableConfigurationException('You can not use a label column with a component column');
}

if ($this->hasComponentView() === false) {
throw new DataTableConfigurationException('You must specify a component view for a component column');
}

$value = $this->getValue($row);
$attributes = ['value' => $value];

if ($this->hasAttributesCallback()) {
$attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this);

if (! is_array($attributes)) {
throw new DataTableConfigurationException('The return type of callback must be an array');
}
}

return \Illuminate\Support\Facades\Blade::render(
'<x-'.$this->getComponentView().' '.new ComponentAttributeBag($attributes).' />');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration;

trait ViewComponentColumnConfiguration
{
/**
* Defines the View Component to be used for the Column
*/
public function component(string $component): self
{
$this->componentView = $component;

return $this;
}
}
22 changes: 22 additions & 0 deletions src/Views/Traits/Helpers/ViewComponentColumnHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers;

trait ViewComponentColumnHelpers
{
/**
* Retrieves the defined Component View
*/
public function getComponentView(): string
{
return $this->componentView;
}

/**
* Determines whether a Component View has been set
*/
public function hasComponentView(): bool
{
return isset($this->componentView);
}
}
27 changes: 27 additions & 0 deletions tests/Http/TestComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Http;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class TestComponent extends Component
{
public int $testItem = 0;

public function __construct(int $age)
{
$this->testItem = $age * 110;
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return \Illuminate\Support\Facades\Blade::render(
'<div>'.($this->testItem ?? 'Unknown').'</div>');

}
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
use BladeUI\Icons\BladeIconsServiceProvider;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\DB;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\{PetsTable,PetsTableUnpaginated,SpeciesTable};
use Rappasoft\LaravelLivewireTables\Tests\Http\TestComponent;
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
Expand All @@ -30,6 +32,8 @@ protected function setUp(): void
{
parent::setUp();

Blade::component('test-component', TestComponent::class);

if (! Breed::where('id', 1)->get()) {
include_once __DIR__.'/../database/migrations/create_test_tables.php.stub';
(new \CreateTestTables())->down();
Expand Down
55 changes: 55 additions & 0 deletions tests/Views/Columns/ViewComponentColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Views\Columns;

use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ViewComponentColumn;

final class ViewComponentColumnTest extends TestCase
{
public function test_can_set_the_column_title(): void
{
$column = ViewComponentColumn::make('Total Users');

$this->assertSame('Total Users', $column->getTitle());
}

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->assertTrue($column->hasComponentView());
}

public function test_can_not_omit_component(): void
{
$this->expectException(DataTableConfigurationException::class);

$column = ViewComponentColumn::make('Age 2', 'age')
->attributes(fn ($value, $row, Column $column) => [
'age' => $row->age,
]);
$contents = $column->getContents(Pet::find(1));
$this->assertSame('<div>2420</div>', $contents);

}

public function test_can_render_component(): void
{

$column = ViewComponentColumn::make('Age 2', 'age')
->component('test-component')
->attributes(fn ($value, $row, Column $column) => [
'age' => $row->age,
]);
$contents = $column->getContents(Pet::find(1));
$this->assertSame('<div>2420</div>', $contents);

}
}
Loading