From 266457fe30cafa166c250f837f07c4650e295051 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 19:28:49 +0000 Subject: [PATCH 01/22] Basic ColorColumn - With ColorCallback --- .../views/includes/columns/color.blade.php | 15 +++++++ src/Views/Columns/ColorColumn.php | 39 +++++++++++++++++++ .../ColorColumnConfiguration.php | 26 +++++++++++++ .../Traits/Helpers/ColorColumnHelpers.php | 30 ++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 resources/views/includes/columns/color.blade.php create mode 100644 src/Views/Columns/ColorColumn.php create mode 100644 src/Views/Traits/Configuration/ColorColumnConfiguration.php create mode 100644 src/Views/Traits/Helpers/ColorColumnHelpers.php diff --git a/resources/views/includes/columns/color.blade.php b/resources/views/includes/columns/color.blade.php new file mode 100644 index 000000000..b11cc8b0a --- /dev/null +++ b/resources/views/includes/columns/color.blade.php @@ -0,0 +1,15 @@ +
+
!empty($customClasses['class']), + 'h-6 w-6 rounded-md self-center' => $customClasses['default'] ?? false, + ]) + @style([ + "background-color: {$color}" => $color, + ]) + > +
+
diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php new file mode 100644 index 000000000..6a0fa73a3 --- /dev/null +++ b/src/Views/Columns/ColorColumn.php @@ -0,0 +1,39 @@ +attributes['class'] = $this->getCustomClasses(); + + + return view($this->getView()) + ->withColor($this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row)) + ->withCustomClasses($this->getCustomClasses()) + ->withAttributes($this->attributes); + } +} diff --git a/src/Views/Traits/Configuration/ColorColumnConfiguration.php b/src/Views/Traits/Configuration/ColorColumnConfiguration.php new file mode 100644 index 000000000..539c06c90 --- /dev/null +++ b/src/Views/Traits/Configuration/ColorColumnConfiguration.php @@ -0,0 +1,26 @@ +customClasses = ['class' => $customClasses, 'default' => $append]; + + // $this->customClasses = ($append) ? $customClasses . ' ' . $this->customClasses . ' ' . $customClasses : $customClasses; + + return $this; + } + + public function color(callable $callback): self + { + $this->colorCallback = $callback; + + return $this; + } + +} diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php new file mode 100644 index 000000000..de13bf19b --- /dev/null +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -0,0 +1,30 @@ +customClasses ?? ['class' => '', 'default' => false]; + } + + public function getView(): string + { + return $this->view; + } + + // TODO: Test + public function getColorCallback(): ?callable + { + return $this->colorCallback; + } + + public function hasColorCallback(): bool + { + return $this->colorCallback !== null; + } +} From 731d57e97585fe6786e179aa078d2c9116ee961c Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 19:29:10 +0000 Subject: [PATCH 02/22] Fix styling --- src/Views/Columns/ColorColumn.php | 76 +++++++++---------- .../ColorColumnConfiguration.php | 51 ++++++------- 2 files changed, 62 insertions(+), 65 deletions(-) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 6a0fa73a3..c9f6d7ca0 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -1,39 +1,37 @@ -attributes['class'] = $this->getCustomClasses(); - - - return view($this->getView()) - ->withColor($this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row)) - ->withCustomClasses($this->getCustomClasses()) - ->withAttributes($this->attributes); - } -} +attributes['class'] = $this->getCustomClasses(); + + return view($this->getView()) + ->withColor($this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row)) + ->withCustomClasses($this->getCustomClasses()) + ->withAttributes($this->attributes); + } +} diff --git a/src/Views/Traits/Configuration/ColorColumnConfiguration.php b/src/Views/Traits/Configuration/ColorColumnConfiguration.php index 539c06c90..6f03b383b 100644 --- a/src/Views/Traits/Configuration/ColorColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ColorColumnConfiguration.php @@ -1,26 +1,25 @@ -customClasses = ['class' => $customClasses, 'default' => $append]; - - // $this->customClasses = ($append) ? $customClasses . ' ' . $this->customClasses . ' ' . $customClasses : $customClasses; - - return $this; - } - - public function color(callable $callback): self - { - $this->colorCallback = $callback; - - return $this; - } - -} +customClasses = ['class' => $customClasses, 'default' => $append]; + + // $this->customClasses = ($append) ? $customClasses . ' ' . $this->customClasses . ' ' . $customClasses : $customClasses; + + return $this; + } + + public function color(callable $callback): self + { + $this->colorCallback = $callback; + + return $this; + } +} From 658920ce770b7b8ce9700c9662d2d335965bdb73 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 20:37:23 +0000 Subject: [PATCH 03/22] Add ColorColumn --- CHANGELOG.md | 1 + docs/columns/other-column-types.md | 27 +++++++++++++++++++ .../views/includes/columns/color.blade.php | 6 +---- src/Views/Columns/ColorColumn.php | 24 ++++++++++------- .../ColorColumnConfiguration.php | 17 +++++------- .../Traits/Helpers/ColorColumnHelpers.php | 19 ++++++++----- 6 files changed, 63 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40d5fad59..b1671ee9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file ### New Features - Add capability to use as a Full Page Component - Add DateColumn +- Add ColorColumn ### Tweaks - Internal - modify GitHub workflows to improve caching, but use unique caches per workflow matrix diff --git a/docs/columns/other-column-types.md b/docs/columns/other-column-types.md index 6378dd7f6..54050805c 100644 --- a/docs/columns/other-column-types.md +++ b/docs/columns/other-column-types.md @@ -81,6 +81,33 @@ 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'; + + } + ), +``` ## Date Columns diff --git a/resources/views/includes/columns/color.blade.php b/resources/views/includes/columns/color.blade.php index b11cc8b0a..7fa4a913e 100644 --- a/resources/views/includes/columns/color.blade.php +++ b/resources/views/includes/columns/color.blade.php @@ -2,11 +2,7 @@ 'items-center content-center place-content-center place-items-center', ]) > -
!empty($customClasses['class']), - 'h-6 w-6 rounded-md self-center' => $customClasses['default'] ?? false, - ]) +
class(['h-6 w-6 rounded-md self-center' => $attributeBag['default'] ?? (empty($attributeBag['class']) || (!empty($attributeBag['class']) && ($attributeBag['default'] ?? false)))]) }} @style([ "background-color: {$color}" => $color, ]) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index c9f6d7ca0..116e40300 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -3,6 +3,7 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\HtmlString; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ColorColumnConfiguration; @@ -16,22 +17,27 @@ class ColorColumn extends Column ColorColumnHelpers; public string $emptyValue = ''; - - public array $customClasses = []; - - public array $attributes = []; - + protected mixed $colorCallback = null; + protected mixed $attributesCallback = null; + protected string $view = 'livewire-tables::includes.columns.color'; - public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + public function __construct(string $title, ?string $from = null) { - $this->attributes['class'] = $this->getCustomClasses(); + parent::__construct($title, $from); + if (!isset($from) || $from === null) + { + $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 + { return view($this->getView()) ->withColor($this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row)) - ->withCustomClasses($this->getCustomClasses()) - ->withAttributes($this->attributes); + ->withAttributeBag(new \Illuminate\View\ComponentAttributeBag($this->hasAttributesCallback() ? app()->call($this->getAttributesCallback(), ['row' => $row]) : [])); } } diff --git a/src/Views/Traits/Configuration/ColorColumnConfiguration.php b/src/Views/Traits/Configuration/ColorColumnConfiguration.php index 6f03b383b..6dd84862f 100644 --- a/src/Views/Traits/Configuration/ColorColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ColorColumnConfiguration.php @@ -4,22 +4,19 @@ trait ColorColumnConfiguration { - /** - * Retrieve the Custom Classes to use for the Column - */ - public function setCustomClasses(string $customClasses, bool $append = false): self - { - $this->customClasses = ['class' => $customClasses, 'default' => $append]; - // $this->customClasses = ($append) ? $customClasses . ' ' . $this->customClasses . ' ' . $customClasses : $customClasses; + public function color(callable $callback): self + { + $this->colorCallback = $callback; return $this; } - - public function color(callable $callback): self + + public function attributes(callable $callback): self { - $this->colorCallback = $callback; + $this->attributesCallback = $callback; return $this; } + } diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php index de13bf19b..f74c55ab1 100644 --- a/src/Views/Traits/Helpers/ColorColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -4,13 +4,6 @@ trait ColorColumnHelpers { - /** - * Retrieve the Column Classes to use for the Column - */ - public function getCustomClasses(): array - { - return $this->customClasses ?? ['class' => '', 'default' => false]; - } public function getView(): string { @@ -27,4 +20,16 @@ public function hasColorCallback(): bool { return $this->colorCallback !== null; } + + public function getAttributesCallback(): ?callable + { + return $this->attributesCallback; + } + + public function hasAttributesCallback(): bool + { + return $this->attributesCallback !== null; + } + + } From 90e6d836f6f8fabe37b6ec52d50d43e5ee6cfe91 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 20:37:49 +0000 Subject: [PATCH 04/22] Fix styling --- src/Views/Columns/ColorColumn.php | 8 +++----- .../Traits/Configuration/ColorColumnConfiguration.php | 4 +--- src/Views/Traits/Helpers/ColorColumnHelpers.php | 3 --- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 116e40300..4f986ccb3 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -3,7 +3,6 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\HtmlString; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ColorColumnConfiguration; @@ -17,7 +16,7 @@ class ColorColumn extends Column ColorColumnHelpers; public string $emptyValue = ''; - + protected mixed $colorCallback = null; protected mixed $attributesCallback = null; @@ -27,11 +26,10 @@ class ColorColumn extends Column public function __construct(string $title, ?string $from = null) { parent::__construct($title, $from); - if (!isset($from) || $from === null) - { + if (! isset($from) || $from === null) { $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 diff --git a/src/Views/Traits/Configuration/ColorColumnConfiguration.php b/src/Views/Traits/Configuration/ColorColumnConfiguration.php index 6dd84862f..fdb387b46 100644 --- a/src/Views/Traits/Configuration/ColorColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ColorColumnConfiguration.php @@ -4,19 +4,17 @@ trait ColorColumnConfiguration { - public function color(callable $callback): self { $this->colorCallback = $callback; return $this; } - + public function attributes(callable $callback): self { $this->attributesCallback = $callback; return $this; } - } diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php index f74c55ab1..13acf0522 100644 --- a/src/Views/Traits/Helpers/ColorColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -4,7 +4,6 @@ trait ColorColumnHelpers { - public function getView(): string { return $this->view; @@ -30,6 +29,4 @@ public function hasAttributesCallback(): bool { return $this->attributesCallback !== null; } - - } From 8d56d33188ffd3952ccc397866ebb106f909f63b Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:17:55 +0000 Subject: [PATCH 05/22] Add Tests --- .../migrations/create_test_tables.php.stub | 1 + database/sqlite.database | Bin 40960 -> 40960 bytes tests/TestCase.php | 10 +-- tests/Views/Columns/ColorColumnTest.php | 81 ++++++++++++++++++ 4 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 tests/Views/Columns/ColorColumnTest.php diff --git a/database/migrations/create_test_tables.php.stub b/database/migrations/create_test_tables.php.stub index 632a3129a..ef7720eae 100644 --- a/database/migrations/create_test_tables.php.stub +++ b/database/migrations/create_test_tables.php.stub @@ -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'); diff --git a/database/sqlite.database b/database/sqlite.database index 953fb91ee0635163b12488ff895eebba22de746d..e3c6f68a2e4bc9e1dd1658d3f59c39973ec882ff 100644 GIT binary patch delta 372 zcmZoTz|?SnX@az%Bm)Bj7ZAe$=R_T2c}WJnWCmVdeg;mCxeWYG{04mS9CLa0@`!RT z;@06}-`FV6!PS(`!Y(c<%Ghc+S(5jjgi=~!S$%CFAA@5SHG|AJqg|L|r(0a5;XMHWs54q<*p zMkZ;e)I3uo10!Qy0}~)(W;mg200eGsDvUrS(t?VdjMC1DMJ2h3c}7MMb%sE7EMQdz z2K*Zs 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([ diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php new file mode 100644 index 000000000..b6770ae97 --- /dev/null +++ b/tests/Views/Columns/ColorColumnTest.php @@ -0,0 +1,81 @@ +assertSame('Favorite Color', $column->getTitle()); + } + + /** @test */ + public function can_infer_field_name_from_title_if_no_from(): void + { + $column = ColorColumn::make('Favorite Color'); + + $this->assertNull($column->getField()); + } + + /** @test */ + public function can_set_base_field_from_from(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color'); + + $this->assertSame('favorite_color', $column->getField()); + } + + /** @test */ + public function can_set_relation_field_from_from(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color'); + + $this->assertSame('favorite_color', $column->getField()); + } + + /** @test */ + public function can_get_column_formatted_contents(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color') ; + + $rows = $this->basicTable->getRows(); + + $this->assertSame($rows->first()->favorite_color, $column->getValue($rows->first())); + $this->assertSame($rows->last()->favorite_color, $column->getValue($rows->last())); + $this->assertSame($rows->slice(2,1)->first()->favorite_color, $column->getValue($rows->slice(2,1)->first())); + + } + + /** @test */ + public function can_get_column_contents_from_color(): void + { + $column = ColorColumn::make('Species Color')->color( + function ($row) { + if ($row->species_id == 1) + { + return '#ff0000'; + } + else if ($row->species_id == 2) + { + return '#008000'; + } + else return '#ffa500'; + + } + ); + + $rows = $this->basicTable->setAdditionalSelects(['pets.species_id as species_id'])->getRows(); + + $this->assertSame('#ff0000', app()->call($column->getColorCallback(), ['row' => $rows->first()])); + $this->assertSame('#ffa500', app()->call($column->getColorCallback(), ['row' => $rows->last()])); + $this->assertSame('#008000', app()->call($column->getColorCallback(), ['row' => $rows->slice(2,1)->first()])); + } + + +} From f8a2b3d6fe696339b683aa1e1feef9daa42f2978 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 21:18:24 +0000 Subject: [PATCH 06/22] Fix styling --- tests/Views/Columns/ColorColumnTest.php | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index b6770ae97..2f74f96a0 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -42,13 +42,13 @@ public function can_set_relation_field_from_from(): void /** @test */ public function can_get_column_formatted_contents(): void { - $column = ColorColumn::make('Favorite Color', 'favorite_color') ; + $column = ColorColumn::make('Favorite Color', 'favorite_color'); $rows = $this->basicTable->getRows(); $this->assertSame($rows->first()->favorite_color, $column->getValue($rows->first())); $this->assertSame($rows->last()->favorite_color, $column->getValue($rows->last())); - $this->assertSame($rows->slice(2,1)->first()->favorite_color, $column->getValue($rows->slice(2,1)->first())); + $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getValue($rows->slice(2, 1)->first())); } @@ -57,16 +57,14 @@ public function can_get_column_contents_from_color(): void { $column = ColorColumn::make('Species Color')->color( function ($row) { - if ($row->species_id == 1) - { + if ($row->species_id == 1) { return '#ff0000'; - } - else if ($row->species_id == 2) - { + } elseif ($row->species_id == 2) { return '#008000'; + } else { + return '#ffa500'; } - else return '#ffa500'; - + } ); @@ -74,8 +72,6 @@ function ($row) { $this->assertSame('#ff0000', app()->call($column->getColorCallback(), ['row' => $rows->first()])); $this->assertSame('#ffa500', app()->call($column->getColorCallback(), ['row' => $rows->last()])); - $this->assertSame('#008000', app()->call($column->getColorCallback(), ['row' => $rows->slice(2,1)->first()])); + $this->assertSame('#008000', app()->call($column->getColorCallback(), ['row' => $rows->slice(2, 1)->first()])); } - - } From e73808c9640f18cb76aac0f94a943196069d983c Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:21:12 +0000 Subject: [PATCH 07/22] Remove null check on string --- src/Views/Columns/ColorColumn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 4f986ccb3..50e8cc0d0 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -26,7 +26,7 @@ class ColorColumn extends Column public function __construct(string $title, ?string $from = null) { parent::__construct($title, $from); - if (! isset($from) || $from === null) { + if (! isset($from)) { $this->label(fn () => null); } From 748a0ba236eec5df7af46b6ba0ac4127584f4156 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:26:51 +0000 Subject: [PATCH 08/22] Change Callbacks to callable --- src/Views/Columns/ColorColumn.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 50e8cc0d0..69f9c1ecc 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -17,9 +17,9 @@ class ColorColumn extends Column public string $emptyValue = ''; - protected mixed $colorCallback = null; + protected ?object $colorCallback = null; - protected mixed $attributesCallback = null; + protected ?object $attributesCallback = null; protected string $view = 'livewire-tables::includes.columns.color'; From cc1f425968cf98bee9898399b3f3dded4c7977ab Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:39:31 +0000 Subject: [PATCH 09/22] Add Tests for getView, has() methods --- src/Views/Columns/ColorColumn.php | 6 ++-- .../Traits/Helpers/ColorColumnHelpers.php | 29 +++++++++++++++---- tests/Views/Columns/ColorColumnTest.php | 13 +++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 69f9c1ecc..87f38d3fd 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -15,8 +15,6 @@ class ColorColumn extends Column use ColorColumnConfiguration, ColorColumnHelpers; - public string $emptyValue = ''; - protected ?object $colorCallback = null; protected ?object $attributesCallback = null; @@ -35,7 +33,7 @@ public function __construct(string $title, ?string $from = null) public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { return view($this->getView()) - ->withColor($this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row)) - ->withAttributeBag(new \Illuminate\View\ComponentAttributeBag($this->hasAttributesCallback() ? app()->call($this->getAttributesCallback(), ['row' => $row]) : [])); + ->withColor($this->getColor($row)) + ->withAttributeBag($this->getAttributeBag($row)); } } diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php index 13acf0522..c5636fa27 100644 --- a/src/Views/Traits/Helpers/ColorColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -2,31 +2,48 @@ namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers; +use Illuminate\View\ComponentAttributeBag; + trait ColorColumnHelpers { - public function getView(): string + // TODO: Test + protected function getView(): string { return $this->view; } // TODO: Test - public function getColorCallback(): ?callable + protected function getColor($row): string { - return $this->colorCallback; + return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row); + } + + // TODO: Test + protected function getAttributeBag($row) + { + return new ComponentAttributeBag($this->hasAttributesCallback() ? app()->call($this->getAttributesCallback(), ['row' => $row]) : []); } - public function hasColorCallback(): bool + protected function getColorCallback(): ?callable + { + return $this->colorCallback; + } + + protected function hasColorCallback(): bool { return $this->colorCallback !== null; } - public function getAttributesCallback(): ?callable + // TODO: Test + protected function getAttributesCallback(): ?callable { return $this->attributesCallback; } - public function hasAttributesCallback(): bool + protected function hasAttributesCallback(): bool { return $this->attributesCallback !== null; } + + } diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index 2f74f96a0..eb41dbde4 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -15,6 +15,14 @@ public function can_set_the_column_title(): void $this->assertSame('Favorite Color', $column->getTitle()); } + /** @test */ + public function can_get_the_column_view(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color'); + + $this->assertSame('livewire-tables::includes.columns.color', $column->getView()); + } + /** @test */ public function can_infer_field_name_from_title_if_no_from(): void { @@ -43,6 +51,8 @@ public function can_set_relation_field_from_from(): void public function can_get_column_formatted_contents(): void { $column = ColorColumn::make('Favorite Color', 'favorite_color'); + $this->assertFalse($column->hasColorCallback()); + $this->assertFalse($column->hasAttributesCallback()); $rows = $this->basicTable->getRows(); @@ -68,6 +78,9 @@ function ($row) { } ); + $this->assertTrue($column->hasColorCallback()); + $this->assertFalse($column->hasAttributesCallback()); + $rows = $this->basicTable->setAdditionalSelects(['pets.species_id as species_id'])->getRows(); $this->assertSame('#ff0000', app()->call($column->getColorCallback(), ['row' => $rows->first()])); From 6c868a81e7eb506d31c1778368da360bf71972dc Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 21:39:56 +0000 Subject: [PATCH 10/22] Fix styling --- src/Views/Traits/Helpers/ColorColumnHelpers.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php index c5636fa27..c33de096b 100644 --- a/src/Views/Traits/Helpers/ColorColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -28,7 +28,7 @@ protected function getColorCallback(): ?callable { return $this->colorCallback; } - + protected function hasColorCallback(): bool { return $this->colorCallback !== null; @@ -44,6 +44,4 @@ protected function hasAttributesCallback(): bool { return $this->attributesCallback !== null; } - - } From c1c8fe1a322744ad611c31e9c2b64e768f9576d3 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:41:33 +0000 Subject: [PATCH 11/22] Change Helpers to Public --- src/Views/Traits/Helpers/ColorColumnHelpers.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php index c33de096b..6f4ed3442 100644 --- a/src/Views/Traits/Helpers/ColorColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -7,40 +7,40 @@ trait ColorColumnHelpers { // TODO: Test - protected function getView(): string + public function getView(): string { return $this->view; } // TODO: Test - protected function getColor($row): string + public function getColor($row): string { return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row); } // TODO: Test - protected function getAttributeBag($row) + public function getAttributeBag($row) { return new ComponentAttributeBag($this->hasAttributesCallback() ? app()->call($this->getAttributesCallback(), ['row' => $row]) : []); } - protected function getColorCallback(): ?callable + public function getColorCallback(): ?callable { return $this->colorCallback; } - protected function hasColorCallback(): bool + public function hasColorCallback(): bool { return $this->colorCallback !== null; } // TODO: Test - protected function getAttributesCallback(): ?callable + public function getAttributesCallback(): ?callable { return $this->attributesCallback; } - protected function hasAttributesCallback(): bool + public function hasAttributesCallback(): bool { return $this->attributesCallback !== null; } From 48d2232cd92f77692bc00777baf88b726578e23a Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:44:29 +0000 Subject: [PATCH 12/22] Add getColor Tests --- tests/Views/Columns/ColorColumnTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index eb41dbde4..f2844f8cf 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -60,6 +60,11 @@ public function can_get_column_formatted_contents(): void $this->assertSame($rows->last()->favorite_color, $column->getValue($rows->last())); $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getValue($rows->slice(2, 1)->first())); + $this->assertSame($rows->first()->favorite_color, $column->getColor($rows->first())); + $this->assertSame($rows->last()->favorite_color, $column->getColor($rows->last())); + $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getColor($rows->slice(2, 1)->first())); + + } /** @test */ @@ -86,5 +91,10 @@ function ($row) { $this->assertSame('#ff0000', app()->call($column->getColorCallback(), ['row' => $rows->first()])); $this->assertSame('#ffa500', app()->call($column->getColorCallback(), ['row' => $rows->last()])); $this->assertSame('#008000', app()->call($column->getColorCallback(), ['row' => $rows->slice(2, 1)->first()])); + + $this->assertSame($rows->first()->favorite_color, $column->getColor($rows->first())); + $this->assertSame($rows->last()->favorite_color, $column->getColor($rows->last())); + $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getColor($rows->slice(2, 1)->first())); + } } From 7206b7af8e7102fbba6cf608cfe1729bc98921df Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 21:44:51 +0000 Subject: [PATCH 13/22] Fix styling --- tests/Views/Columns/ColorColumnTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index f2844f8cf..d8880bf87 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -64,7 +64,6 @@ public function can_get_column_formatted_contents(): void $this->assertSame($rows->last()->favorite_color, $column->getColor($rows->last())); $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getColor($rows->slice(2, 1)->first())); - } /** @test */ @@ -91,7 +90,7 @@ function ($row) { $this->assertSame('#ff0000', app()->call($column->getColorCallback(), ['row' => $rows->first()])); $this->assertSame('#ffa500', app()->call($column->getColorCallback(), ['row' => $rows->last()])); $this->assertSame('#008000', app()->call($column->getColorCallback(), ['row' => $rows->slice(2, 1)->first()])); - + $this->assertSame($rows->first()->favorite_color, $column->getColor($rows->first())); $this->assertSame($rows->last()->favorite_color, $column->getColor($rows->last())); $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getColor($rows->slice(2, 1)->first())); From e46a429d03783553793ec48d66b4c5663056cb9e Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:49:00 +0000 Subject: [PATCH 14/22] Add defaultValue Option --- src/Views/Columns/ColorColumn.php | 2 ++ .../Traits/Configuration/ColorColumnConfiguration.php | 8 ++++++++ src/Views/Traits/Helpers/ColorColumnHelpers.php | 7 ++++++- tests/Views/Columns/ColorColumnTest.php | 8 ++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 87f38d3fd..9b54a39d2 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -19,6 +19,8 @@ class ColorColumn extends Column protected ?object $attributesCallback = null; + protected string $defaultValue = ''; + protected string $view = 'livewire-tables::includes.columns.color'; public function __construct(string $title, ?string $from = null) diff --git a/src/Views/Traits/Configuration/ColorColumnConfiguration.php b/src/Views/Traits/Configuration/ColorColumnConfiguration.php index fdb387b46..c480afe03 100644 --- a/src/Views/Traits/Configuration/ColorColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ColorColumnConfiguration.php @@ -17,4 +17,12 @@ public function attributes(callable $callback): self return $this; } + + public function defaultValue(string $defaultValue): self + { + $this->defaultValue = $defaultValue; + + return $this; + } + } diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php index 6f4ed3442..3153c6739 100644 --- a/src/Views/Traits/Helpers/ColorColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -12,10 +12,15 @@ 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); + return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row) ?? $this->getDefaultValue(); } // TODO: Test diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index d8880bf87..7cc5217c9 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -39,6 +39,14 @@ public function can_set_base_field_from_from(): void $this->assertSame('favorite_color', $column->getField()); } + /** @test */ + public function can_set_default_value(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color')->defaultValue('#FEFEFE'); + + $this->assertSame('#FEFEFE', $column->getDefaultValue()); + } + /** @test */ public function can_set_relation_field_from_from(): void { From d973b68dd67ab0fb9e8c48c251cc6d073ac41f30 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 21:49:20 +0000 Subject: [PATCH 15/22] Fix styling --- src/Views/Traits/Configuration/ColorColumnConfiguration.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Views/Traits/Configuration/ColorColumnConfiguration.php b/src/Views/Traits/Configuration/ColorColumnConfiguration.php index c480afe03..02ce4a35a 100644 --- a/src/Views/Traits/Configuration/ColorColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ColorColumnConfiguration.php @@ -24,5 +24,4 @@ public function defaultValue(string $defaultValue): self return $this; } - } From e447234c547f7bbd25c20d9cf77829aeed9e4ec9 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 22:34:07 +0000 Subject: [PATCH 16/22] Fix broken tests --- database/database.sqlite | Bin 40960 -> 40960 bytes src/Views/Columns/ColorColumn.php | 16 +++-- .../Traits/Helpers/ColorColumnHelpers.php | 4 +- tests/Views/Columns/ColorColumnTest.php | 67 +++++++++++++++--- 4 files changed, 72 insertions(+), 15 deletions(-) diff --git a/database/database.sqlite b/database/database.sqlite index 957f996387e69b63fb1285e188684ca953a4f003..6ab3c4998ca5a6a52b5fff19f99084a346c5b6e9 100644 GIT binary patch delta 342 zcmZoTz|?SnX@ayMKLY~;7ZAe$=R_T2d42}HWCmVdeg;mCxeWYG{04mS9CLa0@`!RT z;@06}-`FV6!PS(`!Y(c<%Ghc+S(5jjgi=~!S$y=qK892oG z6&abNoij3%^UaM6jEr>+Omz)Rm>EtfyMciUBT$jFpdu%uv~yxnNp51Ekr70lAy6HQ zvH=)~F#}Zz^D{6qO8X{O8Zo5-#ogQt41hir23o? kFcYw)C&0$Jxyds@)Br6FDXp-8C@})t2Qt;bU{OE-07t|{h5!Hn delta 195 zcmZoTz|?SnX@ayMF9QPu7ZAe$$3z`td0qy+a4(PyCl@;de-pm}Upy~A&t4u;?nT@> zT*c~qjO?WNp51EkrB(HfB*mi?IOYe diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 9b54a39d2..9eb908712 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -15,13 +15,13 @@ class ColorColumn extends Column use ColorColumnConfiguration, ColorColumnHelpers; - protected ?object $colorCallback = null; + public ?object $colorCallback = null; - protected ?object $attributesCallback = null; + public ?object $attributesCallback = null; - protected string $defaultValue = ''; + public string $defaultValue = ''; - protected string $view = 'livewire-tables::includes.columns.color'; + public string $view = 'livewire-tables::includes.columns.color'; public function __construct(string $title, ?string $from = null) { @@ -38,4 +38,12 @@ public function getContents(Model $row): null|string|\Illuminate\Support\HtmlStr ->withColor($this->getColor($row)) ->withAttributeBag($this->getAttributeBag($row)); } + + public function getValue(Model $row) + { + $value = parent::getValue($row); + + return !is_null($value) ? $value : $this->getDefaultValue(); + } + } diff --git a/src/Views/Traits/Helpers/ColorColumnHelpers.php b/src/Views/Traits/Helpers/ColorColumnHelpers.php index 3153c6739..9ac741b56 100644 --- a/src/Views/Traits/Helpers/ColorColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColorColumnHelpers.php @@ -20,7 +20,7 @@ public function getDefaultValue(): string // TODO: Test public function getColor($row): string { - return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : $this->getValue($row) ?? $this->getDefaultValue(); + return $this->hasColorCallback() ? app()->call($this->getColorCallback(), ['row' => $row]) : ($this->getValue($row) ?? $this->getDefaultValue()); } // TODO: Test @@ -36,7 +36,7 @@ public function getColorCallback(): ?callable public function hasColorCallback(): bool { - return $this->colorCallback !== null; + return isset($this->colorCallback); } // TODO: Test diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index 7cc5217c9..841a72ddc 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -56,22 +56,52 @@ public function can_set_relation_field_from_from(): void } /** @test */ - public function can_get_column_formatted_contents(): void + public function can_check_color_callback_presence(): void { $column = ColorColumn::make('Favorite Color', 'favorite_color'); $this->assertFalse($column->hasColorCallback()); + + $column->color( + function ($row) { + if ($row->species_id == 1) { + return '#ff0000'; + } elseif ($row->species_id == 2) { + return '#008000'; + } else { + return '#ffa500'; + } + + } + ); + $this->assertTrue($column->hasColorCallback()); + + } + + /** @test */ + public function can_check_attribute_callback_presence(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color'); $this->assertFalse($column->hasAttributesCallback()); + } + - $rows = $this->basicTable->getRows(); - $this->assertSame($rows->first()->favorite_color, $column->getValue($rows->first())); - $this->assertSame($rows->last()->favorite_color, $column->getValue($rows->last())); - $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getValue($rows->slice(2, 1)->first())); + /** @test */ + public function can_get_column_formatted_contents(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color'); + + $rows = $this->basicTable->setAdditionalSelects(['pets.favorite_color as favorite_color'])->getRows(); + $this->assertSame($rows->first()->favorite_color, $column->getValue($rows->first())); $this->assertSame($rows->first()->favorite_color, $column->getColor($rows->first())); + $this->assertSame($rows->last()->favorite_color, $column->getColor($rows->last())); - $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getColor($rows->slice(2, 1)->first())); + $this->assertSame($rows->last()->favorite_color, $column->getValue($rows->last())); + $currentRow = $rows->slice(2, 1)->first(); + $this->assertSame($currentRow->favorite_color, $column->getValue($currentRow)); + $this->assertSame($currentRow->favorite_color, $column->getColor($currentRow)); } /** @test */ @@ -98,10 +128,29 @@ function ($row) { $this->assertSame('#ff0000', app()->call($column->getColorCallback(), ['row' => $rows->first()])); $this->assertSame('#ffa500', app()->call($column->getColorCallback(), ['row' => $rows->last()])); $this->assertSame('#008000', app()->call($column->getColorCallback(), ['row' => $rows->slice(2, 1)->first()])); + } - $this->assertSame($rows->first()->favorite_color, $column->getColor($rows->first())); - $this->assertSame($rows->last()->favorite_color, $column->getColor($rows->last())); - $this->assertSame($rows->slice(2, 1)->first()->favorite_color, $column->getColor($rows->slice(2, 1)->first())); + /** @test */ + public function can_get_column_color_from_color(): void + { + $column = ColorColumn::make('Species Color')->color( + function ($row) { + if ($row->species_id == 1) { + return '#ff0000'; + } elseif ($row->species_id == 2) { + return '#008000'; + } else { + return '#ffa500'; + } + + } + ); + $rows = $this->basicTable->setAdditionalSelects(['pets.species_id as species_id'])->getRows(); + + $this->assertSame('#ff0000', $column->getColor($rows->first())); + $this->assertSame('#ffa500', $column->getColor($rows->last())); + $this->assertSame('#008000', $column->getColor($rows->slice(2, 1)->first())); } + } From a8b9b0c351b298e08a27acf429389b379b516037 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 22:34:33 +0000 Subject: [PATCH 17/22] Fix styling --- src/Views/Columns/ColorColumn.php | 3 +-- tests/Views/Columns/ColorColumnTest.php | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 9eb908712..02b87a3fa 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -43,7 +43,6 @@ public function getValue(Model $row) { $value = parent::getValue($row); - return !is_null($value) ? $value : $this->getDefaultValue(); + return ! is_null($value) ? $value : $this->getDefaultValue(); } - } diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index 841a72ddc..6586778f9 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -83,8 +83,6 @@ public function can_check_attribute_callback_presence(): void $column = ColorColumn::make('Favorite Color', 'favorite_color'); $this->assertFalse($column->hasAttributesCallback()); } - - /** @test */ public function can_get_column_formatted_contents(): void @@ -152,5 +150,4 @@ function ($row) { $this->assertSame('#008000', $column->getColor($rows->slice(2, 1)->first())); } - } From 8dde5e39e96090c5bd575b8695d43e3843a2e086 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 22:45:07 +0000 Subject: [PATCH 18/22] Add AttributeTest --- tests/Views/Columns/ColorColumnTest.php | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index 6586778f9..80604700e 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -84,6 +84,36 @@ public function can_check_attribute_callback_presence(): void $this->assertFalse($column->hasAttributesCallback()); } + + /** @test */ + public function can_set_attribute_callback(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color'); + $this->assertFalse($column->hasAttributesCallback()); + + $column->attributes(function ($row) { + return [ + 'class' => '!rounded-lg self-center', + 'default' => true, + ]; + }); + + $this->assertTrue($column->hasAttributesCallback()); + } + + /** @test */ + public function can_get_attribute_callback(): void + { + $column = ColorColumn::make('Favorite Color', 'favorite_color')->attributes(function ($row) { + return [ + 'class' => '!rounded-lg self-center', + 'default' => true, + ]; + }); + $rows = $this->basicTable->setAdditionalSelects(['pets.favorite_color as favorite_color'])->getRows(); + $this->assertSame(['class' => '!rounded-lg self-center','default' => true], $column->getAttributeBag($rows->first())->getAttributes()); + } + /** @test */ public function can_get_column_formatted_contents(): void { From 696dae65286c169ddc8e0cda9dd2483cbea28158 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sat, 9 Dec 2023 22:45:32 +0000 Subject: [PATCH 19/22] Fix styling --- tests/Views/Columns/ColorColumnTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Views/Columns/ColorColumnTest.php b/tests/Views/Columns/ColorColumnTest.php index 80604700e..396a7e32d 100644 --- a/tests/Views/Columns/ColorColumnTest.php +++ b/tests/Views/Columns/ColorColumnTest.php @@ -84,7 +84,6 @@ public function can_check_attribute_callback_presence(): void $this->assertFalse($column->hasAttributesCallback()); } - /** @test */ public function can_set_attribute_callback(): void { @@ -111,7 +110,7 @@ public function can_get_attribute_callback(): void ]; }); $rows = $this->basicTable->setAdditionalSelects(['pets.favorite_color as favorite_color'])->getRows(); - $this->assertSame(['class' => '!rounded-lg self-center','default' => true], $column->getAttributeBag($rows->first())->getAttributes()); + $this->assertSame(['class' => '!rounded-lg self-center', 'default' => true], $column->getAttributeBag($rows->first())->getAttributes()); } /** @test */ From 65ce437c5d5561d03bfc5877bdb4f922f4a16568 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 23:05:13 +0000 Subject: [PATCH 20/22] Add Attributes Docs --- docs/columns/other-column-types.md | 11 +++++++++++ resources/views/includes/columns/color.blade.php | 7 +++++-- src/Views/Columns/ColorColumn.php | 6 +++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/columns/other-column-types.md b/docs/columns/other-column-types.md index 54050805c..dc80da449 100644 --- a/docs/columns/other-column-types.md +++ b/docs/columns/other-column-types.md @@ -109,6 +109,17 @@ ColorColumn::make('Favourite Colour') ), ``` +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 Date columns provide an easy way to display dates in a given format, without having to use repetitive format() methods or partial views. diff --git a/resources/views/includes/columns/color.blade.php b/resources/views/includes/columns/color.blade.php index 7fa4a913e..1422f223c 100644 --- a/resources/views/includes/columns/color.blade.php +++ b/resources/views/includes/columns/color.blade.php @@ -1,8 +1,11 @@
$isTailwind, ]) > -
class(['h-6 w-6 rounded-md self-center' => $attributeBag['default'] ?? (empty($attributeBag['class']) || (!empty($attributeBag['class']) && ($attributeBag['default'] ?? false)))]) }} +
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, ]) diff --git a/src/Views/Columns/ColorColumn.php b/src/Views/Columns/ColorColumn.php index 02b87a3fa..2899a09fb 100644 --- a/src/Views/Columns/ColorColumn.php +++ b/src/Views/Columns/ColorColumn.php @@ -35,14 +35,14 @@ public function __construct(string $title, ?string $from = null) public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { return view($this->getView()) + ->withIsTailwind($this->getComponent()->isTailwind()) + ->withIsBootstrap($this->getComponent()->isBootstrap()) ->withColor($this->getColor($row)) ->withAttributeBag($this->getAttributeBag($row)); } public function getValue(Model $row) { - $value = parent::getValue($row); - - return ! is_null($value) ? $value : $this->getDefaultValue(); + return parent::getValue($row) ?? $this->getDefaultValue(); } } From b02a235762fc6115d18c656f58a5921b7e6c5e26 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 23:36:01 +0000 Subject: [PATCH 21/22] Update Changelog --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1671ee9d..5f3355d2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,9 @@ 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 ColorColumn +- 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 From a45c6c06d86ee09c8f2ca675bc64dc646a87907d Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 9 Dec 2023 23:37:53 +0000 Subject: [PATCH 22/22] Add reference to tidying classes/traits --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f3355d2d..bc2842a41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file - 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