-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial Commit - Awaiting Docs & Tests * Tweak to IconColumn label behaviour * Adding IconColumn documentation * Add Additional Tests - Replace Test Database * Add Visuals Test for IconColumn * Add Icon Columns to Other Column Types * Undo FrontendAssetsTest Changes --------- Co-authored-by: lrljoe <[email protected]>
- Loading branch information
Showing
17 changed files
with
361 additions
and
9 deletions.
There are no files selected for viewing
Binary file not shown.
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,87 @@ | ||
--- | ||
title: Icon Columns (beta) | ||
weight: 10 | ||
--- | ||
|
||
Icon columns provide a way to display icons in your table without having to use `format()` or partial views. | ||
|
||
### setIcon | ||
setIcon requires a valid path to an SVG (Directly or via a Library), it receives the $row, and $value (if available) to help you customise which icon to use | ||
```php | ||
IconColumn::make('Icon', 'status') | ||
->setIcon(function ($row, $value) { | ||
if($value == 1) { | ||
return "heroicon-o-check-circle"; | ||
} | ||
else | ||
{ | ||
return "heroicon-o-x-circle"; | ||
} | ||
}), | ||
``` | ||
|
||
### attributes | ||
Attributes receives the $row, and $value (if available) to help you customise which attributes to apply, you may pass both classes, and other SVG specific attributes. | ||
```php | ||
IconColumn::make('Icon', 'status') | ||
->setIcon(function ($row, $value) { if($value == 1) { return "heroicon-o-check-circle"; } else { return "heroicon-o-x-circle"; } }) | ||
->attributes(function ($row, $value) { | ||
if($value == 1) { | ||
return [ | ||
'class' => 'w-6 h-6', | ||
'stroke' => '#008000' | ||
]; | ||
} | ||
else | ||
{ | ||
return [ | ||
'class' => 'w-3 h-3', | ||
'stroke' => '#FF0000' | ||
]; | ||
} | ||
}), | ||
``` | ||
|
||
For example: | ||
### Example | ||
```php | ||
IconColumn::make('Icon', 'status') | ||
->setIcon(function ($row, $value) { if($value == 1) { return "heroicon-o-check-circle"; } else { return "heroicon-o-x-circle"; } }) | ||
->attributes(function ($row, $value) { | ||
if($value == 3) { | ||
return [ | ||
'class' => 'w-3 h-3', | ||
'stroke' => '#008000' | ||
]; | ||
} | ||
else if($value == 2) { | ||
return [ | ||
'class' => 'w-3 h-3', | ||
'stroke' => '#0000FF' | ||
]; | ||
} | ||
else | ||
{ | ||
return [ | ||
'class' => 'w-3 h-3', | ||
'stroke' => '#FF0000' | ||
]; | ||
} | ||
}), | ||
``` | ||
|
||
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
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
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
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,7 @@ | ||
<div class="livewire-tables-columns-icon"> | ||
@svg( | ||
$icon, | ||
$classes, | ||
$attributes, | ||
) | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Views\Columns; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; | ||
use Rappasoft\LaravelLivewireTables\Views\Column; | ||
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\IconColumnConfiguration; | ||
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\IconColumnHelpers; | ||
use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; | ||
|
||
class IconColumn extends Column | ||
{ | ||
use IsColumn; | ||
use IconColumnConfiguration, | ||
IconColumnHelpers; | ||
|
||
public ?\Closure $iconCallback; | ||
|
||
protected string $view = 'livewire-tables::includes.columns.icon'; | ||
|
||
public function __construct(string $title, ?string $from = null) | ||
{ | ||
parent::__construct($title, $from); | ||
if (! isset($from)) { | ||
$this->label(fn () => null); | ||
} | ||
|
||
$this->html(); | ||
} | ||
|
||
public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View | ||
{ | ||
$attributeBag = $this->getAttributeBag($row); | ||
|
||
return view($this->getView()) | ||
->withIsTailwind($this->isTailwind()) | ||
->withIsBootstrap($this->isBootstrap()) | ||
->withIcon($this->getIcon($row)) | ||
->withClasses($attributeBag['class']) | ||
->withAttributes(collect($attributeBag)->except('class')->toArray()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Views/Traits/Configuration/IconColumnConfiguration.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,13 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration; | ||
|
||
trait IconColumnConfiguration | ||
{ | ||
public function setIcon(\Closure $callback): self | ||
{ | ||
$this->iconCallback = $callback; | ||
|
||
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
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\Views\Traits\Helpers; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\View\ComponentAttributeBag; | ||
use Rappasoft\LaravelLivewireTables\Views\Traits\Columns\HasDefaultStringValue; | ||
|
||
trait IconColumnHelpers | ||
{ | ||
use HasDefaultStringValue; | ||
|
||
public function getIcon(Model $row): string | ||
{ | ||
return $this->hasIconCallback() ? app()->call($this->getIconCallback(), ['row' => $row, 'value' => $this->getValue($row) ?? '']) : ($this->getValue($row)); | ||
} | ||
|
||
public function getIconCallback(): ?callable | ||
{ | ||
return $this->iconCallback; | ||
} | ||
|
||
public function hasIconCallback(): bool | ||
{ | ||
return isset($this->iconCallback); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Visuals\Columns; | ||
|
||
use Exception; | ||
use Illuminate\View\ViewException; | ||
use Livewire\Livewire; | ||
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; | ||
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\FailingTables\{BrokenSecondaryHeaderTable, NoBuildMethodTable, NoPrimaryKeyTable}; | ||
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\{PetsTable,PetsTableAttributes}; | ||
use Rappasoft\LaravelLivewireTables\Tests\TestCase; | ||
|
||
final class IconColumnVisualsTest extends TestCase | ||
{ | ||
private $testErrors; | ||
|
||
public function test_icon_column_renders_correctly(): void | ||
{ | ||
Livewire::test(new class extends PetsTable | ||
{ | ||
public function configure(): void | ||
{ | ||
$this->setPrimaryKey('id'); | ||
} | ||
|
||
public function columns(): array | ||
{ | ||
return [ | ||
\Rappasoft\LaravelLivewireTables\Views\Column::make('Name')->searchable(), | ||
\Rappasoft\LaravelLivewireTables\Views\Columns\IconColumn::make('Old Age', 'age') | ||
->setIcon(function (\Rappasoft\LaravelLivewireTables\Tests\Models\Pet $row, int $value) { | ||
if ($value >= 5) { | ||
return 'heroicon-o-check-circle'; | ||
} else { | ||
return 'heroicon-o-x-circle'; | ||
} | ||
}), | ||
]; | ||
} | ||
|
||
public function filters(): array | ||
{ | ||
return []; | ||
} | ||
}) | ||
->call('setSearch', 'Cartman') | ||
->assertSeeHtmlInOrder([ | ||
'<div class="livewire-tables-columns-icon">', | ||
'<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">', | ||
'<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/>', | ||
'</svg></div>', | ||
]) | ||
->assertDontSeeHtml('<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/>') | ||
->call('setSearch', 'May') | ||
->assertDontSeeHtml('<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/>') | ||
->assertSeeHtmlInOrder([ | ||
'<div class="livewire-tables-columns-icon">', | ||
'<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">', | ||
'<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/>', | ||
'</svg></div>', | ||
]); | ||
|
||
} | ||
} |
Oops, something went wrong.