-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* AddViewComponentColumn * Add ViewComponentColumn Docs * Add ViewComponentColumnTests --------- Co-authored-by: lrljoe <[email protected]>
- Loading branch information
Showing
8 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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).' />'); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |