diff options
| author | Sam Light <samlight1994@gmail.com> | 2026-07-27 22:28:18 +0100 |
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2026-07-27 22:28:18 +0100 |
| commit | 1dc1088933c906d4676056d53395d34ce0a68d01 (patch) | |
| tree | b3c39b1e58305e2b0985678b720991883372667f /src | |
| parent | 827a53363e7a03329d6e5972d9d9be99d17c889e (diff) | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/Columns/Column.php | 6 | ||||
| -rw-r--r-- | src/Support/SortDirections.php | 44 | ||||
| -rw-r--r-- | src/TableComponent.php | 32 |
3 files changed, 72 insertions, 10 deletions
diff --git a/src/Columns/Column.php b/src/Columns/Column.php index ede94f7..1943a42 100644 --- a/src/Columns/Column.php +++ b/src/Columns/Column.php @@ -31,6 +31,9 @@ class Column */ private ?Closure $slotFn = null; + /** + * @var ?Closure(Builder<Model>, SortDirection): void + */ private ?Closure $sortFn = null; /** @@ -92,9 +95,8 @@ class Column * @template TModel of Model * * @param Builder<TModel> $query - * @param SortDirection|'asc'|'desc'|null $dir */ - public function applySort(Builder $query, SortDirection|string|null $dir): void + public function applySort(Builder $query, SortDirection $dir): void { if ($this->sortFn !== null) { ($this->sortFn)($query, $dir); diff --git a/src/Support/SortDirections.php b/src/Support/SortDirections.php new file mode 100644 index 0000000..def0f53 --- /dev/null +++ b/src/Support/SortDirections.php @@ -0,0 +1,44 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\LaralightTables\Support; + +use SortDirection; + +/** + * Converts between SortDirection and the 'asc'/'desc' strings used by the + * query builder and the Livewire wire/URL boundary. + * + * SortDirection is a pure enum, so it carries no value of its own. + */ +final class SortDirections +{ + public const ASC = 'asc'; + + public const DESC = 'desc'; + + /** + * @return 'asc'|'desc' + */ + public static function value(SortDirection $dir): string + { + return match ($dir) { + SortDirection::Ascending => self::ASC, + SortDirection::Descending => self::DESC, + }; + } + + public static function tryFrom(SortDirection|string|null $value): ?SortDirection + { + if ($value instanceof SortDirection) { + return $value; + } + + return match ($value === null ? null : strtolower($value)) { + self::ASC => SortDirection::Ascending, + self::DESC => SortDirection::Descending, + default => null, + }; + } +} diff --git a/src/TableComponent.php b/src/TableComponent.php index e36029e..196d5f8 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -13,6 +13,7 @@ use Illuminate\Support\Str; use Illuminate\View\View; use Lightscale\LaralightAssets\Facades\Assets; use Lightscale\LaralightTables\Columns\Column; +use Lightscale\LaralightTables\Support\SortDirections; use Lightscale\LaralightTables\Toolbar\Filter; use Livewire\Attributes\Url; use Livewire\Component; @@ -66,10 +67,12 @@ abstract class TableComponent extends Component public ?string $order = null; /** - * @var SortDirection|'asc'|'desc'|null + * Wire/URL transport only — read through getOrderDirection(). + * + * @var 'asc'|'desc'|null */ #[Url] - public SortDirection|string|null $orderDirection = null; + public ?string $orderDirection = null; public function mount(): void { @@ -115,18 +118,28 @@ abstract class TableComponent extends Component $this->resetPage(); } + public function getOrderDirection(): ?SortDirection + { + return SortDirections::tryFrom($this->orderDirection); + } + + protected function setOrderDirection(?SortDirection $dir): void + { + $this->orderDirection = $dir === null ? null : SortDirections::value($dir); + } + public function orderBy(string $column): void { if ($column === $this->order) { - if ($this->orderDirection === 'desc') { + if ($this->getOrderDirection() === SortDirection::Descending) { $this->order = null; - $this->orderDirection = null; + $this->setOrderDirection(null); } else { - $this->orderDirection = 'desc'; + $this->setOrderDirection(SortDirection::Descending); } } else { $this->order = $column; - $this->orderDirection = 'asc'; + $this->setOrderDirection(SortDirection::Ascending); } } @@ -203,8 +216,10 @@ abstract class TableComponent extends Component protected function applyOrder(Builder $query): void { $column = $this->getOrderColumn(); - if ($column) { - $column->applySort($query, $this->orderDirection); + $dir = $this->getOrderDirection(); + + if ($column !== null && $dir !== null) { + $column->applySort($query, $dir); } } @@ -267,6 +282,7 @@ abstract class TableComponent extends Component return view('laralight-tables::table', [ 'rootClass' => $this->rootClass, 'tableClass' => $this->tableClass, + 'sortDirection' => $this->getOrderDirection(), ] + compact( 'data', 'allColumns', 'columns', 'toolbars', )); |
