From 2ebaf3bfb23804db5e8f150436630873b49c76b2 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 1 Sep 2024 18:43:07 +0100 Subject: [PATCH] Use Core HasTheme Methods (#1915) * Centralise Theme Methods * Fix styling * Fix for broken tests * Fix styling * Remove persisted computed properties --------- Co-authored-by: lrljoe --- 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 | 26 ++++++++++--- src/Views/Traits/Helpers/ColumnHelpers.php | 20 ---------- src/Views/Traits/IsColumn.php | 5 +-- .../Traits/Visuals/PaginationVisualsTest.php | 16 ++++---- 8 files changed, 36 insertions(+), 85 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index fc1b20153..11625c8de 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..5d229879a 100644 --- a/src/Views/Traits/Core/HasTheme.php +++ b/src/Views/Traits/Core/HasTheme.php @@ -2,34 +2,50 @@ namespace Rappasoft\LaravelLivewireTables\Views\Traits\Core; +use Livewire\Attributes\{Computed,Locked}; + trait HasTheme { - protected string $theme = 'tailwind'; + #[Locked] + public ?string $theme; + + public function getTheme(): string + { + return $this->theme ?? ($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] public function isTailwind(): bool { - return $this->theme != 'bootstrap-4' && $this->theme != 'bootstrap-5'; + return ! $this->isBootstrap4() && ! $this->isBootstrap5(); } + #[Computed] public function isBootstrap(): bool { - return $this->theme == 'bootstrap-4' || $this->theme == 'bootstrap-5'; + return $this->isBootstrap4() || $this->isBootstrap5(); } + #[Computed] public function isBootstrap4(): bool { - return $this->theme == 'bootstrap-4'; + return $this->getTheme() === 'bootstrap-4'; } + #[Computed] 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..42bbfb5b4 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; @@ -57,7 +58,5 @@ trait IsColumn protected bool $hasTableRowUrl = false; - protected string $theme = 'tailwind'; - protected bool $isReorderColumn = false; } diff --git a/tests/Traits/Visuals/PaginationVisualsTest.php b/tests/Traits/Visuals/PaginationVisualsTest.php index 95d9f5e56..95cdfd062 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,7 +210,7 @@ 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('setTheme', 'bootstrap-4') ->call('disableDetailedPagination', 'standard') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to') @@ -220,7 +220,7 @@ 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('setTheme', 'bootstrap-4') ->call('disableDetailedPagination', 'simple') ->assertDontSeeHtml('Showing') ->assertDontSeeHtml('to'); @@ -229,7 +229,7 @@ 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('setTheme', 'bootstrap-5') ->call('enableDetailedPagination', 'standard') ->assertSeeHtmlInOrder(['
', 'Showing', @@ -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');