From ce1b69bb875b269d7b824976d14f48cf252dd34b Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 1 Sep 2024 17:42:21 +0100 Subject: [PATCH 1/5] Centralise Theme Methods --- src/Traits/ComponentUtilities.php | 6 +-- src/Traits/HasAllTraits.php | 3 ++ src/Traits/Helpers/ComponentHelpers.php | 38 ------------------- .../Configuration/ColumnConfiguration.php | 7 ---- src/Views/Traits/Core/HasTheme.php | 23 +++++++++-- src/Views/Traits/Helpers/ColumnHelpers.php | 20 ---------- src/Views/Traits/IsColumn.php | 7 ++-- 7 files changed, 27 insertions(+), 77 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index fc1b20153..13a2cb3c3 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -16,8 +16,6 @@ trait ComponentUtilities public array $table = []; - public ?string $theme = null; - protected Builder $builder; protected $model; @@ -60,8 +58,8 @@ abstract public function configure(): void; public function mountComponentUtilities(): void { // Sets the Theme - tailwind/bootstrap - if (is_null($this->theme)) { - $this->setTheme(); + if (!isset($this->theme) || is_null($this->theme)) { + $this->setTheme(config('livewire-tables.theme', 'tailwind')); } $this->generateDataTableFingerprint(); diff --git a/src/Traits/HasAllTraits.php b/src/Traits/HasAllTraits.php index 7bef6fee8..8877bc69d 100644 --- a/src/Traits/HasAllTraits.php +++ b/src/Traits/HasAllTraits.php @@ -2,11 +2,14 @@ namespace Rappasoft\LaravelLivewireTables\Traits; +use Rappasoft\LaravelLivewireTables\Views\Traits\Core\HasTheme; + trait HasAllTraits { // Note Specific Order Below! use WithTableHooks; use WithLoadingPlaceholder; + use HasTheme; use ComponentUtilities, WithActions, WithData, diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 0480f39e4..b027be813 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -57,44 +57,6 @@ public function getModel() return $this->model; } - public function setTheme(): void - { - $theme = $this->getTheme(); - - if ($theme === 'bootstrap-4' || $theme === 'bootstrap-5') { - $this->setPaginationTheme('bootstrap'); - } - } - - public function getTheme(): string - { - return $this->theme ?? config('livewire-tables.theme', 'tailwind'); - } - - #[Computed] - public function isTailwind(): bool - { - return $this->getTheme() === 'tailwind'; - } - - #[Computed] - public function isBootstrap(): bool - { - return $this->getTheme() === 'bootstrap-4' || $this->getTheme() === 'bootstrap-5'; - } - - #[Computed] - public function isBootstrap4(): bool - { - return $this->getTheme() === 'bootstrap-4'; - } - - #[Computed] - public function isBootstrap5(): bool - { - return $this->getTheme() === 'bootstrap-5'; - } - /** * Get the translated empty message of the table */ diff --git a/src/Views/Traits/Configuration/ColumnConfiguration.php b/src/Views/Traits/Configuration/ColumnConfiguration.php index 2b20f595c..c46b01663 100644 --- a/src/Views/Traits/Configuration/ColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ColumnConfiguration.php @@ -84,13 +84,6 @@ public function setColumnLabelStatus(bool $status): void $this->displayColumnLabel = $status; } - public function setTheme(string $theme): self - { - $this->theme = $theme; - - return $this; - } - public function setHasTableRowUrl(bool $hasTableRowUrl): self { $this->hasTableRowUrl = $hasTableRowUrl; diff --git a/src/Views/Traits/Core/HasTheme.php b/src/Views/Traits/Core/HasTheme.php index 65ccfb885..b81d1edad 100644 --- a/src/Views/Traits/Core/HasTheme.php +++ b/src/Views/Traits/Core/HasTheme.php @@ -2,34 +2,49 @@ namespace Rappasoft\LaravelLivewireTables\Views\Traits\Core; +use Livewire\Attributes\Computed; + trait HasTheme { protected string $theme = 'tailwind'; + public function getTheme(): string + { + return $this->theme ?? config('livewire-tables.theme', 'tailwind'); + } + public function setTheme(string $theme): self { $this->theme = $theme; + if (($theme === 'bootstrap-4' || $theme === 'bootstrap-5') && method_exists($this, 'setPaginationTheme')) { + $this->setPaginationTheme('bootstrap'); + } + return $this; } + #[Computed(persist: true, seconds: 60)] public function isTailwind(): bool { - return $this->theme != 'bootstrap-4' && $this->theme != 'bootstrap-5'; + return !$this->isBootstrap4() && !$this->isBootstrap5(); } + #[Computed(persist: true, seconds: 60)] public function isBootstrap(): bool { - return $this->theme == 'bootstrap-4' || $this->theme == 'bootstrap-5'; + return $this->isBootstrap4() || $this->isBootstrap5(); } + #[Computed(persist: true, seconds: 60)] public function isBootstrap4(): bool { - return $this->theme == 'bootstrap-4'; + return $this->getTheme() === 'bootstrap-4'; } + #[Computed(persist: true, seconds: 60)] public function isBootstrap5(): bool { - return $this->theme == 'bootstrap-5'; + return $this->getTheme() === 'bootstrap-5'; } } diff --git a/src/Views/Traits/Helpers/ColumnHelpers.php b/src/Views/Traits/Helpers/ColumnHelpers.php index 1b016590c..da220287e 100644 --- a/src/Views/Traits/Helpers/ColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColumnHelpers.php @@ -211,26 +211,6 @@ public function getHasTableRowUrl(): bool return $this->hasTableRowUrl; } - public function isTailwind(): bool - { - return $this->theme != 'bootstrap-4' && $this->theme != 'bootstrap-5'; - } - - public function isBootstrap(): bool - { - return $this->theme == 'bootstrap-4' || $this->theme == 'bootstrap-5'; - } - - public function isBootstrap4(): bool - { - return $this->theme == 'bootstrap-4'; - } - - public function isBootstrap5(): bool - { - return $this->theme == 'bootstrap-5'; - } - public function getIsReorderColumn(): bool { return $this->isReorderColumn; diff --git a/src/Views/Traits/IsColumn.php b/src/Views/Traits/IsColumn.php index 7ff25cf3f..b648d3dd0 100644 --- a/src/Views/Traits/IsColumn.php +++ b/src/Views/Traits/IsColumn.php @@ -5,7 +5,7 @@ use Rappasoft\LaravelLivewireTables\DataTableComponent; use Rappasoft\LaravelLivewireTables\Views\Traits\Columns\{HasVisibility, IsCollapsible, IsSearchable, IsSelectable, IsSortable}; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ColumnConfiguration; -use Rappasoft\LaravelLivewireTables\Views\Traits\Core\{HasAttributes,HasFooter,HasSecondaryHeader,HasView}; +use Rappasoft\LaravelLivewireTables\Views\Traits\Core\{HasAttributes,HasFooter,HasSecondaryHeader,HasTheme,HasView}; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\{ColumnHelpers,RelationshipHelpers}; trait IsColumn @@ -20,6 +20,7 @@ trait IsColumn HasAttributes, HasFooter, HasSecondaryHeader, + HasTheme, HasView, HasVisibility; @@ -56,8 +57,6 @@ trait IsColumn protected ?string $customSlug = null; protected bool $hasTableRowUrl = false; - - protected string $theme = 'tailwind'; - + protected bool $isReorderColumn = false; } From b8da090b8e49a62b961c09e05201fb9313de88ec Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 1 Sep 2024 16:42:51 +0000 Subject: [PATCH 2/5] Fix styling --- src/Traits/ComponentUtilities.php | 2 +- src/Views/Traits/Core/HasTheme.php | 2 +- src/Views/Traits/IsColumn.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 13a2cb3c3..11625c8de 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -58,7 +58,7 @@ abstract public function configure(): void; public function mountComponentUtilities(): void { // Sets the Theme - tailwind/bootstrap - if (!isset($this->theme) || is_null($this->theme)) { + if (! isset($this->theme) || is_null($this->theme)) { $this->setTheme(config('livewire-tables.theme', 'tailwind')); } $this->generateDataTableFingerprint(); diff --git a/src/Views/Traits/Core/HasTheme.php b/src/Views/Traits/Core/HasTheme.php index b81d1edad..7e1b9ab3a 100644 --- a/src/Views/Traits/Core/HasTheme.php +++ b/src/Views/Traits/Core/HasTheme.php @@ -27,7 +27,7 @@ public function setTheme(string $theme): self #[Computed(persist: true, seconds: 60)] public function isTailwind(): bool { - return !$this->isBootstrap4() && !$this->isBootstrap5(); + return ! $this->isBootstrap4() && ! $this->isBootstrap5(); } #[Computed(persist: true, seconds: 60)] diff --git a/src/Views/Traits/IsColumn.php b/src/Views/Traits/IsColumn.php index b648d3dd0..42bbfb5b4 100644 --- a/src/Views/Traits/IsColumn.php +++ b/src/Views/Traits/IsColumn.php @@ -57,6 +57,6 @@ trait IsColumn protected ?string $customSlug = null; protected bool $hasTableRowUrl = false; - + protected bool $isReorderColumn = false; } From 47e7abe4664d7291cf91c3eea88cf0d2a89602fe Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 1 Sep 2024 18:30:05 +0100 Subject: [PATCH 3/5] Fix for broken tests --- src/Views/Traits/Core/HasTheme.php | 7 +++--- .../Traits/Visuals/PaginationVisualsTest.php | 22 +++++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Views/Traits/Core/HasTheme.php b/src/Views/Traits/Core/HasTheme.php index 7e1b9ab3a..b38b9c6e1 100644 --- a/src/Views/Traits/Core/HasTheme.php +++ b/src/Views/Traits/Core/HasTheme.php @@ -2,15 +2,16 @@ namespace Rappasoft\LaravelLivewireTables\Views\Traits\Core; -use Livewire\Attributes\Computed; +use Livewire\Attributes\{Computed,Locked}; trait HasTheme { - protected string $theme = 'tailwind'; + #[Locked] + public ?string $theme; public function getTheme(): string { - return $this->theme ?? config('livewire-tables.theme', 'tailwind'); + return $this->theme ?? ($this->theme = config('livewire-tables.theme', 'tailwind')); } public function setTheme(string $theme): self diff --git a/tests/Traits/Visuals/PaginationVisualsTest.php b/tests/Traits/Visuals/PaginationVisualsTest.php index 95d9f5e56..a00c7ad98 100644 --- a/tests/Traits/Visuals/PaginationVisualsTest.php +++ b/tests/Traits/Visuals/PaginationVisualsTest.php @@ -184,7 +184,7 @@ public function test_detailed_pagination_is_not_displayed_simple_tw(): void public function test_detailed_pagination_is_displayed_standard_bs4(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-4') + ->call('setTheme', 'bootstrap-4') ->call('enableDetailedPagination', 'standard') ->assertSeeHtmlInOrder(['
', 'Showing', @@ -197,7 +197,7 @@ public function test_detailed_pagination_is_displayed_standard_bs4(): void public function test_detailed_pagination_is_displayed_simple_bs4(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-4') + ->call('setTheme', 'bootstrap-4') ->call('enableDetailedPagination', 'simple') ->assertSeeHtmlInOrder(['
', 'Showing', @@ -210,8 +210,8 @@ public function test_detailed_pagination_is_displayed_simple_bs4(): void public function test_detailed_pagination_is_not_displayed_standard_bs4(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-4') - ->call('disableDetailedPagination', 'standard') + ->call('setTheme', 'bootstrap-4') + ->call('disableDetailedPagination', 'standard') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to') ->assertDontSeeHtml('of'); @@ -220,8 +220,8 @@ public function test_detailed_pagination_is_not_displayed_standard_bs4(): void public function test_detailed_pagination_is_not_displayed_simple_bs4(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-4') - ->call('disableDetailedPagination', 'simple') + ->call('setTheme', 'bootstrap-4') + ->call('disableDetailedPagination', 'simple') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to'); } @@ -229,8 +229,8 @@ public function test_detailed_pagination_is_not_displayed_simple_bs4(): void public function test_detailed_pagination_is_displayed_standard_bs5(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-5') - ->call('enableDetailedPagination', 'standard') + ->call('setTheme', 'bootstrap-5') + ->call('enableDetailedPagination', 'standard') ->assertSeeHtmlInOrder(['
', 'Showing', '1', @@ -242,7 +242,7 @@ public function test_detailed_pagination_is_displayed_standard_bs5(): void public function test_detailed_pagination_is_displayed_simple_bs5(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-5') + ->call('setTheme', 'bootstrap-5') ->call('enableDetailedPagination', 'simple') ->assertSeeHtmlInOrder(['
', 'Showing', @@ -255,7 +255,7 @@ public function test_detailed_pagination_is_displayed_simple_bs5(): void public function test_detailed_pagination_is_not_displayed_standard_bs5(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-5') + ->call('setTheme', 'bootstrap-5') ->call('disableDetailedPagination', 'standard') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to') @@ -265,7 +265,7 @@ public function test_detailed_pagination_is_not_displayed_standard_bs5(): void public function test_detailed_pagination_is_not_displayed_simple_bs5(): void { Livewire::test(PetsTable::class) - ->set('theme', 'bootstrap-5') + ->call('setTheme', 'bootstrap-5') ->call('disableDetailedPagination', 'simple') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to'); From 04c29af278c7ec20b3c8c46888cbe9e34b42afca Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 1 Sep 2024 17:30:34 +0000 Subject: [PATCH 4/5] Fix styling --- tests/Traits/Visuals/PaginationVisualsTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Traits/Visuals/PaginationVisualsTest.php b/tests/Traits/Visuals/PaginationVisualsTest.php index a00c7ad98..95cdfd062 100644 --- a/tests/Traits/Visuals/PaginationVisualsTest.php +++ b/tests/Traits/Visuals/PaginationVisualsTest.php @@ -210,8 +210,8 @@ public function test_detailed_pagination_is_displayed_simple_bs4(): void public function test_detailed_pagination_is_not_displayed_standard_bs4(): void { Livewire::test(PetsTable::class) - ->call('setTheme', 'bootstrap-4') - ->call('disableDetailedPagination', 'standard') + ->call('setTheme', 'bootstrap-4') + ->call('disableDetailedPagination', 'standard') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to') ->assertDontSeeHtml('of'); @@ -220,8 +220,8 @@ public function test_detailed_pagination_is_not_displayed_standard_bs4(): void public function test_detailed_pagination_is_not_displayed_simple_bs4(): void { Livewire::test(PetsTable::class) - ->call('setTheme', 'bootstrap-4') - ->call('disableDetailedPagination', 'simple') + ->call('setTheme', 'bootstrap-4') + ->call('disableDetailedPagination', 'simple') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to'); } @@ -229,8 +229,8 @@ public function test_detailed_pagination_is_not_displayed_simple_bs4(): void public function test_detailed_pagination_is_displayed_standard_bs5(): void { Livewire::test(PetsTable::class) - ->call('setTheme', 'bootstrap-5') - ->call('enableDetailedPagination', 'standard') + ->call('setTheme', 'bootstrap-5') + ->call('enableDetailedPagination', 'standard') ->assertSeeHtmlInOrder(['
', 'Showing', '1', @@ -242,7 +242,7 @@ public function test_detailed_pagination_is_displayed_standard_bs5(): void public function test_detailed_pagination_is_displayed_simple_bs5(): void { Livewire::test(PetsTable::class) - ->call('setTheme', 'bootstrap-5') + ->call('setTheme', 'bootstrap-5') ->call('enableDetailedPagination', 'simple') ->assertSeeHtmlInOrder(['
', 'Showing', @@ -255,7 +255,7 @@ public function test_detailed_pagination_is_displayed_simple_bs5(): void public function test_detailed_pagination_is_not_displayed_standard_bs5(): void { Livewire::test(PetsTable::class) - ->call('setTheme', 'bootstrap-5') + ->call('setTheme', 'bootstrap-5') ->call('disableDetailedPagination', 'standard') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to') @@ -265,7 +265,7 @@ public function test_detailed_pagination_is_not_displayed_standard_bs5(): void public function test_detailed_pagination_is_not_displayed_simple_bs5(): void { Livewire::test(PetsTable::class) - ->call('setTheme', 'bootstrap-5') + ->call('setTheme', 'bootstrap-5') ->call('disableDetailedPagination', 'simple') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to'); From 2bd0f1a51f38ee06a071e3c705ab718cc13ca836 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 1 Sep 2024 18:39:49 +0100 Subject: [PATCH 5/5] Remove persisted computed properties --- src/Views/Traits/Core/HasTheme.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Views/Traits/Core/HasTheme.php b/src/Views/Traits/Core/HasTheme.php index b38b9c6e1..5d229879a 100644 --- a/src/Views/Traits/Core/HasTheme.php +++ b/src/Views/Traits/Core/HasTheme.php @@ -25,25 +25,25 @@ public function setTheme(string $theme): self return $this; } - #[Computed(persist: true, seconds: 60)] + #[Computed] public function isTailwind(): bool { return ! $this->isBootstrap4() && ! $this->isBootstrap5(); } - #[Computed(persist: true, seconds: 60)] + #[Computed] public function isBootstrap(): bool { return $this->isBootstrap4() || $this->isBootstrap5(); } - #[Computed(persist: true, seconds: 60)] + #[Computed] public function isBootstrap4(): bool { return $this->getTheme() === 'bootstrap-4'; } - #[Computed(persist: true, seconds: 60)] + #[Computed] public function isBootstrap5(): bool { return $this->getTheme() === 'bootstrap-5';