Skip to content

Commit

Permalink
Merge pull request #1590 from LowerRockLabs/ColorColumn
Browse files Browse the repository at this point in the history
Add ColorColumn
  • Loading branch information
lrljoe authored Dec 9, 2023
2 parents 69368b8 + a45c6c0 commit c06563e
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 7 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ All notable changes to `laravel-livewire-tables` will be documented in this file

## UNRELEASED
### New Features
- Add capability to use as a Full Page Component
- Add DateColumn
- Add capability to use as a Full Page Component by @amshehzad and @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1580
- Add DateColumn by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1589
- Add ColorColumn by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1590

### Tweaks
- Internal - modify GitHub workflows to improve caching, but use unique caches per workflow matrix
- Internal - remove superfluous PHPStan ignoreErrors
- Internal - update Test Suite to also test at PHP 8.3
- Internal - tidying Classes & Traits by @lrljoe
- Docs - Update Anonymous Column documents to reference ability to use strings as well as views

## [v3.1.4] - 2023-12-04
Expand Down
Binary file modified database/database.sqlite
Binary file not shown.
1 change: 1 addition & 0 deletions database/migrations/create_test_tables.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CreateTestTables extends Migration
$table->string('name')->index();
$table->string('age')->nullable();
$table->date('last_visit')->nullable();
$table->string('favorite_color')->nullable();
$table->integer('species_id')->unsigned()->nullable();
$table->integer('breed_id')->unsigned()->nullable();
$table->foreign('species_id')->references('id')->on('species');
Expand Down
Binary file modified database/sqlite.database
Binary file not shown.
38 changes: 38 additions & 0 deletions docs/columns/other-column-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@ If you would like the BooleanColumn to display a plain Yes/No, you can set:
BooleanColumn::make('Active')
->yesNo()
```
## Color Columns

Color columns provide an easy way to a Color in a Column

You may pass either pass a CSS-compliant colour as a field
```php
ColorColumn::make('Favourite Colour', 'favourite_color'),
```

Or you may use a Callback
```php
ColorColumn::make('Favourite Colour')
->color(
function ($row) {
if ($row->success_rate < 40)
{
return '#ff0000';
}
else if ($row->success_rate > 90)
{
return '#008000';
}
else return '#ffa500';

}
),
```

You may also specify attributes to use on the div displaying the color, to adjust the size or appearance, this receives the full row. By default, this will replace the standard classes, to retain them, set "default" to true. To then over-ride, you should prefix your classes with "!" to signify importance.
```php
ColorColumn::make('Favourite Colour')
->attributes(function ($row) {
return [
'class' => '!rounded-lg self-center',
'default' => true,
];
}),
```

## Date Columns

Expand Down
14 changes: 14 additions & 0 deletions resources/views/includes/columns/color.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div @class([
'items-center content-center place-content-center place-items-center' => $isTailwind,
])
>
<div {{ $attributeBag->class([
'h-6 w-6 rounded-md self-center' => $isTailwind && ($attributeBag['default'] ?? (empty($attributeBag['class']) || (!empty($attributeBag['class']) && ($attributeBag['default'] ?? false)))),
]) }}
@style([
"background-color: {$color}" => $color,
])
>
</div>
</div>
48 changes: 48 additions & 0 deletions src/Views/Columns/ColorColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\ColorColumnConfiguration;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ColorColumnHelpers;
use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn;

class ColorColumn extends Column
{
use IsColumn;
use ColorColumnConfiguration,
ColorColumnHelpers;

public ?object $colorCallback = null;

public ?object $attributesCallback = null;

public string $defaultValue = '';

public string $view = 'livewire-tables::includes.columns.color';

public function __construct(string $title, ?string $from = null)
{
parent::__construct($title, $from);
if (! isset($from)) {
$this->label(fn () => null);
}

}

public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View

Check warning on line 35 in src/Views/Columns/ColorColumn.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Columns/ColorColumn.php#L35

Added line #L35 was not covered by tests
{
return view($this->getView())
->withIsTailwind($this->getComponent()->isTailwind())
->withIsBootstrap($this->getComponent()->isBootstrap())
->withColor($this->getColor($row))
->withAttributeBag($this->getAttributeBag($row));

Check warning on line 41 in src/Views/Columns/ColorColumn.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Columns/ColorColumn.php#L37-L41

Added lines #L37 - L41 were not covered by tests
}

public function getValue(Model $row)
{
return parent::getValue($row) ?? $this->getDefaultValue();
}
}
27 changes: 27 additions & 0 deletions src/Views/Traits/Configuration/ColorColumnConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration;

trait ColorColumnConfiguration
{
public function color(callable $callback): self
{
$this->colorCallback = $callback;

return $this;
}

public function attributes(callable $callback): self
{
$this->attributesCallback = $callback;

return $this;
}

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

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

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers;

use Illuminate\View\ComponentAttributeBag;

trait ColorColumnHelpers
{
// TODO: Test
public function getView(): string
{
return $this->view;
}

public function getDefaultValue(): string
{
return $this->defaultValue;
}

// TODO: Test
public function getColor($row): string
{
return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : ($this->getValue($row) ?? $this->getDefaultValue());
}

// TODO: Test
public function getAttributeBag($row)
{
return new ComponentAttributeBag($this->hasAttributesCallback() ? app()->call($this->getAttributesCallback(), ['row' => $row]) : []);
}

public function getColorCallback(): ?callable
{
return $this->colorCallback;
}

public function hasColorCallback(): bool
{
return isset($this->colorCallback);
}

// TODO: Test
public function getAttributesCallback(): ?callable
{
return $this->attributesCallback;
}

public function hasAttributesCallback(): bool
{
return $this->attributesCallback !== null;
}
}
10 changes: 5 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected function setUp(): void
]);

Pet::insert([
['id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4, 'last_visit' => '2023-01-04'],
['id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4, 'last_visit' => '2023-02-04'],
['id' => 3, 'name' => 'May', 'age' => 2, 'species_id' => 2, 'breed_id' => 102, 'last_visit' => null],
['id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200, 'last_visit' => '2023-04-04'],
['id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202, 'last_visit' => '2023-05-04'],
['id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4, 'last_visit' => '2023-01-04', 'favorite_color' => '#000000'],
['id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4, 'last_visit' => '2023-02-04', 'favorite_color' => '#FF0000'],
['id' => 3, 'name' => 'May', 'age' => 2, 'species_id' => 2, 'breed_id' => 102, 'last_visit' => null, 'favorite_color' => '#00FF00'],
['id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200, 'last_visit' => '2023-04-04', 'favorite_color' => '#0000FF'],
['id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202, 'last_visit' => '2023-05-04', 'favorite_color' => '#FFFFFF'],
]);

Veterinary::insert([
Expand Down
Loading

0 comments on commit c06563e

Please sign in to comment.