Skip to content

Commit

Permalink
Add option to disable allowing filters if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Jan 25, 2024
1 parent 21784af commit 365ecdd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

return [
// Enable the query string feature of Livewire, saving the filters in the URL
'enable_query_string' => false,

// Only allow filters for fields and conditions already loaded in the page to be applied
'only_allow_active_filters' => true,

];
4 changes: 3 additions & 1 deletion src/Http/Livewire/Traits/GenerateParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ trait GenerateParams
{
protected function generateParams()
{
$params = $this->removeParamsNotInFiltersCollection();
$params = config('statamic-livewire-filters.only_allow_active_filters')
? $this->removeParamsNotInFiltersCollection()
: $this->params;

return Parameters::make(array_merge(
['from' => $this->collections],
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/LivewireCollectionComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Facades\Reach\StatamicLivewireFilters\Tests\Factories\EntryFactory;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Livewire\Livewire;
use Reach\StatamicLivewireFilters\Http\Livewire\LivewireCollection as LivewireCollectionComponent;
use Reach\StatamicLivewireFilters\Tests\PreventSavingStacheItemsToDisk;
Expand Down Expand Up @@ -226,6 +227,31 @@ public function check_that_filter_gets_ignored_if_not_registered()
->assertSee('Black Shirt');
}

/** @test */
public function check_that_filter_gets_applied_if_check_is_disabled_in_config()
{
Config::set('statamic-livewire-filters.only_allow_active_filters', false);

$params = [
'from' => 'clothes',
];

Livewire::test(LivewireCollectionComponent::class, ['params' => $params])
->assertSet('collections', 'clothes')
->dispatch('filter-updated',
field: 'colors',
condition: 'taxonomy',
payload: 'red',
command: 'add',
modifier: 'any',
)
->assertSet('params', [
'taxonomy:colors:any' => 'red',
])
->assertSee('Red Shirt')
->assertDontSee('Yellow Shirt');
}

/** @test */
public function it_sets_query_scope_parameters()
{
Expand Down

0 comments on commit 365ecdd

Please sign in to comment.