From 3271df3fafb7ce0a39a72b0f003cf92e904c7fad Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 27 Oct 2024 03:39:05 +0000 Subject: [PATCH 1/9] Add QueryStringForSort --- docs/datatable/query-string.md | 51 ++++++++ .../QueryStrings/HasQueryStringForSort.php | 76 ++++++++++++ src/Traits/WithSorting.php | 14 +-- .../QueryStrings/QueryStringForSortTest.php | 110 ++++++++++++++++++ 4 files changed, 240 insertions(+), 11 deletions(-) create mode 100644 src/Traits/Core/QueryStrings/HasQueryStringForSort.php create mode 100644 tests/Traits/Core/QueryStrings/QueryStringForSortTest.php diff --git a/docs/datatable/query-string.md b/docs/datatable/query-string.md index f0763a643..c830ef15a 100644 --- a/docs/datatable/query-string.md +++ b/docs/datatable/query-string.md @@ -153,4 +153,55 @@ public function configure(): void { $this->setQueryStringAliasForSearch('search'); } +``` + +## Sorts + +The sorts query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods: + +### setQueryStringStatusForSort + +Enable/disable the query string for sort + +```php +public function configure(): void +{ + $this->setQueryStringStatusForSort(true); + $this->setQueryStringStatusForSort(false); +} +``` + +### setQueryStringForSortEnabled + +Enable the query string for sort + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatusForSort(true) + $this->setQueryStringForSortEnabled(); +} +``` + +### setQueryStringForSortDisabled + +Disable the query string for sort + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatusForSort(false) + $this->setQueryStringForSortDisabled(); +} +``` + +### setQueryStringAliasForSort + +Change the Alias in the URL for the sorts, otherwise defaults to "$tablename-sorts" + +```php +public function configure(): void +{ + $this->setQueryStringAliasForSort('sorts'); +} ``` \ No newline at end of file diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php new file mode 100644 index 000000000..06597f80c --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -0,0 +1,76 @@ +queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; + + } + public function setupQueryStringStatusForSort(): void + { + if (! $this->hasQueryStringStatusForSort()) { + $this->setQueryStringForSortEnabled(); + } + } + + public function hasQueryStringStatusForSort(): bool + { + return isset($this->queryStringStatusForSort); + } + + public function getQueryStringStatusForSort(): bool + { + return $this->queryStringStatusForSort ?? true; + } + + public function queryStringForSortEnabled(): bool + { + $this->setupQueryStringStatusForSort(); + + return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); + } + + public function setQueryStringStatusForSort(bool $status): self + { + $this->queryStringStatusForSort = $status; + + return $this; + } + + public function setQueryStringForSortEnabled(): self + { + return $this->setQueryStringStatusForSort(true); + } + + public function setQueryStringForSortDisabled(): self + { + return $this->setQueryStringStatusForSort(false); + } + + public function hasQueryStringAliasForSort(): bool + { + return isset($this->queryStringAliasForSort); + } + + public function getQueryStringAliasForSort(): string + { + return $this->queryStringAliasForSort ?? $this->getQueryStringAlias().'-sorts'; + } + + public function setQueryStringAliasForSort(string $alias): self + { + $this->queryStringAliasForSort = $alias; + + return $this; + } +} diff --git a/src/Traits/WithSorting.php b/src/Traits/WithSorting.php index 78c8e9a73..d9bc7bb48 100644 --- a/src/Traits/WithSorting.php +++ b/src/Traits/WithSorting.php @@ -7,12 +7,14 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Traits\Configuration\SortingConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\SortingHelpers; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForSort; trait WithSorting { use SortingConfiguration, SortingHelpers; - + use HasQueryStringForSort; + public array $sorts = []; public Collection $sortableColumns; @@ -31,16 +33,6 @@ trait WithSorting public string $defaultSortingLabelDesc = 'Z-A'; - public function queryStringWithSorting(): array - { - if ($this->queryStringIsEnabled() && $this->sortingIsEnabled()) { - return [ - 'sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-sorts'], - ]; - } - - return []; - } public function sortBy(string $columnSelectName): ?string { diff --git a/tests/Traits/Core/QueryStrings/QueryStringForSortTest.php b/tests/Traits/Core/QueryStrings/QueryStringForSortTest.php new file mode 100644 index 000000000..7db6d3eb9 --- /dev/null +++ b/tests/Traits/Core/QueryStrings/QueryStringForSortTest.php @@ -0,0 +1,110 @@ +setDataTableFingerprint('test'); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(true, $mock->getQueryStringStatusForSort()); + } + + public function test_can_disable_sort_query_string_status(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + $this->setQueryStringForSortDisabled(); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(false, $mock->getQueryStringStatusForSort()); + } + + public function test_can_enable_sort_query_string_status(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + $this->setQueryStringForSortDisabled(); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(false, $mock->getQueryStringStatusForSort()); + $mock->setQueryStringForSortEnabled(); + $this->assertSame(true, $mock->getQueryStringStatusForSort()); + + } + + public function test_can_get_default_sort_query_string_alias(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame('table-sorts', $mock->getQueryStringAliasForSort()); + + } + + public function test_can_change_default_sort_query_string_alias(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame('table-sorts', $mock->getQueryStringAliasForSort()); + $mock->setQueryStringAliasForSort('pet-sorts'); + $this->assertSame('pet-sorts', $mock->getQueryStringAliasForSort()); + $this->assertTrue($mock->hasQueryStringAliasForSort()); + } +} From 365928364d83301cd356b79c0d614500e4adfc7a Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 27 Oct 2024 03:39:32 +0000 Subject: [PATCH 2/9] Fix styling --- .../QueryStrings/HasQueryStringForSort.php | 153 +++++++++--------- src/Traits/WithSorting.php | 5 +- 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php index 06597f80c..9bb544f44 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -1,76 +1,77 @@ -queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; - - } - public function setupQueryStringStatusForSort(): void - { - if (! $this->hasQueryStringStatusForSort()) { - $this->setQueryStringForSortEnabled(); - } - } - - public function hasQueryStringStatusForSort(): bool - { - return isset($this->queryStringStatusForSort); - } - - public function getQueryStringStatusForSort(): bool - { - return $this->queryStringStatusForSort ?? true; - } - - public function queryStringForSortEnabled(): bool - { - $this->setupQueryStringStatusForSort(); - - return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); - } - - public function setQueryStringStatusForSort(bool $status): self - { - $this->queryStringStatusForSort = $status; - - return $this; - } - - public function setQueryStringForSortEnabled(): self - { - return $this->setQueryStringStatusForSort(true); - } - - public function setQueryStringForSortDisabled(): self - { - return $this->setQueryStringStatusForSort(false); - } - - public function hasQueryStringAliasForSort(): bool - { - return isset($this->queryStringAliasForSort); - } - - public function getQueryStringAliasForSort(): string - { - return $this->queryStringAliasForSort ?? $this->getQueryStringAlias().'-sorts'; - } - - public function setQueryStringAliasForSort(string $alias): self - { - $this->queryStringAliasForSort = $alias; - - return $this; - } -} +queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; + + } + + public function setupQueryStringStatusForSort(): void + { + if (! $this->hasQueryStringStatusForSort()) { + $this->setQueryStringForSortEnabled(); + } + } + + public function hasQueryStringStatusForSort(): bool + { + return isset($this->queryStringStatusForSort); + } + + public function getQueryStringStatusForSort(): bool + { + return $this->queryStringStatusForSort ?? true; + } + + public function queryStringForSortEnabled(): bool + { + $this->setupQueryStringStatusForSort(); + + return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); + } + + public function setQueryStringStatusForSort(bool $status): self + { + $this->queryStringStatusForSort = $status; + + return $this; + } + + public function setQueryStringForSortEnabled(): self + { + return $this->setQueryStringStatusForSort(true); + } + + public function setQueryStringForSortDisabled(): self + { + return $this->setQueryStringStatusForSort(false); + } + + public function hasQueryStringAliasForSort(): bool + { + return isset($this->queryStringAliasForSort); + } + + public function getQueryStringAliasForSort(): string + { + return $this->queryStringAliasForSort ?? $this->getQueryStringAlias().'-sorts'; + } + + public function setQueryStringAliasForSort(string $alias): self + { + $this->queryStringAliasForSort = $alias; + + return $this; + } +} diff --git a/src/Traits/WithSorting.php b/src/Traits/WithSorting.php index d9bc7bb48..1ed30366e 100644 --- a/src/Traits/WithSorting.php +++ b/src/Traits/WithSorting.php @@ -6,15 +6,15 @@ use Illuminate\Support\Collection; use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Traits\Configuration\SortingConfiguration; -use Rappasoft\LaravelLivewireTables\Traits\Helpers\SortingHelpers; use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForSort; +use Rappasoft\LaravelLivewireTables\Traits\Helpers\SortingHelpers; trait WithSorting { use SortingConfiguration, SortingHelpers; use HasQueryStringForSort; - + public array $sorts = []; public Collection $sortableColumns; @@ -33,7 +33,6 @@ trait WithSorting public string $defaultSortingLabelDesc = 'Z-A'; - public function sortBy(string $columnSelectName): ?string { From 3b28b792b85c31b9144ad919ca02792029c1ac24 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 27 Oct 2024 04:43:14 +0000 Subject: [PATCH 3/9] Adjust for SortQueryString --- .../Core/QueryStrings/HasQueryString.php | 61 ++++++++ .../QueryStrings/HasQueryStringForSearch.php | 43 ++---- .../QueryStrings/HasQueryStringForSort.php | 143 ++++++++---------- 3 files changed, 141 insertions(+), 106 deletions(-) create mode 100644 src/Traits/Core/QueryStrings/HasQueryString.php diff --git a/src/Traits/Core/QueryStrings/HasQueryString.php b/src/Traits/Core/QueryStrings/HasQueryString.php new file mode 100644 index 000000000..58c018be0 --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryString.php @@ -0,0 +1,61 @@ + [], + 'filter' => [], + 'search' => [], + 'sorts' => [], + ]; + + protected function getQueryStringConfig(string $type): array + { + return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? [])); + } + + protected function hasQueryStringConfigStatus(string $type): bool + { + return isset($this->getQueryStringConfig($type)['status']); + } + + protected function getQueryStringConfigStatus(string $type): bool + { + return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus(); + } + + protected function hasQueryStringConfigAlias(string $type): bool + { + return isset($this->getQueryStringConfig($type)['alias']); + } + + protected function getQueryStringConfigAlias(string $type): string + { + return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias()."-".$type; + } + + + protected function setQueryStringConfig(string $type, array $config): self + { + $this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config); + + return $this; + } + + protected function setQueryStringConfigStatus(string $type, bool $status): self + { + return $this->setQueryStringConfig($type, ['status' => $status]); + + } + + protected function setQueryStringConfigAlias(string $type, string $alias): self + { + return $this->setQueryStringConfig($type,['alias' => $alias]); + } + +} \ No newline at end of file diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php index 10cac7fb9..6737a178f 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php @@ -2,39 +2,32 @@ namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings; -use Livewire\Attributes\Locked; - trait HasQueryStringForSearch { - #[Locked] - public ?bool $queryStringStatusForSearch; - - protected ?string $queryStringAliasForSearch; - protected function queryStringHasQueryStringForSearch(): array { return ($this->queryStringForSearchEnabled() && $this->searchIsEnabled()) ? ['search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSearch()]] : []; } - public function setupQueryStringStatusForSearch(): void + protected function setupQueryStringStatusForSearch(): void { if (! $this->hasQueryStringStatusForSearch()) { $this->setQueryStringForSearchEnabled(); } } - public function hasQueryStringStatusForSearch(): bool + protected function hasQueryStringStatusForSearch(): bool { - return isset($this->queryStringStatusForSearch); + return $this->hasQueryStringConfigStatus('search'); } - public function getQueryStringStatusForSearch(): bool + protected function getQueryStringStatusForSearch(): bool { - return $this->queryStringStatusForSearch ?? true; + return $this->getQueryStringConfigStatus("search"); } - public function queryStringForSearchEnabled(): bool + protected function queryStringForSearchEnabled(): bool { $this->setupQueryStringStatusForSearch(); @@ -43,39 +36,31 @@ public function queryStringForSearchEnabled(): bool public function setQueryStringStatusForSearch(bool $status): self { - $this->queryStringStatusForSearch = $status; - - return $this; + return $this->setQueryStringConfigStatus("search", $status); } public function setQueryStringForSearchEnabled(): self { - $this->setQueryStringStatusForSearch(true); - - return $this; + return $this->setQueryStringStatusForSearch(true); } public function setQueryStringForSearchDisabled(): self { - $this->setQueryStringStatusForSearch(false); - - return $this; + return $this->setQueryStringStatusForSearch(false); } - public function hasQueryStringAliasForSearch(): bool + protected function hasQueryStringAliasForSearch(): bool { - return isset($this->queryStringAliasForSearch); + return $this->hasQueryStringConfigAlias('search'); } - public function getQueryStringAliasForSearch(): string + protected function getQueryStringAliasForSearch(): string { - return $this->queryStringAliasForSearch ?? $this->getQueryStringAlias().'-search'; + return $this->getQueryStringConfigAlias("search"); } public function setQueryStringAliasForSearch(string $alias): self { - $this->queryStringAliasForSearch = $alias; - - return $this; + return $this->setQueryStringConfigAlias("search", $alias); } } diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php index 9bb544f44..6a5a29ec6 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -1,77 +1,66 @@ -queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; - - } - - public function setupQueryStringStatusForSort(): void - { - if (! $this->hasQueryStringStatusForSort()) { - $this->setQueryStringForSortEnabled(); - } - } - - public function hasQueryStringStatusForSort(): bool - { - return isset($this->queryStringStatusForSort); - } - - public function getQueryStringStatusForSort(): bool - { - return $this->queryStringStatusForSort ?? true; - } - - public function queryStringForSortEnabled(): bool - { - $this->setupQueryStringStatusForSort(); - - return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); - } - - public function setQueryStringStatusForSort(bool $status): self - { - $this->queryStringStatusForSort = $status; - - return $this; - } - - public function setQueryStringForSortEnabled(): self - { - return $this->setQueryStringStatusForSort(true); - } - - public function setQueryStringForSortDisabled(): self - { - return $this->setQueryStringStatusForSort(false); - } - - public function hasQueryStringAliasForSort(): bool - { - return isset($this->queryStringAliasForSort); - } - - public function getQueryStringAliasForSort(): string - { - return $this->queryStringAliasForSort ?? $this->getQueryStringAlias().'-sorts'; - } - - public function setQueryStringAliasForSort(string $alias): self - { - $this->queryStringAliasForSort = $alias; - - return $this; - } -} +queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; + + } + public function setupQueryStringStatusForSort(): void + { + if (! $this->hasQueryStringStatusForSort()) { + $this->setQueryStringForSortEnabled(); + } + } + + protected function hasQueryStringStatusForSort(): bool + { + return $this->hasQueryStringConfigStatus('sorts'); + } + + protected function getQueryStringStatusForSort(): bool + { + return $this->getQueryStringConfigStatus("sorts"); + } + + protected function queryStringForSortEnabled(): bool + { + $this->setupQueryStringStatusForSort(); + + return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); + } + + public function setQueryStringStatusForSort(bool $status): self + { + return $this->setQueryStringConfigStatus("sorts", $status); + } + + public function setQueryStringForSortEnabled(): self + { + return $this->setQueryStringStatusForSort(true); + } + + public function setQueryStringForSortDisabled(): self + { + return $this->setQueryStringStatusForSort(false); + } + + protected function hasQueryStringAliasForSort(): bool + { + return $this->hasQueryStringConfigAlias('sorts'); + } + + protected function getQueryStringAliasForSort(): string + { + return $this->getQueryStringConfigAlias("sorts"); + } + + public function setQueryStringAliasForSort(string $alias): self + { + return $this->setQueryStringConfigAlias("sorts", $alias); + } +} From df926b7634062f1fa9d586e6ef25e9996d5506b4 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 27 Oct 2024 04:43:38 +0000 Subject: [PATCH 4/9] Fix styling --- .../Core/QueryStrings/HasQueryString.php | 120 ++++++++-------- .../QueryStrings/HasQueryStringForSearch.php | 8 +- .../QueryStrings/HasQueryStringForSort.php | 132 +++++++++--------- 3 files changed, 129 insertions(+), 131 deletions(-) diff --git a/src/Traits/Core/QueryStrings/HasQueryString.php b/src/Traits/Core/QueryStrings/HasQueryString.php index 58c018be0..7c1f567b6 100644 --- a/src/Traits/Core/QueryStrings/HasQueryString.php +++ b/src/Traits/Core/QueryStrings/HasQueryString.php @@ -1,61 +1,59 @@ - [], - 'filter' => [], - 'search' => [], - 'sorts' => [], - ]; - - protected function getQueryStringConfig(string $type): array - { - return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? [])); - } - - protected function hasQueryStringConfigStatus(string $type): bool - { - return isset($this->getQueryStringConfig($type)['status']); - } - - protected function getQueryStringConfigStatus(string $type): bool - { - return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus(); - } - - protected function hasQueryStringConfigAlias(string $type): bool - { - return isset($this->getQueryStringConfig($type)['alias']); - } - - protected function getQueryStringConfigAlias(string $type): string - { - return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias()."-".$type; - } - - - protected function setQueryStringConfig(string $type, array $config): self - { - $this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config); - - return $this; - } - - protected function setQueryStringConfigStatus(string $type, bool $status): self - { - return $this->setQueryStringConfig($type, ['status' => $status]); - - } - - protected function setQueryStringConfigAlias(string $type, string $alias): self - { - return $this->setQueryStringConfig($type,['alias' => $alias]); - } - -} \ No newline at end of file + [], + 'filter' => [], + 'search' => [], + 'sorts' => [], + ]; + + protected function getQueryStringConfig(string $type): array + { + return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? [])); + } + + protected function hasQueryStringConfigStatus(string $type): bool + { + return isset($this->getQueryStringConfig($type)['status']); + } + + protected function getQueryStringConfigStatus(string $type): bool + { + return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus(); + } + + protected function hasQueryStringConfigAlias(string $type): bool + { + return isset($this->getQueryStringConfig($type)['alias']); + } + + protected function getQueryStringConfigAlias(string $type): string + { + return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias().'-'.$type; + } + + protected function setQueryStringConfig(string $type, array $config): self + { + $this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config); + + return $this; + } + + protected function setQueryStringConfigStatus(string $type, bool $status): self + { + return $this->setQueryStringConfig($type, ['status' => $status]); + + } + + protected function setQueryStringConfigAlias(string $type, string $alias): self + { + return $this->setQueryStringConfig($type, ['alias' => $alias]); + } +} diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php index 6737a178f..80d676c4a 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php @@ -24,7 +24,7 @@ protected function hasQueryStringStatusForSearch(): bool protected function getQueryStringStatusForSearch(): bool { - return $this->getQueryStringConfigStatus("search"); + return $this->getQueryStringConfigStatus('search'); } protected function queryStringForSearchEnabled(): bool @@ -36,7 +36,7 @@ protected function queryStringForSearchEnabled(): bool public function setQueryStringStatusForSearch(bool $status): self { - return $this->setQueryStringConfigStatus("search", $status); + return $this->setQueryStringConfigStatus('search', $status); } public function setQueryStringForSearchEnabled(): self @@ -56,11 +56,11 @@ protected function hasQueryStringAliasForSearch(): bool protected function getQueryStringAliasForSearch(): string { - return $this->getQueryStringConfigAlias("search"); + return $this->getQueryStringConfigAlias('search'); } public function setQueryStringAliasForSearch(string $alias): self { - return $this->setQueryStringConfigAlias("search", $alias); + return $this->setQueryStringConfigAlias('search', $alias); } } diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php index 6a5a29ec6..2c7ae2cd5 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -1,66 +1,66 @@ -queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; - - } - public function setupQueryStringStatusForSort(): void - { - if (! $this->hasQueryStringStatusForSort()) { - $this->setQueryStringForSortEnabled(); - } - } - - protected function hasQueryStringStatusForSort(): bool - { - return $this->hasQueryStringConfigStatus('sorts'); - } - - protected function getQueryStringStatusForSort(): bool - { - return $this->getQueryStringConfigStatus("sorts"); - } - - protected function queryStringForSortEnabled(): bool - { - $this->setupQueryStringStatusForSort(); - - return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); - } - - public function setQueryStringStatusForSort(bool $status): self - { - return $this->setQueryStringConfigStatus("sorts", $status); - } - - public function setQueryStringForSortEnabled(): self - { - return $this->setQueryStringStatusForSort(true); - } - - public function setQueryStringForSortDisabled(): self - { - return $this->setQueryStringStatusForSort(false); - } - - protected function hasQueryStringAliasForSort(): bool - { - return $this->hasQueryStringConfigAlias('sorts'); - } - - protected function getQueryStringAliasForSort(): string - { - return $this->getQueryStringConfigAlias("sorts"); - } - - public function setQueryStringAliasForSort(string $alias): self - { - return $this->setQueryStringConfigAlias("sorts", $alias); - } -} +queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; + + } + + public function setupQueryStringStatusForSort(): void + { + if (! $this->hasQueryStringStatusForSort()) { + $this->setQueryStringForSortEnabled(); + } + } + + protected function hasQueryStringStatusForSort(): bool + { + return $this->hasQueryStringConfigStatus('sorts'); + } + + protected function getQueryStringStatusForSort(): bool + { + return $this->getQueryStringConfigStatus('sorts'); + } + + protected function queryStringForSortEnabled(): bool + { + $this->setupQueryStringStatusForSort(); + + return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); + } + + public function setQueryStringStatusForSort(bool $status): self + { + return $this->setQueryStringConfigStatus('sorts', $status); + } + + public function setQueryStringForSortEnabled(): self + { + return $this->setQueryStringStatusForSort(true); + } + + public function setQueryStringForSortDisabled(): self + { + return $this->setQueryStringStatusForSort(false); + } + + protected function hasQueryStringAliasForSort(): bool + { + return $this->hasQueryStringConfigAlias('sorts'); + } + + protected function getQueryStringAliasForSort(): string + { + return $this->getQueryStringConfigAlias('sorts'); + } + + public function setQueryStringAliasForSort(string $alias): self + { + return $this->setQueryStringConfigAlias('sorts', $alias); + } +} From 4cd0f8b0e98edc8afc2019a66f576e37e1eab5f9 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 27 Oct 2024 04:50:02 +0000 Subject: [PATCH 5/9] Tests --- .../Core/QueryStrings/HasQueryStringForSearch.php | 10 +++++----- src/Traits/Core/QueryStrings/HasQueryStringForSort.php | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php index 80d676c4a..c987e4ccc 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php @@ -17,17 +17,17 @@ protected function setupQueryStringStatusForSearch(): void } } - protected function hasQueryStringStatusForSearch(): bool + public function hasQueryStringStatusForSearch(): bool { return $this->hasQueryStringConfigStatus('search'); } - protected function getQueryStringStatusForSearch(): bool + public function getQueryStringStatusForSearch(): bool { return $this->getQueryStringConfigStatus('search'); } - protected function queryStringForSearchEnabled(): bool + public function queryStringForSearchEnabled(): bool { $this->setupQueryStringStatusForSearch(); @@ -49,12 +49,12 @@ public function setQueryStringForSearchDisabled(): self return $this->setQueryStringStatusForSearch(false); } - protected function hasQueryStringAliasForSearch(): bool + public function hasQueryStringAliasForSearch(): bool { return $this->hasQueryStringConfigAlias('search'); } - protected function getQueryStringAliasForSearch(): string + public function getQueryStringAliasForSearch(): string { return $this->getQueryStringConfigAlias('search'); } diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php index 2c7ae2cd5..08b60dcfc 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -10,19 +10,19 @@ protected function queryStringHasQueryStringForSort(): array } - public function setupQueryStringStatusForSort(): void + protected function setupQueryStringStatusForSort(): void { if (! $this->hasQueryStringStatusForSort()) { $this->setQueryStringForSortEnabled(); } } - protected function hasQueryStringStatusForSort(): bool + public function hasQueryStringStatusForSort(): bool { return $this->hasQueryStringConfigStatus('sorts'); } - protected function getQueryStringStatusForSort(): bool + public function getQueryStringStatusForSort(): bool { return $this->getQueryStringConfigStatus('sorts'); } @@ -49,12 +49,12 @@ public function setQueryStringForSortDisabled(): self return $this->setQueryStringStatusForSort(false); } - protected function hasQueryStringAliasForSort(): bool + public function hasQueryStringAliasForSort(): bool { return $this->hasQueryStringConfigAlias('sorts'); } - protected function getQueryStringAliasForSort(): string + public function getQueryStringAliasForSort(): string { return $this->getQueryStringConfigAlias('sorts'); } From 0304d9a8923d43939ebd65903a43baa9bb395c53 Mon Sep 17 00:00:00 2001 From: LRLJoe Date: Sun, 27 Oct 2024 05:16:41 +0000 Subject: [PATCH 6/9] Tweaks to Configure Approach --- src/Traits/ComponentUtilities.php | 35 +++++++++++++------ .../QueryStringConfiguration.php | 2 +- .../QueryStrings/HasQueryStringForFilter.php | 2 +- .../QueryStrings/HasQueryStringForSort.php | 2 +- src/Traits/HasAllTraits.php | 2 +- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 8cf7bad19..e6babae86 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -8,11 +8,13 @@ use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString; trait ComponentUtilities { use ComponentConfiguration, ComponentHelpers; + use HasQueryString; public array $table = []; @@ -48,6 +50,8 @@ trait ComponentUtilities protected bool $useComputedProperties = true; + protected bool $hasRunConfigure = false; + /** * Set any configuration options */ @@ -71,16 +75,7 @@ public function mountComponentUtilities(): void */ public function bootedComponentUtilities(): void { - // Fire Lifecycle Hooks for configuring - $this->callHook('configuring'); - $this->callTraitHook('configuring'); - - // Call the configure() method - $this->configure(); - - // Fire Lifecycle Hooks for configured - $this->callHook('configured'); - $this->callTraitHook('configured'); + $this->runCoreConfiguration(); // Make sure a primary key is set if (! $this->hasPrimaryKey()) { @@ -89,6 +84,26 @@ public function bootedComponentUtilities(): void } + protected function runCoreConfiguration(): void + { + if (!$this->hasRunConfigure) + { + // Fire Lifecycle Hooks for configuring + $this->callHook('configuring'); + $this->callTraitHook('configuring'); + + // Call the configure() method + $this->configure(); + + // Fire Lifecycle Hooks for configured + $this->callHook('configured'); + $this->callTraitHook('configured'); + + $this->hasRunConfigure = true; + + } + } + /** * Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts */ diff --git a/src/Traits/Configuration/QueryStringConfiguration.php b/src/Traits/Configuration/QueryStringConfiguration.php index 771c640c2..fb0dedfad 100644 --- a/src/Traits/Configuration/QueryStringConfiguration.php +++ b/src/Traits/Configuration/QueryStringConfiguration.php @@ -14,7 +14,7 @@ public function setQueryStringAlias(string $queryStringAlias): self public function setupQueryStringStatus(): void { if (! $this->hasQueryStringStatus()) { - $this->configure(); + $this->runCoreConfiguration(); if (! $this->hasQueryStringStatus()) { $this->setQueryStringEnabled(); } diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForFilter.php b/src/Traits/Core/QueryStrings/HasQueryStringForFilter.php index 624242301..add0a331f 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForFilter.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForFilter.php @@ -20,7 +20,7 @@ protected function queryStringHasQueryStringForFilter(): array ] : []; } - public function setupQueryStringStatusForFilter(): void + protected function setupQueryStringStatusForFilter(): void { if (! $this->hasQueryStringStatusForFilter()) { $this->setQueryStringForFilterEnabled(); diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php index 08b60dcfc..cbdad07af 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -27,7 +27,7 @@ public function getQueryStringStatusForSort(): bool return $this->getQueryStringConfigStatus('sorts'); } - protected function queryStringForSortEnabled(): bool + public function queryStringForSortEnabled(): bool { $this->setupQueryStringStatusForSort(); diff --git a/src/Traits/HasAllTraits.php b/src/Traits/HasAllTraits.php index 70ddd3eb5..1c6b62b91 100644 --- a/src/Traits/HasAllTraits.php +++ b/src/Traits/HasAllTraits.php @@ -12,6 +12,7 @@ trait HasAllTraits use WithLoadingPlaceholder; use HasTheme; use ComponentUtilities, + WithQueryString, WithActions, WithData, WithColumns, @@ -28,7 +29,6 @@ trait HasAllTraits WithEvents, WithFilters, WithFooter, - WithQueryString, WithRefresh, WithReordering, WithSecondaryHeader, From efae78ae84202023e46743ea53c647521b98a22f Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 27 Oct 2024 05:17:08 +0000 Subject: [PATCH 7/9] Fix styling --- src/Traits/ComponentUtilities.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index e6babae86..8249505e5 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -7,8 +7,8 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration; -use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString; +use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; trait ComponentUtilities { @@ -86,8 +86,7 @@ public function bootedComponentUtilities(): void protected function runCoreConfiguration(): void { - if (!$this->hasRunConfigure) - { + if (! $this->hasRunConfigure) { // Fire Lifecycle Hooks for configuring $this->callHook('configuring'); $this->callTraitHook('configuring'); @@ -98,7 +97,7 @@ protected function runCoreConfiguration(): void // Fire Lifecycle Hooks for configured $this->callHook('configured'); $this->callTraitHook('configured'); - + $this->hasRunConfigure = true; } From 2df655867a64b0a56d2ed218bacb0a051927c8a6 Mon Sep 17 00:00:00 2001 From: LRLJoe Date: Sun, 27 Oct 2024 05:35:10 +0000 Subject: [PATCH 8/9] Minor Tweaks --- src/Traits/ComponentUtilities.php | 7 +++---- src/Traits/HasAllTraits.php | 2 +- src/Traits/WithQueryString.php | 4 +++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 8249505e5..fc8dd1fa7 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -7,14 +7,12 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration; -use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString; use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; trait ComponentUtilities { use ComponentConfiguration, ComponentHelpers; - use HasQueryString; public array $table = []; @@ -86,7 +84,8 @@ public function bootedComponentUtilities(): void protected function runCoreConfiguration(): void { - if (! $this->hasRunConfigure) { + if (!$this->hasRunConfigure) + { // Fire Lifecycle Hooks for configuring $this->callHook('configuring'); $this->callTraitHook('configuring'); @@ -97,7 +96,7 @@ protected function runCoreConfiguration(): void // Fire Lifecycle Hooks for configured $this->callHook('configured'); $this->callTraitHook('configured'); - + $this->hasRunConfigure = true; } diff --git a/src/Traits/HasAllTraits.php b/src/Traits/HasAllTraits.php index 1c6b62b91..b8da012d7 100644 --- a/src/Traits/HasAllTraits.php +++ b/src/Traits/HasAllTraits.php @@ -12,9 +12,9 @@ trait HasAllTraits use WithLoadingPlaceholder; use HasTheme; use ComponentUtilities, - WithQueryString, WithActions, WithData, + WithQueryString, WithColumns, WithSorting, WithSearch, diff --git a/src/Traits/WithQueryString.php b/src/Traits/WithQueryString.php index ccc11d5e9..6fd429abd 100644 --- a/src/Traits/WithQueryString.php +++ b/src/Traits/WithQueryString.php @@ -5,12 +5,14 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Traits\Configuration\QueryStringConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\QueryStringHelpers; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString; trait WithQueryString { use QueryStringConfiguration, QueryStringHelpers; - + use HasQueryString; + #[Locked] public ?bool $queryStringStatus; From 4539af679a6ce0fdb381630b2bec95eaae07322e Mon Sep 17 00:00:00 2001 From: lrljoe Date: Sun, 27 Oct 2024 05:35:36 +0000 Subject: [PATCH 9/9] Fix styling --- src/Traits/ComponentUtilities.php | 5 ++--- src/Traits/WithQueryString.php | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index fc8dd1fa7..de24d0e72 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -84,8 +84,7 @@ public function bootedComponentUtilities(): void protected function runCoreConfiguration(): void { - if (!$this->hasRunConfigure) - { + if (! $this->hasRunConfigure) { // Fire Lifecycle Hooks for configuring $this->callHook('configuring'); $this->callTraitHook('configuring'); @@ -96,7 +95,7 @@ protected function runCoreConfiguration(): void // Fire Lifecycle Hooks for configured $this->callHook('configured'); $this->callTraitHook('configured'); - + $this->hasRunConfigure = true; } diff --git a/src/Traits/WithQueryString.php b/src/Traits/WithQueryString.php index 6fd429abd..56dee3699 100644 --- a/src/Traits/WithQueryString.php +++ b/src/Traits/WithQueryString.php @@ -4,15 +4,15 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Traits\Configuration\QueryStringConfiguration; -use Rappasoft\LaravelLivewireTables\Traits\Helpers\QueryStringHelpers; use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString; +use Rappasoft\LaravelLivewireTables\Traits\Helpers\QueryStringHelpers; trait WithQueryString { use QueryStringConfiguration, QueryStringHelpers; use HasQueryString; - + #[Locked] public ?bool $queryStringStatus;