Skip to content

Commit

Permalink
Use Core HasTheme Methods (#1915)
Browse files Browse the repository at this point in the history
* Centralise Theme Methods

* Fix styling

* Fix for broken tests

* Fix styling

* Remove persisted computed properties

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Sep 1, 2024
1 parent bf84029 commit 2ebaf3b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 85 deletions.
6 changes: 2 additions & 4 deletions src/Traits/ComponentUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ trait ComponentUtilities

public array $table = [];

public ?string $theme = null;

protected Builder $builder;

protected $model;
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions src/Traits/HasAllTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 0 additions & 38 deletions src/Traits/Helpers/ComponentHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 0 additions & 7 deletions src/Views/Traits/Configuration/ColumnConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 21 additions & 5 deletions src/Views/Traits/Core/HasTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
20 changes: 0 additions & 20 deletions src/Views/Traits/Helpers/ColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/Views/Traits/IsColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +20,7 @@ trait IsColumn
HasAttributes,
HasFooter,
HasSecondaryHeader,
HasTheme,
HasView,
HasVisibility;

Expand Down Expand Up @@ -57,7 +58,5 @@ trait IsColumn

protected bool $hasTableRowUrl = false;

protected string $theme = 'tailwind';

protected bool $isReorderColumn = false;
}
16 changes: 8 additions & 8 deletions tests/Traits/Visuals/PaginationVisualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(['<div class="col-12 col-md-6 text-center text-md-right text-muted">',
'<span>Showing</span>',
Expand All @@ -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(['<div class="col-12 col-md-6 overflow-auto">',
'<span>Showing</span>',
Expand All @@ -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('<span>Showing</span>')
->assertDontSeeHtml('<span>to</span>')
Expand All @@ -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('<span>Showing</span>')
->assertDontSeeHtml('<span>to</span>');
Expand All @@ -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(['<div class="col-12 col-md-6 text-center text-md-end text-muted">',
'<span>Showing</span>',
Expand All @@ -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(['<div class="col-12 col-md-6 text-center text-md-end text-muted">',
'<span>Showing</span>',
Expand All @@ -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('<span>Showing</span>')
->assertDontSeeHtml('<span>to</span>')
Expand All @@ -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('<span>Showing</span>')
->assertDontSeeHtml('<span>to</span>');
Expand Down

0 comments on commit 2ebaf3b

Please sign in to comment.