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/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 8cf7bad19..de24d0e72 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -48,6 +48,8 @@ trait ComponentUtilities protected bool $useComputedProperties = true; + protected bool $hasRunConfigure = false; + /** * Set any configuration options */ @@ -71,16 +73,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 +82,25 @@ 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/HasQueryString.php b/src/Traits/Core/QueryStrings/HasQueryString.php new file mode 100644 index 000000000..7c1f567b6 --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryString.php @@ -0,0 +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]); + } +} 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/HasQueryStringForSearch.php b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php index 10cac7fb9..c987e4ccc 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php @@ -2,22 +2,15 @@ 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(); @@ -26,12 +19,12 @@ public function setupQueryStringStatusForSearch(): void public function hasQueryStringStatusForSearch(): bool { - return isset($this->queryStringStatusForSearch); + return $this->hasQueryStringConfigStatus('search'); } public function getQueryStringStatusForSearch(): bool { - return $this->queryStringStatusForSearch ?? true; + return $this->getQueryStringConfigStatus('search'); } public function queryStringForSearchEnabled(): bool @@ -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 { - return isset($this->queryStringAliasForSearch); + return $this->hasQueryStringConfigAlias('search'); } public 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 new file mode 100644 index 000000000..cbdad07af --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -0,0 +1,66 @@ +queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; + + } + + protected function setupQueryStringStatusForSort(): void + { + if (! $this->hasQueryStringStatusForSort()) { + $this->setQueryStringForSortEnabled(); + } + } + + public function hasQueryStringStatusForSort(): bool + { + return $this->hasQueryStringConfigStatus('sorts'); + } + + public function getQueryStringStatusForSort(): bool + { + return $this->getQueryStringConfigStatus('sorts'); + } + + public 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); + } + + public function hasQueryStringAliasForSort(): bool + { + return $this->hasQueryStringConfigAlias('sorts'); + } + + public function getQueryStringAliasForSort(): string + { + return $this->getQueryStringConfigAlias('sorts'); + } + + public function setQueryStringAliasForSort(string $alias): self + { + return $this->setQueryStringConfigAlias('sorts', $alias); + } +} diff --git a/src/Traits/HasAllTraits.php b/src/Traits/HasAllTraits.php index 70ddd3eb5..b8da012d7 100644 --- a/src/Traits/HasAllTraits.php +++ b/src/Traits/HasAllTraits.php @@ -14,6 +14,7 @@ trait HasAllTraits use ComponentUtilities, WithActions, WithData, + WithQueryString, WithColumns, WithSorting, WithSearch, @@ -28,7 +29,6 @@ trait HasAllTraits WithEvents, WithFilters, WithFooter, - WithQueryString, WithRefresh, WithReordering, WithSecondaryHeader, diff --git a/src/Traits/WithQueryString.php b/src/Traits/WithQueryString.php index ccc11d5e9..56dee3699 100644 --- a/src/Traits/WithQueryString.php +++ b/src/Traits/WithQueryString.php @@ -4,12 +4,14 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Traits\Configuration\QueryStringConfiguration; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString; use Rappasoft\LaravelLivewireTables\Traits\Helpers\QueryStringHelpers; trait WithQueryString { use QueryStringConfiguration, QueryStringHelpers; + use HasQueryString; #[Locked] public ?bool $queryStringStatus; diff --git a/src/Traits/WithSorting.php b/src/Traits/WithSorting.php index 78c8e9a73..1ed30366e 100644 --- a/src/Traits/WithSorting.php +++ b/src/Traits/WithSorting.php @@ -6,12 +6,14 @@ use Illuminate\Support\Collection; use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Traits\Configuration\SortingConfiguration; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForSort; use Rappasoft\LaravelLivewireTables\Traits\Helpers\SortingHelpers; trait WithSorting { use SortingConfiguration, SortingHelpers; + use HasQueryStringForSort; public array $sorts = []; @@ -31,17 +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()); + } +}