-
-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customisation for QueryString specific to Filters (#2011)
* Add customisation for QueryString specific to Filters * Move all QueryString Filter Code to HasQueryStringForFilter --------- Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
305 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
title: Query String | ||
weight: 5 | ||
--- | ||
|
||
The query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods: | ||
|
||
## Global | ||
### setQueryStringStatus | ||
|
||
Enable/disable the query string. | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
$this->setQueryStringStatus(true); | ||
$this->setQueryStringStatus(false); | ||
} | ||
``` | ||
|
||
### setQueryStringEnabled | ||
|
||
Enable the query string. | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
// Shorthand for $this->setQueryStringStatus(true) | ||
$this->setQueryStringEnabled(); | ||
} | ||
``` | ||
|
||
### setQueryStringDisabled | ||
|
||
Disable the query string. | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
// Shorthand for $this->setQueryStringStatus(false) | ||
$this->setQueryStringDisabled(); | ||
} | ||
``` | ||
|
||
### setQueryStringAlias | ||
|
||
Change the Alias in the URL, otherwise defaults to "$tablename" | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
$this->setQueryStringAlias('table1'); | ||
} | ||
``` | ||
|
||
## Filters | ||
|
||
The filter query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods: | ||
|
||
### setQueryStringStatusForFilter | ||
|
||
Enable/disable the query string for the filters | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
$this->setQueryStringStatusForFilter(true); | ||
$this->setQueryStringStatusForFilter(false); | ||
} | ||
``` | ||
|
||
### setQueryStringForFilterEnabled | ||
|
||
Enable the query string for the filters | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
// Shorthand for $this->setQueryStringStatusForFilter(true) | ||
$this->setQueryStringForFilterEnabled(); | ||
} | ||
``` | ||
|
||
### setQueryStringForFilterDisabled | ||
|
||
Disable the query string for the filters | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
// Shorthand for $this->setQueryStringStatusForFilter(false) | ||
$this->setQueryStringForFilterDisabled(); | ||
} | ||
``` | ||
|
||
### setQueryStringAliasForFilter | ||
|
||
Change the Alias in the URL for the filter, otherwise defaults to "$tablename-filters" | ||
|
||
```php | ||
public function configure(): void | ||
{ | ||
$this->setQueryStringAliasForFilter('filtervalues'); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings; | ||
|
||
use Livewire\Attributes\Locked; | ||
|
||
trait HasQueryStringForFilter | ||
{ | ||
#[Locked] | ||
public ?bool $queryStringStatusForFilter; | ||
|
||
protected ?string $queryStringAliasForFilter; | ||
|
||
protected function queryStringHasQueryStringForFilter(): array | ||
{ | ||
if ($this->queryStringForFilterIsEnabled()) { | ||
return [ | ||
'appliedFilters' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()], | ||
'filterComponents' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()], | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
public function setupQueryStringStatusForFilter(): void | ||
{ | ||
if (! $this->hasQueryStringStatusForFilter()) { | ||
$this->setQueryStringForFilterEnabled(); | ||
} | ||
} | ||
|
||
public function hasQueryStringStatusForFilter(): bool | ||
{ | ||
return isset($this->queryStringStatusForFilter); | ||
} | ||
|
||
public function getQueryStringStatusForFilter(): bool | ||
{ | ||
return $this->queryStringStatusForFilter ?? true; | ||
} | ||
|
||
public function queryStringForFilterIsEnabled(): bool | ||
{ | ||
$this->setupQueryStringStatusForFilter(); | ||
|
||
return ($this->queryStringIsEnabled() === true || $this->getQueryStringStatusForFilter() === true) && $this->filtersAreEnabled(); | ||
} | ||
|
||
public function setQueryStringStatusForFilter(bool $status): self | ||
{ | ||
$this->queryStringStatusForFilter = $status; | ||
|
||
return $this; | ||
} | ||
|
||
public function setQueryStringForFilterEnabled(): self | ||
{ | ||
$this->setQueryStringStatusForFilter(true); | ||
|
||
return $this; | ||
} | ||
|
||
public function setQueryStringForFilterDisabled(): self | ||
{ | ||
$this->setQueryStringStatusForFilter(false); | ||
|
||
return $this; | ||
} | ||
|
||
public function hasQueryStringAliasForFilter(): bool | ||
{ | ||
return isset($this->queryStringAliasForFilter); | ||
} | ||
|
||
public function getQueryStringAliasForFilter(): string | ||
{ | ||
return $this->queryStringAliasForFilter ?? $this->getQueryStringAlias().'-filters'; | ||
} | ||
|
||
public function setQueryStringAliasForFilter(string $alias): self | ||
{ | ||
$this->queryStringAliasForFilter = $alias; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/Traits/Core/QueryStrings/QueryStringForFiltersTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Core\QueryStrings; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Depends; | ||
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable; | ||
use Rappasoft\LaravelLivewireTables\Tests\TestCase; | ||
|
||
final class QueryStringForFiltersTest extends TestCase | ||
{ | ||
public function test_can_get_default_filter_query_string_status(): void | ||
{ | ||
$mock = new class extends PetsTable | ||
{ | ||
public ?array $testAttributesArray; | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDataTableFingerprint('test'); | ||
} | ||
}; | ||
|
||
$mock->configure(); | ||
$mock->boot(); | ||
|
||
$this->assertSame(true, $mock->getQueryStringStatusForFilter()); | ||
} | ||
|
||
public function test_can_disable_filter_query_string_status(): void | ||
{ | ||
$mock = new class extends PetsTable | ||
{ | ||
public ?array $testAttributesArray; | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDataTableFingerprint('test'); | ||
$this->setQueryStringForFilterDisabled(); | ||
} | ||
}; | ||
|
||
$mock->configure(); | ||
$mock->boot(); | ||
|
||
$this->assertSame(false, $mock->getQueryStringStatusForFilter()); | ||
} | ||
|
||
public function test_can_enable_filter_query_string_status(): void | ||
{ | ||
$mock = new class extends PetsTable | ||
{ | ||
public ?array $testAttributesArray; | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDataTableFingerprint('test'); | ||
$this->setQueryStringForFilterDisabled(); | ||
} | ||
}; | ||
|
||
$mock->configure(); | ||
$mock->boot(); | ||
|
||
$this->assertSame(false, $mock->getQueryStringStatusForFilter()); | ||
$mock->setQueryStringForFilterEnabled(); | ||
$this->assertSame(true, $mock->getQueryStringStatusForFilter()); | ||
|
||
} | ||
|
||
public function test_can_get_default_filter_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-filters', $mock->getQueryStringAliasForFilter()); | ||
|
||
} | ||
|
||
public function test_can_change_default_filter_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-filters', $mock->getQueryStringAliasForFilter()); | ||
$mock->setQueryStringAliasForFilter('pet-filters'); | ||
$this->assertSame('pet-filters', $mock->getQueryStringAliasForFilter()); | ||
} | ||
} |